@@ -37,6 +37,7 @@ extern "C" { | |||
#include "modbus_crc.h" | |||
#include "flash_save.h" | |||
#include "modbus_log.h" | |||
#include "tim.h" | |||
/* USER CODE END Includes */ | |||
extern UART_HandleTypeDef huart1; | |||
@@ -51,6 +52,7 @@ extern UART_HandleTypeDef huart1; | |||
#define MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F ///> 写多个线圈 | |||
#define MODBUS_FC_WRITE_MULTIPLE_REGS 0x10 ///> 写多个保持寄存器 | |||
#define MODBUS_FC_SEND_LOG 0x46 ///> 发送Modbus日志 | |||
#define MODBUS_FC_CLEAR_PLUSE 0x47 ///> 清除脉冲计数 | |||
///> ==================== Modbus异常码定义 ==================== | |||
#define MODBUS_EX_NONE 0x00 ///> 无异常 | |||
@@ -93,7 +95,7 @@ void Modbus_SetHoldingRegister(uint16_t addr, uint16_t value); | |||
uint16_t Modbus_GetHoldingRegister(uint16_t addr); | |||
void Modbus_Handle_SendLog(uint8_t* frame, uint16_t length); | |||
void Modbus_Process_Write_Reg(uint8_t *frame, uint16_t length); | |||
void Modbus_Clear_Pluse(uint8_t* frame, uint16_t length); | |||
// USART功能函数 | |||
void USART1_SendData(uint8_t* data, uint16_t len); | |||
uint8_t USART1_SendData_DMA(uint8_t* data, uint16_t len); | |||
@@ -56,20 +56,14 @@ void MX_GPIO_Init(void) | |||
/*Configure GPIO pin : PB5 */ | |||
GPIO_InitStruct.Pin = GPIO_PIN_5; | |||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; | |||
GPIO_InitStruct.Pull = GPIO_NOPULL; | |||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); | |||
/*Configure GPIO pin : PB7 */ | |||
GPIO_InitStruct.Pin = GPIO_PIN_7; | |||
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; | |||
GPIO_InitStruct.Pull = GPIO_PULLUP; | |||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); | |||
/*Configure GPIO pin : PG12 */ | |||
GPIO_InitStruct.Pin = GPIO_PIN_12; | |||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; | |||
GPIO_InitStruct.Pull = GPIO_NOPULL; | |||
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; | |||
GPIO_InitStruct.Pull = GPIO_PULLUP; | |||
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct); | |||
/*Configure GPIO pins : PH6 PH8 PH9 PH7 */ | |||
@@ -83,6 +77,9 @@ void MX_GPIO_Init(void) | |||
HAL_NVIC_SetPriority(EXTI9_5_IRQn, 3, 0); | |||
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); | |||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0); | |||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); | |||
} | |||
/* USER CODE BEGIN 2 */ | |||
@@ -100,7 +97,7 @@ static volatile uint8_t s_trigger_flag = 0; ///< 触发标志,防止重 | |||
*/ | |||
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) | |||
{ | |||
if (GPIO_Pin == GPIO_PIN_7) | |||
if (GPIO_Pin == GPIO_PIN_12||GPIO_Pin == GPIO_PIN_5) | |||
{ | |||
uint32_t current_time = HAL_GetTick(); | |||
/* 防抖处理:检查时间间隔和触发标志 */ | |||
@@ -318,12 +318,24 @@ void EXTI9_5_IRQHandler(void) | |||
/* USER CODE BEGIN EXTI9_5_IRQn 0 */ | |||
/* USER CODE END EXTI9_5_IRQn 0 */ | |||
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_7); | |||
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_5); | |||
/* USER CODE BEGIN EXTI9_5_IRQn 1 */ | |||
/* USER CODE END EXTI9_5_IRQn 1 */ | |||
} | |||
void EXTI15_10_IRQHandler(void) | |||
{ | |||
/* USER CODE BEGIN EXTI15_10_IRQn 0 */ | |||
/* USER CODE END EXTI15_10_IRQn 0 */ | |||
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_12); | |||
/* USER CODE BEGIN EXTI15_10_IRQn 1 */ | |||
/* USER CODE END EXTI15_10_IRQn 1 */ | |||
} | |||
/* USER CODE BEGIN 1 */ | |||
/* USER CODE END 1 */ |
@@ -963,17 +963,18 @@ static void PLSR_UpdateGlobalPulseCount(int32_t current_pulse_count) | |||
{ | |||
if (current_pulse_count > s_last_total_pulse) { | |||
int32_t pulse_increment = current_pulse_count - s_last_total_pulse; | |||
if (s_pulse_count_direction) | |||
{ | |||
// 脉冲计数方向为1:递增 | |||
g_plsr_total_pulse_count += pulse_increment; | |||
} | |||
else | |||
{ | |||
// 脉冲计数方向为0:递减(支持负数显示) | |||
g_plsr_total_pulse_count -= pulse_increment; | |||
} | |||
// 脉冲计数方向为1:递增 | |||
g_plsr_total_pulse_count += pulse_increment; | |||
// if (s_pulse_count_direction) | |||
// { | |||
// // 脉冲计数方向为1:递增 | |||
// g_plsr_total_pulse_count += pulse_increment; | |||
// } | |||
// else | |||
// { | |||
// // 脉冲计数方向为0:递减(支持负数显示) | |||
// g_plsr_total_pulse_count -= pulse_increment; | |||
// } | |||
s_last_total_pulse = current_pulse_count; | |||
} | |||
// 将32位全局累加脉冲计数分解为两个16位寄存器(支持负数) | |||
@@ -1524,6 +1525,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) | |||
// 更新路由结构体的脉冲计数(使用原子操作保证一致性) | |||
g_plsr_route.pulse_count = current_pulse_count; | |||
// 处理加减速过程中的频率更新(使用新的直线加减速算法) | |||
if(g_plsr_route.run_state == PLSR_STATE_ACCEL || g_plsr_route.run_state == PLSR_STATE_DECEL) | |||
{ | |||
@@ -307,7 +307,7 @@ void Modbus_Process(void) | |||
frame_len++; | |||
// 检查是否为完整帧 | |||
if(frame_len >= 8) | |||
if(frame_len >= 5) | |||
{ | |||
// 验证CRC | |||
uint16_t crc_received = (frame[frame_len-1] << 8) | frame[frame_len-2]; | |||
@@ -332,6 +332,9 @@ void Modbus_Process(void) | |||
case MODBUS_FC_SEND_LOG: | |||
Modbus_Handle_SendLog(frame, frame_len); | |||
break; | |||
case MODBUS_FC_CLEAR_PLUSE: | |||
Modbus_Clear_Pluse(frame, frame_len); | |||
break; | |||
default: | |||
Modbus_Send_Exception(function_code, MODBUS_EX_ILLEGAL_FUNCTION); | |||
break; | |||
@@ -342,6 +345,25 @@ void Modbus_Process(void) | |||
} | |||
} | |||
void Modbus_Clear_Pluse(uint8_t* frame, uint16_t length) | |||
{ | |||
if(length != 5) | |||
{ | |||
Modbus_Send_Exception(MODBUS_FC_CLEAR_PLUSE, MODBUS_EX_ILLEGAL_DATA_VALUE); | |||
return; | |||
} | |||
uint8_t response[3]; | |||
response[0] = ModbusSlave.slave_address; | |||
response[1] = MODBUS_FC_CLEAR_PLUSE; | |||
response[2] = 0; // 无数据返回 | |||
g_plsr_total_pulse_count = 0; // 假设0x100寄存器用于存储脉冲计数 | |||
int32_t signed_count = g_plsr_total_pulse_count; // 使用有符号数进行计算 | |||
ModbusSlave.holding_regs[0x1000] = signed_count & 0xFFFF; // 低16位 | |||
ModbusSlave.holding_regs[0x1001] = (signed_count >> 16) & 0xFFFF; // 高16位 | |||
Modbus_Send_Response(response, 3); | |||
} | |||
/** | |||
* @brief 处理Modbus日志发送请求 | |||
* @param frame 接收的Modbus帧 | |||