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);