generated from hulk/gd32e23x_template_cmake_vscode
fix(timer):修复在输出高电平时,CCR=ARR情况下,驱动信号有毛刺。设置CCR=ARR+1,即可解决要输出高电平时的毛刺问题。
This commit is contained in:
@@ -5,6 +5,8 @@
|
|||||||
#include "gd32e23x.h"
|
#include "gd32e23x.h"
|
||||||
#include "stdbool.h"
|
#include "stdbool.h"
|
||||||
|
|
||||||
|
#define ACTUARTOR_PERIOD_MAX 1000u
|
||||||
|
|
||||||
void actuator_init(void);
|
void actuator_init(void);
|
||||||
|
|
||||||
void actuator_output(actuator_channel_t channel, uint16_t duty, actuator_direction_t direction);
|
void actuator_output(actuator_channel_t channel, uint16_t duty, actuator_direction_t direction);
|
||||||
|
|||||||
+7
-7
@@ -68,7 +68,7 @@ void actuator_init(void) {
|
|||||||
|
|
||||||
void actuator_output(actuator_channel_t channel, uint16_t duty, actuator_direction_t direction) {
|
void actuator_output(actuator_channel_t channel, uint16_t duty, actuator_direction_t direction) {
|
||||||
|
|
||||||
if (duty > 999) duty = 999; // Limit duty cycle to max value
|
if (duty > ACTUARTOR_PERIOD_MAX) duty = ACTUARTOR_PERIOD_MAX; // Limit duty cycle to max value
|
||||||
|
|
||||||
if (direction != DIRECTION_PUSH && direction != DIRECTION_PULL) {
|
if (direction != DIRECTION_PUSH && direction != DIRECTION_PULL) {
|
||||||
// Invalid direction, handle error if necessary
|
// Invalid direction, handle error if necessary
|
||||||
@@ -86,7 +86,7 @@ void actuator_output(actuator_channel_t channel, uint16_t duty, actuator_directi
|
|||||||
} else if (direction == DIRECTION_PULL) {
|
} else if (direction == DIRECTION_PULL) {
|
||||||
SEGGER_RTT_printf(0, "[ACTUATOR] Actuator CH1 PULL with duty %d\n", duty);
|
SEGGER_RTT_printf(0, "[ACTUATOR] Actuator CH1 PULL with duty %d\n", duty);
|
||||||
gpio_bit_set(ACTUATOR_CH1_GPIO_PORT, ACTUATOR_CH1_GPIO_PIN); // Set direction HIGH
|
gpio_bit_set(ACTUATOR_CH1_GPIO_PORT, ACTUATOR_CH1_GPIO_PIN); // Set direction HIGH
|
||||||
timer_channel_output_pulse_value_config(ACTUATOR_PWM_TIMER, ACTUATOR_CH1_PWM_TIMER_CH, (999 - duty));
|
timer_channel_output_pulse_value_config(ACTUATOR_PWM_TIMER, ACTUATOR_CH1_PWM_TIMER_CH, (ACTUARTOR_PERIOD_MAX - duty));
|
||||||
}
|
}
|
||||||
} else if (channel == ACTUATOR_CH2) {
|
} else if (channel == ACTUATOR_CH2) {
|
||||||
if (direction == DIRECTION_PUSH) {
|
if (direction == DIRECTION_PUSH) {
|
||||||
@@ -96,7 +96,7 @@ void actuator_output(actuator_channel_t channel, uint16_t duty, actuator_directi
|
|||||||
} else if (direction == DIRECTION_PULL) {
|
} else if (direction == DIRECTION_PULL) {
|
||||||
SEGGER_RTT_printf(0, "[ACTUATOR] Actuator CH2 PULL with duty %d\n", duty);
|
SEGGER_RTT_printf(0, "[ACTUATOR] Actuator CH2 PULL with duty %d\n", duty);
|
||||||
gpio_bit_set(ACTUATOR_CH2_GPIO_PORT, ACTUATOR_CH2_GPIO_PIN); // Set direction HIGH
|
gpio_bit_set(ACTUATOR_CH2_GPIO_PORT, ACTUATOR_CH2_GPIO_PIN); // Set direction HIGH
|
||||||
timer_channel_output_pulse_value_config(ACTUATOR_PWM_TIMER, ACTUATOR_CH2_PWM_TIMER_CH, (999 - duty));
|
timer_channel_output_pulse_value_config(ACTUATOR_PWM_TIMER, ACTUATOR_CH2_PWM_TIMER_CH, (ACTUARTOR_PERIOD_MAX - duty));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Invalid channel, handle error if necessary
|
// Invalid channel, handle error if necessary
|
||||||
@@ -110,17 +110,17 @@ void actuator_output(actuator_channel_t channel, uint16_t duty, actuator_directi
|
|||||||
void actuator_stop(actuator_channel_t channel) {
|
void actuator_stop(actuator_channel_t channel) {
|
||||||
if(channel == ACTUATOR_CH1) {
|
if(channel == ACTUATOR_CH1) {
|
||||||
SEGGER_RTT_printf(0, "[ACTUATOR] Stopping Actuator CH1\n");
|
SEGGER_RTT_printf(0, "[ACTUATOR] Stopping Actuator CH1\n");
|
||||||
timer_channel_output_pulse_value_config(ACTUATOR_PWM_TIMER, ACTUATOR_CH1_PWM_TIMER_CH, 999); // Ensure CH1 is off
|
timer_channel_output_pulse_value_config(ACTUATOR_PWM_TIMER, ACTUATOR_CH1_PWM_TIMER_CH, ACTUARTOR_PERIOD_MAX); // Ensure CH1 is off
|
||||||
gpio_bit_set(ACTUATOR_CH1_GPIO_PORT, ACTUATOR_CH1_GPIO_PIN); // Set CH1 direction High
|
gpio_bit_set(ACTUATOR_CH1_GPIO_PORT, ACTUATOR_CH1_GPIO_PIN); // Set CH1 direction High
|
||||||
} else if(channel == ACTUATOR_CH2) {
|
} else if(channel == ACTUATOR_CH2) {
|
||||||
SEGGER_RTT_printf(0, "[ACTUATOR] Stopping Actuator CH2\n");
|
SEGGER_RTT_printf(0, "[ACTUATOR] Stopping Actuator CH2\n");
|
||||||
timer_channel_output_pulse_value_config(ACTUATOR_PWM_TIMER, ACTUATOR_CH2_PWM_TIMER_CH, 999); // Ensure CH2 is off
|
timer_channel_output_pulse_value_config(ACTUATOR_PWM_TIMER, ACTUATOR_CH2_PWM_TIMER_CH, ACTUARTOR_PERIOD_MAX); // Ensure CH2 is off
|
||||||
gpio_bit_set(ACTUATOR_CH2_GPIO_PORT, ACTUATOR_CH2_GPIO_PIN); // Set CH2 direction High
|
gpio_bit_set(ACTUATOR_CH2_GPIO_PORT, ACTUATOR_CH2_GPIO_PIN); // Set CH2 direction High
|
||||||
} else {
|
} else {
|
||||||
// Invalid channel, handle error if necessary
|
// Invalid channel, handle error if necessary
|
||||||
SEGGER_RTT_printf(0, "[ACTUATOR] Invalid channel specified for stop: %d\n", channel);
|
SEGGER_RTT_printf(0, "[ACTUATOR] Invalid channel specified for stop: %d\n", channel);
|
||||||
timer_channel_output_pulse_value_config(ACTUATOR_PWM_TIMER, ACTUATOR_CH1_PWM_TIMER_CH, 999); // Ensure CH1 is off
|
timer_channel_output_pulse_value_config(ACTUATOR_PWM_TIMER, ACTUATOR_CH1_PWM_TIMER_CH, ACTUARTOR_PERIOD_MAX); // Ensure CH1 is off
|
||||||
timer_channel_output_pulse_value_config(ACTUATOR_PWM_TIMER, ACTUATOR_CH2_PWM_TIMER_CH, 999); // Ensure CH2 is off
|
timer_channel_output_pulse_value_config(ACTUATOR_PWM_TIMER, ACTUATOR_CH2_PWM_TIMER_CH, ACTUARTOR_PERIOD_MAX); // Ensure CH2 is off
|
||||||
gpio_bit_set(ACTUATOR_CH1_GPIO_PORT, ACTUATOR_CH1_GPIO_PIN); // Set CH1 direction High
|
gpio_bit_set(ACTUATOR_CH1_GPIO_PORT, ACTUATOR_CH1_GPIO_PIN); // Set CH1 direction High
|
||||||
gpio_bit_set(ACTUATOR_CH2_GPIO_PORT, ACTUATOR_CH2_GPIO_PIN); // Set CH2 direction High
|
gpio_bit_set(ACTUATOR_CH2_GPIO_PORT, ACTUATOR_CH2_GPIO_PIN); // Set CH2 direction High
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -458,7 +458,7 @@ void handle_command(const uint8_t *frame, uint8_t len) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p_val > (uint32_t)ACTUATOR_CH2 || s_val > 999u) {
|
if (p_val > (uint32_t)ACTUATOR_CH2 || s_val > ACTUARTOR_PERIOD_MAX) {
|
||||||
send_response(RESP_TYPE_PARAM_ERR, s_report_status_err, sizeof(s_report_status_err));
|
send_response(RESP_TYPE_PARAM_ERR, s_report_status_err, sizeof(s_report_status_err));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -480,7 +480,7 @@ void handle_command(const uint8_t *frame, uint8_t len) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p_val > (uint32_t)ACTUATOR_CH2 || s_val > 999u) {
|
if (p_val > (uint32_t)ACTUATOR_CH2 || s_val > ACTUARTOR_PERIOD_MAX) {
|
||||||
send_response(RESP_TYPE_PARAM_ERR, s_report_status_err, sizeof(s_report_status_err));
|
send_response(RESP_TYPE_PARAM_ERR, s_report_status_err, sizeof(s_report_status_err));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user