165 lines
3.4 KiB
C
165 lines
3.4 KiB
C
|
|
||
|
|
||
|
#include "AHT20.h"
|
||
|
|
||
|
|
||
|
MYI2C_Struct SENx = {0};
|
||
|
|
||
|
|
||
|
|
||
|
void AHT20_Init(MYI2C_Struct *pst,uint16_t ReadTimMS,uint16_t xAddr)
|
||
|
{
|
||
|
pst->Adrr =xAddr;
|
||
|
pst->Step = SENSOR_IDLE;
|
||
|
|
||
|
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)
|
||
|
{
|
||
|
/* Send STRAT condition a second time */
|
||
|
IIC_Start();
|
||
|
/* Send slave address for Write Regsiter */
|
||
|
IIC_Send_Byte((device_addr<<1) + 0);
|
||
|
/* Ack */
|
||
|
if(IIC_Wait_Ack()==1)NoAck++;
|
||
|
/*Send register_addr*/
|
||
|
IIC_Send_Byte((register_addr));
|
||
|
/* Ack */
|
||
|
if(IIC_Wait_Ack()==1)NoAck++;
|
||
|
|
||
|
// MYI2C_IIC_Stop(pst);
|
||
|
IIC_Stop();
|
||
|
}
|
||
|
/* Send STRAT condition a second time */
|
||
|
IIC_Start();
|
||
|
/* Send slave address for Read */
|
||
|
IIC_Send_Byte((device_addr<<1)+1 );
|
||
|
/* Ack */
|
||
|
if(IIC_Wait_Ack()==1)NoAck++;
|
||
|
/* While there is data to be read */
|
||
|
while(len && NoAck==0 && len<MYI2C_Buffer_Size) //
|
||
|
{
|
||
|
*pDat=IIC_Read_Byte(len-1);
|
||
|
/* Ack */
|
||
|
pDat++;
|
||
|
len--;
|
||
|
}
|
||
|
/* Send STOP Condition */
|
||
|
IIC_Stop();
|
||
|
|
||
|
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)
|
||
|
{
|
||
|
unsigned int NoAck=0;
|
||
|
|
||
|
/* Send STRAT condition */
|
||
|
IIC_Start();
|
||
|
/* Send slave address for write */
|
||
|
IIC_Send_Byte((device_addr<<1) + 0);
|
||
|
/* ACK */
|
||
|
if(IIC_Wait_Ack()==1)NoAck++;
|
||
|
/* Send register_addr for read */
|
||
|
IIC_Send_Byte((register_addr));
|
||
|
/* ACK */
|
||
|
if(IIC_Wait_Ack()==1)NoAck++;
|
||
|
while(NoAck==0 && len && len<MYI2C_Buffer_Size)//
|
||
|
{
|
||
|
/* Send the byte to be written */
|
||
|
IIC_Send_Byte(*pDat);
|
||
|
/* Acknowledgement */
|
||
|
IIC_Wait_Ack();
|
||
|
pDat++;
|
||
|
len--;
|
||
|
}
|
||
|
/* Send STOP condition */
|
||
|
IIC_Stop();
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|