再此给出两个定时器中断,用于控制步进电机的转速
定时器1用来控制步进电机的速度,定时器2控制步进电机脉冲信号
控制一个步进电机感觉STM32已经很累了,多轴联动估计难,要优化的很好才行。有兴趣的朋友,一起来搞一个开源多轴联动的方案吧。
uint16_t capture = 0;
__IO uint16_t bujindianji1maichongjiange = 10000;
__IO uint16_t bujindianji2maichongjiange = 10000;
__IO uint16_t bujindianji3maichongjiange = 10000;
__IO uint16_t bujindianji4maichongjiange = 10000;
s32 ti,ai;
u32 tim[ 10 ];
void TIM1_UP_TIM16_IRQHandler(void)
{
if(TIM_GetITStatus(TIM1, TIM_IT_Update))
{
tim[ 0 ]++;
tim[ 1 ]++;
tim[ 2 ]++;
if(bujindianji1mubiaobushu > bujindianji1xianbushu)
{
bujindianji1jiansusuoxubushu = ((bujindianji1xiansubu/20 * bujindianji1xiansubu) / (bujindianji1jiasubu)*10);
if((bujindianji1mubiaobushu - bujindianji1xianbushu)<bujindianji1jiansusuoxubushu)
{
bujindianji1jiajiansu = -1;
}
if(bujindianji1jiajiansu == -1)
{
bujindianji1xiansubu-=bujindianji1jiasubu / 100;
}
if(bujindianji1jiajiansu == 1)
{
bujindianji1xiansubu+=bujindianji1jiasubu / 100;
}
if(bujindianji1xiansubu > (bujindianji1max * bujindianji1yihaomimaichongshu/1000))bujindianji1xiansubu =( bujindianji1max * bujindianji1yihaomimaichongshu/1000);
if(bujindianji1xiansubu < (bujindianji1min * bujindianji1yihaomimaichongshu/1000))bujindianji1xiansubu =( bujindianji1min * bujindianji1yihaomimaichongshu/1000);
if(bujindianji1xiansubu > (bujindianji1mubiaosudu * bujindianji1yihaomimaichongshu/1000))bujindianji1xiansubu =( bujindianji1mubiaosudu * bujindianji1yihaomimaichongshu/1000);
bujindianji1maichongjiange = 2000000/bujindianji1xiansubu;
}
}
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);//Çå3yTIMxμÄÖD¶Ï′y′|àíλ
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
if(bujindianji1mubiaobushu > bujindianji1xianbushu) //2½½øμç»ú1Õy×a
{
bujindianji1maichongyinjiao =! bujindianji1maichongyinjiao;
bujindianji1xianbushu++;
//bujindianji1xianweizhi = bujinxianji1xianbushu /
}
if(bujindianji1mubiaobushu == bujindianji1xianbushu)
{
bujindianji1jiajiansu = 1;
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
capture = TIM_GetCapture1(TIM2);
TIM_SetCompare1(TIM2, capture + bujindianji1maichongjiange);
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
capture = TIM_GetCapture2(TIM2);
TIM_SetCompare2(TIM2, capture + bujindianji2maichongjiange);
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
capture = TIM_GetCapture3(TIM2);
TIM_SetCompare3(TIM2, capture + bujindianji3maichongjiange);
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);
capture = TIM_GetCapture4(TIM2);
TIM_SetCompare4(TIM2, capture + bujindianji4maichongjiange);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>