本文來源電子發燒友社區,作者:華仔stm32, 帖子地址:https://bbs.elecfans.com/jishu_2299452_1_1.html
在前面驅動OLED的前提下,驅動AHT20并顯示到顯示屏,是監測溫濕度常用的手法。
1、在app目錄下新建i2caht20文件夾,把oled_ssd1306.c以及oled_ssd1306.h拷貝到該目錄下。
2、新建aht20.c、aht20.h,以及aht20.test.c,以及BUILD.gn三個文件。
3、aht20.c內容如下:
/*
* Copyright (C) 2021 HiHope Open Source Organization .
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*
* limitations under the License.
*/
#include "aht20.h"
#include
#include
#include
#include "hi_i2c.h"
#include "hi_errno.h"
#define AHT20_I2C_IDX HI_I2C_IDX_0
#define AHT20_STARTUP_TIME 20*1000 // 上電啟動時間
#define AHT20_CALIBRATION_TIME 40*1000 // 初始化(校準)時間
#define AHT20_MEASURE_TIME 75*1000 // 測量時間
#define AHT20_DEVICE_ADDR 0x38
#define AHT20_READ_ADDR ((0x38<<1)|0x1)
#define AHT20_WRITE_ADDR ((0x38<<1)|0x0)
#define AHT20_CMD_CALIBRATION 0xBE // 初始化(校準)命令
#define AHT20_CMD_CALIBRATION_ARG0 0x08
#define AHT20_CMD_CALIBRATION_ARG1 0x00
/**
* 傳感器在采集時需要時間,主機發出測量指令(0xAC)后,延時75毫秒以上再讀取轉換后的數據并判斷返回的狀態位是否正常。
* 若狀態比特位[Bit7]為0代表數據可正常讀取,為1時傳感器為忙狀態,主機需要等待數據處理完成。
**/
#define AHT20_CMD_TRIGGER 0xAC // 觸發測量命令
#define AHT20_CMD_TRIGGER_ARG0 0x33
#define AHT20_CMD_TRIGGER_ARG1 0x00
// 用于在無需關閉和再次打開電源的情況下,重新啟動傳感器系統,軟復位所需時間不超過20 毫秒
#define AHT20_CMD_RESET 0xBA // 軟復位命令
#define AHT20_CMD_STATUS 0x71 // 獲取狀態命令
/**
* STATUS 命令回復:
* 1. 初始化后觸發測量之前,STATUS 只回復 1B 狀態值;
* 2. 觸發測量之后,STATUS 回復6B: 1B 狀態值 + 2B 濕度 + 4b濕度 + 4b溫度 + 2B 溫度
* RH = Srh / 2^20 * 100%
* T = St / 2^20 * 200 - 50
**/
#define AHT20_STATUS_BUSY_SHIFT 7 // bit[7] Busy indication
#define AHT20_STATUS_BUSY_MASK (0x1<#define AHT20_STATUS_BUSY(status) ((status & AHT20_STATUS_BUSY_MASK) >> AHT20_STATUS_BUSY_SHIFT)
#define AHT20_STATUS_MODE_SHIFT 5 // bit[6:5] Mode Status
#define AHT20_STATUS_MODE_MASK (0x3<#define AHT20_STATUS_MODE(status) ((status & AHT20_STATUS_MODE_MASK) >> AHT20_STATUS_MODE_SHIFT)
// bit[4] Reserved
#define AHT20_STATUS_CALI_SHIFT 3 // bit[3] CAL Enable
#define AHT20_STATUS_CALI_MASK (0x1<#define AHT20_STATUS_CALI(status) ((status & AHT20_STATUS_CALI_MASK) >> AHT20_STATUS_CALI_SHIFT)
// bit[2:0] Reserved
#define AHT20_STATUS_RESPONSE_MAX 6
#define AHT20_RESOLUTION (1<<20) // 2^20
#define AHT20_MAX_RETRY 10
static uint32_t AHT20_Read(uint8_t* buffer, uint32_t buffLen)
{
hi_i2c_data data = { 0 };
data.receive_buf = buffer;
data.receive_len = buffLen;
uint32_t retval = hi_i2c_read(AHT20_I2C_IDX, AHT20_READ_ADDR, &data);
if (retval != HI_ERR_SUCCESS) {
printf("I2cRead() failed, %0X!n", retval);
return retval;
}
return HI_ERR_SUCCESS;
}
static uint32_t AHT20_Write(uint8_t* buffer, uint32_t buffLen)
{
hi_i2c_data data = { 0 };
data.send_buf = buffer;
data.send_len = buffLen;
uint32_t retval = hi_i2c_write(AHT20_I2C_IDX, AHT20_WRITE_ADDR, &data);
if (retval != HI_ERR_SUCCESS) {
printf("I2cWrite(%02X) failed, %0X!n", buffer[0], retval);
return retval;
}
return HI_ERR_SUCCESS;
}
// 發送獲取狀態命令
static uint32_t AHT20_StatusCommand(void)
{
uint8_t statusCmd[] = { AHT20_CMD_STATUS };
return AHT20_Write(statusCmd, sizeof(statusCmd));
}
// 發送軟復位命令
static uint32_t AHT20_ResetCommand(void)
{
uint8_t resetCmd[] = {AHT20_CMD_RESET};
return AHT20_Write(resetCmd, sizeof(resetCmd));
}
// 發送初始化校準命令
static uint32_t AHT20_CalibrateCommand(void)
{
uint8_t clibrateCmd[] = {AHT20_CMD_CALIBRATION, AHT20_CMD_CALIBRATION_ARG0, AHT20_CMD_CALIBRATION_ARG1};
return AHT20_Write(clibrateCmd, sizeof(clibrateCmd));
}
// 讀取溫濕度值之前, 首先要看狀態字的校準使能位Bit[3]是否為 1(通過發送0x71可以獲取一個字節的狀態字),
// 如果不為1,要發送0xBE命令(初始化),此命令參數有兩個字節, 第一個字節為0x08,第二個字節為0x00。
uint32_t AHT20_Calibrate(void)
{
uint32_t retval = 0;
uint8_t buffer[AHT20_STATUS_RESPONSE_MAX];
memset(&buffer, 0x0, sizeof(buffer));
retval = AHT20_StatusCommand();
if (retval != HI_ERR_SUCCESS) {
return retval;
}
retval = AHT20_Read(buffer, sizeof(buffer));
if (retval != HI_ERR_SUCCESS) {
return retval;
}
if (AHT20_STATUS_BUSY(buffer[0]) || !AHT20_STATUS_CALI(buffer[0])) {
retval = AHT20_ResetCommand();
if (retval != HI_ERR_SUCCESS) {
return retval;
}
usleep(AHT20_STARTUP_TIME);
retval = AHT20_CalibrateCommand();
usleep(AHT20_CALIBRATION_TIME);
return retval;
}
return HI_ERR_SUCCESS;
}
// 發送 觸發測量 命令,開始測量
uint32_t AHT20_StartMeasure(void)
{
uint8_t triggerCmd[] = {AHT20_CMD_TRIGGER, AHT20_CMD_TRIGGER_ARG0, AHT20_CMD_TRIGGER_ARG1};
return AHT20_Write(triggerCmd, sizeof(triggerCmd));
}
// 接收測量結果,拼接轉換為標準值
uint32_t AHT20_GetMeasureResult(float* temp, float* humi)
{
uint32_t retval = 0, i = 0;
if (temp == NULL || humi == NULL) {
return HI_ERR_FAILURE;
}
uint8_t buffer[AHT20_STATUS_RESPONSE_MAX];
memset(&buffer, 0x0, sizeof(buffer));
retval = AHT20_Read(buffer, sizeof(buffer)); // recv status command result
if (retval != HI_ERR_SUCCESS) {
return retval;
}
for (i = 0; AHT20_STATUS_BUSY(buffer[0]) && i < AHT20_MAX_RETRY; i++) {
// printf("AHT20 device busy, retry %d/%d!rn", i, AHT20_MAX_RETRY);
usleep(AHT20_MEASURE_TIME);
retval = AHT20_Read(buffer, sizeof(buffer)); // recv status command result
if (retval != HI_ERR_SUCCESS) {
return retval;
}
}
if (i >= AHT20_MAX_RETRY) {
printf("AHT20 device always busy!rn");
return HI_ERR_SUCCESS;
}
uint32_t humiRaw = buffer[1];
humiRaw = (humiRaw << 8) | buffer[2];
humiRaw = (humiRaw << 4) | ((buffer[3] & 0xF0) >> 4);
*humi = humiRaw / (float)AHT20_RESOLUTION * 100;
uint32_t tempRaw = buffer[3] & 0x0F;
tempRaw = (tempRaw << 8) | buffer[4];
tempRaw = (tempRaw << 8) | buffer[5];
*temp = tempRaw / (float)AHT20_RESOLUTION * 200 - 50;
// printf("humi = %05X, %f, temp= %05X, %frn", humiRaw, *humi, tempRaw, *temp);
return HI_ERR_SUCCESS;
}
)<>)<>)<>
4、aht20.h內容如下:
/*
* Copyright (C) 2021 HiHope Open Source Organization .
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*
* limitations under the License.
*/
#ifndef AHT20_H
#define AHT20_H
#include
uint32_t AHT20_Calibrate(void);
uint32_t AHT20_StartMeasure(void);
uint32_t AHT20_GetMeasureResult(float* temp, float* humi);
#endif // AHT20_H
5、aht20_test.c內容如下:
/*
* Copyright (C) 2021 HiHope Open Source Organization .
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*
* limitations under the License.
*/
#include "aht20.h"
#include
#include
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hi_gpio.h"
#include "hi_io.h"
#include "hi_i2c.h"
#include "oled_ssd1306.h"
void Aht20TestTask(void* arg)
{
(void) arg;
uint32_t retval = 0;
char str_temp[12];
char str_Humi[12];
hi_io_set_func(HI_IO_NAME_GPIO_13, HI_IO_FUNC_GPIO_13_I2C0_SDA);
hi_io_set_func(HI_IO_NAME_GPIO_14, HI_IO_FUNC_GPIO_14_I2C0_SCL);
hi_i2c_init(HI_I2C_IDX_0, 400*1000);
OledInit();
OledFillScreen(0x00);
OledShowString(26, 0, "AHT20-TEST!", FONT8x16);
retval = AHT20_Calibrate();
printf("AHT20_Calibrate: %drn", retval);
while (1) {
float temp = 0.0, humi = 0.0;
retval = AHT20_StartMeasure();
printf("AHT20_StartMeasure: %drn", retval);
retval = AHT20_GetMeasureResult(&temp, &humi);
sprintf(str_temp,"TEMP:%.2f",temp);
sprintf(str_Humi,"HUMI:%.2f",humi);
printf("AHT20_GetMeasureResult: %d, temp = %.2f, humi = %.2frn", retval, temp, humi);
OledShowString(26,2,str_temp,FONT8x16);
OledShowString(26,4,str_Humi,FONT8x16);
sleep(1);
}
}
void Aht20Test(void)
{
osThreadAttr_t attr;
attr.name = "Aht20Task";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 4096;
attr.priority = osPriorityNormal;
if (osThreadNew(Aht20TestTask, NULL, &attr) == NULL) {
printf("[Aht20Test] Failed to create Aht20TestTask!n");
}
}
APP_FEATURE_INIT(Aht20Test);
7、BUILD.gn:
#Copyright (C) 2021 HiHope Open Source Organization .
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#
#limitations under the License.
static_library("i2cd_emo") {
sources = [
"aht20_test.c",
"aht20.c",
"oled_ssd1306.c"
]
include_dirs = [
"http://utils/native/lite/include",
"http://kernel/liteos_m/components/cmsis/2.0",
"http://base/iot_hardware/peripheral/interfaces/kits",
"http://device/hisilicon/hispark_pegasus/sdk_liteos/include"
]
}
8、修改app/BUILD.gn如下:
import("http://build/lite/config/component/lite_component.gni")
lite_component("app") {
features = [
"i2caht20:i2cd_emo",
]
}
然后就可以編譯下載了。
運行效果如下圖:
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
wi-fi
+關注
關注
14文章
2162瀏覽量
124788 -
HarmonyOS
+關注
關注
79文章
1980瀏覽量
30395 -
HiSpark
+關注
關注
1文章
156瀏覽量
6943
發布評論請先 登錄
相關推薦
遠程溫濕度實時監測系統應用方案
通信、云計算、大數據分析等技術。傳感器作為系統的“眼睛”,負責實時采集環境中的溫濕度數據。通過Wi-Fi、藍牙、LoRa、NB-IoT等通訊方式,將傳感器采集的數據傳輸至云端服務器。云平臺負責接收、存儲和處理傳感器傳輸的數據。
【正點原子STM32H7R3開發套件試用體驗】DS18B20、DHT11溫濕度采集
使用正點原子STM32H7R3開發套件,結合DS18B20、DHT11溫濕度傳感器,采集環境溫度和濕度數據,并通過 LCD 屏
發表于 01-01 11:16
嵌入式學習-飛凌嵌入式ElfBoard ELF 1板卡-使用AHT20進行環境監測之開發板測試
溫濕度打開ELF 1板卡,在/home/root下輸入如下命令開始測試,可以看到程序可以正常運行沒有報錯,并直接返回測量到的數值。root@ELF1:~# ./elf1_cmd_aht20此時用手指觸摸
發表于 11-29 09:04
CW32模塊使用案例 AHT10溫濕度傳感器
AHT10,新一代溫濕度傳感器在尺寸與智能方面建立了新的標準:它嵌入了適于回流焊的雙列扁平無引腳SMD 封裝,底面 4 x 5mm ,高度1.6mm。傳感器輸出經過標定的數字信號,標準 I 2 C
嵌入式學習-飛凌嵌入式ElfBoard ELF 1板卡-使用AHT20進行環境監測之編寫程序
/10.0;
temp = (float)t1/10.0;因為溫濕度不能用常量表示,所以需要做相應數學計算轉換成浮點量。
(四)關閉設備close(fd);詳細代碼
elf1_cmd_aht20
發表于 11-28 09:34
飛凌嵌入式ElfBoard ELF 1板卡-使用AHT20進行環境監測之開發板測試
。測試設備周圍溫濕度打開ELF 1板卡,在/home/root下輸入如下命令開始測試,可以看到程序可以正常運行沒有報錯,并直接返回測量到的數值。root@ELF1:~# ./elf1_cmd_aht20此時用手指觸摸
發表于 11-28 09:30
嵌入式學習-飛凌嵌入式ElfBoard ELF 1板卡-使用AHT20進行環境監測之AHT20傳感器介紹
i2c接口的的AHT20溫濕度傳感器。i2c硬件原理見硬件手冊,通信協議見3.2.2小節,我們前面這些章節已經介紹了i2c的基本通信原理,本節我們主要關注的是AHT20作為i2c從設備,是如何與ELF
發表于 11-27 09:09
飛凌嵌入式ElfBoard ELF 1板卡-使用AHT20進行環境監測之編寫程序
)t1/10.0;因為溫濕度不能用常量表示,所以需要做相應數學計算轉換成浮點量。
(四)關閉設備close(fd);詳細代碼
elf1_cmd_aht20.c:#include \"
發表于 11-27 09:05
飛凌嵌入式ElfBoard ELF 1板卡-使用AHT20進行環境監測之AHT20傳感器介紹
i2c接口的的AHT20溫濕度傳感器。i2c硬件原理見硬件手冊,通信協議見3.2.2小節,我們前面這些章節已經介紹了i2c的基本通信原理,本節我們主要關注的是AHT20作為i2c從設備,是如何與ELF
發表于 11-26 09:36
想要準確地測量環境溫濕度?溫濕度傳感器是關鍵!
? 溫濕度是生產生活中最重要的環境指標之一,不僅人需要在適宜的溫濕度條件下保持良好的精神狀態和敏捷的思維,食品、藥品、各種儀器設備等都對環境溫濕度有特殊的要求。基于對環境溫濕度的要求,
驗證物聯網Wi-Fi HaLow用例的MM6108-EKH08開發套件來啦
驗證物聯網Wi-Fi HaLow用例的MM6108-EKH08開發套件來啦 MM6108-EKH08開發套件專為驗證物聯網Wi-Fi HaLow用例而設計。該
學習筆記|如何用Go程序采集溫濕度傳感器數據
在共創社內部的交流中,先前有一位成員展示了如何借助C語言來實現對AHT20溫濕度傳感器數據的讀取。這一實例觸發了另一位共創官的靈感,他決定采納Go語言重新構建這一數據采集流程。接下來,我們將詳細解析
Wi-Fi HaLow和傳統Wi-Fi的區別
HaLow和傳統Wi-Fi之間的區別,并探討其各自的優缺點。 Wi-Fi HaLow是一種低功耗、長范圍的無線網絡標準。與傳統的Wi-Fi標準相比
評論