Mercurial > ~darius > hgwebdir.cgi > stm32temp
comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/TIM/OnePulse/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/OnePulse/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>© 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_OnePulse | |
30 * @{ | |
31 */ | |
32 | |
33 /* Private typedef -----------------------------------------------------------*/ | |
34 /* Private define ------------------------------------------------------------*/ | |
35 /* Private macro -------------------------------------------------------------*/ | |
36 /* Private variables ---------------------------------------------------------*/ | |
37 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; | |
38 TIM_ICInitTypeDef TIM_ICInitStructure; | |
39 TIM_OCInitTypeDef TIM_OCInitStructure; | |
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 /* System Clocks Configuration */ | |
63 RCC_Configuration(); | |
64 | |
65 /* Configure the GPIO ports */ | |
66 GPIO_Configuration(); | |
67 | |
68 /* TIM4 configuration: One Pulse mode ------------------------ | |
69 The external signal is connected to TIM4_CH2 pin (PB.07), | |
70 The Rising edge is used as active edge, | |
71 The One Pulse signal is output on TIM4_CH1 pin (PB.06) | |
72 The TIM_Pulse defines the delay value | |
73 The (TIM_Period - TIM_Pulse) defines the One Pulse value. | |
74 TIM2CLK = SystemCoreClock, we want to get TIM2 counter clock at 24 MHz: | |
75 - Prescaler = (TIM2CLK / TIM2 counter clock) - 1 | |
76 The Autoreload value is 65535 (TIM4->ARR), so the maximum frequency value | |
77 to trigger the TIM4 input is 24000000/65535 = 300 Hz. | |
78 | |
79 The TIM_Pulse defines the delay value, the delay value is fixed | |
80 to 682.6 us: | |
81 delay = CCR1/TIM4 counter clock = 682.6 us. | |
82 The (TIM_Period - TIM_Pulse) defines the One Pulse value, | |
83 the pulse value is fixed to 2.048 ms: | |
84 One Pulse value = (TIM_Period - TIM_Pulse) / TIM4 counter clock = 2.048 ms. | |
85 | |
86 * SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density | |
87 and Connectivity line devices and to 24 MHz for Value line devices | |
88 ------------------------------------------------------------ */ | |
89 | |
90 /* Compute the prescaler value */ | |
91 PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1; | |
92 /* Time base configuration */ | |
93 TIM_TimeBaseStructure.TIM_Period = 65535; | |
94 TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; | |
95 TIM_TimeBaseStructure.TIM_ClockDivision = 0; | |
96 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; | |
97 | |
98 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); | |
99 | |
100 /* TIM4 PWM2 Mode configuration: Channel1 */ | |
101 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; | |
102 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; | |
103 TIM_OCInitStructure.TIM_Pulse = 16383; | |
104 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; | |
105 | |
106 TIM_OC1Init(TIM4, &TIM_OCInitStructure); | |
107 | |
108 /* TIM4 configuration in Input Capture Mode */ | |
109 | |
110 TIM_ICStructInit(&TIM_ICInitStructure); | |
111 | |
112 TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; | |
113 TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; | |
114 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; | |
115 TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; | |
116 TIM_ICInitStructure.TIM_ICFilter = 0; | |
117 | |
118 TIM_ICInit(TIM4, &TIM_ICInitStructure); | |
119 | |
120 /* One Pulse Mode selection */ | |
121 TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Single); | |
122 | |
123 /* Input Trigger selection */ | |
124 TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2); | |
125 | |
126 /* Slave Mode selection: Trigger Mode */ | |
127 TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Trigger); | |
128 | |
129 while (1) | |
130 {} | |
131 } | |
132 | |
133 /** | |
134 * @brief Configures the different system clocks. | |
135 * @param None | |
136 * @retval None | |
137 */ | |
138 void RCC_Configuration(void) | |
139 { | |
140 /* TIM4 clock enable */ | |
141 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); | |
142 | |
143 /* GPIOB clock enable */ | |
144 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); | |
145 } | |
146 | |
147 /** | |
148 * @brief Configure the GPIOD Pins. | |
149 * @param None | |
150 * @retval None | |
151 */ | |
152 void GPIO_Configuration(void) | |
153 { | |
154 GPIO_InitTypeDef GPIO_InitStructure; | |
155 | |
156 /* TIM4_CH1 pin (PB.06) configuration */ | |
157 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; | |
158 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; | |
159 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; | |
160 | |
161 GPIO_Init(GPIOB, &GPIO_InitStructure); | |
162 | |
163 /* TIM4_CH2 pin (PB.07) configuration */ | |
164 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; | |
165 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; | |
166 | |
167 GPIO_Init(GPIOB, &GPIO_InitStructure); | |
168 } | |
169 | |
170 #ifdef USE_FULL_ASSERT | |
171 | |
172 /** | |
173 * @brief Reports the name of the source file and the source line number | |
174 * where the assert_param error has occurred. | |
175 * @param file: pointer to the source file name | |
176 * @param line: assert_param error line source number | |
177 * @retval None | |
178 */ | |
179 void assert_failed(uint8_t* file, uint32_t line) | |
180 { | |
181 /* User can add his own implementation to report the file name and line number, | |
182 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ | |
183 | |
184 while (1) | |
185 {} | |
186 } | |
187 | |
188 #endif | |
189 | |
190 /** | |
191 * @} | |
192 */ | |
193 | |
194 /** | |
195 * @} | |
196 */ | |
197 | |
198 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ |