87指令查询优化

- 调整 IO 控制任务的延迟时间,从 200ms 改为 50ms
- 添加 U74HC245 GPIO 初始化函数
- 优化 TCA6416 初始化流程,调整端口极性和方向设置
- 移除 TCP 服务器中冗余的 TCA6416 写入逻辑
This commit is contained in:
qiuxin 2025-06-03 15:11:16 +08:00
parent 0e8af8fa30
commit 0d8cd8c2f2
4 changed files with 27 additions and 40 deletions

View File

@ -503,6 +503,7 @@ void start_io_control_task(void const *argument)
// 获取信号量 // 获取信号量
if(osSemaphoreWait(io_semaphoreHandle, 100) == osOK) if(osSemaphoreWait(io_semaphoreHandle, 100) == osOK)
{ {
//写入第一块
TCA6416_WritePort(0, TCA6416_WritePort_buff[1]); TCA6416_WritePort(0, TCA6416_WritePort_buff[1]);
osDelay(5); osDelay(5);
TCA6416_WritePort(1, TCA6416_WritePort_buff[0]); TCA6416_WritePort(1, TCA6416_WritePort_buff[0]);
@ -541,7 +542,7 @@ void start_io_control_task(void const *argument)
// 200ms扫描周期 // 200ms扫描周期
osDelay(200); osDelay(50);
} }
/* USER CODE END start_io_control_task */ /* USER CODE END start_io_control_task */
} }

View File

@ -87,6 +87,7 @@ uint8_t TCA6416_SetPortPolarity2(uint8_t port, uint8_t polarity); // 第二个
uint8_t TCA6416_SetPortPolarity3(uint8_t port, uint8_t polarity); // 第三个芯片的设置极性函数 uint8_t TCA6416_SetPortPolarity3(uint8_t port, uint8_t polarity); // 第三个芯片的设置极性函数
uint8_t TCA6416_WritePin(uint8_t port, uint8_t pin, uint8_t state); uint8_t TCA6416_WritePin(uint8_t port, uint8_t pin, uint8_t state);
uint8_t TCA6416_ReadPin(uint8_t port, uint8_t pin, uint8_t *state); uint8_t TCA6416_ReadPin(uint8_t port, uint8_t pin, uint8_t *state);
void U74HC245_GPIO_Init(void);
/* 软件I2C底层函数 */ /* 软件I2C底层函数 */
void I2C_Start(void); void I2C_Start(void);

View File

@ -44,6 +44,21 @@ void TCA6416_GPIO_Init2(void)
SCL_HIGH2(); SCL_HIGH2();
SDA_HIGH2(); SDA_HIGH2();
} }
void U74HC245_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
// 使能GPIOE时钟
__HAL_RCC_GPIOD_CLK_ENABLE();
//74HC245 DIR端口设置
GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; // 开漏输出
GPIO_InitStruct.Pull = GPIO_PULLDOWN; // 下拉
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4 | GPIO_PIN_7, GPIO_PIN_RESET);
}
/** /**
* @brief I2C起始信号 * @brief I2C起始信号
@ -565,10 +580,14 @@ uint8_t TCA6416_SetPortPolarity2(uint8_t port, uint8_t polarity)
uint8_t TCA6416_Init3(void) uint8_t TCA6416_Init3(void)
{ {
// GPIO已在TCA6416_GPIO_Init()中初始化 // GPIO已在TCA6416_GPIO_Init()中初始化
U74HC245_GPIO_Init();
// 增加延时以确保设备稳定 // 增加延时以确保设备稳定
HAL_Delay(10); HAL_Delay(10);
// 设置默认极性(非反转)
if(TCA6416_SetPortPolarity3(0, 0x00) != 0) return 1;
if(TCA6416_SetPortPolarity3(1, 0x00) != 0) return 1;
// 默认将所有引脚设置为输入 // 默认将所有引脚设置为输入
if(TCA6416_SetPortDirection3(0, 0xFF) != 0) return 1; if(TCA6416_SetPortDirection3(0, 0xFF) != 0) return 1;
if(TCA6416_SetPortDirection3(1, 0xFF) != 0) return 1; if(TCA6416_SetPortDirection3(1, 0xFF) != 0) return 1;
@ -577,9 +596,7 @@ uint8_t TCA6416_Init3(void)
if(TCA6416_WritePort3(0, 0x00) != 0) return 1; if(TCA6416_WritePort3(0, 0x00) != 0) return 1;
if(TCA6416_WritePort3(1, 0x00) != 0) return 1; if(TCA6416_WritePort3(1, 0x00) != 0) return 1;
// 设置默认极性(非反转)
if(TCA6416_SetPortPolarity3(0, 0x00) != 0) return 1;
if(TCA6416_SetPortPolarity3(1, 0x00) != 0) return 1;
return 0; return 0;
} }

View File

@ -810,43 +810,11 @@ static err_t tcpecho_recv_control(void *arg, struct tcp_pcb *tpcb, struct pbuf *
//中间4个字节是比例阀 //中间4个字节是比例阀
// 数字量输出刷新第一个芯片的输出状态 // 数字量输出刷新第一个芯片的输出状态
TCA6416_WritePort_buff[0] = tcp_rx_data[31]; TCA6416_WritePort_buff[0] = tcp_rx_data[31];
TCA6416_WritePort_buff[1] = tcp_rx_data[32]; TCA6416_WritePort_buff[1] = tcp_rx_data[32];
// if(tcp_rx_data[27] != 0xFF || tcp_rx_data[28] != 0xFF)
// { // 只在值变化时更新
// // 获取信号量保护
// if(osSemaphoreWait(io_semaphoreHandle, 100) == osOK)
// {
// // 设置输出前先确认端口方向
// TCA6416_SetPortDirection(0, 0x00); // 确保Port0为输出
// osDelay(5);
// TCA6416_SetPortDirection(1, 0x00); // 确保Port1为输出
// osDelay(5);
// // 写入输出值
// TCA6416_WritePort(0, tcp_rx_data[27]); // Port0的值8-15
// osDelay(5);
// TCA6416_WritePort(1, tcp_rx_data[28]); // Port1的值0-7
// osDelay(5);
// // 验证写入是否成功
// uint8_t verify0, verify1;
// if(TCA6416_ReadPort(0, &verify0) == 0 && TCA6416_ReadPort(1, &verify1) == 0)
// {
// if(verify0 != tcp_rx_data[27] || verify1 != tcp_rx_data[28])
// {
// // 如果验证失败,重试一次
// TCA6416_WritePort(0, tcp_rx_data[27]);
// osDelay(5);
// TCA6416_WritePort(1, tcp_rx_data[28]);
// }
// }
// // 释放信号量
// osSemaphoreRelease(io_semaphoreHandle);
// }
// }
// 保存设置的数据到adc_set_data数组 // 保存设置的数据到adc_set_data数组
memcpy(adc_set_data, tcp_rx_data + 9, body_len); memcpy(adc_set_data, tcp_rx_data + 9, body_len);