定时器需要一个时钟输入,在每个时钟触发时,做如下操作
因此,实际配置中需要配置如下几步
时钟源和分频系数等时钟参数
预置值和目标值
计数方式,增长和减少
循环方式,单次还是循环触发
VOID TIMER2_IRQHandler(VOID)
{
if(TIMER_GetIntBitState(TIMER2,TIMER_INT_UPDATE) != RESET)
{
/* 定时器中断中,第一步必须先清除定时器中断标记,防止中断反复进入 */
TIMER_ClearIntBitState(TIMER2,TIMER_INT_UPDATE);
if (gTimerLedFlag != 0)
{
DRV_LED_On(DRV_LED1);
gTimerLedFlag = 0;
return;
}
DRV_LED_Off(DRV_LED1);
gTimerLedFlag++;
}
}
VOID DRV_TIMER_Timer2Init(VOID)
{
TIMER_BaseInitPara TIMER_BaseInitParaStructure;
NVIC_InitPara NVIC_InitStructure;
RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_TIMER2,ENABLE);
TIMER_DeInit(TIMER2);
TIMER_BaseInitParaStructure.TIMER_Prescaler = 10800-1; /* 10KHz */
TIMER_BaseInitParaStructure.TIMER_CounterMode = TIMER_COUNTER_UP;
TIMER_BaseInitParaStructure.TIMER_Period = 10000-1; /* 10000*10KHz = 1s */
TIMER_BaseInitParaStructure.TIMER_ClockDivision = TIMER_CDIV_DIV1;
TIMER_BaseInit(TIMER2,&TIMER_BaseInitParaStructure);
TIMER_INTConfig(TIMER2, TIMER_INT_UPDATE, ENABLE);
NVIC_InitStructure.NVIC_IRQ = TIMER2_IRQn;
NVIC_InitStructure.NVIC_IRQPreemptPriority = 0;
NVIC_InitStructure.NVIC_IRQSubPriority = 0;
NVIC_InitStructure.NVIC_IRQEnable = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIMER_SinglePulseMode(TIMER2, TIMER_SP_MODE_REPETITIVE);
TIMER_Enable(TIMER2,ENABLE);
} |