comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/TIM/OCActive/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/OCActive/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_OCActive
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 = 1000;
40 uint16_t CCR2_Val = 500;
41 uint16_t CCR3_Val = 250;
42 uint16_t CCR4_Val = 125;
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 /* Configure the GPIO ports */
69 GPIO_Configuration();
70
71 /* ---------------------------------------------------------------
72 TIM3 Configuration:
73 TIM3CLK = SystemCoreClock / 2,
74 The objective is to get TIM3 counter clock at 1 KHz:
75 - Prescaler = (TIM3CLK / TIM3 counter clock) - 1
76 And generate 4 signals with 4 different delays:
77 TIM3_CH1 delay = CCR1_Val/TIM3 counter clock = 1000 ms
78 TIM3_CH2 delay = CCR2_Val/TIM3 counter clock = 500 ms
79 TIM3_CH3 delay = CCR3_Val/TIM3 counter clock = 250 ms
80 TIM3_CH4 delay = CCR4_Val/TIM3 counter clock = 125 ms
81
82 * SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
83 and Connectivity line devices and to 24 MHz for Low-Density Value line and
84 Medium-Density Value line devices
85 --------------------------------------------------------------- */
86 /*Compute the prescaler value */
87 PrescalerValue = (uint16_t) (SystemCoreClock / 2000) - 1;
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 Active Mode configuration: Channel1 */
97 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Active;
98 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
99 TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
100 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
101 TIM_OC1Init(TIM3, &TIM_OCInitStructure);
102
103 TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
104
105 /* Output Compare Active 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 Active 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 Active 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_ARRPreloadConfig(TIM3, ENABLE);
130
131 #ifdef STM32F10X_CL
132 /* Set PD.07 pin */
133 GPIO_SetBits(GPIOD, GPIO_Pin_7);
134 #else
135 /* Set PC.06 pin */
136 GPIO_SetBits(GPIOC, GPIO_Pin_6);
137 #endif
138
139 /* TIM3 enable counter */
140 TIM_Cmd(TIM3, ENABLE);
141
142 while (1)
143 {}
144 }
145
146 /**
147 * @brief Configures the different system clocks.
148 * @param None
149 * @retval None
150 */
151 void RCC_Configuration(void)
152 {
153 /* PCLK1 = HCLK/4 */
154 RCC_PCLK1Config(RCC_HCLK_Div4);
155
156 /* TIM3 clock enable */
157 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
158
159 /* GPIOA and GPIOC clock enable */
160 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOB |
161 RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
162 }
163
164 /**
165 * @brief Configure the TIM3 and the GPIOE Pins.
166 * @param None
167 * @retval None
168 */
169 void GPIO_Configuration(void)
170 {
171 GPIO_InitTypeDef GPIO_InitStructure;
172
173 #ifdef STM32F10X_CL
174 /*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */
175 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
176 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
177 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
178
179 GPIO_Init(GPIOC, &GPIO_InitStructure);
180
181 GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
182
183 /* GPIOD Configuration: Pin7 an Output push-pull */
184 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
185 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
186
187 GPIO_Init(GPIOD, &GPIO_InitStructure);
188 #else
189 /* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */
190 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
191 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
192 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
193
194 GPIO_Init(GPIOA, &GPIO_InitStructure);
195
196 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
197 GPIO_Init(GPIOB, &GPIO_InitStructure);
198
199 /* GPIOC Configuration: Pin6 an Output push-pull */
200 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
201 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
202
203 GPIO_Init(GPIOC, &GPIO_InitStructure);
204 #endif
205 }
206
207 #ifdef USE_FULL_ASSERT
208
209 /**
210 * @brief Reports the name of the source file and the source line number
211 * where the assert_param error has occurred.
212 * @param file: pointer to the source file name
213 * @param line: assert_param error line source number
214 * @retval None
215 */
216 void assert_failed(uint8_t* file, uint32_t line)
217 {
218 /* User can add his own implementation to report the file name and line number,
219 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
220
221 while (1)
222 {}
223 }
224
225 #endif
226
227 /**
228 * @}
229 */
230
231 /**
232 * @}
233 */
234
235 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/