初始化等没有做,只写了读与写,能够正常读取数据
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define AHT20_DEVICE_ADDR 0x38
#define DEFAULT_I2C_BUS "/dev/i2c-0"
// CRC校验类型:CRC8/MAXIM
//多项式:X8+X5+X4+1
// Poly:0011 0001 0x31
//高位放到后面就变成 1000 1100 0x8c
// C现实代码:
uint8_t Calc_CRC8(uint8_t *message, uint8_t Num)
{
uint8_t i;
uint8_t byte;
uint8_t crc = 0xFF;
for (byte = 0; byte < Num; byte++)
{
crc ^= (message[byte]);
for (i = 8; i > 0; --i)
{
if (crc & 0x80)
crc = (crc << 1) ^ 0x31;
else
crc = (crc << 1);
}
}
return crc;
}
/*****************************************************************************
i2c写函数,参数1:设备号,参数2:从设备地址,参数3:要写入的数据缓冲区,参数4:写入数据大小
******************************************************************************/
int i2c_write(int fd, unsigned char device_addr, unsigned char *buff, int ByteNo)
{
struct i2c_rdwr_ioctl_data msg_rdwr;
struct i2c_msg i2cmsg;
int i;
unsigned char sendbuf[15] = {0};
memset(sendbuf, 0, 15);
memcpy(sendbuf, buff, ByteNo);
printf("write_buffer:=");
for (i = 0; i < ByteNo; i++)
printf("%.2X ", sendbuf[i]);
printf("\r\n");
printf(" i2c_write ByteNo:=[%d],device_addr:=[%.2X]\n", ByteNo, device_addr);
printf("\r\n");
msg_rdwr.msgs = &i2cmsg;
msg_rdwr.nmsgs = 1;
i2cmsg.addr = device_addr;
i2cmsg.flags = 0; // write
i2cmsg.len = ByteNo;
i2cmsg.buf = sendbuf;
if ((i = ioctl(fd, I2C_RDWR, &msg_rdwr)) < 0)
{
perror("ioctl()");
fprintf(stderr, "ioctl returned %d\n", i);
return -1;
}
return 0;
}
/*****************************************************************************
i2c读函数,参数1:设备号,参数2:从设备地址,参数3:读取数据缓冲区,参数4:读取数据大小
******************************************************************************/
int i2c_read(int fd, unsigned char device_addr, unsigned char *buff, int ByteNo)
{
struct i2c_rdwr_ioctl_data msg_rdwr;
struct i2c_msg i2cmsg;
int i;
printf("i2c_read ByteNo:=[%d],device_addr:[%.2X]\n", ByteNo, device_addr);
printf("\r\n");
msg_rdwr.msgs = &i2cmsg;
msg_rdwr.nmsgs = 1;
i2cmsg.addr = device_addr;
i2cmsg.flags = I2C_M_RD; // read
i2cmsg.len = ByteNo;
i2cmsg.buf = buff;
if ((i = ioctl(fd, I2C_RDWR, &msg_rdwr)) < 0)
{
perror("ioctl()");
fprintf(stderr, "ioctl returned %d\n", i);
return -1;
}
return 0;
}
int show_data(unsigned char *readbuf)
{
unsigned char AHT20_OutData[10];
uint32_t H1, T1;
int i;
u_int16_t w_SetCrc1 = Calc_CRC8((uint8_t *)readbuf, 6);
printf("检查crc值为= %.2X; \n", w_SetCrc1);
printf("read_buffer:=");
for (i = 0; i < 7; i++)
printf("%.2X ", readbuf[i]);
printf("\r\n");
if ((readbuf[0] & 0x68) == 0x08)
{
H1 = readbuf[1];
H1 = (H1 << 8) | readbuf[2];
H1 = (H1 << 8) | readbuf[3];
H1 = H1 >> 4;
H1 = (H1 * 1000) / 1024 / 1024;
T1 = readbuf[3];
T1 = T1 & 0x0000000F;
T1 = (T1 << 8) | readbuf[4];
T1 = (T1 << 8) | readbuf[5];
T1 = (T1 * 2000) / 1024 / 1024 - 500;
AHT20_OutData[0] = (H1 >> 8) & 0x000000FF;
AHT20_OutData[1] = H1 & 0x000000FF;
AHT20_OutData[2] = (T1 >> 8) & 0x000000FF;
AHT20_OutData[3] = T1 & 0x000000FF;
}
else
{
AHT20_OutData[0] = 0xFF;
AHT20_OutData[1] = 0xFF;
AHT20_OutData[2] = 0xFF;
AHT20_OutData[3] = 0xFF;
printf("读取失败.......................................................");
}
int t = T1 / 10;
int t1 = T1 % 10;
float Temp = (float)(t + t1 * 0.1);
int h = H1 / 10;
int h1 = H1 % 10;
float Humi = (float)(h + h1 * 0.1);
printf("温度:[%.1f],湿度:[%.1f]\r\n", Temp, Humi);
printf("\r\n");
return 0;
}
int main(int argc, char **argv)
{
printf("----start---------\n");
int i;
unsigned char writebuf[3] = {0xAC, 0x33, 0x00};
unsigned char readbuf[7];
int fd = open(DEFAULT_I2C_BUS, O_RDWR);
if (fd < 0)
{
printf("open failed/n");
return -1;
}
while (1)
{
memset(readbuf, 0, sizeof(readbuf));
i2c_write(fd, AHT20_DEVICE_ADDR, writebuf, 3);
usleep(100 * 1000); // 休眠一秒
i2c_read(fd, AHT20_DEVICE_ADDR, readbuf, 7);
show_data(readbuf);
usleep(2000 * 1000); // 休眠一秒
}
close(fd);
return 0;
}
运行效果: