GD32F407 timer延迟函数,非中断
void dri_timer_delay_init(dri_timer_calc_type type)
{
timer_parameter_struct ptr_time;
rcu_periph_clock_enable(PERIPH_DELAY_TIMER);
rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
timer_deinit(DELAY_TIMER);
/* Time base configuration */
ptr_time.clockdivision = TIMER_CKDIV_DIV1;
ptr_time.counterdirection = TIMER_COUNTER_DOWN;
ptr_time.period = 1;
if(type == DRI_TIMER_CALC_TYPE_S)
{
//预分频 168MHz / 1680 = 100kHz(1ms),计数值设置为1000则为1S延时
ptr_time.period = 1000;
ptr_time.prescaler = 1680-1;
}
else if(type == DRI_TIMER_CALC_TYPE_MS)
{
ptr_time.prescaler = 1680-1; //100kHz ,定时器计数1次为ms
}
else if(type == DRI_TIMER_CALC_TYPE_US)
{
ptr_time.prescaler = 168-1; //1MHz ,计数1次为us
}
else
{
ptr_time.prescaler = 1680-1;
}
timer_init(DELAY_TIMER, &ptr_time);
/* TIM interrupt enable */
// __TIM_INTR_ENABLE(DELAY_TIMER, TIM_INTR_UPDATE);
/* TIM enable */
timer_enable(DELAY_TIMER);
}
/************************************************
函数名称 : TIMDelay_us
功 能 : 定时器延时 N us
参 数 : Times --- N值
返 回 值 : 无
作 者 :
*************************************************/
void dri_timer_delay_us(uint32_t Times)
{
uint32_t counter = Times & 0xFFFFFFFF;
TIMER_CNT(TIMER3) = counter; //设置计数值
TIMER_CTL0(TIMER3) |= TIMER_CTL0_CEN;
while(TIMER_CNT(TIMER3) > 1);
TIMER_CTL0(TIMER3) &= (uint16_t)(~TIMER_CTL0_CEN);
}
纯属记录学习,如有问题,欢迎指正!