其他分享
首页 > 其他分享> > AHT10_温湿度传感器

AHT10_温湿度传感器

作者:互联网

基于ESP8266 non_os sdk 开发

关于ATH10的介绍就不过多赘述了,不了解的伙伴直接去百度找相关资料进行学习,这里我直接给出驱动代码了。

AHT10 头文件

#ifndef _I2C_ATH10_H_
#define _I2C_ATH10_H_
#include "c_types.h"
void delayms(u32 ms);
void AHT10Init();
void AHT10_RST();
unsigned char AHT10_Status();
unsigned char AHT10_CalEN();
void ATH10_Init();
void AHT10_Mea();
void AHT10_Read_Data();

#endif

AHT10 C文件

#include "driver/i2c_master.h"
#include "osapi.h"
#include "c_types.h"
#include "gpio.h"
#define AHT10AddWr 0x70 //AHT10写数据地址
#define AHT10AddRd 0x71 //AHT10读数据地址

void delayms(u32 ms)
{
	while (ms--)
	{
		os_delay_us(1000);
	}
}
void AHT10Init() //AHT10初始化
{
	u8 ack;
	delayms(12);
	i2c_master_start();
	i2c_master_writeByte(0x70);
	ack = i2c_master_checkAck();
	i2c_master_writeByte(0xe1); //配置寄存器
	ack = i2c_master_checkAck();
	i2c_master_writeByte(0x08);
	ack = i2c_master_checkAck();
	i2c_master_writeByte(0x00);
	ack = i2c_master_checkAck();
	if (!ack)
	{
		os_printf("AHT10Init failed\n");
	}
	i2c_master_stop();
	delayms(400);
}
void AHT10_RST() //软复位
{
	i2c_master_start();
	i2c_master_writeByte(AHT10AddWr);

	i2c_master_writeByte(0xba);
	i2c_master_stop();
}
unsigned char AHT10_Status() //读取AHT10状态寄存器
{
	unsigned char byte_first;
	i2c_master_start();
	i2c_master_writeByte(AHT10AddRd);
	byte_first = i2c_master_readByte(); //接收数据
	i2c_master_send_nack();				//非应答
	i2c_master_stop();
	return byte_first;
}
unsigned char AHT10_CalEN() //判断AHT10校准使能
{
	unsigned char val;
	val = AHT10_Status();
	if ((val & 0x68) == 0x08)
		return 1;
	else
		return 0;
}
void ATH10_Init()
{
	AHT10Init();
	//等待状态字status的bit[3]=1时采取读取数据。如果bit[3]不等于1
	//发软件复位给0xBA给ATH10,再重新初始化ATH10
	if (AHT10_CalEN() == 0)
	{
		while (AHT10_CalEN() == 0)
		{
			system_soft_wdt_feed();
			AHT10_RST();
			delayms(25);
			AHT10Init();
			if (AHT10_CalEN() == 1)
			{
				os_printf("bit3 ok\n");
				break;
			}
		}
	}
	else
	{
		os_printf("bit3 ok\n");
	}
}
void AHT10_Mea() //触发测量
{
	u8 ack;
	i2c_master_start();
	i2c_master_writeByte(AHT10AddWr);
	 ack = i2c_master_checkAck();
	delayms(20);
	i2c_master_writeByte(0xac); //触发测量指令
	 ack = i2c_master_checkAck();
	delayms(20);
	i2c_master_writeByte(0x33);
	delayms(20);
	 ack = i2c_master_checkAck();
	i2c_master_writeByte(0x00);
	delayms(20);
	 ack = i2c_master_checkAck();
	if(!ack)
	{
		os_printf("AHT10_Mea failed\n");
	}
	i2c_master_stop();
}
void AHT10_Read_Data() //接收湿度温度数据
{
	int ct[2];
	volatile unsigned char byte_1th = 0;
	volatile unsigned char byte_2th = 0;
	volatile unsigned char byte_3th = 0;
	volatile unsigned char byte_4th = 0;
	volatile unsigned char byte_5th = 0;
	volatile unsigned char byte_6th = 0;
	u32 retudata = 0;
	unsigned char cnt = 0;
	int temp;
	int Rh;
	AHT10_Mea();  //触发测量
	delayms(200); //延时200毫秒
	cnt = 0;
	while (((AHT10_Status() & 0x80) == 0x80))
	{
		delayms(3);
		if (cnt++ >= 100)
		{
			break;
		}
	}
	i2c_master_start();
	i2c_master_writeByte(AHT10AddRd);
	if (!i2c_master_checkAck())
	{
		os_printf("no get Addrd\n");
	}
	byte_1th = i2c_master_readByte(); //状态数据
	i2c_master_send_ack();			  //应答
	delayms(10);
	byte_2th = i2c_master_readByte(); //湿度数据
	i2c_master_send_ack();
	delayms(10);
	byte_3th = i2c_master_readByte(); //湿度数据
	i2c_master_send_ack();
	delayms(10);
	byte_4th = i2c_master_readByte(); //高4位为湿度  低4位为温度
	i2c_master_send_ack();
	delayms(10);
	byte_5th = i2c_master_readByte(); //温度数据
	i2c_master_send_ack();
	delayms(10);
	byte_6th = i2c_master_readByte(); //温度数据
	i2c_master_send_nack();
	i2c_master_stop();

	retudata = 0; //原始湿度数据合成
	retudata = (retudata | byte_2th) << 8;
	retudata = (retudata | byte_3th) << 8;
	retudata = retudata | byte_4th;
	retudata = retudata >> 4;
	ct[0] = retudata;

	retudata = 0; //原始温度数据合成
	retudata = (retudata | byte_4th) << 8;
	retudata = (retudata | byte_5th) << 8;
	retudata = retudata | byte_6th;
	retudata = retudata & 0xfffff;
	ct[1] = retudata;
	delayms(10);

	Rh = (ct[0] * 1000 / 1024 / 1024);
	temp = (ct[1] * 200 * 10 / 1024 / 1024 - 500); //计算温度
	os_printf("temp: %d.%d℃  RH: %d.%d%\n", temp / 10, temp % 10, Rh / 10, Rh % 10);
}

标签:delayms,ack,温湿度,master,传感器,byte,i2c,AHT10
来源: https://blog.csdn.net/m0_50620305/article/details/113845253