全志allwinner A40i平台ads1115模块AD采样

平台环境

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值