aht10温湿度传感器驱动源码和数据手册
时间: 2024-06-09 16:06:06 浏览: 203
aht10温湿度传感器是一款数字式温湿度传感器,采用I2C接口通信。以下是aht10温湿度传感器的驱动源码和数据手册。
## 驱动源码
### Arduino
```
#include <Wire.h>
#define AHT10_I2C_ADDR 0x38
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
Wire.beginTransmission(AHT10_I2C_ADDR);
Wire.write(0xAC);
Wire.write(0x33);
Wire.write(0x00);
Wire.endTransmission();
delay(80);
Wire.requestFrom(AHT10_I2C_ADDR, 6);
byte data[6];
for(int i=0;i<6;i++){
data[i] = Wire.read();
}
int humidity_raw = ((int)data[1] << 12) | ((int)data[2] << 4) | ((int)data[3] >> 4);
float humidity = (float)humidity_raw / 1048576.0 * 100.0;
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
int temperature_raw = ((int)data[3] & 0x0F) << 16 | ((int)data[4] << 8) | ((int)data[5]);
float temperature = (float)temperature_raw / 1048576.0 * 200.0 - 50.0;
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println("C");
delay(2000);
}
```
### STM32
```
#include "stm32f10x.h"
#include "stm32f10x_i2c.h"
#define AHT10_I2C_ADDR 0x38
I2C_InitTypeDef i2c_init_struct;
GPIO_InitTypeDef gpio_init_struct;
void I2C_Configuration() {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
gpio_init_struct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
gpio_init_struct.GPIO_Mode = GPIO_Mode_AF_OD;
gpio_init_struct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &gpio_init_struct);
i2c_init_struct.I2C_ClockSpeed = 100000;
i2c_init_struct.I2C_Mode = I2C_Mode_I2C;
i2c_init_struct.I2C_DutyCycle = I2C_DutyCycle_2;
i2c_init_struct.I2C_OwnAddress1 = 0x00;
i2c_init_struct.I2C_Ack = I2C_Ack_Enable;
i2c_init_struct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &i2c_init_struct);
I2C_Cmd(I2C1, ENABLE);
}
void I2C_WriteByte(uint8_t address, uint8_t data) {
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, address, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, data);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1, ENABLE);
}
void I2C_ReadBytes(uint8_t address, uint8_t* data, uint8_t length) {
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, address, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, 0xAC);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, 0x33);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, 0x00);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1, ENABLE);
delay_us(80000);
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, address, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
for(int i=0;i<length;i++){
if(i == length-1){
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_GenerateSTOP(I2C1, ENABLE);
}
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
data[i] = I2C_ReceiveData(I2C1);
}
I2C_AcknowledgeConfig(I2C1, ENABLE);
}
int main(void) {
I2C_Configuration();
while(1) {
uint8_t data[6];
I2C_ReadBytes(AHT10_I2C_ADDR, data, 6);
int humidity_raw = ((int)data[1] << 12) | ((int)data[2] << 4) | ((int)data[3] >> 4);
float humidity = (float)humidity_raw / 1048576.0 * 100.0;
printf("Humidity: %f %%\n", humidity);
int temperature_raw = ((int)data[3] & 0x0F) << 16 | ((int)data[4] << 8) | ((int)data[5]);
float temperature = (float)temperature_raw / 1048576.0 * 200.0 - 50.0;
printf("Temperature: %f C\n", temperature);
delay_ms(2000);
}
}
```
## 数据手册
aht10温湿度传感器的数据手册可以在以下链接中找到:
https://www.mouser.cn/datasheet/2/744/Aosong_AHT10_Datasheet_V1.0-1660812.pdf
阅读全文