利用stm32f103的TIM2实现精确延时
扫描二维码
随时随地手机看文章
void TIM_ResetCounter(TIM_TypeDef * TIMx)
{
TIMx->CNT = 0;
}
void TIM2_Delay_ms(unsigned int cms)
{
unsigned short temp = 0;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 36000 - 1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseStructure.TIM_Period = cms * 2;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_Cmd(TIM2, ENABLE);
do
{
temp = TIM_GetCounter(TIM2);
}while(temp != 0x01);
TIM_Cmd(TIM2, DISABLE);
TIM_ResetCounter(TIM2);
}