定时器中断
①TIM_TimeBaseInitTypeDef
NVIC_InitTypeDef //结构体定义
②PSC ARR计算
To get TIM4 counter clock at 2 KHz, the prescaler is computed as follows:
Prescaler = (TIM4CLK / counter clock) - 1
Prescaler = (168 MHz/(2 * 2 KHz)) - 1 =
To get TIM4 output clock at 1 Hz, the period (ARR)) is computed as follows:
ARR = (TIM4 counter clock / output clock) - 1
③RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIMx, ENABLE); //开时钟
④NVIC_InitTypeDef //写入结构体参数
⑤void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct);
⑥typedef struct
{
uint16_t TIM_Prescaler; //PSC 分频数
uint16_t TIM_CounterMode; #define TIM_CounterMode_Up ((uint16_t)0x0000)√
#define TIM_CounterMode_Down ((uint16_t)0x0010)
#define TIM_CounterMode_CenterAligned1 ((uint16_t)0x0020)
#define TIM_CounterMode_CenterAligned2 ((uint16_t)0x0040)
#define TIM_CounterMode_CenterAligned3 ((uint16_t)0x0060)
uint32_t TIM_Period; //ARR计数器
uint16_t TIM_ClockDivision; //分频器 基于APB等时钟下的再分频
#define TIM_CKD_DIV1 ((uint16_t)0x0000)√
#define TIM_CKD_DIV2 ((uint16_t)0x0100)
#define TIM_CKD_DIV4 ((uint16_t)0x0200)
uint8_t TIM_RepetitionCounter; /*!< Specifies the repetition counter value. Each time the RCR downcounter
reaches zero, an update event is generated and counting restarts
from the RCR value (N).
This means in PWM mode that (N+1) corresponds to:
- the number of PWM periods in edge-aligned mode
- the number of half PWM period in center-aligned mode
This parameter must be a number between 0x00 and 0xFF.
@note This parameter is valid only for TIM1 and TIM8. */
} TIM_TimeBaseInitTypeDef;
⑦void TIM_TimeBaseInit(TIM_TypeDef* TIMx, TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct);
* @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
⑧TIM_PrescalerConfig(TIMx,PrescalerValue,TIM_PSCReloadMode_Immediate); //重装分频值
⑨void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState) //中断声明
uint16_t TIM_IT:
* @arg TIM_IT_Update: TIM1 update Interrupt source √
* @arg TIM_IT_CC1: TIM Capture Compare 1 Interrupt source
* @arg TIM_IT_CC2: TIM Capture Compare 2 Interrupt source
* @arg TIM_IT_CC3: TIM Capture Compare 3 Interrupt source
* @arg TIM_IT_CC4: TIM Capture Compare 4 Interrupt source
* @arg TIM_IT_COM: TIM Commutation Interrupt source
* @arg TIM_IT_Trigger: TIM Trigger Interrupt source √
* @arg TIM_IT_Break: TIM Break Interrupt source
* @param TIMx: where x can be 1 to 14 to select the TIMx peripheral.
*
* @note For TIM6 and TIM7 only the parameter TIM_IT_Update can be used
* @note For TIM9 and TIM12 only one of the following parameters can be used: TIM_IT_Update,
* TIM_IT_CC1, TIM_IT_CC2 or TIM_IT_Trigger.
* @note For TIM10, TIM11, TIM13 and TIM14 only one of the following parameters can
* be used: TIM_IT_Update or TIM_IT_CC1
* @note TIM_IT_COM and TIM_IT_Break can be used only with TIM1 and TIM8
⑩void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState) //启用时钟
* @param TIMx: where x can be 1 to 14 to select the TIMx peripheral.
①开中断服务:void TIMx_IRQHandler(void); //中断处理
范例:
if(TIM_GetFlagStatus(TIMx,TIM_IT_Update)!=RESET
|| TIM_GetFlagStatus(TIMx,TIM_IT_Trigger)!=RESET) //溢出中断
{
LED_Toggle(red);
}
TIM_ClearFlag(TIMx,TIM_IT_Update);
TIM_ClearFlag(TIMx,TIM_IT_Trigger);
* @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
uint16_t TIM_FLAG:
* @arg TIM_FLAG_Update: TIM update Flag √
* @arg TIM_FLAG_CC1: TIM Capture Compare 1 Flag
* @arg TIM_FLAG_CC2: TIM Capture Compare 2 Flag
* @arg TIM_FLAG_CC3: TIM Capture Compare 3 Flag
* @arg TIM_FLAG_CC4: TIM Capture Compare 4 Flag
* @arg TIM_FLAG_COM: TIM Commutation Flag
* @arg TIM_FLAG_Trigger: TIM Trigger Flag √
* @arg TIM_FLAG_Break: TIM Break Flag
* @arg TIM_FLAG_CC1OF: TIM Capture Compare 1 over capture Flag
* @arg TIM_FLAG_CC2OF: TIM Capture Compare 2 over capture Flag
* @arg TIM_FLAG_CC3OF: TIM Capture Compare 3 over capture Flag
* @arg TIM_FLAG_CC4OF: TIM Capture Compare 4 over capture Flag