diff --git a/User/bsp/inc/bsp_step_moto.h b/User/bsp/inc/bsp_step_moto.h index 56a449e..ad4aa5f 100644 --- a/User/bsp/inc/bsp_step_moto.h +++ b/User/bsp/inc/bsp_step_moto.h @@ -31,6 +31,7 @@ typedef struct int64_t pv_pulse; // 当前的脉冲值 int64_t sv_pulse; // 电脑端的设定脉冲值 int64_t prev_pv_pulse;// 上一次脉冲值,用于检测过零点 + int32_t zero_counter; // 过零点步数计数器,每走一步+1或-1,到STEP_PER_LAP翻转PA6 } MOTO_T; extern MOTO_T g_tMoto; diff --git a/User/bsp/src/bsp_step_moto.c b/User/bsp/src/bsp_step_moto.c index a1a10a7..820c118 100644 --- a/User/bsp/src/bsp_step_moto.c +++ b/User/bsp/src/bsp_step_moto.c @@ -55,6 +55,7 @@ void bsp_InitStepMoto(void) g_tMoto.pv_pulse = 0; g_tMoto.sv_pulse = 0; g_tMoto.prev_pv_pulse = 0; + g_tMoto.zero_counter = 0; } //------------------------------------------------------------------------------ @@ -116,6 +117,7 @@ void MOTO_ZorePos(void) g_tMoto.sv_pulse = 0; g_tMoto.pv_pulse = 0; g_tMoto.prev_pv_pulse = 0; + g_tMoto.zero_counter = 0; GPIO_PORT_ZERO_SIG->BRR = GPIO_PIN_ZERO_SIG; // 褰掗浂鏃 PA6 杈撳嚭浣 BEEP_Start(1500, 5, 5, 3); } @@ -199,14 +201,21 @@ void MOTO_ISR(void) if (g_tMoto.pv_pulse < g_tMoto.sv_pulse) { g_tMoto.pv_pulse++; + g_tMoto.zero_counter++; } else if (g_tMoto.pv_pulse > g_tMoto.sv_pulse) { g_tMoto.pv_pulse--; + g_tMoto.zero_counter--; } - // 妫娴嬭繃闆剁偣锛歱v_pulse 姣忚法瓒婁竴涓 STEP_PER_LAP锛堝嵆姣忚浆涓鍦堬級缈昏浆PA6 - if ((g_tMoto.prev_pv_pulse / STEP_PER_LAP) != (g_tMoto.pv_pulse / STEP_PER_LAP)) + if (g_tMoto.zero_counter >= STEP_PER_LAP) { + g_tMoto.zero_counter -= STEP_PER_LAP; + ZERO_SIG_TOGGLE(); + } + else if (g_tMoto.zero_counter <= -STEP_PER_LAP) + { + g_tMoto.zero_counter += STEP_PER_LAP; ZERO_SIG_TOGGLE(); } break;