您的位置 首页 开关

使用stm32f103的TIM2完成准确延时

voidTIM_ResetCounter(TIM_TypeDef*TIMx){TIMx->CNT=0;}voidTIM2_Delay_ms(unsignedintcms){unsignedsh.

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);/*此处不能计数到0*/TIM_Cmd(TIM2, DISABLE);TIM_ResetCounter(TIM2);/*将计数寄存器的值康复为0是有必要的*/}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/dianyuan/kaiguan/265913.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部