1 | static void setup_Timer16(void) {
|
2 | TIM_OCInitTypeDef TIM_OCInitStructure;
|
3 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
4 | GPIO_InitTypeDef GPIO_InitStructure;
|
5 |
|
6 | // clock on Timer 16 enable
|
7 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE);
|
8 |
|
9 | // GPIOB clocks enable
|
10 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
|
11 |
|
12 | // GPIOB Configuration: Pin PB8 AF2 connecting to TIM16 Ch1
|
13 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
|
14 | GPIO_InitStructure.GPIO_Mode = /*GPIO_Mode_OUT*/GPIO_Mode_AF;
|
15 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
16 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
17 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
18 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
19 |
|
20 | // Connect TIM16 pins to AF2
|
21 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_2);
|
22 |
|
23 | TIM_DeInit(TIM16 );
|
24 | TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
|
25 |
|
26 | /* Time base configuration */
|
27 | TIM_TimeBaseStructure.TIM_Period = (SystemCoreClock / 50000/*lcd_Contrast*/) - 1;
|
28 | TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
|
29 | TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
|
30 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
31 |
|
32 | TIM_TimeBaseInit(TIM16, &TIM_TimeBaseStructure);
|
33 |
|
34 | // Timer output compare configuration
|
35 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
|
36 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
|
37 | TIM_OCInitStructure.TIM_OCMode = /*TIM_OCMode_Timing*/TIM_OCMode_Toggle;
|
38 | TIM_OCInitStructure.TIM_Pulse = 500;
|
39 | TIM_OC1Init(TIM16, &TIM_OCInitStructure);
|
40 |
|
41 | /*
|
42 | NVIC_InitTypeDef nvic;
|
43 | nvic.NVIC_IRQChannel = TIM16_IRQn;
|
44 | nvic.NVIC_IRQChannelCmd = ENABLE;
|
45 | nvic.NVIC_IRQChannelPriority = 3;
|
46 | NVIC_Init(&nvic);
|
47 |
|
48 |
|
49 | TIM_ITConfig(TIM16, TIM_IT_CC1 | TIM_IT_Update, ENABLE);
|
50 | */
|
51 |
|
52 | /* TIM16 enable counter */
|
53 | TIM_Cmd(TIM16, ENABLE);
|
54 |
|
55 | }
|