1 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
2 | TIM_OCInitTypeDef TIM_OCInitStructure;
|
3 | __IO uint16_t CCR1_Val = 54618;
|
4 | __IO bool TIM3_flag = false;
|
5 | uint16_t PrescalerValue = 0;
|
6 |
|
7 | void initTimer(void);
|
8 |
|
9 | int main(void)
|
10 | {
|
11 | // other init stuff
|
12 | initTimer();
|
13 | TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);
|
14 | TIM_Cmd(TIM3, ENABLE);
|
15 |
|
16 | while (1)
|
17 | {
|
18 | if (TIM3_flag == true){
|
19 | // do stuff
|
20 | TIM3_flag = false;
|
21 | }
|
22 | // do stuff
|
23 |
|
24 | }
|
25 | }
|
26 |
|
27 | void initTimer(void){
|
28 | // Init GPIOD
|
29 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
|
30 | GPIO_InitTypeDef GPIO_InitStructure;
|
31 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
|
32 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
33 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
34 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
35 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
36 | GPIO_Init(GPIOD, &GPIO_InitStructure);
|
37 | GPIO_SetBits(GPIOD,GPIO_Pin_14);
|
38 |
|
39 | // Init interrupt
|
40 | NVIC_InitTypeDef NVIC_InitStructure;
|
41 |
|
42 | /* TIM3 clock enable */
|
43 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
|
44 |
|
45 | /* Enable the TIM3 gloabal Interrupt */
|
46 | NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
|
47 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
48 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
|
49 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
50 | NVIC_Init(&NVIC_InitStructure);
|
51 |
|
52 | /* Compute the prescaler value */
|
53 | PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 500000) - 1;
|
54 |
|
55 | /* Time base configuration */
|
56 | TIM_TimeBaseStructure.TIM_Period = 65535;
|
57 | TIM_TimeBaseStructure.TIM_Prescaler = 0;
|
58 | TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
59 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
60 |
|
61 | TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
|
62 |
|
63 | /* Prescaler configuration */
|
64 | TIM_PrescalerConfig(TIM3, PrescalerValue, TIM_PSCReloadMode_Immediate);
|
65 |
|
66 | /* Output Compare Timing Mode configuration: Channel1 */
|
67 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
|
68 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
|
69 | TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
|
70 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
|
71 |
|
72 | TIM_OC1Init(TIM3, &TIM_OCInitStructure);
|
73 |
|
74 | TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
|
75 | }
|