PCF8574AT是飞利浦的一个IO扩展IC,I2C通讯接口,有8个IO口可以配置,只有一个寄存器。常见的一种应用是用它来作为LCD1602的I2C通讯界面。
在Arduino的LiquidCrystal_I2C库中,默认情况下PCF8574AT使用0x3F作为高7位地址,将它左移一位以后就是7E,最低位为0是写操作,最低位为1是读操作,它在通讯的时候自动进行转换。
Arduino是如此使用:
LiquidCrystal_I2C lcd(0x3F,20,4); //LCD2004, 地址0x3F
但是在STM32的HAL库中,需要我们先对地址进行转换,我们在System Workbench中把鼠标放在I2C的相关函数上,比如HAL_I2C_Master_Transmit(),会出现如下提示:
**
* @brief Transmits in master mode an amount of data in blocking mode.
* @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for the specified I2C.
* @param DevAddress Target device address: The device 7 bits address value
* in datasheet must be shifted to the left before calling the interface
* @param pData Pointer to data buffer
* @param Size Amount of data to be sent
* @param Timeout Timeout duration
* @retval HAL status
*/
意思就是函数在使用datasheet中的7位I2C地址时,必须先左移一位。所以使用
HAL_I2C_Master_Transmit()
对PCF8574AT进行写操作时地址就是0x7E,读操作时地址就是0x7F。
如果我们希望单独操作某个IO(P7-P0),就对那个bit进行单独写1或者清0即可(STM HAL库)。
#define Write_Add_LCD 0x7E
#define Read_Add_LCD 0x7F
uint8_t val;
HAL_I2C_Master_Receive (&hi2c1, Read_Add_LCD,&val, 1, 1000);
val |= 1<<3; //P3 bit置1
HAL_I2C_Master_Transmit (&hi2c1, Write_Add_LCD,&val, 1, 1000);
HAL_I2C_Master_Receive (&hi2c1, Read_Add_LCD,&val, 1, 1000);
val &= ~(1<<3); //P3 bit清0
HAL_I2C_Master_Transmit (&hi2c1, Write_Add_LCD,&val, 1, 1000);
(注:为避免同一总线上I2C地址冲突,对A0/A1/A2电平进行配置,可以有8种不同地址的选择)