1 | RCC->APB1ENR |= RCC_APB1Periph_TIM2;
|
2 | RCC->APB2ENR |= RCC_APB2Periph_TIM1;
|
3 |
|
4 | TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
|
5 | TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Reset);
|
6 | TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_External1);
|
7 | TIM_SelectInputTrigger(TIM2, TIM_TS_ITR0);
|
8 |
|
9 | TIM_TimeBaseInitTypeDef timerInitStructure;
|
10 | timerInitStructure.TIM_Prescaler = 2 - 1;
|
11 | timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
12 | timerInitStructure.TIM_Period = 4 - 1;
|
13 | timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
|
14 | timerInitStructure.TIM_RepetitionCounter = 0;
|
15 | TIM_TimeBaseInit(TIM1, &timerInitStructure);
|
16 |
|
17 | timerInitStructure.TIM_Prescaler = 2 - 1;
|
18 | timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
19 | timerInitStructure.TIM_Period = 2048 - 1;
|
20 | timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
|
21 | timerInitStructure.TIM_RepetitionCounter = 0;
|
22 | TIM_TimeBaseInit(TIM2, &timerInitStructure);
|
23 |
|
24 | NVIC_InitTypeDef nvicStructure;
|
25 | nvicStructure.NVIC_IRQChannel = TIM2_IRQn;
|
26 | nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
27 | nvicStructure.NVIC_IRQChannelSubPriority = 1;
|
28 | nvicStructure.NVIC_IRQChannelCmd = ENABLE;
|
29 | NVIC_Init(&nvicStructure);
|
30 | TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
31 |
|
32 | TIM_OCInitTypeDef ocInitStruct;
|
33 | ocInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
|
34 | ocInitStruct.TIM_OutputState = TIM_OutputState_Enable;
|
35 | ocInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
|
36 | ocInitStruct.TIM_Pulse = 2;
|
37 | TIM_OC1Init(TIM1, &ocInitStruct);
|
38 | TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
|
39 |
|
40 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
|
41 | TIM_Cmd(TIM1, ENABLE);
|
42 | TIM_Cmd(TIM2, ENABLE);
|