初级会员

- 积分
- 65
- 金钱
- 65
- 注册时间
- 2021-9-14
- 在线时间
- 18 小时
|
10金钱
程序:基于STM32精确控制步进电机步数
芯片 STM32f103zet6
使用 HAL库
软件 STM32CubeIDE
通过高级定时器PWM输出脉冲,在高级定时器up回调函数内使用重复寄存器来计算步数
串口控制动作指令
重复寄存器RCR赋值是在中断回调函数赋值的
TIM8->RCR = RCR_VAL; //*********RCR_VAL是我设定值
rcr_finish只是个标志位;rcr_integer rcr_remainder 是设定值的部分组成 motor_dir 是方向的参数 CW 等等无关紧要
关键是:我RCR赋值在回调函数里面怎么就没用,开机设定走200步,就只走一步,之后走两百步正常,随后走第一次三百步,只走了200步,第之后走三百步正常。随后走500只走300步,随后正常,就好像就晚一拍。
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {//定时器中断回调函数
- if(htim->Instance == htim8.Instance)
- {
- if(rcr_finish == 0)
- {
- if(rcr_integer != 0)
- {
- TIM8->RCR = RCR_VAL;
- rcr_integer--;
- }
- else if(rcr_remainder != 0)
- {
- TIM8->RCR = rcr_remainder - 1;
- rcr_remainder = 0;
- rcr_finish = 1;
- }
- else
- goto out;
- // __HAL_TIM_MOE_ENABLE(&htim8);
- HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_3);
- // HAL_TIM_Base_Start_IT(&htim8);
- if(motor_dir == CW)
- current_pos += (TIM8->RCR + 1);
- else
- current_pos -= (TIM8->RCR + 1);
- }
- else
- {
- out: rcr_finish = 1;
- HAL_TIM_PWM_Stop(&htim8, TIM_CHANNEL_3);
- }
- }
- }
复制代码
stm32中文参考手册有这一段描述:
但是hal库,我搞不清楚,故不知道该怎么做,
|
|