comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/TIM/TIM9_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/TIM9_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 TIM9_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 CCR1Val = 32768;
40 __IO uint16_t CCR2Val = 16384;
41 uint16_t PrescalerValue = 0;
42
43 /* Private function prototypes -----------------------------------------------*/
44 void GPIO_Configuration(void);
45 void NVIC_Configuration(void);
46
47 /* Private functions ---------------------------------------------------------*/
48
49 /**
50 * @brief Main program
51 * @param None
52 * @retval None
53 */
54 int main(void)
55 {
56 /*!< At this stage the microcontroller clock setting is already configured,
57 this is done through SystemInit() function which is called from startup
58 file (startup_stm32f10x_xx.s) before to branch to application main.
59 To reconfigure the default setting of SystemInit() function, refer to
60 system_stm32f10x.c file
61 */
62
63 /* Configure TIM9 pins */
64 GPIO_Configuration();
65
66 /* NVIC Configuration */
67 NVIC_Configuration();
68
69 /* ---------------------------------------------------------------------------
70 TIM9 Configuration: Output Compare Toggle Mode:
71 TIM9CLK = SystemCoreClock (72MHz),
72 The objective is to get TIM9 counter clock at 24 MHz:
73 - Prescaler = (TIM9CLK / TIM9 counter clock) - 1
74 CC1 update rate = TIM9 counter clock / CCR1Val = 732.4 Hz
75 CC2 update rate = TIM9 counter clock / CCR2Val = 1464.8 Hz
76 ----------------------------------------------------------------------------*/
77 /* Compute the prescaler value */
78 PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
79
80 /* Time base configuration */
81 TIM_TimeBaseStructure.TIM_Period = 65535;
82 TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
83 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
84 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
85
86 TIM_TimeBaseInit(TIM9, &TIM_TimeBaseStructure);
87
88 /* Output Compare Toggle Mode configuration: Channel1 */
89 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
90 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
91 TIM_OCInitStructure.TIM_Pulse = CCR1Val;
92 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
93 TIM_OC1Init(TIM9, &TIM_OCInitStructure);
94
95 TIM_OC1PreloadConfig(TIM9, TIM_OCPreload_Disable);
96
97 /* Output Compare Toggle Mode configuration: Channel2 */
98 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
99 TIM_OCInitStructure.TIM_Pulse = CCR2Val;
100
101 TIM_OC2Init(TIM9, &TIM_OCInitStructure);
102
103 TIM_OC2PreloadConfig(TIM9, TIM_OCPreload_Disable);
104
105 /* TIM enable counter */
106 TIM_Cmd(TIM9, ENABLE);
107
108 /* TIM IT enable */
109 TIM_ITConfig(TIM9, TIM_IT_CC1 | TIM_IT_CC2, ENABLE);
110
111 while (1)
112 {
113 }
114 }
115
116 /**
117 * @brief Configure TIM9 pins.
118 * @param None
119 * @retval None
120 */
121 void GPIO_Configuration(void)
122 {
123 GPIO_InitTypeDef GPIO_InitStructure;
124
125 /* Enable TIM9 and GPIOA clock */
126 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9 | RCC_APB2Periph_GPIOA, ENABLE);
127
128 /* GPIOA Configuration:TIM9 Channel1 and 2 as alternate function push-pull */
129 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
130 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
131 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
132
133 GPIO_Init(GPIOA, &GPIO_InitStructure);
134 }
135
136 /**
137 * @brief Configure the nested vectored interrupt controller.
138 * @param None
139 * @retval None
140 */
141 void NVIC_Configuration(void)
142 {
143 NVIC_InitTypeDef NVIC_InitStructure;
144
145 /* Enable the TIM9 global Interrupt */
146 NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_TIM9_IRQn;
147 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
148 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
149 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
150 NVIC_Init(&NVIC_InitStructure);
151 }
152
153 #ifdef USE_FULL_ASSERT
154
155 /**
156 * @brief Reports the name of the source file and the source line number
157 * where the assert_param error has occurred.
158 * @param file: pointer to the source file name
159 * @param line: assert_param error line source number
160 * @retval None
161 */
162 void assert_failed(uint8_t* file, uint32_t line)
163 {
164 /* User can add his own implementation to report the file name and line number,
165 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
166
167 while (1)
168 {}
169 }
170
171 #endif
172 /**
173 * @}
174 */
175
176 /**
177 * @}
178 */
179
180 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/