1. 测试了IO拓展芯片TCA9554PWR,测试结果正常,测试通过

This commit is contained in:
Misaki
2025-08-24 19:11:42 +08:00
parent 8807542ff2
commit fa066408e2
10 changed files with 426 additions and 2 deletions
+94
View File
@@ -0,0 +1,94 @@
#include "TCA9554PWR.h"
/***************************************************** Operation register REG ****************************************************/
uint8_t Read_REG(uint8_t REG) // Read the value of the TCA9554PWR register REG
{
uint8_t bitsStatus = 0;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (TCA9554_ADDRESS << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, REG, true);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (TCA9554_ADDRESS << 1) | I2C_MASTER_READ, true);
i2c_master_read_byte(cmd, &bitsStatus, I2C_MASTER_NACK);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
return bitsStatus;
}
void Write_REG(uint8_t REG,uint8_t Data) // Write Data to the REG register of the TCA9554PWR
{
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (TCA9554_ADDRESS << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, REG, true);
i2c_master_write_byte(cmd, Data, true);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_MASTER_NUM,cmd,I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
}
/********************************************************** Set EXIO mode **********************************************************/
void Mode_EXIO(uint8_t Pin,uint8_t State) // Set the mode of the TCA9554PWR Pin. The default is Output mode (output mode or input mode). State: 0= Output mode 1= input mode
{
uint8_t bitsStatus = Read_REG(TCA9554_CONFIG_REG);
uint8_t Data = (0x01 << (Pin-1)) | bitsStatus;
Write_REG(TCA9554_CONFIG_REG,Data);
}
void Mode_EXIOS(uint8_t PinState) // Set the mode of the 7 pins from the TCA9554PWR with PinState
{
Write_REG(TCA9554_CONFIG_REG,PinState);
}
/********************************************************** Read EXIO status **********************************************************/
uint8_t Read_EXIO(uint8_t Pin) // Read the level of the TCA9554PWR Pin
{
uint8_t inputBits =Read_REG(TCA9554_INPUT_REG);
uint8_t bitStatus = (inputBits >> (Pin-1)) & 0x01;
return bitStatus;
}
uint8_t Read_EXIOS(void) // Read the level of all pins of TCA9554PWR
{
uint8_t inputBits = Read_REG(TCA9554_INPUT_REG);
return inputBits;
}
/********************************************************** Set the EXIO output status **********************************************************/
void Set_EXIO(uint8_t Pin,bool State) // Sets the level state of the Pin without affecting the other pins(PIN1~8)
{
uint8_t Data = 0;
uint8_t bitsStatus = Read_REG(TCA9554_OUTPUT_REG);
if(Pin < 9 && Pin > 0){
if(State)
Data = (0x01 << (Pin-1)) | bitsStatus;
else
Data = (~(0x01 << (Pin-1)) & bitsStatus);
Write_REG(TCA9554_OUTPUT_REG,Data);
}
else
printf("Parameter error, please enter the correct parameter!\r\n");
}
void Set_EXIOS(uint8_t PinState) // Set 7 pins to the PinState state such as :PinState=0x23, 0010 0011 state (the highest bit is not used)
{
Write_REG(TCA9554_OUTPUT_REG,PinState);
}
/********************************************************** Flip EXIO state **********************************************************/
void Set_Toggle(uint8_t Pin) // Flip the level of the TCA9554PWR Pin
{
uint8_t bitsStatus = Read_EXIO(Pin);
Set_EXIO(Pin,(bool)!bitsStatus);
}
/********************************************************* TCA9554PWR Initializes the device ***********************************************************/
void TCA9554PWR_Init(uint8_t PinState) // Set the seven pins to PinState state, for example :PinState=0x23, 0010 0011 State (the highest bit is not used) (Output mode or input mode) 0= Output mode 1= Input mode. The default value is output mode
{
// i2c_master_init();
Mode_EXIOS(PinState);
}
esp_err_t EXIO_Init(void)
{
TCA9554PWR_Init(0x00);
return ESP_OK;
}
+44
View File
@@ -0,0 +1,44 @@
#pragma once
#include <stdio.h>
#include "I2C_Driver.h"
#define TCA9554_EXIO1 0x01
#define TCA9554_EXIO2 0x02
#define TCA9554_EXIO3 0x03
#define TCA9554_EXIO4 0x04
#define TCA9554_EXIO5 0x05
#define TCA9554_EXIO6 0x06
#define TCA9554_EXIO7 0x07
#define TCA9554_EXIO8 0x08
/****************************************************** The macro defines the TCA9554PWR information ******************************************************/
#define TCA9554_ADDRESS 0x20 // TCA9554PWR I2C address
// TCA9554PWR寄存器地址
#define TCA9554_INPUT_REG 0x00 // Input register,input level
#define TCA9554_OUTPUT_REG 0x01 // Output register, high and low level output
#define TCA9554_Polarity_REG 0x02 // The Polarity Inversion register (register 2) allows polarity inversion of pins defined as inputs by the Configuration register.
#define TCA9554_CONFIG_REG 0x03 // Configuration register, mode configuration
// esp_err_t i2c_master_init(void); // Example Initialize I2C to host mode
/***************************************************** Operation register REG ****************************************************/
uint8_t Read_REG(uint8_t REG); // Read the value of the TCA9554PWR register REG
void Write_REG(uint8_t REG,uint8_t Data); // Write Data to the REG register of the TCA9554PWR
/********************************************************** Set EXIO mode **********************************************************/
void Mode_EXIO(uint8_t Pin,uint8_t State); // Set the mode of the TCA9554PWR Pin. The default is Output mode (output mode or input mode). State: 0= Output mode 1= input mode
void Mode_EXIOS(uint8_t PinState); // Set the mode of the 7 pins from the TCA9554PWR with PinState
/********************************************************** Read EXIO status **********************************************************/
uint8_t Read_EXIO(uint8_t Pin); // Read the level of the TCA9554PWR Pin
uint8_t Read_EXIOS(void); // Read the level of all pins of TCA9554PWR, the default read input level state, want to get the current IO output state, pass the parameter TCA9554_OUTPUT_REG, such as Read_EXIOS(TCA9554_OUTPUT_REG);
/********************************************************** Set the EXIO output status **********************************************************/
void Set_EXIO(uint8_t Pin,bool State); // Sets the level state of the Pin without affecting the other pins
void Set_EXIOS(uint8_t PinState); // Set 7 pins to the PinState state such as :PinState=0x23, 0010 0011 state (the highest bit is not used)
/********************************************************** Flip EXIO state **********************************************************/
void Set_Toggle(uint8_t Pin); // Flip the level of the TCA9554PWR Pin
/********************************************************* TCA9554PWR Initializes the device ***********************************************************/
void TCA9554PWR_Init(uint8_t PinState); // Set the seven pins to PinState state, for example :PinState=0x23, 0010 0011 State (the highest bit is not used) (Output mode or input mode) 0= Output mode 1= Input mode. The default value is output mode
esp_err_t EXIO_Init(void);