comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/TIM/PWM_Output/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/PWM_Output/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 TIM_PWM_Output
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 CCR1_Val = 333;
40 uint16_t CCR2_Val = 249;
41 uint16_t CCR3_Val = 166;
42 uint16_t CCR4_Val = 83;
43 uint16_t PrescalerValue = 0;
44
45 /* Private function prototypes -----------------------------------------------*/
46 void RCC_Configuration(void);
47 void GPIO_Configuration(void);
48
49 /* Private functions ---------------------------------------------------------*/
50
51 /**
52 * @brief Main program
53 * @param None
54 * @retval None
55 */
56 int main(void)
57 {
58 /*!< At this stage the microcontroller clock setting is already configured,
59 this is done through SystemInit() function which is called from startup
60 file (startup_stm32f10x_xx.s) before to branch to application main.
61 To reconfigure the default setting of SystemInit() function, refer to
62 system_stm32f10x.c file
63 */
64
65 /* System Clocks Configuration */
66 RCC_Configuration();
67
68 /* GPIO Configuration */
69 GPIO_Configuration();
70
71 /* -----------------------------------------------------------------------
72 TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
73 The TIM3CLK frequency is set to SystemCoreClock (Hz), to get TIM3 counter
74 clock at 24 MHz the Prescaler is computed as following:
75 - Prescaler = (TIM3CLK / TIM3 counter clock) - 1
76 SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
77 and Connectivity line devices and to 24 MHz for Low-Density Value line and
78 Medium-Density Value line devices
79
80 The TIM3 is running at 36 KHz: TIM3 Frequency = TIM3 counter clock/(ARR + 1)
81 = 24 MHz / 666 = 36 KHz
82 TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
83 TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
84 TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
85 TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
86 ----------------------------------------------------------------------- */
87 /* Compute the prescaler value */
88 PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
89 /* Time base configuration */
90 TIM_TimeBaseStructure.TIM_Period = 665;
91 TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
92 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
93 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
94
95 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
96
97 /* PWM1 Mode configuration: Channel1 */
98 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
99 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
100 TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
101 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
102
103 TIM_OC1Init(TIM3, &TIM_OCInitStructure);
104
105 TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
106
107 /* PWM1 Mode configuration: Channel2 */
108 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
109 TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
110
111 TIM_OC2Init(TIM3, &TIM_OCInitStructure);
112
113 TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
114
115 /* PWM1 Mode configuration: Channel3 */
116 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
117 TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
118
119 TIM_OC3Init(TIM3, &TIM_OCInitStructure);
120
121 TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
122
123 /* PWM1 Mode configuration: Channel4 */
124 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
125 TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
126
127 TIM_OC4Init(TIM3, &TIM_OCInitStructure);
128
129 TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
130
131 TIM_ARRPreloadConfig(TIM3, ENABLE);
132
133 /* TIM3 enable counter */
134 TIM_Cmd(TIM3, ENABLE);
135
136 while (1)
137 {}
138 }
139
140 /**
141 * @brief Configures the different system clocks.
142 * @param None
143 * @retval None
144 */
145 void RCC_Configuration(void)
146 {
147 /* TIM3 clock enable */
148 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
149
150 /* GPIOA and GPIOB clock enable */
151 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
152 RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
153 }
154
155 /**
156 * @brief Configure the TIM3 Ouput Channels.
157 * @param None
158 * @retval None
159 */
160 void GPIO_Configuration(void)
161 {
162 GPIO_InitTypeDef GPIO_InitStructure;
163
164 #ifdef STM32F10X_CL
165 /*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */
166 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
167 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
168 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
169
170 GPIO_Init(GPIOC, &GPIO_InitStructure);
171
172 GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
173
174 #else
175 /* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */
176 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
177 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
178 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
179
180 GPIO_Init(GPIOA, &GPIO_InitStructure);
181
182 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
183 GPIO_Init(GPIOB, &GPIO_InitStructure);
184 #endif
185 }
186
187 #ifdef USE_FULL_ASSERT
188
189 /**
190 * @brief Reports the name of the source file and the source line number
191 * where the assert_param error has occurred.
192 * @param file: pointer to the source file name
193 * @param line: assert_param error line source number
194 * @retval None
195 */
196 void assert_failed(uint8_t* file, uint32_t line)
197 {
198 /* User can add his own implementation to report the file name and line number,
199 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
200
201 while (1)
202 {}
203 }
204
205 #endif
206
207 /**
208 * @}
209 */
210
211 /**
212 * @}
213 */
214
215 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/