i.mx6ull linux4.1.15 读取ads1115数据

i.mx6ull linux4.1.15 读取ads1115数据


没有在网上找到linux下读取ADS1115AD模块数据的代码,根据野火的i2c mpu6050 改编的,希望能帮到有需要的同学

贴代码:

`


/************************************************************/
//文件名:ADS1115.c
//功能:ads1115读取模拟电压
//使用说明: (1)
//          (2)
//          (3)
//          (4)
//作者:xinfei
//日期:2020-04-11

/************************************************************/
//包含头文件

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/select.h>
#include<sys/time.h>
#include<errno.h>
#include<string.h>
//宏定义
#define ADS1115_address                             0x48          //ADS1115地址
#define I2C_SLAVE                                   0x0703       //IIC从器件的地址设置
#define ADS1115_configure                           0x00   //转换寄存器地址
#define ADS1115_register                            0x01   //配置寄存器地址
#define ADS1115_high                                0x82   //高八位 默认通道in0-in1   参考电压4096mv
#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_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   //低八位不动
typedef unsigned char uint8;
int fd = -1;
//
//函数声明
static uint8 ADS1115_init();
static uint8 ADS1115_write(uint8 ADS1115_GB);
static uint8 ADS1115_read(int fd, uint8 * val);
static uint8 printarray(uint8 Array[], uint8 Num);
short GetData(uint8 passageway, uint8 pga);

static uint8 ADS1115_init()
{
   fd  = open("/dev/i2c-0", 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);
    }

    printf("open /dev/i2c-0 success !\n");          // open i2c dev file succes

    if (ioctl(fd, I2C_SLAVE, ADS1115_address) < 0)
    { //set i2c address
        printf("地址设置失败!\n");
        close(fd);
        return - 1;
    }
    printf("set slave address to 0x%x success!\n", ADS1115_address);
}

static uint8 ADS1115_write(uint8 ADS1115_GB)
{
 

    char buf[3] = {ADS1115_register,ADS1115_GB,ADS1115_low};
    int w_count = 0;
    w_count = write(fd,buf,3);
        //printf("set slave address to 0x%x success!\n", w_count);
    
    return (1);
}

//MPU6050 read byte
static uint8 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;
            }

        }

    }

    return - 1;
}

//get data
short GetData(uint8 passageway, uint8 pga)
{
    char H, L;
    uint8 ADS1115_GB = ADS1115_high+passageway+pga;
    ADS1115_write(ADS1115_GB);
    ADS1115_read(fd,  &H);
    usleep(1000);
    ADS1115_read(fd,  &L);
    return (H << 8) +L;
}

int main(int argc, char * argv[])
{
    ADS1115_init();

    while (1)
    {
        usleep(1000 * 300);
        printf("通道0:%6d\n ", GetData(ADS1115_in0,ADS1115_2048));
        usleep(1000 * 300);
        printf("通道1:%6d\n ", GetData(ADS1115_in1,0));
        usleep(1000 * 300);
        printf("通道2:%6d\n ", GetData(ADS1115_in2,ADS1115_512));
        usleep(1000 * 300);
        printf("通道3:%6d\n ", GetData(ADS1115_in3,ADS1115_1024));
        sleep(5);
    }

    close(fd);
}
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
i.MX6ULL是NXP半导体推出的一款低功耗、高集成度的应用处理器,适用于嵌入式系统和物联网设备。在使用i.MX6ULL处理器运行Linux操作系统时,安全性是一个重要的考虑因素。 i.MX6ULL Linux系统的安全性可以从多个方面来考虑。首先,Linux操作系统本身具有良好的安全性,具备权限管理、访问控制、安全隔离等功能。通过合理的配置和管理,可以保护系统免受外部攻击。 其次,i.MX6ULL芯片本身也提供了一些安全特性,如硬件加密引擎和安全启动功能。硬件加密引擎可以加密数据传输,确保数据的机密性;安全启动功能则提供了验证和保护引导加载程序的机制,防止恶意软件的篡改。 此外,i.MX6ULL还支持TrustZone技术,通过硬件隔离将系统分成安全世界和非安全世界。在安全世界中,可以运行关键的安全任务,而非安全世界则可以运行一般的应用程序。这种硬件隔离可以提供额外的保护,防止恶意程序对关键数据和系统功能的攻击。 最后,开发者在使用i.MX6ULL时还可以采取一些安全措施,如定期更新操作系统和软件包、使用强密码、限制外部设备的访问等。同时,定期进行安全漏洞扫描和风险评估,及时修复发现的漏洞和弱点,也是提高系统安全性的重要手段。 综上所述,i.MX6ULL在运行Linux操作系统时可以通过合理配置、硬件特性和开发者的安全措施来提高系统的安全性,保障系统和数据的保密性、完整性和可用性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值