STM32直流电机控制程序简明教程

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1STM32直流减速电机控制(来自网络,特别推荐)直流减速电机控制中,最常用的方法就是通过PWM来控制直流电机的转速。在控制小车走直线的过程中,需要两者的转速一置(如果要走得很直,还需要在短时间内保证两者的行程大致相当,这可以用PID算法来控制,以后的文章中会专门叙述)。因此,在检测到两者转速不一样时,需要动态调整其中一个或两个轮子的PWM的点空比(简单点的就以一个轮为基准,调整另外一个轮子即可;如果以一个固定的标准的话,需要调整两个轮子的PWM占空比)。程序第一步:设置GPIO,略(输出PWM的管脚用Mode_AF_PP即可)程序第二步:设置定时器,(保证产生两路PWM即可,我用的是TIM4)voidTIM4_Configuration(void){TIM_TimeBaseInitTypeDefTIM_TimeBaseInitStructure;TIM_OCInitTypeDefTIM_OCInitStructure;//时间基初始化TIM_TimeBaseInitStructure.TIM_Period=144;//18K/144=125Hz,这个是电机PWM的频率TIM_TimeBaseInitStructure.TIM_Prescaler=4000;//72000000/4000=18KTIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0x0000;TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);//输出比较模式设置,用于4路PWM输出TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;//输出PWMTIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//使能正向通道TIM_OCInitStructure.TIM_OutputNState=TIM_OutputState_Disable;//失能反向通道TIM_OCInitStructure.TIM_Pulse=PWM_L;//左轮DIR的占空比TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;//输出极性为低电平TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCPolarity_High;//互补输出极性为高电平TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;TIM_OC1Init(TIM4,&TIM_OCInitStructure);//PWM_L初始化TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Disable);//改变点空比后,立即产生效应2TIM_OCInitStructure.TIM_Pulse=PWM_R;//左轮PWM的占空比TIM_OC2Init(TIM4,&TIM_OCInitStructure);//PWM_R初始化TIM_OC2PreloadConfig(TIM4,TIM_OCPreload_Disable);//改变点空比后,立即产生效应//使能定时器4TIM_Cmd(TIM4,ENABLE);TIM_CtrlPWMOutputs(TIM4,ENABLE);}程序第三步:在SysTick中断中,读取两个轮子的速(具体的方法是:每0.1秒读一次,并以此人作为速度的依据),并比较,如果以右轮为基准,则调整左轮的PWM占空比。涉及到关键语句是:TIM_SetCompare1(;)u16COUN1=0;u16COUN2=0;volatileu16Dist_L=0;//左轮行程脉冲数volatileu16Dist_R=0;//右轮行程脉冲数voidSysTick_Handler(void){COUN1=TIM1-CNT;//左轮在0.1秒里脉冲数COUN2=TIM2-CNT;//右轮在0.1秒里脉冲数Dist_L=Dist_L+COUN1;//左轮行程脉冲数Dist_R=Dist_R+COUN2;//右轮行程脉冲数if((COUN1-COUN2)2){PWM_L=TIM_GetCapture1(TIM4);TIM_SetCompare1(TIM4,PWM_L-4);}elseif((COUN2-COUN1)2){PWM_L=TIM_GetCapture1(TIM4);TIM_SetCompare1(TIM4,PWM_L+4);}TIM_SetCounter(TIM1,0);TIM_SetCounter(TIM2,0);}3关于TIM_SetCompareX(;)这个函数,还是有很多用途的,其中另外一个用途,就是用于产生不同频率的PWM,具体程序如下:u16capture=0;externvu16CCR1_Val;externvu16CCR2_Val;externvu16CCR3_Val;externvu16CCR4_Val;voidTIM2_IRQHandler(void){/*TIM2_CH1togglingwithfrequency=183.1Hz*/if(TIM_GetITStatus(TIM2,TIM_IT_CC1)!=RESET){TIM_ClearITPendingBit(TIM2,TIM_IT_CC1);capture=TIM_GetCapture1(TIM2);TIM_SetCompare1(TIM2,capture+CCR1_Val);}/*TIM2_CH2togglingwithfrequency=366.2Hz*/if(TIM_GetITStatus(TIM2,TIM_IT_CC2)!=RESET){TIM_ClearITPendingBit(TIM2,TIM_IT_CC2);capture=TIM_GetCapture2(TIM2);TIM_SetCompare2(TIM2,capture+CCR2_Val);}/*TIM2_CH3togglingwithfrequency=732.4Hz*/if(TIM_GetITStatus(TIM2,TIM_IT_CC3)!=RESET){TIM_ClearITPendingBit(TIM2,TIM_IT_CC3);capture=TIM_GetCapture3(TIM2);TIM_SetCompare3(TIM2,capture+CCR3_Val);}/*TIM2_CH4togglingwithfrequency=1464.8Hz*/if(TIM_GetITStatus(TIM2,TIM_IT_CC4)!=RESET){TIM_ClearITPendingBit(TIM2,TIM_IT_CC4);capture=TIM_GetCapture4(TIM2);TIM_SetCompare4(TIM2,capture+CCR4_Val);4}}

1 / 4
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功