我在这个函数中遇到了一个问题,特别是涉及一个结构和一个 for 循环。
每当循环 1 到达 i = 27 并到达第 29 行 (L293D_step) 时,struct STEPPERZ 中的值从 6 变为 1000 万中的某个值。
- bool readSurfaceData()
- {
- directionX = -1 * RESET_DIRECTION;
- for(uint8_t i = 0; i < stepper_Z_steps[servoStatus] / step_length; i++)
- {
- for (uint8_t j = 0; j < (round(stepper_X_steps[servoStatus] / step_length)+1); j++)
- {
- L293D_step(STEPPERX, step_length * directionX);
- x = step_length * directionX;
- fillMatrixSlot_V2(tooth, i, j);
- }
- if (directionX < 0)
- {
- x += step_length;
- }
- else
- {
- x -= step_length;
- }
- directionX *=(-1);
- HAL_Delay(Z_delay);
- L293D_step(STEPPERZ, step_length * RESET_DIRECTION * -1);
- z+=step_length * RESET_DIRECTION * -1;
- HAL_Delay(scan_delay);
- }
- if(!sendData())
- {
- return false;
- }
- return true;
- }
这是 L293D_step:
- void L293D_step(struct Stepper_Driver *driver, int steps_to_move) {
- int steps_left = abs(steps_to_move);
- int this_step = 0;
- if (steps_to_move > 0) {
- driver->direction = 1;
- }
- if (steps_to_move < 0) {
- driver->direction = 0;
- }
- while (steps_left > 0) {
- unsigned long now = HAL_GetTick();
- if ((now - driver->last_step_time) >= (driver->step_delay)) {
- driver->last_step_time = now;
- if (driver->direction == 1) {
- driver->step_number++;
- if (driver->step_number == driver->number_of_steps) {
- driver->step_number = 0;
- }
- } else {
- if (driver->step_number == 0) {
- driver->step_number = driver->number_of_steps;
- }
- driver->step_number--;
- }
- steps_left--;
- this_step = driver ->step_number % 4;
- L293D_stepMotor(driver, this_step);
- }
- }
- }
和 Stepper_Driver 结构:
- struct Stepper_Driver
- {
- uint16_t c1;
- uint16_t c2;
- uint16_t c3;
- uint16_t c4;
- GPIO_TypeDef *p1;
- GPIO_TypeDef *p2;
- GPIO_TypeDef *p3;
- GPIO_TypeDef *p4;
- uint8_t direction;
- unsigned long step_delay;
- uint8_t number_of_steps;
- int step_number;
- unsigned long last_step_time;
- };
我检查了一个单独项目中的功能,它们工作得很好,我一直在检查两者之间的不一致,但到目前为止找不到。如果有人知道可能导致此故障的原因,我们将不胜感激。
编辑:我只记得我一直在玩弄 MX 中的时钟配置,以便更好地了解它的工作原理,并且它运行在比主文件更低的 sysclk 上。不确定这是否是问题所在,但我将对其进行调整以消除这种不一致