1.PCA9546A是个四路I2C开关
2.根据电路图的 接线情况确认I2C从设备的地址0xEC。
3.四路I2C 开关的寄存器地址
#ifndef __TCA9546A_SW_H
#define __TCA9546A_SW_H
/*U95*/
#include "gpio.h"
//从设备地址
#define TCA9546A_SW_ADDR 0xEC
//I2C 开关地址
#define TCA9546A_SW_CHANNEL_0 0x01
#define TCA9546A_SW_CHANNEL_1 0x02
#define TCA9546A_SW_CHANNEL_2 0x04
#define TCA9546A_SW_CHANNEL_3 0x08
#include "I2C.h"
#include "usart.h"
#include "String.h"
#include "BSP_Protocol.h"
#include "gpio.h"
#include "stdio.h"
void TCA9546A_SW_SetChannel(uint8_t channel)
{
uint8_t data;
switch(channel)
{
case 0:
data = TCA9546A_SW_CHANNEL_0;
break;
case 1:
data = TCA9546A_SW_CHANNEL_1;
break;
case 2:
data = TCA9546A_SW_CHANNEL_2;
break;
case 3:
data = TCA9546A_SW_CHANNEL_3;
break;
case 4:
data = TCA9546A_SW_CHANNEL_4;
break;
case 5:
data = TCA9546A_SW_CHANNEL_5;
break;
case 6:
data = TCA9546A_SW_CHANNEL_6;
break;
case 7:
data = TCA9546A_SW_CHANNEL_7;
break;
default:
break;
}
/*选择通道*/
HAL_I2C_Master_Transmit(&hi2c2, TCA9546A_SW_ADDR , &data, 1, 1000);
}
4.串口指令,选择通道,切割字符串
void SET_PDBSW1(void)
{
char String[64];
char *Str;
/*
1.当字符比较到SET_PDBSW1,将 : 后面的内容,以字符串的格式,赋给Str
*/
if(strncmp((char*)RxBuffer1,"SET_PDBSW1",10) == 0)
{
strtok_r((char*)RxBuffer1,":",&Str);
}
/*若 Str 为0,设置通道I2C 通道0 打开*/
if(strncmp((char*)Str,"0",1) == 0)
{
TCA9546A_SW_SetChannel(0);
sprintf(String,"SET_PDB_SW0 OK\r\n");
Send_String_Function(String);
}
if(strncmp((char*)Str,"1",1) == 0)
{
TCA9546A_SW_SetChannel(1);
sprintf(String,"SET_PDB_SW1 OK\r\n");
Send_String_Function(String);
}
if(strncmp((char*)Str,"2",1) == 0)
{
TCA9546A_SW_SetChannel(2);
sprintf(String,"SET_PDB_SW2 OK\r\n");
Send_String_Function(String);
}
if(strncmp((char*)Str,"3",1) == 0)
{
TCA9546A_SW_SetChannel(3);
sprintf(String,"SET_PDB_SW3 OK\r\n");
Send_String_Function(String);
}
}
/*发送字符串函数*/
void Send_String_Function(char *String) //发送数据
{
uint8_t i = 0;
for(i = 0; i < strlen(String);i++)
{
String[i] = toupper(String[i]);//转换大小写
}
printf("%s",String);
}