AHT20温度传感器案例

warning: 这篇文章距离上次修改已过1189天,其中的内容可能已经有所变动。

Arduino:

#include <Adafruit_AHTX0.h>
Adafruit_AHTX0 aht;

void setup() {
  Serial.begin(115200);
  Serial.println("Adafruit AHT10/AHT20 demo!");

  if (!aht.begin()) {
    Serial.println("Could not find AHT? Check wiring");
    while (1) delay(10);
  }
  Serial.println("AHT10 or AHT20 found");
}

void loop() {
  sensors_event_t humidity, temp;
  aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
  Serial.print("Temperature: ");
  Serial.print(temp.temperature);
  Serial.println(" degrees C");
  Serial.print("Humidity: ");
  Serial.print(humidity.relative_humidity);
  Serial.println("% rH");

  delay(500);
}

STC15F104W:

temhum.c文件:

#include "temhum.h"
#include "iic.h"
#include "intrins.h"
#include "delay.h"
#include <uart.h>

//读取AHT20的状态寄存器
unsigned char AHT20_Read_Status(void)
{
    unsigned char Byte_first,flag;    
    I2C_Start();
    I2C_send_byte(0x71);
    flag=I2C_Wait_Ack();
    Byte_first = I2C_read_byte(flag);
    I2C_NAck();
    I2C_Stop();

    return Byte_first;
}

//向AHT20发送AC命令
void AHT20_SendAC(void) 
{
    I2C_Start();
    I2C_send_byte(0x70);    //启动传输后发送的01110000 (最后1bit表示读/写 0--写,1--读)
    I2C_Wait_Ack();
    I2C_send_byte(0xac);//0xAC采集命令 命令参数有两个字节,第一个字节为 0x33,第二个字节为0x00。
    I2C_Wait_Ack();
    I2C_send_byte(0x33);
    I2C_Wait_Ack();
    I2C_send_byte(0x00);
    I2C_Wait_Ack();
    I2C_Stop();
}
//初始化AHT20
void AHT20_Init(void)   
{    
    I2C_init();
    I2C_Start();
    I2C_send_byte(0x70);
    I2C_Wait_Ack();
    I2C_send_byte(0xa8);//0xA8进入NOR工作模式
    I2C_Wait_Ack();
    I2C_send_byte(0x00);
    I2C_Wait_Ack();
    I2C_send_byte(0x00);
    I2C_Wait_Ack();
    I2C_Stop();

    delay_ms(10);//延时10ms左右

    I2C_Start();
    I2C_send_byte(0x70);
    I2C_Wait_Ack();
    I2C_send_byte(0xbe);//0xBE初始化命令,AHT20的初始化命令是0xBE,   AHT10的初始化命令是0xE1
    I2C_Wait_Ack();
    I2C_send_byte(0x08);//相关寄存器bit[3]置1,为校准输出
    I2C_Wait_Ack();
    I2C_send_byte(0x00);
    I2C_Wait_Ack();
    I2C_Stop();
    delay_ms(10);//延时10ms左右
}

void temphum_init()
{
    delay_ms(40);//刚上电,延时40ms才可以读取状态
    
    //首先发0x71读取状态字bit[3],如果=1,为校准输出,无须初始化!!!正常情况下读回来的状态是0x1C或者是0x18,读回来是0x80表示忙状态;
    if(!((AHT20_Read_Status()&0x08)==0x08))
    {
        AHT20_Init(); //初始化AHT20 
    }
}


//没有CRC校验,直接读取AHT20的温度和湿度数据
void AHT20_Read_CTdata(unsigned long *ct) 
{
    unsigned char Byte_1th=0,Byte_2th=0,Byte_3th=0;
    unsigned char Byte_4th=0,Byte_5th=0,Byte_6th=0;
    unsigned long RetuData = 0;   // u32
    unsigned char flag;  // u16
    AHT20_SendAC();//向AHT20发送AC命令
    delay_ms(80);    //大约延时80ms
    
    delay_ms(1);
    
    I2C_Start();
    I2C_send_byte(0x71);
    flag=I2C_Wait_Ack();
    Byte_1th = I2C_read_byte(flag);//状态字
    Byte_2th = I2C_read_byte(flag);//湿度,发送ACK(继续发送)
    
    Byte_3th = I2C_read_byte(flag);//湿度
    Byte_4th = I2C_read_byte(flag);//湿度/温度
    Byte_5th = I2C_read_byte(flag);//温度
    Byte_6th = I2C_read_byte(!flag);//温度,发送NACK(停止发送)  
    I2C_Stop();
    
    //保存得到的数据到RetuData中
    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&0x0fffff;
    ct[1] =RetuData; //温度
    
}

main.c文件:

#include <reg51.h>
#include <intrins.h>
#include <stdio.h>
#include <uart.h>
#include <iic.h>
#include <temhum.h>
#include <delay.h>

typedef unsigned char u8;
typedef unsigned int u16;

// 串口有关
sfr AUXR = 0x8E;
u8 buf_index = 0;
unsigned long CT_data[2];
double hum, tem;
u8 temp_str[5];
u8 hum_shi, hum_ge, hum_dian, tem_shi, tem_ge, tem_dian;

u8 d0,d1,d2;
void main(){
    TMOD = 0x00; //定时器 0 在 16 位自动重装模式
    AUXR = 0x80; //定时器 0 在 1T 模式
    TL0 = BAUD;
    TH0 = BAUD >> 8; //初始化定时器 0 并设定重装值
    TR0 = 1;         //定时器 0 开始运行
    ET0 = 1;         //使能定时器 0 中断
    PT0 = 1;         //提高定时器 0 中断优先级
    EA = 1;          //打开中断开关
    UART_INIT();
    
    temphum_init();     //ATH20初始化
    
    while(1){
        AHT20_Read_CTdata(CT_data);
                
//        hum = CT_data[0]*100*10/1024/1024;  //计算得到湿度值(放大了10倍)
        hum = CT_data[0]*100*10>>20;  //计算得到湿度值(放大了10倍)
        tem = (int)(CT_data[1]*200*10>>20) - 500;//计算得到温度值(放大了10倍)
        
        /*d0=(int)tem>>16;
        d1= (int)tem>>8;
        UART_SEND(d0);
        UART_SEND(d1);
        UART_SEND((int)tem&0xff);*/
        
        hum_shi = (int)hum/100;
        hum_ge = (int)hum/10%10;
        hum_dian = (int)hum%10;
        
        tem_shi = (int)tem/100;
        tem_ge = (int)tem/10%10;
        tem_dian = (int)tem%10;
        
        //temp = d0 + d1 / 10.0;
        //sprintf(str, "%.1f, %.1f", hum, tem);
        
        //sprintf(str, "tem: %.2f", temp);
        //UART_SEND_STRING(str);
        UART_SEND_STRING("humidity: ");
        UART_SEND('0'+hum_shi);
        UART_SEND('0'+hum_ge);
        UART_SEND('.');
        UART_SEND('0'+hum_dian);
        UART_SEND_STRING(", ");
        UART_SEND_STRING("temperature: ");
        UART_SEND('0'+tem_shi);
        UART_SEND('0'+tem_ge);
        UART_SEND('.');
        UART_SEND('0'+tem_dian);
        //sprintf(temp_str, "%.2f", temp);
        //UART_SEND_STRING(temp_str);
        
        //UART_SEND(d0);
        //UART_SEND(d1);
        UART_SEND_STRING("\r\n");
        
        delay_ms(1000);
    }
}

实践中发现,8位单片机也可以进行16位运算!

添加新评论