基于I2C的温湿度采集(AHT20)

目录

一、I2C总线通信协议

1、I2C介绍 

 2.I2C物理层

 3.I2C协议层

 4、软件IIC和硬件IIC

 二、AHT20温湿度采集程序的实现

三、电路连接以及结果演示

四、参考文献 


 

一、I2C总线通信协议

1、I2C介绍 

IIC(Inter-Integrated Circuit)总线是一种由NXP(原PHILIPS)公司开发的两线式串行总线,用于连接微控制器及其外围设备。多用于主控制器和从器件间的主从通信,在小数据量场合使用,传输距离短,任意时刻只能有一个主机等特性。

在CPU与被控IC之间、IC与IC之间进行双向传送,高速IIC总线一般可达400kbps 以上。

注意IIC是为了与低速设备通信而发明的,所以IIC的传输速率比不上SPI

 

 2.I2C物理层

 (1) 它是一个支持设备的总线。“总线”指多个设备共用的信号线。在一个 I2C 通讯总线中,可连接多个 I2C 通讯设备,支持多个通讯主机及多个通讯从机。
(2) 一个 I2C 总线只使用两条总线线路,一条双向串行数据线(SDA) ,一条串行时钟线(SCL)。数据线即用来表示数据,时钟线用于数据收发同步。

 3.I2C协议层

主要是定义了通讯的起始和停止信号、数据有效性、响应、仲裁、时钟同步和地址广播等。 

 通讯的起始和停止信号

 数据有效性

 由图看出,只有在SCL处于高电平时,SDA的数据传输才是有效的。

 4、软件IIC和硬件IIC

IIC分为软件IIC和硬件IIC 

(1)软件IIC通信指的是用单片机的两个I/O端口模拟出来的IIC,用软件控制管脚状态以模拟I2C通信波形,软件模拟寄存器的工作方式。

 直接使用 CPU 内核按照 I2C 协议的要求控制 GPIO 输出高低电平,从而模拟I2C。
使用: 需要在控制产生 I2C 的起始信号时,控制作为SCL 线的 GPIO 引脚输出高电平,然后控制作为 SDA 线的 GPIO 引脚在此期间完成由高电平至低电平的切换,最后再控制SCL线切换为低电平,这样就输出了一个标准的 I2C 起始信号。

 (2)硬件IIC直接利用 STM32 芯片中的硬件 I2C 外设。

只要配置好对应的寄存器,外设就会产生标准串口协议的时序。在初始化好 I2C 外设后,只需要把某寄存器位置 1,此时外设就会控制对应的 SCL 及 SDA 线自动产生 I2C 起始信号,不需要内核直接控制引脚的电平。

 二、AHT20温湿度采集程序的实现

了解AHT20芯片的相关信息
具体信息请到官方下载对应产品介绍文档,资料链接如下

软件下载-温湿度传感器 温湿度芯片 温湿度变送器模块 气体传感器 流量传感器 广州奥松电子股份有限公司http://www.aosong.com/class-36.html主函数main.c

#include "delay.h"
#include "usart.h"
#include "bsp_i2c.h"


int main(void)
{	
	delay_init();     
	uart_init(115200);	//串口通信波特率115200
	IIC_Init();
		while(1)
	{
		printf("温度湿度显示");
		read_AHT20_once();
		delay_ms(1500);
  }
}

AHT20芯片的使用过程 

void  read_AHT20_once(void)
{
	delay_ms(10);

	reset_AHT20();//重置AHT20芯片
	delay_ms(10);

	init_AHT20();//初始化AHT20芯片
	delay_ms(10);

	startMeasure_AHT20();//开始测试AHT20芯片
	delay_ms(80);

	read_AHT20();//读取AHT20采集的到的数据
	delay_ms(5);
}

 AHT20芯片读取数据

void read_AHT20(void)
{
	uint8_t   i;

	for(i=0; i<6; i++)
	{
		readByte[i]=0;
	}
	I2C_Start();//I2C启动

	I2C_WriteByte(0x71);//I2C写数据
	ack_status = Receive_ACK();//收到的应答信息
	readByte[0]= I2C_ReadByte();//I2C读取数据
	Send_ACK();//发送应答信息

	readByte[1]= I2C_ReadByte();
	Send_ACK();

	readByte[2]= I2C_ReadByte();
	Send_ACK();

	readByte[3]= I2C_ReadByte();
	Send_ACK();

	readByte[4]= I2C_ReadByte();
	Send_ACK();

	readByte[5]= I2C_ReadByte();
	SendNot_Ack();
	//Send_ACK();

	I2C_Stop();//I2C停止函数
	//判断读取到的第一个字节是不是0x08,0x08是该芯片读取流程中规定的,如果读取过程没有问题,就对读到的数据进行相应的处理
	if( (readByte[0] & 0x68) == 0x08 )
	{
		H1 = readByte[1];
		H1 = (H1<<8) | readByte[2];
		H1 = (H1<<8) | readByte[3];
		H1 = H1>>4;

		H1 = (H1*1000)/1024/1024;

		T1 = readByte[3];
		T1 = T1 & 0x0000000F;
		T1 = (T1<<8) | readByte[4];
		T1 = (T1<<8) | readByte[5];

		T1 = (T1*2000)/1024/1024 - 500;

		AHT20_OutData[0] = (H1>>8) & 0x000000FF;
		AHT20_OutData[1] = H1 & 0x000000FF;

		AHT20_OutData[2] = (T1>>8) & 0x000000FF;
		AHT20_OutData[3] = T1 & 0x000000FF;
	}
	else
	{
		AHT20_OutData[0] = 0xFF;
		AHT20_OutData[1] = 0xFF;

		AHT20_OutData[2] = 0xFF;
		AHT20_OutData[3] = 0xFF;
		printf("读取失败!!!");

	}
	printf("\r\n");
	//根据AHT20芯片中,温度和湿度的计算公式,得到最终的结果,通过串口显示
	printf("温度:%d%d.%d",T1/100,(T1/10)%10,T1%10);
	printf("湿度:%d%d.%d",H1/100,(H1/10)%10,H1%10);
	printf("\r\n");
}

编译运行之后即可烧录。

三、电路连接以及结果演示

电路连接
AHT20STM32
VCC3V3
GNDGND
SCL

PB6

SDAPB7

实验结果如下:

 

 把手放在模块上或者对着模块吹气都能改变模块的温度与湿度。

四、参考文献 

(2条消息) stm32通过I2C接口实现温湿度(AHT20)的采集_Harriet的博客-CSDN博客icon-default.png?t=LA92https://blog.csdn.net/qq_43279579/article/details/111597278IIC原理超详细讲解---值得一看_Z小旋-CSDN博客_iic原理icon-default.png?t=LA92https://blog.csdn.net/as480133937/article/details/105366932 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于Linux 4.9内核的AHT20 IIC驱动,用于采集温湿度的功能代码。在此之前,您需要先编写一个用户空间程序,用于打开IIC设备节点并读取温湿度数据。以下代码假设您已经完成了这一步骤,并定义了一个名为`aht20_read`的函数,用于读取AHT20温湿度数据。 ``` #include <linux/i2c.h> #include <linux/init.h> #include <linux/module.h> #include <linux/delay.h> #include <linux/kernel.h> // AHT20 I2C Address #define AHT20_I2C_ADDR 0x38 // AHT20 Commands #define AHT20_CMD_INIT 0b1110001 #define AHT20_CMD_MEASURE 0b10101100 #define AHT20_CMD_SOFT_RESET 0b10111010 static int aht20_probe(struct i2c_client *client, const struct i2c_device_id *id) { int ret; u8 buf[3]; // Initialize AHT20 buf[0] = AHT20_CMD_INIT; ret = i2c_master_send(client, buf, 1); if (ret < 0) { dev_err(&client->dev, "Failed to send AHT20 initialization command\n"); return ret; } msleep(20); // Soft reset AHT20 buf[0] = AHT20_CMD_SOFT_RESET; ret = i2c_master_send(client, buf, 1); if (ret < 0) { dev_err(&client->dev, "Failed to send AHT20 soft reset command\n"); return ret; } msleep(20); return 0; } static int aht20_read_measurement(struct i2c_client *client, u16 *humidity, u16 *temperature) { int ret; u8 buf[6]; // Send measure command to AHT20 buf[0] = AHT20_CMD_MEASURE; ret = i2c_master_send(client, buf, 1); if (ret < 0) { dev_err(&client->dev, "Failed to send AHT20 measure command\n"); return ret; } msleep(80); // Read measurement data from AHT20 ret = i2c_master_recv(client, buf, sizeof(buf)); if (ret < 0) { dev_err(&client->dev, "Failed to read AHT20 measurement data\n"); return ret; } // Convert measurement data to humidity and temperature values *humidity = (buf[1] << 12) | (buf[2] << 4) | (buf[3] >> 4); *temperature = ((buf[3] & 0x0F) << 16) | (buf[4] << 8) | buf[5]; return 0; } static int aht20_read(struct i2c_client *client, u16 *humidity, u16 *temperature) { int ret; // Read measurement data from AHT20 ret = aht20_read_measurement(client, humidity, temperature); if (ret < 0) { dev_err(&client->dev, "Failed to read AHT20 measurement data\n"); return ret; } return 0; } static const struct i2c_device_id aht20_id[] = { { "aht20", 0 }, { } }; MODULE_DEVICE_TABLE(i2c, aht20_id); static struct i2c_driver aht20_driver = { .driver = { .name = "aht20", }, .probe = aht20_probe, .id_table = aht20_id, }; module_i2c_driver(aht20_driver); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("AHT20 IIC Driver"); MODULE_LICENSE("GPL"); ``` 这段代码实现了从AHT20读取温湿度数据的功能。在用户空间程序中,您可以打开IIC设备节点并调用`aht20_read`函数来读取温湿度数据。例如: ``` int fd = open("/dev/i2c-1", O_RDWR); if (fd < 0) { perror("Failed to open IIC device node"); return -1; } int addr = 0x38; if (ioctl(fd, I2C_SLAVE, addr) < 0) { perror("Failed to set IIC slave address"); close(fd); return -1; } u16 humidity, temperature; if (read(fd, &humidity, sizeof(humidity)) != sizeof(humidity)) { perror("Failed to read humidity data"); close(fd); return -1; } if (read(fd, &temperature, sizeof(temperature)) != sizeof(temperature)) { perror("Failed to read temperature data"); close(fd); return -1; } printf("Humidity: %d%%\n", humidity); printf("Temperature: %d.%d C\n", temperature / 100, temperature % 100); close(fd); ``` 请注意,这只是一个示例代码,并且可能需要根据您的具体需求进行修改和调整。在实际使用中,请务必小心谨慎,以确保您的设备和数据的安全。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值