// TIM3 the PWM partially initialized // the PWM output initialization // ARR: automatic reload value // PSC: Clock Prescaler number void TIM3_PWM_Init (ARR U16, U16 PSC) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM3 , eNABLE); // ① enable timer 3 clock (change) RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, eNABLE); // ① enable GPIO and AFIO multiplexing function clock (change) GPIO_PinRemapConfig (GPIO_PartialRemap_TIM3, eNABLE); // ② remapping TIM3_CH2-> PB5 (need to remap plus)
//Multiplexing the pin to be output, the output PWM pulse waveform of GPIOB.5 TIM3 CH2, B5 initialize the GPIO GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; // TIM_CH2 (change) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // multiplexing push-pull output GPIO_InitStructure = .GPIO_Speed GPIO_Speed_50MHz; GPIO_Init (GPIOB, & GPIO_InitStructure); // ① initialize the GPIO (change) // initialize TIM3 TIM_TimeBaseStructure.TIM_Period = ARR; // set the value in the auto-reload cycle TIM_TimeBaseStructure.TIM_Prescaler = PSC; // set the pre-separation frequency value TIM_TimeBaseStructure.TIM_ClockDivision = 0 ; // set the clock division: TDTS = Tck_tim = TIM_CounterMode_Up TIM_TimeBaseStructure.TIM_CounterMode; // the TIM up-count mode TIM_TimeBaseInit (TIM3, & TIM_TimeBaseStructure); // ③ initialization TIMx (change) // initialize TIM3 Channel2 PWM mode TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; // selected PWM mode 2 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // comparator output enable TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // output polarity high TIM_OC2Init (TIM3, & TIM_OCInitStructure); // ④ initializing peripherals TIM3 OC2 (OCx change and TIMx) TIM_OC2PreloadConfig (TIM3, TIM_OCPreload_Enable); // enable preload register (OCx change and TIMx) TIM_Cmd (TIM3, the eNABLE); //⑤ TIM3 enable (change) }
void TIM3_Int_Init (ARR U16, U16 PSC) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM3, the ENABLE); // ① TIM3 clock enable (change) // timer initialization TIM3 TIM_TimeBaseStructure.TIM_Period = ARR; // sets the auto loading cycle register value TIM_TimeBaseStructure.TIM_Prescaler = PSC; // set the frequency division of the clock prescaler value TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // set the clock division TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // the TIM counts up TIM_TimeBaseInit (TIM3, & TIM_TimeBaseStructure) ; // ② initialization TIM3 (change) TIM_ITConfig (TIM3, TIM_IT_Update, the ENABLE); // ③ Interrupt allow updates (changes) // interrupt priority NVIC provided NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; // TIM3 interrupt (change) NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0 ; // preemptive priority 0 level NVIC_InitStructure.NVIC_IRQChannelSubPriority = . 3 ; // the order priority. 3 NVIC_InitStructure.NVIC_IRQChannelCmd = the eNABLE; // the IRQ channel is enabled NVIC_Init (& NVIC_InitStructure); // ④ register initialization NVIC TIM_Cmd (TIM3, the eNABLE); // ⑤ enable TIM3 (change) } // timer 3 interrupt service routine ⑥ voidTIM3_IRQHandler ( void ) // TIM3 interrupt (changed) { IF (TIM_GetITStatus (TIM3, TIM_IT_Update) = the RESET!) // Check TIM3 interrupt occurs or not updated (changed) { TIM_ClearITPendingBit (TIM3, TIM_IT_Update); // Clear interrupt update TIM3 flag (change) LEDl =! LEDl; // (rest) } }