comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/TIM/TIM10_PWMOutput/main.c @ 0:c59513fd84fb

Initial commit of STM32 test code.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 03 Oct 2011 21:19:15 +1030
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c59513fd84fb
1 /**
2 ******************************************************************************
3 * @file TIM/TIM10_PWMOutput/main.c
4 * @author MCD Application Team
5 * @version V3.5.0
6 * @date 08-April-2011
7 * @brief Main program body
8 ******************************************************************************
9 * @attention
10 *
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17 *
18 * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
19 ******************************************************************************
20 */
21
22 /* Includes ------------------------------------------------------------------*/
23 #include "stm32f10x.h"
24
25 /** @addtogroup STM32F10x_StdPeriph_Examples
26 * @{
27 */
28
29 /** @addtogroup TIM10_PWMOutput
30 * @{
31 */
32
33 /* Private typedef -----------------------------------------------------------*/
34 /* Private define ------------------------------------------------------------*/
35 /* Private macro -------------------------------------------------------------*/
36 /* Private variables ---------------------------------------------------------*/
37 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
38 TIM_OCInitTypeDef TIM_OCInitStructure;
39 uint16_t CCR1Val = 249;
40 uint16_t PrescalerValue = 0;
41
42 /* Private function prototypes -----------------------------------------------*/
43 void RCC_Configuration(void);
44 void GPIO_Configuration(void);
45
46 /* Private functions ---------------------------------------------------------*/
47
48 /**
49 * @brief Main program
50 * @param None
51 * @retval None
52 */
53 int main(void)
54 {
55 /*!< At this stage the microcontroller clock setting is already configured,
56 this is done through SystemInit() function which is called from startup
57 file (startup_stm32f10x_xx.s) before to branch to application main.
58 To reconfigure the default setting of SystemInit() function, refer to
59 system_stm32f10x.c file
60 */
61
62 /* GPIO Configuration */
63 GPIO_Configuration();
64
65 /* -----------------------------------------------------------------------
66 TIM10 Configuration: generate 1 PWM signal.
67 The TIM10CLK frequency is set to SystemCoreClock (72 MHz), to get TIM10 counter
68 clock at 24 MHz the Prescaler is computed as following:
69 - Prescaler = (TIM10CLK / TIM10 counter clock) - 1
70
71 The TIM10 is running at 36 KHz: TIM10 Frequency = TIM10 counter clock/(ARR + 1)
72 = 24 MHz / 666 = 36 KHz
73 TIM10 Channel1 duty cycle = (TIM10_CCR1/ TIM10_ARR)* 100 = 37.5%
74 ----------------------------------------------------------------------- */
75 /* Compute the prescaler value */
76 PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
77 /* Time base configuration */
78 TIM_TimeBaseStructure.TIM_Period = 665;
79 TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
80 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
81 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
82
83 TIM_TimeBaseInit(TIM10, &TIM_TimeBaseStructure);
84
85 /* PWM1 Mode configuration: Channel1 */
86 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
87 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
88 TIM_OCInitStructure.TIM_Pulse = CCR1Val;
89 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
90
91 TIM_OC1Init(TIM10, &TIM_OCInitStructure);
92
93 TIM_OC1PreloadConfig(TIM10, TIM_OCPreload_Enable);
94
95 TIM_ARRPreloadConfig(TIM10, ENABLE);
96
97 /* TIM10 enable counter */
98 TIM_Cmd(TIM10, ENABLE);
99
100 while (1)
101 {}
102 }
103
104 /**
105 * @brief Configure TIM10 pin.
106 * @param None
107 * @retval None
108 */
109 void GPIO_Configuration(void)
110 {
111 GPIO_InitTypeDef GPIO_InitStructure;
112
113 /* Enable TIM10 and GPIOF clock */
114 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10 | RCC_APB2Periph_GPIOF |
115 RCC_APB2Periph_AFIO, ENABLE);
116
117 /* Remap TIM10_CH1 on PF6 pin */
118 GPIO_PinRemapConfig(GPIO_Remap_TIM10, ENABLE);
119
120 /* GPIOF Configuration: TIM10 Channel1 as alternate function push-pull */
121 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
122 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
123 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
124
125 GPIO_Init(GPIOF, &GPIO_InitStructure);
126 }
127
128 #ifdef USE_FULL_ASSERT
129
130 /**
131 * @brief Reports the name of the source file and the source line number
132 * where the assert_param error has occurred.
133 * @param file: pointer to the source file name
134 * @param line: assert_param error line source number
135 * @retval None
136 */
137 void assert_failed(uint8_t* file, uint32_t line)
138 {
139 /* User can add his own implementation to report the file name and line number,
140 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
141
142 while (1)
143 {}
144 }
145
146 #endif
147
148 /**
149 * @}
150 */
151
152 /**
153 * @}
154 */
155
156 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/