上一回我们已经验证了I2C 能正确运行,由于HTS221和STTS751 都连在同一I2C BUS总线上,用同一个i2c 程序进行分时复用读取即可; 具体代码如下:
uint8_t HTS221_Read_Date(uint8_t SlaveAddress, uint8_t RegAddress)
{
uint8_t recvDate;
uint8_t succ, stime=0;
I2C_Start();
succ = I2C_Send_Byte(SlaveAddress & 0xfe);
while((succ !=1)&&(stime<3))
{
I2C_Stop();
I2C_Start();
succ = I2C_Send_Byte(SlaveAddress & 0xfe); //写
stime++;
}
I2C_Send_Byte(RegAddress);
I2C_Start();
I2C_Send_Byte(SlaveAddress | 0x01); //读
recvDate = I2C_Read_Byte(1); //NACK
I2C_Stop();
return recvDate;
}
测试:
PID = HTS221_Read_Date(HTS221_ADD, HTS221_WHO_AM_I_REG);
printf("PID = %X \r\n", PID);
CyDelay(800);
具体代码可以参考:
HTS221-master.zip
(17.17 KB, 下载次数: 4)
此内容由EEWORLD论坛网友DavidZH原创,如需转载或用于商业用途需征得作者同意并注明出处