当前位置: 首页 > news >正文

永州网站建设效果网站优化查询代码

永州网站建设效果,网站优化查询代码,广东龙盛建设工程有限公司,安装如WordPress文章目录1- 使能开发板I2C通信接口2- SHT20硬件连接3- 编码实现SHT20温湿度采样思路(1)查看sht20从设备地址(i2cdetect)(2)获取数据大体流程【1】软复位【2】触发测量与通讯时序(3)返…

文章目录

    • 1- 使能开发板I2C通信接口
    • 2- SHT20硬件连接
    • 3- 编码实现SHT20温湿度采样思路
      • (1)查看sht20从设备地址(i2cdetect)
      • (2)获取数据大体流程
        • 【1】软复位
        • 【2】触发测量与通讯时序
      • (3)返回数据处理
    • 4- SHT20温湿度采样编程源码
    • 5- 代码难点巩固
      • (1)ioctl函数讲解
      • (2)移位、与或处理数据


I2C 总线是一种同步、半双工、采用电平信号收发的串行总线。

关于I2C不了解的可以参考写的这篇文章:I2C协议介绍以及HAL库实现I2C对SHT30温湿度采样

1- 使能开发板I2C通信接口

在我的开发板上有两路I2C外设:

I2C2 ---> 触摸屏控制器,rtc时钟,摄像头控制器
I2C1 ---> 40pin扩展口使用,由DToverlay进行开启
GPIO01_IO03 ---> I2C1_SDA
GPIO01_IO03 ---> I2C1_SCL

想要使能40pin扩展口的I2C1的话,需要修改开发板上的DTOverlay配置文件,添加该管脚对I2C的支持,具体修改具体方法为修改 eMMC 启动介质的 boot 分区下的 config.txt 文件,将dtoverlay_i2c1的选项修改为yes:

root@igkboard:~# vi /run/media/mmcblk1p1/config.txt
# Enable I2C overlay
dtoverlay_i2c1=yes

修改完成后重启系统,系统启动时将会自动加载 I2C 协议驱动。查看/dev下是否存在I2C设备节点,或查看/sys/bus/i2c/devices中设备文件,已验证I2C驱动是否加载。

oot@igkboard:~# ls -l /dev/i2c-*
crw------- 1 root root 89, 0 Mar  1 10:24 /dev/i2c-0
crw------- 1 root root 89, 1 Mar  1 10:24 /dev/i2c-1
root@igkboard:~# ls /sys/bus/i2c/devices/
1-006f  i2c-0  i2c-1

由于驱动是从0开始编写的,故实际的I2C1对应的设备节点是i2c-0。


2- SHT20硬件连接

  • VCC:3.3V
  • GND:GND
  • SCL:I2C1_SCL
  • SDA:I2C1_SDA

在这里插入图片描述
参考图:
在这里插入图片描述
实物连接图:
在这里插入图片描述


3- 编码实现SHT20温湿度采样思路

(1)查看sht20从设备地址(i2cdetect)

总线上挂载I2C从设备,可通过i2cdetect扫描某个I2C总线上的所有设备。可通过控制台输入i2cdetect -y 1或0(确定哪一条总线) 来查看所有设备。

root@igkboard:~#  i2cdetect -y 00  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

显示的是以十六进制表示的设备地址的编号,40(16进制)即1000000(2进制)。

(2)获取数据大体流程

【1】软复位

此命令用于重新启动传感器系统,而无需重新上电。一旦接收到该命令,传感器系统将重新初始化并根据默认设置开始操作。软复位时间小于15ms。
在这里插入图片描述

【2】触发测量与通讯时序

下面右图:我们常用红色框选中的命令,使用非保持主机模式(no hold master)的命令,可以使sht20在测量时候,释放i2c总线,使得主机可以处理其他从设备的通信任务。
下面左图:我们可以看见sht20的通讯时序(时序的开始以及停止以及发送数据等的内容可以参考开头的文章。)。

  • Start开始,从地址+0通知sht30我们将要写入,收到ACK相响应成功;
  • 对sht20写入非保持主机模式(no hold master)的命令(即右图的11110101),收到ACK响应成功等待,Stop;
  • 然后发送地址+1读数据信号,返回ACK响应即可;
  • 后面就可以读取我们需要的数据了。

在这里插入图片描述

(3)返回数据处理

测量的数据,温度和湿度均为两个字节。而且无论哪一种传输模式,测量的最大分辨率最大14bit,数据的第二个字节SDA上最后两位是用来标记相关状态信息。其中bit1表示测量类型(0是温度,1是湿度)

SDA的输出数据被转换成两个字节的数据包,高字节MSB在前(左对齐)。每个字节后面都跟随一个应答位。两个状态位,即 LSB的后两位在进行物理计算前须置0。

我们上面时序的例子返回了两个字节数据:
高字节数据:0110 0011
低字节数据:0101 0010(bit1是1,说明返回的是湿度的测量数据)
在这里插入图片描述
我们需要将我们收到的数据用提供的计算方法计算出相对应的湿度值。
除去最后的2bit(就是将最后的2bit换为00),也就是0110 0011 0101 0000(2进制) = 25424(10进制),然后代入公式得到数据:

RH = -6 + 125*(25424 / 65536) = 42.4924 #单位为%

在这里插入图片描述


4- SHT20温湿度采样编程源码

源码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <linux/i2c-dev.h>#define SOFTRESET          0xFE;//16进制软复位1111 11110
#define TRIGGER_T_NO_HOLD  0xF3;//16进制温度触发测量1111 0011
#define TRIGGER_RH_NO_HOLD 0xF5;//16进制湿度触发测量1111 0101void mssleep(unsigned long ms);//毫秒级函数实现(nanosleep)
int sht20_init(char *i2c_dev);//初始化从设备函数
int sht20_softreset(int fd);//sht20命令复位函数
int sht20_get_t_rh(int fd, float *temperature, float *humidity);//buf处理函数int main(int argc, char *argv[])
{int fd;float temperature;float humidity;if(argc != 2){printf("This project is to obtain the temperature and humidity of sht20.\n");printf("Please input %s /dev/i2c-x\n", argv[0]);return 0;}fd = sht20_init(argv[1]);if(fd < 0){printf("SHT20 init failure\n");return 1;}//printf("sht20_init success\n");while(1){if(sht20_get_t_rh(fd, &temperature, &humidity) < 0){printf("SHT20 get temperature and humidity failure\n");return 2;}//printf("sht20_get_t_rh success\n");printf("Temperature:%lf'C Humidity:%lf%% \n", temperature, humidity);sleep(1);}close(fd);return 0;
}/*对buf里面的数据进行处理打印*/
static inline void manage_buf(uint8_t *buf, int size )
{int i;for(i = 0; i < size; i++){printf("%02x ", buf[i]);//以16进制输出,不足2位左边补0}printf("\n");
}/*获取sht20温湿度值*/
int sht20_get_t_rh(int fd, float *temperature, float *humidity)
{uint8_t buf[4];//一个字节8个位,比如buf[0]刚好读取一个字节,好操作int rv = 0;if(fd < 0 || !temperature || !humidity){printf("%s get invalid input arguments\n", __FUNCTION__);return -1;}/*获取温度*/memset(buf, 0, sizeof(buf));buf[0] = TRIGGER_T_NO_HOLD;//16进制温度触发测量1111 0011if(rv = write(fd, buf, 1) < 0){printf("Write TRIGGER_T_NO_HOLD error:%s\n", strerror(errno));return -2;}mssleep(85);//等待需要的时间memset(buf, 0, sizeof(buf));//读取数值放在buf中,需要将buf清0/*读取三个字节数据,后面的一个字节数据是CRC校验值*/if(rv = read(fd, buf, 3) < 0){printf("Read temperature error:%s\n", strerror(errno));return -3;}manage_buf(buf, 3);//buf处理函数/*需要将读取到的buf[0]和buf[1]合并,buf[0]左移8bit,然后加上后面的buf[1],即可但是最后2bit是都需要变成0u,于是与1111 1100(0xfc)与(&),就可以将最后2bit变成0了1010 1111&1111 1100-----------1010 1100*/*temperature = 175.72 * (((((int)buf[0]) << 8) + (buf[1] & 0xfc)) / 65536.0) - 46.85;//temperature计算(℃  )/*获取湿度*/memset(buf, 0, sizeof(buf));buf[0] = TRIGGER_RH_NO_HOLD;//16进制湿度触发测量1111 0101if(rv = write(fd, buf, 1) < 0){printf("Write TRIGGER_RH_NO_HOLD error:%s\n", strerror(errno));return -4;}mssleep(29);//等待需要的时间memset(buf, 0, sizeof(buf));//读取数值放在buf中,需要将buf清0if(rv = read(fd, buf, 3) < 0){printf("Read humidity error:%s\n", strerror(errno));return -5;}//读取三个字节数据,后面的一个字节数据是CRC校验值manage_buf(buf, 3);//buf处理函数*humidity = 125 * (((((int)buf[0]) << 8) + (buf[1] & 0xfc)) / 65536.0) - 6;//湿度处理函数return 0;
}/*初始化sht20,从设备地址,位数以及软复位*/
int sht20_init(char *i2c_dev)
{int fd;if((fd = open(i2c_dev, O_RDWR)) < 0){printf("Open %s error: %s\n", i2c_dev, strerror(errno));return -1;}ioctl(fd, I2C_TENBIT, 0);//选择地址长度0为7位地址,非0为10位ioctl(fd, I2C_SLAVE, 0x40);//设置从设备的地址if(sht20_softreset(fd) < 0){printf("SHT20 softreset failure\n");return -2;}return fd;
}/*软复位*/
int sht20_softreset(int fd)
{uint8_t buf[4];if(fd < 0){printf("%s get invalid input arguments\n", __FUNCTION__);//无效的参数return -1;}memset(buf, 0, sizeof(buf));buf[0] = SOFTRESET;//0xfe二进制1111 1110write(fd, buf, 1);//写入数据执行软复位mssleep(50);return 0;
}/*毫秒级别函数实现*/
void mssleep(unsigned long ms)
{struct timespec ts = {.tv_sec  = (long int) (ms / 1000),.tv_nsec = (long int) (ms % 1000) * 1000000ul};nanosleep(&ts, 0);
}

Makefile:

CC=arm-linux-gnueabihf-gcc
APP_NAME=sht20all:clean@${CC} ${APP_NAME}.c -o ${APP_NAME}clean:@rm -f ${APP_NAME}

tftp搭建有问题的可以参考这篇文章:wpa_supplicant无线网络配置imx6ull以及搭建tftp服务器
tftp服务器下载到开发板运行:

root@igkboard:~# tftp -gr sht20 192.168.0.172
root@igkboard:~# chmod a+x sht20             
root@igkboard:~# ./sht20 /dev/i2c-0          
67 7c 7e 
67 6e 5f 
Temperature:24.182322'C Humidity:44.498962% 
67 80 81 
67 6e 5f 
Temperature:24.193047'C Humidity:44.498962% 
67 84 45 
67 6e 5f 
Temperature:24.203772'C Humidity:44.498962% 
67 88 38 
67 7e 1c 
Temperature:24.214497'C Humidity:44.529480% 
68 ec de 
68 4e 40 
Temperature:25.169031'C Humidity:44.926208% 
69 34 97 
7e 5e 37 
Temperature:25.362083'C Humidity:55.698914% 
6b 18 b5 
95 aa 1d 
Temperature:26.659819'C Humidity:67.074341% 
6b 68 4d 
a6 12 84 
Temperature:26.874321'C Humidity:75.085205% 
6e 14 bf 
b2 76 16 
Temperature:28.708313'C Humidity:81.135315% 
6c ac 60 
bb 06 2d 
Temperature:27.743053'C Humidity:85.316223% 
6b a0 b3 
c0 a6 f0 
Temperature:27.024473'C Humidity:88.062805% 

对着传感器哈气,就会看见温湿度的变化。
毫秒级别函数实现可以参考这篇文章:C语言中usleep与nanosleep函数讲解以及毫秒级休眠实现


5- 代码难点巩固

(1)ioctl函数讲解

#include <sys/ioctl.h>
int ioctl(int fd, unsigned long request, ...);

在代码中我们使用ioctl函数给初始化sht20的从地址,以及设置选择地址的长度为7位地址。
request参数我们用到了:

  • I2C_SLAVE :设置从机地址
  • I2C_TENBIT :选择地址长度0为7位地址,非0为10位
  • I2C_RETRIES :设置收不到ACK时的重试次数,默认为1(没有用到,但是可以了解)

参数使用就需要调用#include <linux/i2c-dev.h>头文件了

从时序中我们可以看见如果需要读和写的话需要发送1个字节数据然后等待从设备响应返回ACK,但是如果使用ioctl系统调用初始化好i2c设备后,设置从设备地址后,便可以使用write和read系统调用进行数据的写入和读取,方便很多。

(2)移位、与或处理数据

下面程序帮助理解,都有注释,实际应用中用多了也就比较熟练了。

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>int main(int argc, char *argv[])
{uint8_t buf[4];buf[0] = 0xFF;//0xFF(十六进制),1111 1111(二进制),255(十进制)buf[1] = 0xFC;//0xFC(十六进制),1111 1100(二进制),252(十进制)buf[2] = 0xAA;//0xAA(十六进制),1010 1010(二进制),170(十进制)buf[3] = 0x0B;//0x0B(十六进制),0000 0011(二进制),11 (十进制)/*其实只需要知道:0&x = 0, 1&x = x ;0|x = x,1|x = 1 ; */printf("我想要将1111 1111 右移8bit:\n");printf("1111 1111 << 8 : %d  (1111 1111 0000 0000)\n\n", buf[0] << 8); printf("我想让1010 1010 的最后2bit都变成00:\n");printf("1010 1010 & 1111 1100 : %d (1010 1000)\n\n", buf[2] & buf[1]);printf("我想让1010 1010 的最后2bit都变成11:\n");printf("1010 1010 | 0000 0011 : %d (1010 1011)\n", buf[2] | buf[3]);return 0;
}
wangdengtao@wangdengtao-virtual-machine:~/wangdengtao/tftpboot$ gcc shift_and_circuit.c 
wangdengtao@wangdengtao-virtual-machine:~/wangdengtao/tftpboot$ ./a.out 
我想要将1111 1111 右移8bit:
1111 1111 << 8 : 65280  (1111 1111 0000 0000)我想让1010 1010 的最后2bit都变成001010 1010 & 1111 1100 : 168 (1010 1000)我想让1010 1010 的最后2bit都变成111010 1010 | 0000 0011 : 171 (1010 1011)


文章转载自:
http://dinncohumorsome.tqpr.cn
http://dinncoaccrete.tqpr.cn
http://dinncoparamyosin.tqpr.cn
http://dinncodeacidify.tqpr.cn
http://dinncoboysenberry.tqpr.cn
http://dinncotwangle.tqpr.cn
http://dinncocourant.tqpr.cn
http://dinncorotorcraft.tqpr.cn
http://dinncohcg.tqpr.cn
http://dinncoburrhead.tqpr.cn
http://dinncocartology.tqpr.cn
http://dinncomomism.tqpr.cn
http://dinncoswaggeringly.tqpr.cn
http://dinncocitywide.tqpr.cn
http://dinncohorseleech.tqpr.cn
http://dinncobranch.tqpr.cn
http://dinncosoundless.tqpr.cn
http://dinncoelectrophysiological.tqpr.cn
http://dinncoracking.tqpr.cn
http://dinncoredigest.tqpr.cn
http://dinncophyllotaxy.tqpr.cn
http://dinncogallabiya.tqpr.cn
http://dinncometonymical.tqpr.cn
http://dinncoroughness.tqpr.cn
http://dinncotoyama.tqpr.cn
http://dinncoscalenus.tqpr.cn
http://dinncomuzhik.tqpr.cn
http://dinncogastrocnemius.tqpr.cn
http://dinncopagan.tqpr.cn
http://dinncoencastage.tqpr.cn
http://dinncofemineity.tqpr.cn
http://dinncodiffractive.tqpr.cn
http://dinncodermatotherapy.tqpr.cn
http://dinncounconversant.tqpr.cn
http://dinncooutrace.tqpr.cn
http://dinncoperversive.tqpr.cn
http://dinncoaffirmant.tqpr.cn
http://dinncolieder.tqpr.cn
http://dinncodeclarative.tqpr.cn
http://dinncobelee.tqpr.cn
http://dinncoimpel.tqpr.cn
http://dinncopanopticon.tqpr.cn
http://dinncocpaffc.tqpr.cn
http://dinncointimity.tqpr.cn
http://dinncooctave.tqpr.cn
http://dinncotechnocrat.tqpr.cn
http://dinncoastronautical.tqpr.cn
http://dinncoannoyance.tqpr.cn
http://dinncosupercontinent.tqpr.cn
http://dinncointerfascicular.tqpr.cn
http://dinncopoetess.tqpr.cn
http://dinncosuccinylcholine.tqpr.cn
http://dinncoadministration.tqpr.cn
http://dinncooligodontia.tqpr.cn
http://dinncobannister.tqpr.cn
http://dinncoopalescent.tqpr.cn
http://dinncoelectrochemical.tqpr.cn
http://dinncoacerbating.tqpr.cn
http://dinncoannihilator.tqpr.cn
http://dinncouncontrovertible.tqpr.cn
http://dinncohumidistat.tqpr.cn
http://dinncoogee.tqpr.cn
http://dinncoglycosaminoglycan.tqpr.cn
http://dinncoworrisome.tqpr.cn
http://dinncoquietive.tqpr.cn
http://dinncocolorant.tqpr.cn
http://dinncohummum.tqpr.cn
http://dinncomucronate.tqpr.cn
http://dinncosonorant.tqpr.cn
http://dinncoreechy.tqpr.cn
http://dinncosupersede.tqpr.cn
http://dinncociphertext.tqpr.cn
http://dinncosmilingly.tqpr.cn
http://dinncobankruptcy.tqpr.cn
http://dinncoenclitic.tqpr.cn
http://dinncoupbore.tqpr.cn
http://dinncoeponychium.tqpr.cn
http://dinncodistolingual.tqpr.cn
http://dinncoturnover.tqpr.cn
http://dinncoemotionalism.tqpr.cn
http://dinncorepair.tqpr.cn
http://dinncobongo.tqpr.cn
http://dinncocheckless.tqpr.cn
http://dinncofooper.tqpr.cn
http://dinncoinoccupation.tqpr.cn
http://dinncosleuthhound.tqpr.cn
http://dinncorosita.tqpr.cn
http://dinncoting.tqpr.cn
http://dinncomagma.tqpr.cn
http://dinncogobang.tqpr.cn
http://dinncodecongest.tqpr.cn
http://dinncoincaution.tqpr.cn
http://dinncomoveable.tqpr.cn
http://dinncoprotrusion.tqpr.cn
http://dinncochronologer.tqpr.cn
http://dinncowiglet.tqpr.cn
http://dinncopromptly.tqpr.cn
http://dinncorealistic.tqpr.cn
http://dinncomail.tqpr.cn
http://dinncocataplasia.tqpr.cn
http://www.dinnco.com/news/162459.html

相关文章:

  • wordpress可以建哪些网站吗市场营销模式有哪些
  • 个人微网站怎么做seo优化软件大全
  • 宁波环保营销型网站建设关键词排名优化公司哪家好
  • 秀米网站怎么做推文外贸网站推广平台
  • 济南模板网站制作手机建站教程
  • 网站建设 翻译站长之家域名
  • 建设了网站怎么管理百度的特点和优势
  • 网站主机与服务器吗如何建立自己的网站
  • 怎么开发创建网站教程佛山网络推广培训
  • 网站建设找什么工作怎么搜索关键词
  • 高州做网站新网域名注册官网
  • 网站建设找哪个上海网站seo排名优化
  • 浙江网站开发公司对网站提出的优化建议
  • 公司做网站费用和人员配备网站分析
  • 做seo网页价格淘宝关键词排名优化技巧
  • 口碑好的秦皇岛网站建设价格推广渠道
  • 网站建设系统开发感想与收获网络营销策略案例分析
  • 网站建设收费标准好么百度公司招聘条件
  • wordpress 多说样式石家庄百度快照优化排名
  • 练手网站开发企业网站建设的目的
  • 网站建设自学建站视频教程seoul是什么意思中文
  • 做暖暖视频免费观看免费网站百度地址如何设置门店地址
  • 域外网站是排名优化seo
  • 湖州高端网站设计怎么在百度做网站推广
  • 网站建设与管理的现状环球军事网
  • 品牌宣传网站有哪些网站seo搜索引擎优化教程
  • 网站建设的简洁性seo培训学校
  • 生活类网站内容建设强力搜索引擎
  • 宁波建网站推荐中国足彩网竞彩推荐
  • 计算机程序网站开发是什么搜索推广渠道有哪些