linux读取ads1115ADC例程
硬件接口i2c
应用电路
程序‘
ads1115.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>static int fd = 0;//初始化i2c
//i2c_dev设备文件 i2c_addr 设备地址
//返回 0
int i2c_init(char *i2c_dev, unsigned char i2c_addr)
{int res = 0;fd = open(i2c_dev, O_RDWR);if(fd < 0){printf("[%s]:[%d] open i2c file error\r\n", __FUNCTION__, __LINE__);return -1;}res = ioctl(fd,I2C_TENBIT,0); //7位模式 res = ioctl(fd,I2C_SLAVE, (i2c_addr >> 1)); //设置I2C从设备地址return res;
}//读i2c
//buf数据 len长度
//返回 实际读取的长度
int i2c_readNbyte(unsigned char *buf, int len)
{int res = 0;res = read(fd, buf,len);return res;
}//写i2c
//buf数据 len长度
//返回 实际写的长度
int i2c_writeNbyte(unsigned char *buf, int len)
{int res = 0;res = write(fd, buf,len);return res;
}int i2c_readReg(unsigned int reg_addr, unsigned char *buf, int len)
{int res = 0;unsigned char buff[2];buff[0] = reg_addr >> 8;buff[1] = reg_addr & 0xff;write(fd, buff, 2);res = read(fd, buf,len);return res;
}int i2c_writeReg(unsigned int reg_addr, unsigned char *buf, int len)
{int res = 0,i;unsigned char *buff = 0;buff = (unsigned char *)malloc((len+2));buff[0] = reg_addr >> 8;buff[1] = reg_addr & 0xff;for(i = 0; i < len; i++)buff[(i+2)] = buf[i];res = write(fd, buff, (len+2));free(buff);return res;
}int i2c_close()
{close(fd);return 0;
}int adc_init()
{int ret = 0;i2c_init("/dev/i2c-1", 0x90);return ret;
}int adc_read()
{int ret = 0;const unsigned char read_cmd[] = {0x01, 0x8b, 0x83};const unsigned char read_reg = 0x00;unsigned char read_buff[20] = {0};i2c_writeNbyte(read_cmd, 3);usleep(200);i2c_writeNbyte(&read_reg, 3);i2c_readNbyte(read_buff, 2);ret = read_buff[0] * 256 + read_buff[1];return ret;
}
ads1115.h
#ifndef __ADC_H
#define __ADC_Hint adc_init();
int adc_read();#endif
调用
int read_weight(int *value)
{static char state = 0;if (state == 0){adc_init();state = 1;}*value = adc_read();return 0;
}
linux读取ads1115ADC例程相关推荐
- imx6ul linux读取DS18B20温度
imx6ul linux读取DS18B20温度 首先尝试加载模块 root@imx6ulevk:~# modprobe w1-gpio modprobe: FATAL: Module w1-gpio ...
- linux 读取键盘上下左右键小程序
linux 读取键盘上下左右键小程序 月城雪兔 2016-06-03 17:28:31 因为想读取键盘的上下左右方向键,却发现在网上不容易找到恰当的例子,最后是以一个ros程序简化而来的. 第一,为什 ...
- linux读取nand的文件,Linux Kernel 之AP读写Nand Flash上的Yaffs2文件的全过程浅析
1.1 用top-down的方法分析AP读一个Nand Flash上的file的全过程 我先简单看一个例子,看User Application如何打开一个Yaffs2 file并读写之: int ma ...
- php 读取 linux 文件,PHP读取大文件,linux读取日志
工作中遇到一个问题,运营或客服经常会问一些用户使用中的问题涉及到日志查询,开始的时候来一个给查一下日志..后来发现每天都会问个五六次..没办法后来就给弄了个读日志的工具. 读着读着发现有时候会出现挂掉 ...
- windows和linux运算结果不同,从Windows和Linux读取文件会产生不同的结果(字符编码?)...
目前,我正在尝试以mime格式读取文件,该文件具有png的一些二进制字符串数据. 在Windows中,读取文件会为我提供正确的二进制字符串,这意味着我只需将字符串复制过来并将扩展名更改为png即可看到 ...
- linux 读取内存颗粒,Linux虚拟内存地址转化成物理内存地址
背景 现代手机这种SOC(system on chip),因为功耗.Modem等功能soc上集成了很多core,他们还可以是独立的系统在运转. 比如ADSP简介ADSP(Application Dig ...
- Linux读取SSD的smart信息,使用smartmontools查看SSD的“秘密”信息
仍然担心看不到sm841中的温度吗?您是否仍对Toshiba Q pro看不见写入量和使用寿命值感到不安?为了查看M4 / 00的写入量,您是否仍在使用C300固件升级程序重新启动到纯DOS并麻烦地运 ...
- linux读取内存文件,linux – 使用模块读取内核内存
首先让我先说读任意内核内存是一件棘手的事!并且有很多方法可以做到这一点,它们的复杂程度和灵活性各不相同. 1)硬编码地址. 在内核版本的System.map文件中搜索它: # grep sys_cal ...
- linux设备树例程,iTOP-iMX6-设备树内核-实时时钟RTC以及Linux-c测试例程
当 Linux 开发者谈论一个实时时钟,他们通常指的是某种能记录墙上时间,并且有备用电 池,以至于在系统关机的时候仍然可以工作的器件. Linux 有两个系列广泛兼容的用户空间 RTC 设备节点: • ...
- linux读取 dev tty0,linux命令: ls命令
ls命令:(list directory contents),列出目录内容 用法: ls [option] [file_or_dirs] ls命令常用选项 -l 显示文件的长格式信息 -d 显示目录或 ...
最新文章
- Unity3D脚本属性
- MySQL 8.0新特性--CTE Recurive(二)
- 使用libjpeg.framework压缩UIImage
- cache与buffer
- em算法详细例子及推导_EM算法详解(例子+推导)
- 获取打开文件的路径和文件名
- 通过样式class也是可以控制超链接是否弹窗
- 若依集成jwt实现登录授权访问(单体版)
- 全国高校计算机能力挑战赛C语言组真题
- linux运行完脚本 命令失效,linux – Systemd退出bash脚本,执行导致失败的命令,而不是继续...
- Docker-Docker-file构建Redis5.0.3镜像
- 没有什么内存问题,是一行Python代码解决不了的
- org.jgroups.ChannelException: failed to start p...
- PHP后台-微信小程序发送订阅消息(详细)
- python实现的广域网聊天(无需搭建服务器,基于FICS和socket模块)
- 企业快速构建可落地的IT服务管理体系的五大关键点
- CNC:CNC计算机数控系统技术之数控仿真宏程序代码讲解、案例应用集合之详细攻略
- Apple iPhone 8G手机误升级至2.0降级破解日记
- java查看jvm内存使用情况
- 01 【Verilog实战】同步FIFO的设计(附源码RTL/TB)