一、前言
通過AHT20+BMP280傳感器模塊,用戶可以方便地獲取環(huán)境參數(shù),實(shí)現(xiàn)環(huán)境監(jiān)測和數(shù)據(jù)分析,廣泛應(yīng)用于智能家居、氣象監(jiān)測、環(huán)境監(jiān)測等領(lǐng)域。
?
二、簡介
### AHT20傳感器:
**1. 基本參數(shù):**
- 測量范圍:濕度0%至100%RH,溫度-40°C至85°C
- 分辨率:濕度0.1%,溫度0.1°C
- 精度:±2%RH (25°C時(shí)),±0.3°C (25°C時(shí))
- 通信接口:I2C
- 尺寸:較小,便于集成到各種應(yīng)用中
**2. 特點(diǎn):**
- 高精度和高可靠性
- 快速響應(yīng)時(shí)間
- 低功耗
- 抗干擾能力強(qiáng)
**3. 應(yīng)用:**
- 智能家居系統(tǒng)
- 環(huán)境監(jiān)測
- 消費(fèi)電子產(chǎn)品
- 舒適性控制
### BMP280傳感器:
**1. 基本參數(shù):**
- 測量范圍:溫度-40°C至85°C,壓力300hPa至1100hPa
- 分辨率:溫度0.01°C,壓力0.1hPa
- 精度:±2hPa
- 通信接口:I2C或SPI
- 尺寸:較小,便于集成
**2. 特點(diǎn):**
- 高精度測量
- 可編程的分辨率
- 超低功耗
- 優(yōu)秀的溫度穩(wěn)定性
**3. 應(yīng)用:**
- 智能手機(jī)和穿戴設(shè)備
- 氣象站
- 高精度導(dǎo)航
- 垂直定位
三、資料獲取
關(guān)注微信公眾號--星之援工作室 發(fā)送關(guān)鍵字(AHT20+BMP280)
代碼含重要注釋,開源,可自行移植
????
四、設(shè)備使用
實(shí)現(xiàn)效果
使用串口可以直接獲取到監(jiān)測數(shù)據(jù)
接線
PB8 -> SCL
PB9 -> SDA
?
四、代碼編寫
main.c
?實(shí)現(xiàn)函數(shù)調(diào)用
#include "stm32f10x.h"
#include "stm32f10x_usart.h"
#include "misc.h"
#include "stdio.h"
#include "delay.h"
#include "bsp_i2c.h"
#include "ATH20.h"
#include "BMP280.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
GPIO_InitTypeDef GPIO_InitStructure;
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
FILE __stdout;
_sys_exit(int x)
{
x = x;
}
int fputc(int ch, FILE *f)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
USART_SendData(USART1,(uint8_t)ch);
return ch;
}
void uart_init(u32 bound)
{
//GPIO端口設(shè)置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA時(shí)鐘
USART_DeInit(USART1); //復(fù)位串口1
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽輸出
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA9
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA10
//USART 初始化設(shè)置
USART_InitStructure.USART_BaudRate = bound;//一般設(shè)置為9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長為8位數(shù)據(jù)格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個(gè)停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//無奇偶校驗(yàn)位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數(shù)據(jù)流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發(fā)模式
USART_Init(USART1, &USART_InitStructure); //初始化串口
USART_Cmd(USART1, ENABLE); //使能串口
}
int main(void)
{
uint8_t ret = 0;
float P,T,ALT;
uint32_t CT_data[2];
int c1,t1;
uint8_t LED_Stat = 0;
RCC_Configuration(); //設(shè)置系統(tǒng)時(shí)鐘
GPIO_Configuration(); //IO口設(shè)
I2C_Bus_Init();
uart_init(115200);
ret = ATH20_Init();
if(ret == 0)
{
printf("ATH20傳感器初始化錯(cuò)誤n");
while(1);
}
ret = BMP280_Init();
if(ret != 0x58)
{
printf("BMP280傳感器初始化錯(cuò)誤n");
while(1);
}
while(1)
{
/* 讀取 ATH20 傳感器數(shù)據(jù)*/
while(ATH20_Read_Cal_Enable() == 0)
{
ATH20_Init();//如果為0再使能一次
SoftDelay_ms(30);
}
ATH20_Read_CTdata(CT_data); //讀取溫度和濕度
c1 = CT_data[0] * 1000 / 1024 / 1024; //計(jì)算得到濕度值(放大了10倍,如果c1=523,表示現(xiàn)在濕度為52.3%)
t1 = CT_data[1] * 200 *10 / 1024 / 1024 - 500;//計(jì)算得到溫度值(放大了10倍,如果t1=245,表示現(xiàn)在溫度為24.5℃)
/* 讀取 BMP280 傳感器數(shù)據(jù)*/
BMP280GetData(&P, &T, &ALT);
printf("***************************n");
printf("AHT20溫濕度傳感器測試數(shù)據(jù):n");
printf("溫度: %d.%d ℃n",(t1/10),(t1%10));
printf("濕度: %d.%d %%n",(c1/10),(c1%10));
printf("n");
printf("BMP280傳感器測試數(shù)據(jù):n");
printf("氣壓: %0.4f hPan",P);
printf("溫度: %0.2f ℃n",T);
printf("海拔: %0.2f mn",ALT);
printf("nn");
SoftDelay_ms(1000);
if(LED_Stat == 0)
{
LED_Stat = 1;
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
}
else
{
LED_Stat = 0;
GPIO_SetBits(GPIOA, GPIO_Pin_2);
}
}
}
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
| RCC_APB2Periph_GPIOD| RCC_APB2Periph_GPIOE , ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7; //狀態(tài)LED1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //通用推挽輸出模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //輸出模式最大速度50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
iic.c
實(shí)現(xiàn) IIC 通信
#include "bsp_i2c.h"
#include "stm32f10x_i2c.h"
#include "delay.h"
#include "misc.h"
#include "stdio.h"
#define mdelay SoftDelay_ms
#define Soft_I2C_SDA_STATE GPIO_ReadInputDataBit(Soft_I2C_PORT, Soft_I2C_SDA)
#define Soft_I2C_DELAY Soft_I2C_Delay(100000)
#define Soft_I2C_NOP Soft_I2C_Delay(10)
#define Soft_I2C_READY 0x00
#define Soft_I2C_BUS_BUSY 0x01
#define Soft_I2C_BUS_ERROR 0x02
#define Soft_I2C_NACK 0x00
#define Soft_I2C_ACK 0x01
static void Soft_I2C_Delay(uint32_t dly)
{
while(--dly); //dly=100: 8.75us; dly=100: 85.58 us (SYSCLK=72MHz)
}
static unsigned short RETRY_IN_MLSEC = 55;
/**
* @brief 設(shè)置iic重試時(shí)間
* @param ml_sec:重試的時(shí)間,單位毫秒
* @retval 重試的時(shí)間,單位毫秒
*/
void Set_I2C_Retry(unsigned short ml_sec)
{
RETRY_IN_MLSEC = ml_sec;
}
/**
* @brief 獲取設(shè)置的iic重試時(shí)間
* @param none
* @retval none
*/
unsigned short Get_I2C_Retry(void)
{
return RETRY_IN_MLSEC;
}
static void Soft_I2C_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = Soft_I2C_SCL | Soft_I2C_SDA; //配置使用的I2C口
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //設(shè)置I2C口最大允許輸出速度
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //設(shè)置I2C為開漏輸出
GPIO_Init(Soft_I2C_PORT, &GPIO_InitStructure);
Soft_I2C_SCL_1;
Soft_I2C_SDA_1;
Soft_I2C_DELAY;
}
void I2C_Bus_Init(void)
{
Set_I2C_Retry(5);
Soft_I2C_Configuration();
}
static uint8_t Soft_I2C_START(void)
{
Soft_I2C_SDA_1;
Soft_I2C_NOP;
Soft_I2C_SCL_1;
Soft_I2C_NOP;
if(!Soft_I2C_SDA_STATE)
return Soft_I2C_BUS_BUSY;
Soft_I2C_SDA_0;
Soft_I2C_NOP;
Soft_I2C_SCL_0;
Soft_I2C_NOP;
if(Soft_I2C_SDA_STATE)
return Soft_I2C_BUS_ERROR;
return Soft_I2C_READY;
}
static void Soft_I2C_STOP(void)
{
Soft_I2C_SDA_0;
Soft_I2C_NOP;
Soft_I2C_SCL_1;
Soft_I2C_NOP;
Soft_I2C_SDA_1;
Soft_I2C_NOP;
}
static void Soft_I2C_SendACK(void)
{
Soft_I2C_SDA_0;
Soft_I2C_NOP;
Soft_I2C_SCL_1;
Soft_I2C_NOP;
Soft_I2C_SCL_0;
Soft_I2C_NOP;
}
static void Soft_I2C_SendNACK(void)
{
Soft_I2C_SDA_1;
Soft_I2C_NOP;
Soft_I2C_SCL_1;
Soft_I2C_NOP;
Soft_I2C_SCL_0;
Soft_I2C_NOP;
}
/**
* @brief 等待應(yīng)答信號到來
* @retval 返回值:1,接收應(yīng)答失敗
* 0,接收應(yīng)答成功
*/
uint8_t Soft_I2C_Wait_Ack(void)
{
uint8_t ucErrTime=0;
Soft_I2C_SDA_1;
Soft_I2C_NOP;
Soft_I2C_SCL_1;
Soft_I2C_NOP;
while(Soft_I2C_SDA_STATE)
{
ucErrTime ++;
if(ucErrTime > 250)
{
Soft_I2C_STOP();
return Soft_I2C_BUS_ERROR;
}
}
Soft_I2C_SCL_0;//時(shí)鐘輸出0
return 0;
}
static uint8_t Soft_I2C_SendByte(uint8_t soft_i2c_data)
{
uint8_t i;
Soft_I2C_SCL_0;
for(i=0; i<8; i++)
{
if(soft_i2c_data & 0x80)
Soft_I2C_SDA_1;
else
Soft_I2C_SDA_0;
soft_i2c_data <<= 1;
Soft_I2C_NOP;
Soft_I2C_SCL_1;
Soft_I2C_NOP;
Soft_I2C_SCL_0;
Soft_I2C_NOP;
}
return Soft_I2C_Wait_Ack();
}
static uint8_t Soft_I2C_ReceiveByte(void)
{
uint8_t i,soft_i2c_data;
Soft_I2C_SDA_1;
Soft_I2C_SCL_0;
soft_i2c_data = 0;
for(i=0; i<8; i++)
{
Soft_I2C_SCL_1;
Soft_I2C_NOP;
soft_i2c_data <<= 1;
if(Soft_I2C_SDA_STATE)
soft_i2c_data |= 0x01;
Soft_I2C_SCL_0;
Soft_I2C_NOP;
}
Soft_I2C_SendNACK();
return soft_i2c_data;
}
static uint8_t Soft_I2C_ReceiveByte_WithACK(void)
{
uint8_t i,soft_i2c_data;
Soft_I2C_SDA_1;
Soft_I2C_SCL_0;
soft_i2c_data = 0;
for(i=0; i<8; i++)
{
Soft_I2C_SCL_1;
Soft_I2C_NOP;
soft_i2c_data <<= 1;
if(Soft_I2C_SDA_STATE)
soft_i2c_data |= 0x01;
Soft_I2C_SCL_0;
Soft_I2C_NOP;
}
Soft_I2C_SendACK();
return soft_i2c_data;
}
static uint8_t Soft_DMP_I2C_Write(uint8_t soft_dev_addr, uint8_t soft_reg_addr, uint8_t soft_i2c_len,unsigned char *soft_i2c_data_buf)
{
uint8_t i, result = 0;
Soft_I2C_START();
result = Soft_I2C_SendByte(soft_dev_addr << 1 | I2C_Direction_Transmitter);
if(result != 0) return result;
result = Soft_I2C_SendByte(soft_reg_addr);
if(result != 0) return result;
for (i=0; i<soft_i2c_len; i++)
{
result = Soft_I2C_SendByte(soft_i2c_data_buf[i]);
if (result != 0) return result;
}
Soft_I2C_STOP();
return 0x00;
}
static uint8_t Soft_DMP_I2C_Read(uint8_t soft_dev_addr, uint8_t soft_reg_addr, uint8_t soft_i2c_len,unsigned char *soft_i2c_data_buf)
{
uint8_t result;
Soft_I2C_START();
result = Soft_I2C_SendByte(soft_dev_addr << 1 | I2C_Direction_Transmitter);
if(result != 0) return result;
result = Soft_I2C_SendByte(soft_reg_addr);
//printf("addr:0x%xn",result);
if(result != 0) return result;
Soft_I2C_START();
result = Soft_I2C_SendByte(soft_dev_addr << 1 | I2C_Direction_Receiver);
if(result != 0) return result;
while (soft_i2c_len)
{
if (soft_i2c_len == 1)
*soft_i2c_data_buf = Soft_I2C_ReceiveByte();
else
*soft_i2c_data_buf = Soft_I2C_ReceiveByte_WithACK();
soft_i2c_data_buf ++;
soft_i2c_len --;
}
Soft_I2C_STOP();
return 0x00;
}
/**
* @brief 向IIC設(shè)備的寄存器連續(xù)寫入數(shù)據(jù),帶超時(shí)重試設(shè)置,供mpu接口調(diào)用
* @param Address: IIC設(shè)備地址
* @param RegisterAddr: 寄存器地址
* @param RegisterLen: 要寫入數(shù)據(jù)的長度
* @param RegisterValue: 要指向?qū)懭霐?shù)據(jù)的指針
* @retval 0正常,非0異常
*/
int Sensors_I2C_WriteRegister(unsigned char slave_addr,
unsigned char reg_addr,
unsigned short len,
const unsigned char *data_ptr)
{
char retries = 0;
int ret = 0;
unsigned short retry_in_mlsec = Get_I2C_Retry();
tryWriteAgain:
ret = 0;
ret = Soft_DMP_I2C_Write( slave_addr, reg_addr, len, ( unsigned char *)data_ptr);
if(ret && retry_in_mlsec)
{
if( retries++ > 4 )
return ret;
mdelay(retry_in_mlsec);
goto tryWriteAgain;
}
return ret;
}
/**
* @brief 向IIC設(shè)備的寄存器連續(xù)讀出數(shù)據(jù),帶超時(shí)重試設(shè)置,供mpu接口調(diào)用
* @param Address: IIC設(shè)備地址
* @param RegisterAddr: 寄存器地址
* @param RegisterLen: 要讀取的數(shù)據(jù)長度
* @param RegisterValue: 指向存儲讀出數(shù)據(jù)的指針
* @retval 0正常,非0異常
*/
int Sensors_I2C_ReadRegister(unsigned char slave_addr,
unsigned char reg_addr,
unsigned short len,
unsigned char *data_ptr)
{
char retries = 0;
int ret = 0;
unsigned short retry_in_mlsec = Get_I2C_Retry();
tryReadAgain:
ret = 0;
ret = Soft_DMP_I2C_Read( slave_addr, reg_addr, len, ( unsigned char *)data_ptr);
if(ret && retry_in_mlsec)
{
if( retries++ > 4 )
return ret;
mdelay(retry_in_mlsec);
goto tryReadAgain;
}
return ret;
}
iic.h
#ifndef __BSP_I2C_H
#define __BSP_I2C_H
#include "stm32f10x.h"
/*********************軟件IIC使用的宏****************************/
#define Soft_I2C_SDA GPIO_Pin_9
#define Soft_I2C_SCL GPIO_Pin_8
#define Soft_I2C_PORT GPIOB
//
#define Soft_I2C_SCL_0 GPIO_ResetBits(Soft_I2C_PORT, Soft_I2C_SCL)
#define Soft_I2C_SCL_1 GPIO_SetBits(Soft_I2C_PORT, Soft_I2C_SCL)
#define Soft_I2C_SDA_0 GPIO_ResetBits(Soft_I2C_PORT, Soft_I2C_SDA)
#define Soft_I2C_SDA_1 GPIO_SetBits(Soft_I2C_PORT, Soft_I2C_SDA)
/*等待超時(shí)時(shí)間*/
#define I2CT_FLAG_TIMEOUT ((uint32_t)0x1000)
#define I2CT_LONG_TIMEOUT ((uint32_t)(10 * I2CT_FLAG_TIMEOUT))
void I2C_Bus_Init(void);
void Set_I2C_Retry(unsigned short ml_sec);
unsigned short Get_I2C_Retry(void);
int Sensors_I2C_ReadRegister(unsigned char Address, unsigned char RegisterAddr,
unsigned short RegisterLen, unsigned char *RegisterValue);
int Sensors_I2C_WriteRegister(unsigned char Address, unsigned char RegisterAddr,
unsigned short RegisterLen, const unsigned char *RegisterValue);
#endif
五、參考
基于STM32和ATH20實(shí)現(xiàn)OLED顯示溫濕度https://blog.csdn.net/rzh222/article/details/121546671?ops_request_misc=%257B%2522request%255Fid%2522%253A%252259126DE9-75DF-4924-9566-AFE890C5D4C7%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=59126DE9-75DF-4924-9566-AFE890C5D4C7&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-6-121546671-null-null.142%5Ev100%5Epc_search_result_base4&utm_term=ATH20&spm=1018.2226.3001.4187STM32驅(qū)動(dòng)BMP280模塊https://blog.csdn.net/bdjsm_hh/article/details/107623788?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522CDAA3989-3760-4724-B20D-90FC5527935F%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=CDAA3989-3760-4724-B20D-90FC5527935F&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-107623788-null-null.142%5Ev100%5Epc_search_result_base4&utm_term=BMP280&spm=1018.2226.3001.4187
聯(lián)系方式 微信號:13648103287