comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/TIM/OCToggle/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/OCToggle/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_OCToggle
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 __IO uint16_t CCR1_Val = 32768;
40 __IO uint16_t CCR2_Val = 16384;
41 __IO uint16_t CCR3_Val = 8192;
42 __IO uint16_t CCR4_Val = 4096;
43 uint16_t PrescalerValue = 0;
44
45 /* Private function prototypes -----------------------------------------------*/
46 void RCC_Configuration(void);
47 void GPIO_Configuration(void);
48 void NVIC_Configuration(void);
49
50 /* Private functions ---------------------------------------------------------*/
51
52 /**
53 * @brief Main program
54 * @param None
55 * @retval None
56 */
57 int main(void)
58 {
59 /*!< At this stage the microcontroller clock setting is already configured,
60 this is done through SystemInit() function which is called from startup
61 file (startup_stm32f10x_xx.s) before to branch to application main.
62 To reconfigure the default setting of SystemInit() function, refer to
63 system_stm32f10x.c file
64 */
65
66 /* System Clocks Configuration */
67 RCC_Configuration();
68
69 /* NVIC Configuration */
70 NVIC_Configuration();
71
72 /* GPIO Configuration */
73 GPIO_Configuration();
74
75 /* ---------------------------------------------------------------------------
76 TIM3 Configuration: Output Compare Toggle Mode:
77 TIM3CLK = SystemCoreClock / 2,
78 The objective is to get TIM3 counter clock at 12 MHz:
79 - Prescaler = (TIM3CLK / TIM3 counter clock) - 1
80 CC1 update rate = TIM3 counter clock / CCR1_Val = 366.2 Hz
81 CC2 update rate = TIM3 counter clock / CCR2_Val = 732.4 Hz
82 CC3 update rate = TIM3 counter clock / CCR3_Val = 1464.8 Hz
83 CC4 update rate = TIM3 counter clock / CCR4_Val = 2929.6 Hz
84 ----------------------------------------------------------------------------*/
85 /* Compute the prescaler value */
86 PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
87
88 /* Time base configuration */
89 TIM_TimeBaseStructure.TIM_Period = 65535;
90 TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
91 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
92 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
93
94 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
95
96 /* Output Compare Toggle Mode configuration: Channel1 */
97 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
98 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
99 TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
100 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
101 TIM_OC1Init(TIM3, &TIM_OCInitStructure);
102
103 TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
104
105 /* Output Compare Toggle Mode configuration: Channel2 */
106 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
107 TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
108
109 TIM_OC2Init(TIM3, &TIM_OCInitStructure);
110
111 TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
112
113 /* Output Compare Toggle Mode configuration: Channel3 */
114 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
115 TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
116
117 TIM_OC3Init(TIM3, &TIM_OCInitStructure);
118
119 TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
120
121 /* Output Compare Toggle Mode configuration: Channel4 */
122 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
123 TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
124
125 TIM_OC4Init(TIM3, &TIM_OCInitStructure);
126
127 TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
128
129 /* TIM enable counter */
130 TIM_Cmd(TIM3, ENABLE);
131
132 /* TIM IT enable */
133 TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
134
135 while (1)
136 {}
137 }
138
139 /**
140 * @brief Configures the different system clocks.
141 * @param None
142 * @retval None
143 */
144 void RCC_Configuration(void)
145 {
146 /* PCLK1 = HCLK/4 */
147 RCC_PCLK1Config(RCC_HCLK_Div4);
148
149 /* TIM3 clock enable */
150 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
151
152 /* GPIOA clock enable */
153 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB|
154 RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
155 }
156
157 /**
158 * @brief Configure the TIM3 Pins.
159 * @param None
160 * @retval None
161 */
162 void GPIO_Configuration(void)
163 {
164 GPIO_InitTypeDef GPIO_InitStructure;
165
166 #ifdef STM32F10X_CL
167 /*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */
168 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
169 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
170 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
171
172 GPIO_Init(GPIOC, &GPIO_InitStructure);
173
174 GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
175
176 #else
177 /* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */
178 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
179 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
180 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
181
182 GPIO_Init(GPIOA, &GPIO_InitStructure);
183
184 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
185 GPIO_Init(GPIOB, &GPIO_InitStructure);
186 #endif
187 }
188
189 /**
190 * @brief Configure the nested vectored interrupt controller.
191 * @param None
192 * @retval None
193 */
194 void NVIC_Configuration(void)
195 {
196 NVIC_InitTypeDef NVIC_InitStructure;
197
198 /* Enable the TIM3 global Interrupt */
199 NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
200 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
201 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
202 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
203 NVIC_Init(&NVIC_InitStructure);
204 }
205
206 #ifdef USE_FULL_ASSERT
207
208 /**
209 * @brief Reports the name of the source file and the source line number
210 * where the assert_param error has occurred.
211 * @param file: pointer to the source file name
212 * @param line: assert_param error line source number
213 * @retval None
214 */
215 void assert_failed(uint8_t* file, uint32_t line)
216 {
217 /* User can add his own implementation to report the file name and line number,
218 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
219
220 while (1)
221 {}
222 }
223
224 #endif
225 /**
226 * @}
227 */
228
229 /**
230 * @}
231 */
232
233 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/