148 lines
3.2 KiB
C
148 lines
3.2 KiB
C
/*
|
|
* AHT20.c
|
|
*
|
|
* Created on: Sep 24, 2024
|
|
* Author: 10425
|
|
*/
|
|
|
|
|
|
#include "AHT20.h"
|
|
|
|
#define I2C_HANDLE hi2c2
|
|
#define AHT20_TIMEOUT 10
|
|
|
|
MYI2C_Struct SENx = {0};
|
|
|
|
|
|
|
|
void AHT20_Init(MYI2C_Struct *pst,uint16_t ReadTimMS,uint16_t xAddr)
|
|
{
|
|
HAL_StatusTypeDef result;
|
|
pst->Adrr =xAddr;
|
|
pst->Step = SENSOR_IDLE;
|
|
|
|
result=HAL_I2C_IsDeviceReady(&I2C_HANDLE, pst->Adrr<<1, 10, 200);
|
|
if(ReadTimMS>MinReadTim)pst->SetRTim=ReadTimMS;
|
|
else pst->SetRTim=MinReadTim;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t AHT20_READ_FUNC(MYI2C_Struct *pst,uint8_t device_addr,uint8_t register_addr,uint8_t *pDat,uint8_t len)
|
|
{
|
|
uint8_t NoAck=0;
|
|
|
|
if(register_addr)
|
|
{
|
|
// 发送寄存器地址
|
|
if (HAL_I2C_Mem_Write(&I2C_HANDLE, (device_addr << 1), register_addr, I2C_MEMADD_SIZE_8BIT, NULL, 0, AHT20_TIMEOUT) != HAL_OK)
|
|
{
|
|
NoAck++;
|
|
}
|
|
}
|
|
// 读取数据
|
|
if (HAL_I2C_Master_Receive(&I2C_HANDLE, (device_addr << 1) | 0x01, pDat, len, AHT20_TIMEOUT) != HAL_OK)
|
|
{
|
|
NoAck++;
|
|
}
|
|
|
|
pst->ErrFlag=NoAck;
|
|
|
|
return NoAck;
|
|
}
|
|
/*******************************************************************************
|
|
* Function Name :
|
|
* Description :
|
|
* Input : None
|
|
* Output : None
|
|
* Return :None
|
|
*******************************************************************************/
|
|
uint8_t AHT20_WRITE_FUNC(MYI2C_Struct *pst,uint8_t device_addr,uint8_t register_addr,uint8_t *pDat,uint8_t len)
|
|
{
|
|
uint8_t NoAck = 0;
|
|
|
|
// 将要发送的数据缓冲区
|
|
uint8_t data_buffer[len + 1];
|
|
data_buffer[0] = register_addr; // 第一个字节是寄存器地址
|
|
memcpy(&data_buffer[1], pDat, len); // 后续字节是要写入的数据
|
|
|
|
// 发送设备地址、寄存器地址和数据
|
|
if (HAL_I2C_Master_Transmit(&I2C_HANDLE, (device_addr << 1), data_buffer, len + 1, HAL_MAX_DELAY) != HAL_OK)
|
|
{
|
|
NoAck++;
|
|
}
|
|
|
|
// 更新错误标志
|
|
pst->ErrFlag = NoAck;
|
|
|
|
return NoAck;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t CheckCrc8(uint8_t *pDat,uint8_t Lenth)
|
|
{
|
|
uint8_t crc = 0xff, i, j;
|
|
|
|
for (i = 0; i < Lenth ; i++)
|
|
{
|
|
crc = crc ^ *pDat;
|
|
for (j = 0; j < 8; j++)
|
|
{
|
|
if (crc & 0x80) crc = (crc << 1) ^ 0x31;
|
|
else crc <<= 1;
|
|
}
|
|
pDat++;
|
|
}
|
|
return crc;
|
|
}
|
|
|
|
void AHT20_Handle(MYI2C_Struct *pst)
|
|
{
|
|
uint32_t s32x;
|
|
|
|
pst->timcnt += MYI2C_Tick;
|
|
|
|
|
|
if(pst->timcnt>PowerOnTim && pst->Step==SENSOR_IDLE)
|
|
{
|
|
pst->Step=SENSOR_MEASURE;
|
|
pst->SendByte[0]=0x33;
|
|
pst->SendByte[1]=0x00;
|
|
AHT20_WRITE_FUNC(pst,pst->Adrr,0xAC, &pst->SendByte[0], 2);
|
|
}
|
|
else if(pst->timcnt>MeasureTim && pst->Step==SENSOR_MEASURE)
|
|
{
|
|
pst->Step=SENSOR_COMPLETE;
|
|
AHT20_READ_FUNC(pst,pst->Adrr,0, &pst->ReadByte[0], 7);
|
|
if(pst->ErrFlag==0)
|
|
{
|
|
if((CheckCrc8(&pst->ReadByte[0],6)==pst->ReadByte[6])&&((pst->ReadByte[0]&0x98) == 0x18))
|
|
{
|
|
s32x=pst->ReadByte[1];s32x=s32x<<8;s32x+=pst->ReadByte[2];s32x=s32x<<8;s32x+=pst->ReadByte[3];s32x=s32x>>4;
|
|
pst->RH=s32x;
|
|
pst->RH=pst->RH*100/1048576;
|
|
s32x=pst->ReadByte[3]&0x0F;s32x=s32x<<8;s32x+=pst->ReadByte[4];s32x=s32x<<8;s32x+=pst->ReadByte[5];
|
|
pst->T=s32x;
|
|
pst->T=pst->T*200/1048576-50;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
pst->RH=0;
|
|
pst->T=0;
|
|
}
|
|
}
|
|
else if(pst->timcnt>pst->SetRTim)
|
|
{
|
|
pst->Step=SENSOR_IDLE;
|
|
pst->timcnt=0;
|
|
}
|
|
|
|
}
|
|
|