自助建子站wordpress粉色主题
news/
2025/9/24 13:36:57/
文章来源:
自助建子站,wordpress粉色主题,腾讯网页游戏平台,响应式网站的宽度1、引言
用SHT30测温湿度#xff0c;SHT30是I2C通信总线#xff0c;具体信息去看Datasheet文档#xff1a;https://pdf1.alldatasheet.com/datasheet-pdf/view/897974/ETC2/SHT30.html。操作系统是Linux#xff0c;机器是CM3计算板#xff0c;当然也可以是树莓派和其他主…1、引言
用SHT30测温湿度SHT30是I2C通信总线具体信息去看Datasheet文档https://pdf1.alldatasheet.com/datasheet-pdf/view/897974/ETC2/SHT30.html。操作系统是Linux机器是CM3计算板当然也可以是树莓派和其他主机。
2、设备树打开I2C接口
linux的I2C需要打开I2C的设备树才能在/dev中找到具体方式是
sudo vim /dev/config.txt
打开注释或者新增以下内容
dtparami2c_armon
dtoverlayi2c0
dtoverlayi2c1然后重启查看/dev下边有没有i2c-0和i2c-1出现。执行ls /dev/ 3、一切皆文件的驱动编写
linux中的I2C驱动主要包括ioctl,write,read三个函数。其中ioctl的cmd常用到以下配置 I2C_SLAVEI2C从机地址用来设定I2C从机地址 I2C_SLAVE_FORCE用来修改I2C从机地址 I2C_TENBIT设置从机地址占用的位数取值为0表示从机地址为7 bit取值为1表示机地址为10bit。
具体地贴代码了
/******************************************************************************** File Name : cm3I2C.c* Description : This file provides code for the gateway i2c driver.* Author : jackwang by jiawang16foxmail.com* Date : 2019-08-17******************************************************************************
*/
/*! Include header */
#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
#include unistd.h
#include sys/ioctl.h#include cm3I2C.h/*! debug info define */
#define __DEBUG 1
#if __DEBUG#define debug printf
#else#define debug
#endif/*! cm3 i2c dev setup, e.g. /dev/i2c-0 */
int cm3I2CSetup(char* dev)
{int fd;fd open(dev, O_RDWR);if ( fd 0 ){debug([Error] failed to open the i2c bus: %s.\n, dev);return -1;}return fd;
}/*! cm3 i2c slave address bits setup, 0-7,1-10 */
int cm3I2CSlaveAddrBitSetup(int fd, int bits)
{if ( ioctl(fd, I2C_TENBIT, bits) 0) {debug([Error] failed to set i2c addr bits.\n);return -1;}return 0;
}/*! cm3 i2c slave address setup */
int cm3I2CSlaveAddrSetup(int fd, int addr)
{if ( ioctl(fd, I2C_SLAVE_FORCE, addr) 0 ){debug([Error] failed to set i2c slave address.\n);return -1;}return 0;
}/*! cm3 i2c read slave device reg */
int cm3I2CRead(int fd, unsigned char*buf, int buflength)
{if ( read(fd, buf, buflength) 0){debug([Error] failed to read i2c.\n);return -1;}return 0;
}/*! cm3 i2c write slave device reg */
int cm3I2CWrite(int fd, unsigned char*buf, int buflength)
{if ( write(fd, buf, buflength) ! buflength ){debug([Error] failed to write i2c.\n);return -1;}return 0;
}/*! cm3 i2c dev-handler close */
void cm3I2CClose(int fd)
{close(fd);
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/915764.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!