1.器件原理图
2.设备地址
设备地址取决于 A0,A1,A2三个Pin 脚怎么接,TCA9548A 的地址范围为0x00~0x77,原理图R_NC1,R_NC2,R_NC3 没有焊接,故A0,A1,A2,接到GND被拉低,故地址为 0x70。
#define TCA9548A_SLAVE_ADDR 0x70 //地址
#define TCA9548A_WRITE_BIT 0x00 //W
#define TCA9548A_READ_BIT 0x01 //R
3.设备总线 PF0,PF1,主控为STM32F407,第2个硬件IIC
不同通道寄存器地址,通道使能。
#define TCA9548A_CHANNEL_0 0x01
#define TCA9548A_CHANNEL_1 0x02
#define TCA9548A_CHANNEL_2 0x04
#define TCA9548A_CHANNEL_3 0x08
#define TCA9548A_CHANNEL_4 0x10
#define TCA9548A_CHANNEL_5 0x20
#define TCA9548A_CHANNEL_6 0x40
#define TCA9548A_CHANNEL_7 0x80
4. .h文件
#ifndef __BSP_IIC_H
#define __BSP_IIC_H
#include "gpio.h"
#define IIC_SDA_Read() HAL_GPIO_ReadPin (IIC_SDA_GPIO_Port,IIC_SDA_Pin)
#define IIC_SDA_High() HAL_GPIO_WritePin (IIC_SDA_GPIO_Port,IIC_SDA_Pin,GPIO_PIN_SET)
#define IIC_SDA_Low() HAL_GPIO_WritePin (IIC_SDA_GPIO_Port,IIC_SDA_Pin,GPIO_PIN_RESET)
#define IIC_SCL_High() HAL_GPIO_WritePin (IIC_SCL_GPIO_Port,IIC_SCL_Pin,GPIO_PIN_SET)
#define IIC_SCL_Low() HAL_GPIO_WritePin (IIC_SCL_GPIO_Port,IIC_SCL_Pin,GPIO_PIN_RESET)
#define TCA9548A_SLAVE_ADDR 0x70 //地址
#define TCA9548A_WRITE_BIT 0x00
#define TCA9548A_READ_BIT 0x01
#define TCA9548A_CHANNEL_0 0x01
#define TCA9548A_CHANNEL_1 0x02
#define TCA9548A_CHANNEL_2 0x04
#define TCA9548A_CHANNEL_3 0x08
#define TCA9548A_CHANNEL_4 0x10
#define TCA9548A_CHANNEL_5 0x20
#define TCA9548A_CHANNEL_6 0x40
#define TCA9548A_CHANNEL_7 0x80
extern void IIC_Start (void);
extern void IIC_Stop(void);
extern uint8_t IIC_Wait_Ack(void);
extern void TCA9548A_SetChannel(uint8_t channel);
extern uint8_t IIC_ReadByte(uint8_t ReadAck);
extern void IIC_Ack(void);
extern void IIC_NAck(void);
#endif
5. .c 源文件
HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
形参分别为I2C通道,设备地址,数据个数,超时时间
1)设备地址是8 byte,,存储数据的指针,
void TCA9548A_SetChannel(uint8_t channel)
{
uint8_t data;
switch(channel)
{
case 0:
data = TCA9548A_CHANNEL_0;
break;
case 1:
data = TCA9548A_CHANNEL_1;
break;
case 2:
data = TCA9548A_CHANNEL_2;
break;
case 3:
data = TCA9548A_CHANNEL_3;
break;
case 4:
data = TCA9548A_CHANNEL_4;
break;
case 5:
data = TCA9548A_CHANNEL_5;
break;
case 6:
data = TCA9548A_CHANNEL_6;
break;
case 7:
data = TCA9548A_CHANNEL_7;
break;
default:
break;
}
HAL_I2C_Master_Transmit(&hi2c2, (TCA9548A_SLAVE_ADDR << 1) , &data, 1, 10);
}
如何判断是否切换通道成功
1)main 函数里面定义你想写的数据,用示波器抓写入数据的波形
uint8_t data[3] ={0x55,0x01,0x00};
TCA9548A_SetChannel(1);//切换通道1
HAL_I2C_Master_Transmit(&hi2c2, (TCA9548A_SLAVE_ADDR << 1) | TCA9548A_WRITE_BIT, &data[0],3, 1000 );
2)示波器波形图
uint8_t data[3] ={0x55,0x01,0x00};
在起始信号开始后读设备地址:0xE0 1110 0000
data0: 0101 0101 为0x55
data1:0000 0001
data2:0000 0000
箭头处为产生的应答信号~