AHT20实现温湿度采集

AHT20+串口采集温湿度

AHT20

AHT20是国内奥松生成的I2C接口的MEMS温湿度传感器,ADC位数为20Bit,具有体积小、精度高、成本低等优点。相较于AHT10,最显著的变化是体积由 541.6mm,缩小到 331.0mm。相对湿度精度 RH=±2%,温度精度 T=±0.3°C。相对湿度测量范围 RH=0~100%,温度测量范围 T=-40~85°C。
在这里插入图片描述
其中I2C接口有硬件I2C和软件I2C之分, 所谓硬件I2C对应芯片上的I2C外设,有相应I2C驱动电路,其所使用的I2C管脚也是专用的;软件I2C一般是用GPIO管脚,用软件控制管脚状态以模拟I2C通信波形。
硬件I2C的效率要远高于软件的,而软件I2C由于不受管脚限制,接口比较灵活。
软件I2C即模拟I2C 是通过GPIO,软件模拟寄存器的工作方式,而硬件(固件)I2C是直接调用内部寄存器进行配置。

温湿度功能实现

本文基于st-link和AHT20实现采集温湿度数据,通过串口发送到上位机
在野火官网上下载教程源码压缩包,解压
  • 选择其中的一个空白文件工程打开
    在这里插入图片描述
  • 添加相关的代码
    右键点击USER文件夹,选择manage project items
    在这里插入图片描述
    添加此工程中的usart.c,usart.h,bsp_i2.c,bsp_i2c.h, delay.c ,delay.h ,sys.c ,sys.h文件
  • 修改main.c
#include "delay.h"
#include "usart.h"
#include "bsp_i2c.h"
int main(void)
{	
	delay_init();     
	uart_init(115200);	 
	IIC_Init();
		while(1)
	{
		printf("wait:   ");
		read_AHT20_once();
		delay_ms(1500);
  }
}

修改bsp_i2.c部分代码

void read_AHT20(void)
{
	uint8_t   i;

	for(i=0; i<6; i++)
	{
		readByte[i]=0;
	}

	//-------------
	I2C_Start();

	I2C_WriteByte(0x71);
	ack_status = Receive_ACK();
	readByte[0]= I2C_ReadByte();
	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();

	//--------------
	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("wrong|||");

	}
	printf("\r\n");
	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");
}
  • 效果展示
    连接AHT20与野火开发版
    在这里插入图片描述

    使用st-link烧录,然后PC机打开野火多功能助手显示
    在这里插入图片描述

总结+参考

代码调试是借鉴别人的,在这个方面还是需要继续学习。线路的接法要参考芯片手册。
硬件i2c和软件i2c

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个基于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); ``` 请注意,这只是一个示例代码,并且可能需要根据您的具体需求进行修改和调整。在实际使用中,请务必小心谨慎,以确保您的设备和数据的安全。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值