平台环境
1、基于全志a40i平台
2、ads1115使用的是M5stack UNIT-VMeter电压传感器,I2C总线,工作为AIN0与AIN1差分,支持±36V电压检测
参考代码
//宏定义
#define ADS1115_address 0x49 //ADS1115地址
#define I2C_SLAVE 0x0703 //IIC从器件的地址设置
#define ADS1115_configure 0x00 //转换寄存器地址
#define ADS1115_register 0x01 //配置寄存器地址
#define ADS1115_high 0x82 //高八位 默认通道in0-in1 参考电压4096mv
#define ADS1115_in0_in1 0x00 //差分in0_in1
#define ADS1115_in0_in3 0x10 //差分in0_in3
#define ADS1115_in1_in3 0x20 //差分in1_in3
#define ADS1115_in2_in3 0x30 //差分in2_in3
#define ADS1115_in0 0x40 //单端通道0
#define ADS1115_in1 0x50 //单端通道1
#define ADS1115_in2 0x60 //单端通道2
#define ADS1115_in3 0x70 //单端通道3
#define ADS1115_4096 0x00 //pga 4096
#define ADS1115_2048 0x02 //pga 2048
#define ADS1115_1024 0x04 //pga 1024
#define ADS1115_512 0x06 //pga 512
#define ADS1115_256 0x08 //pga 256
#define ADS1115_low 0xe3 //低八位不动
static int32_t ADS1115_init()
{
fd = open(DEV_I2C, O_RDWR); // open file and enable read and write
if (fd < 0)
{
perror("Can't open /dev/ADS1115 \n"); // open i2c dev file fail
exit(1);
}
LOGINFO("open %s success !\n",DEV_I2C); // open i2c dev file succes
if (ioctl(fd, I2C_SLAVE, ADS1115_address) < 0)
{ //set i2c address
LOGINFO("地址设置失败!\n");
close(fd);
return - 1;
}
LOGINFO("set slave address to 0x%x success!\n", ADS1115_address);
return 0;
}
// write byte
static int32_t ADS1115_write(uint8 ADS1115_GB)
{
char buf[3] = {ADS1115_register,ADS1115_GB,ADS1115_low};
write(fd,buf,3);
return (1);
}
// read byte
static int32_t ADS1115_read(int fd, uint8 * val)
{
int retries;
char buf[1] = {ADS1115_configure};
for (retries = 5; retries; retries--)
{
if (write(fd, buf, 1) == 1)
{
if (read(fd, val, 1) == 1)
{
return 0;
}
}
}
//LOGERROR("ADS1115_read error!\n");
return -1;
}
//get data
int GetData(uint8 passageway, uint8 pga)
{
uint8 H, L;
uint8 ADS1115_GB = ADS1115_high+passageway+pga;
ADS1115_write(ADS1115_GB);
usleep(10000);
if(ADS1115_read(fd, &H) < 0)
return -1;
usleep(1000);
if(ADS1115_read(fd, &L) < 0)
return -1;
//printf("\nADS1115_GB:0x%02X---%u\n",ADS1115_GB,(H << 8) +L);
return (H << 8) +L;
}
*参考文献
[1]:https://blog.csdn.net/weixin_44807826/article/details/105449856