From 0d8cd8c2f25abbc3d67260717a53019218228adf Mon Sep 17 00:00:00 2001 From: qiuxin Date: Tue, 3 Jun 2025 15:11:16 +0800 Subject: [PATCH] =?UTF-8?q?87=E6=8C=87=E4=BB=A4=E6=9F=A5=E8=AF=A2=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20-=20=E8=B0=83=E6=95=B4=20IO=20=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E7=9A=84=E5=BB=B6=E8=BF=9F=E6=97=B6=E9=97=B4?= =?UTF-8?q?=EF=BC=8C=E4=BB=8E=20200ms=20=E6=94=B9=E4=B8=BA=2050ms=20-=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20U74HC245=20GPIO=20=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E5=87=BD=E6=95=B0=20-=20=E4=BC=98=E5=8C=96=20TCA6416?= =?UTF-8?q?=20=E5=88=9D=E5=A7=8B=E5=8C=96=E6=B5=81=E7=A8=8B=EF=BC=8C?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=AB=AF=E5=8F=A3=E6=9E=81=E6=80=A7=E5=92=8C?= =?UTF-8?q?=E6=96=B9=E5=90=91=E8=AE=BE=E7=BD=AE=20-=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=20TCP=20=E6=9C=8D=E5=8A=A1=E5=99=A8=E4=B8=AD=E5=86=97=E4=BD=99?= =?UTF-8?q?=E7=9A=84=20TCA6416=20=E5=86=99=E5=85=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Src/freertos.c | 3 ++- User/application/inc/TCA6416.h | 1 + User/application/src/TCA6416.c | 27 ++++++++++++++++++----- User/application/src/tcpserverc.c | 36 ++----------------------------- 4 files changed, 27 insertions(+), 40 deletions(-) diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index d5810b4..000a6f0 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -503,6 +503,7 @@ void start_io_control_task(void const *argument) // 获取信号量 if(osSemaphoreWait(io_semaphoreHandle, 100) == osOK) { + //写入第一块 TCA6416_WritePort(0, TCA6416_WritePort_buff[1]); osDelay(5); TCA6416_WritePort(1, TCA6416_WritePort_buff[0]); @@ -541,7 +542,7 @@ void start_io_control_task(void const *argument) // 200ms扫描周期 - osDelay(200); + osDelay(50); } /* USER CODE END start_io_control_task */ } diff --git a/User/application/inc/TCA6416.h b/User/application/inc/TCA6416.h index ae131a6..7b70c1f 100644 --- a/User/application/inc/TCA6416.h +++ b/User/application/inc/TCA6416.h @@ -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_WritePin(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底层函数 */ void I2C_Start(void); diff --git a/User/application/src/TCA6416.c b/User/application/src/TCA6416.c index ba9232a..ac62f32 100644 --- a/User/application/src/TCA6416.c +++ b/User/application/src/TCA6416.c @@ -44,6 +44,21 @@ void TCA6416_GPIO_Init2(void) SCL_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起始信号 @@ -565,10 +580,14 @@ uint8_t TCA6416_SetPortPolarity2(uint8_t port, uint8_t polarity) uint8_t TCA6416_Init3(void) { // GPIO已在TCA6416_GPIO_Init()中初始化 - + U74HC245_GPIO_Init(); // 增加延时以确保设备稳定 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(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(1, 0x00) != 0) return 1; - // 设置默认极性(非反转) - if(TCA6416_SetPortPolarity3(0, 0x00) != 0) return 1; - if(TCA6416_SetPortPolarity3(1, 0x00) != 0) return 1; + return 0; } diff --git a/User/application/src/tcpserverc.c b/User/application/src/tcpserverc.c index 0c4a3d5..63c29ac 100644 --- a/User/application/src/tcpserverc.c +++ b/User/application/src/tcpserverc.c @@ -810,43 +810,11 @@ static err_t tcpecho_recv_control(void *arg, struct tcp_pcb *tpcb, struct pbuf * //中间4个字节是比例阀 + // 数字量输出刷新第一个芯片的输出状态 TCA6416_WritePort_buff[0] = tcp_rx_data[31]; 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数组 memcpy(adc_set_data, tcp_rx_data + 9, body_len);