以8M主时钟定时30us为例,代码如下:
//溢出值:15(0xF)
//溢出次数:1
//实际延时:0.030000(ms)
//延时误差:0.000000(ms)
//计数频率:500000.000000(Hz)
//计数周期:0.002000(ms)
//溢出频率:33333.333333(Hz)
//溢出周期:0.030000(ms)
void Tim4Init(void)
{
//TIM4定时器时钟允许
CLK->PCKENR1 |= (1<<4);
TIM4->ARR =15;//自动装载值
TIM4->PSCR=4;//2^4=16分频(最大7)
//向上计数自动更新开始计数
TIM4->CR1 = TIM4_CR1_ARPE|TIM4_CR1_CEN;
//中断允许
TIM4->IER |= TIM1_IER_UIE;
}
//约30us中断一次
INTERRUPT void TIM4_IRQHandler(void)
{
//...用户程序
TIM4->SR1 =0; //清中断标记
}
|
|