511 lines
11 KiB
C
511 lines
11 KiB
C
|
/*******************************************/
|
|||
|
/*@<40><>Ȩ<EFBFBD><C8A8><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>ݰ<EFBFBD><DDB0>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˾ */
|
|||
|
/*@<40><><EFBFBD>ߣ<EFBFBD><DFA3><EFBFBD>ʪ<EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5> */
|
|||
|
/*@<40>汾<EFBFBD><E6B1BE>V1.2 */
|
|||
|
/*******************************************/
|
|||
|
#include "stm32f10x.h"
|
|||
|
#include "AHT20.h"
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
void Delay_N10us(uint32_t t)//<2F><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
uint32_t k;
|
|||
|
|
|||
|
while(t--)
|
|||
|
{
|
|||
|
for (k = 0; k < 2; k++);//110
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void SensorDelay_us(uint32_t t)//<2F><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
|
|||
|
for(t = t-2; t>0; t--)
|
|||
|
{
|
|||
|
Delay_N10us(1);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void Delay_4us(void) //<2F><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
Delay_N10us(1);
|
|||
|
Delay_N10us(1);
|
|||
|
Delay_N10us(1);
|
|||
|
Delay_N10us(1);
|
|||
|
}
|
|||
|
void Delay_5us(void) //<2F><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
Delay_N10us(1);
|
|||
|
Delay_N10us(1);
|
|||
|
Delay_N10us(1);
|
|||
|
Delay_N10us(1);
|
|||
|
Delay_N10us(1);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
void Delay_1ms(uint32_t t) //<2F><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
while(t--)
|
|||
|
{
|
|||
|
SensorDelay_us(1000);//////<2F><>ʱ1ms
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
void AHT20_Clock_Init(void) //<2F><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
|
|||
|
}
|
|||
|
|
|||
|
void SDA_Pin_Output_High(void) //<2F><>PB15<31><35><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>ߵ<EFBFBD>ƽ<EFBFBD><C6BD> PB15<31><35>ΪI2C<32><43>SDA
|
|||
|
{
|
|||
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD;//<2F><>©<EFBFBD><C2A9><EFBFBD><EFBFBD>
|
|||
|
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
|
|||
|
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
|||
|
GPIO_Init(GPIOB,& GPIO_InitStruct);
|
|||
|
GPIO_SetBits(GPIOB,GPIO_Pin_1);
|
|||
|
}
|
|||
|
|
|||
|
void SDA_Pin_Output_Low(void) //<2F><>P15<31><35><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>͵<EFBFBD>ƽ
|
|||
|
{
|
|||
|
|
|||
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD;//<2F><>©<EFBFBD><C2A9><EFBFBD><EFBFBD>
|
|||
|
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
|
|||
|
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
|||
|
GPIO_Init(GPIOB,& GPIO_InitStruct);
|
|||
|
GPIO_ResetBits(GPIOB,GPIO_Pin_1);
|
|||
|
}
|
|||
|
|
|||
|
void SDA_Pin_IN_FLOATING(void) //SDA<44><41><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
|
|||
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;//
|
|||
|
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
|
|||
|
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
|||
|
GPIO_Init( GPIOB,&GPIO_InitStruct);
|
|||
|
}
|
|||
|
|
|||
|
void SCL_Pin_Output_High(void) //SCL<43><4C><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD>ƽ<EFBFBD><C6BD>P14<31><34>ΪI2C<32><43>SCL
|
|||
|
{
|
|||
|
GPIO_SetBits(GPIOB,GPIO_Pin_0);
|
|||
|
}
|
|||
|
|
|||
|
void SCL_Pin_Output_Low(void) //SCL<43><4C><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>ƽ
|
|||
|
{
|
|||
|
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
|
|||
|
}
|
|||
|
|
|||
|
void Init_I2C_Sensor_Port(void) //<2F><>ʼ<EFBFBD><CABC>I2C<32>ӿ<EFBFBD>,<2C><><EFBFBD><EFBFBD>Ϊ<EFBFBD>ߵ<EFBFBD>ƽ
|
|||
|
{
|
|||
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|||
|
|
|||
|
AHT20_Clock_Init();
|
|||
|
|
|||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD;//<2F><>©<EFBFBD><C2A9><EFBFBD><EFBFBD>
|
|||
|
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
|
|||
|
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
|||
|
GPIO_Init(GPIOB,&GPIO_InitStruct);
|
|||
|
GPIO_SetBits(GPIOB,GPIO_Pin_1);//<2F><><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD>ƽ
|
|||
|
|
|||
|
|
|||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD;//<2F><>©<EFBFBD><C2A9><EFBFBD><EFBFBD>
|
|||
|
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
|
|||
|
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
|||
|
GPIO_Init(GPIOB,&GPIO_InitStruct);
|
|||
|
GPIO_SetBits(GPIOB,GPIO_Pin_0);//<2F><><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD>ƽ
|
|||
|
|
|||
|
}
|
|||
|
void I2C_Start(void) //I2C<32><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>START<52>ź<EFBFBD>
|
|||
|
{
|
|||
|
SDA_Pin_Output_High();
|
|||
|
SensorDelay_us(8);
|
|||
|
SCL_Pin_Output_High();
|
|||
|
SensorDelay_us(8);
|
|||
|
SDA_Pin_Output_Low();
|
|||
|
SensorDelay_us(8);
|
|||
|
SCL_Pin_Output_Low();
|
|||
|
SensorDelay_us(8);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
void AHT20_WR_Byte(uint8_t Byte) //<2F><>AHT20дһ<D0B4><D2BB><EFBFBD>ֽ<EFBFBD>
|
|||
|
{
|
|||
|
uint8_t Data,N,i;
|
|||
|
Data=Byte;
|
|||
|
i = 0x80;
|
|||
|
for(N=0;N<8;N++)
|
|||
|
{
|
|||
|
SCL_Pin_Output_Low();
|
|||
|
Delay_4us();
|
|||
|
if(i&Data)
|
|||
|
{
|
|||
|
SDA_Pin_Output_High();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
SDA_Pin_Output_Low();
|
|||
|
}
|
|||
|
|
|||
|
SCL_Pin_Output_High();
|
|||
|
Delay_4us();
|
|||
|
Data <<= 1;
|
|||
|
|
|||
|
}
|
|||
|
SCL_Pin_Output_Low();
|
|||
|
SensorDelay_us(8);
|
|||
|
SDA_Pin_IN_FLOATING();
|
|||
|
SensorDelay_us(8);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
uint8_t AHT20_RD_Byte(void)//<2F><>AHT20<32><30>ȡһ<C8A1><D2BB><EFBFBD>ֽ<EFBFBD>
|
|||
|
{
|
|||
|
uint8_t Byte,i,a;
|
|||
|
Byte = 0;
|
|||
|
SCL_Pin_Output_Low();
|
|||
|
SDA_Pin_IN_FLOATING();
|
|||
|
SensorDelay_us(8);
|
|||
|
for(i=0;i<8;i++)
|
|||
|
{
|
|||
|
SCL_Pin_Output_High();
|
|||
|
Delay_5us();
|
|||
|
a=0;
|
|||
|
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)) a=1;
|
|||
|
Byte = (Byte<<1)|a;
|
|||
|
SCL_Pin_Output_Low();
|
|||
|
Delay_5us();
|
|||
|
}
|
|||
|
SDA_Pin_IN_FLOATING();
|
|||
|
SensorDelay_us(8);
|
|||
|
return Byte;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
uint8_t Receive_ACK(void) //<2F><>AHT20<32>Ƿ<EFBFBD><C7B7>лظ<D0BB>ACK
|
|||
|
{
|
|||
|
uint16_t CNT;
|
|||
|
CNT = 0;
|
|||
|
SCL_Pin_Output_Low();
|
|||
|
SDA_Pin_IN_FLOATING();
|
|||
|
SensorDelay_us(8);
|
|||
|
SCL_Pin_Output_High();
|
|||
|
SensorDelay_us(8);
|
|||
|
while((GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)) && CNT < 100)
|
|||
|
CNT++;
|
|||
|
if(CNT == 100)
|
|||
|
{
|
|||
|
return 0;
|
|||
|
}
|
|||
|
SCL_Pin_Output_Low();
|
|||
|
SensorDelay_us(8);
|
|||
|
return 1;
|
|||
|
}
|
|||
|
|
|||
|
void Send_ACK(void) //<2F><><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>ACK<43>ź<EFBFBD>
|
|||
|
{
|
|||
|
SCL_Pin_Output_Low();
|
|||
|
SensorDelay_us(8);
|
|||
|
SDA_Pin_Output_Low();
|
|||
|
SensorDelay_us(8);
|
|||
|
SCL_Pin_Output_High();
|
|||
|
SensorDelay_us(8);
|
|||
|
SCL_Pin_Output_Low();
|
|||
|
SensorDelay_us(8);
|
|||
|
SDA_Pin_IN_FLOATING();
|
|||
|
SensorDelay_us(8);
|
|||
|
}
|
|||
|
|
|||
|
void Send_NOT_ACK(void) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>ACK
|
|||
|
{
|
|||
|
SCL_Pin_Output_Low();
|
|||
|
SensorDelay_us(8);
|
|||
|
SDA_Pin_Output_High();
|
|||
|
SensorDelay_us(8);
|
|||
|
SCL_Pin_Output_High();
|
|||
|
SensorDelay_us(8);
|
|||
|
SCL_Pin_Output_Low();
|
|||
|
SensorDelay_us(8);
|
|||
|
SDA_Pin_Output_Low();
|
|||
|
SensorDelay_us(8);
|
|||
|
}
|
|||
|
|
|||
|
void Stop_I2C(void) //һ<><D2BB>Э<EFBFBD><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
SDA_Pin_Output_Low();
|
|||
|
SensorDelay_us(8);
|
|||
|
SCL_Pin_Output_High();
|
|||
|
SensorDelay_us(8);
|
|||
|
SDA_Pin_Output_High();
|
|||
|
SensorDelay_us(8);
|
|||
|
}
|
|||
|
|
|||
|
uint8_t AHT20_Read_Status(void)//<2F><>ȡAHT20<32><30>״̬<D7B4>Ĵ<EFBFBD><C4B4><EFBFBD>
|
|||
|
{
|
|||
|
|
|||
|
uint8_t Byte_first;
|
|||
|
I2C_Start();
|
|||
|
AHT20_WR_Byte(0x71);
|
|||
|
Receive_ACK();
|
|||
|
Byte_first = AHT20_RD_Byte();
|
|||
|
Send_NOT_ACK();
|
|||
|
Stop_I2C();
|
|||
|
return Byte_first;
|
|||
|
}
|
|||
|
|
|||
|
void AHT20_SendAC(void) //<2F><>AHT20<32><30><EFBFBD><EFBFBD>AC<41><43><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
|
|||
|
I2C_Start();
|
|||
|
AHT20_WR_Byte(0x70);
|
|||
|
Receive_ACK();
|
|||
|
AHT20_WR_Byte(0xac);//0xAC<41>ɼ<EFBFBD><C9BC><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Receive_ACK();
|
|||
|
AHT20_WR_Byte(0x33);
|
|||
|
Receive_ACK();
|
|||
|
AHT20_WR_Byte(0x00);
|
|||
|
Receive_ACK();
|
|||
|
Stop_I2C();
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
//CRCУ<43><D0A3><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD>CRC8/MAXIM
|
|||
|
//<2F><><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>X8+X5+X4+1
|
|||
|
//Poly<6C><79>0011 0001 0x31
|
|||
|
//<2F><>λ<EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͱ<EFBFBD><CDB1><EFBFBD> 1000 1100 0x8c
|
|||
|
//C<><43>ʵ<EFBFBD><CAB5><EFBFBD>룺
|
|||
|
uint8_t Calc_CRC8(uint8_t *message,uint8_t Num)
|
|||
|
{
|
|||
|
uint8_t i;
|
|||
|
uint8_t byte;
|
|||
|
uint8_t crc=0xFF;
|
|||
|
for(byte=0; byte<Num; byte++)
|
|||
|
{
|
|||
|
crc^=(message[byte]);
|
|||
|
for(i=8;i>0;--i)
|
|||
|
{
|
|||
|
if(crc&0x80) crc=(crc<<1)^0x31;
|
|||
|
else crc=(crc<<1);
|
|||
|
}
|
|||
|
}
|
|||
|
return crc;
|
|||
|
}
|
|||
|
|
|||
|
void AHT20_Read_CTdata(uint32_t *ct) //û<><C3BB>CRCУ<43>飬ֱ<E9A3AC>Ӷ<EFBFBD>ȡAHT20<32><30><EFBFBD>¶Ⱥ<C2B6>ʪ<EFBFBD><CAAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
volatile uint8_t Byte_1th=0;
|
|||
|
volatile uint8_t Byte_2th=0;
|
|||
|
volatile uint8_t Byte_3th=0;
|
|||
|
volatile uint8_t Byte_4th=0;
|
|||
|
volatile uint8_t Byte_5th=0;
|
|||
|
volatile uint8_t Byte_6th=0;
|
|||
|
uint32_t RetuData = 0;
|
|||
|
uint16_t cnt = 0;
|
|||
|
AHT20_SendAC();//<2F><>AHT20<32><30><EFBFBD><EFBFBD>AC<41><43><EFBFBD><EFBFBD>
|
|||
|
Delay_1ms(80);//<2F><>ʱ80ms<6D><73><EFBFBD><EFBFBD>
|
|||
|
cnt = 0;
|
|||
|
while(((AHT20_Read_Status()&0x80)==0x80))//ֱ<><D6B1>״̬bit[7]Ϊ0<CEAA><30><EFBFBD><EFBFBD>ʾΪ<CABE><CEAA><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD>Ϊ1<CEAA><31><EFBFBD><EFBFBD>ʾæ״̬
|
|||
|
{
|
|||
|
SensorDelay_us(1508);
|
|||
|
if(cnt++>=100)
|
|||
|
{
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
I2C_Start();
|
|||
|
AHT20_WR_Byte(0x71);
|
|||
|
Receive_ACK();
|
|||
|
Byte_1th = AHT20_RD_Byte();//״̬<D7B4>֣<EFBFBD><D6A3><EFBFBD>ѯ<EFBFBD><D1AF>״̬Ϊ0x98,<2C><>ʾΪæ״̬<D7B4><CCAC>bit[7]Ϊ1<CEAA><31>״̬Ϊ0x1C<31><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0x0C<30><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0x08<30><38>ʾΪ<CABE><CEAA><EFBFBD><EFBFBD>״̬<D7B4><CCAC>bit[7]Ϊ0
|
|||
|
Send_ACK();
|
|||
|
Byte_2th = AHT20_RD_Byte();//ʪ<><CAAA>
|
|||
|
Send_ACK();
|
|||
|
Byte_3th = AHT20_RD_Byte();//ʪ<><CAAA>
|
|||
|
Send_ACK();
|
|||
|
Byte_4th = AHT20_RD_Byte();//ʪ<><CAAA>/<2F>¶<EFBFBD>
|
|||
|
Send_ACK();
|
|||
|
Byte_5th = AHT20_RD_Byte();//<2F>¶<EFBFBD>
|
|||
|
Send_ACK();
|
|||
|
Byte_6th = AHT20_RD_Byte();//<2F>¶<EFBFBD>
|
|||
|
Send_NOT_ACK();
|
|||
|
Stop_I2C();
|
|||
|
|
|||
|
RetuData = (RetuData|Byte_2th)<<8;
|
|||
|
RetuData = (RetuData|Byte_3th)<<8;
|
|||
|
RetuData = (RetuData|Byte_4th);
|
|||
|
RetuData =RetuData >>4;
|
|||
|
ct[0] = RetuData;//ʪ<><CAAA>
|
|||
|
RetuData = 0;
|
|||
|
RetuData = (RetuData|Byte_4th)<<8;
|
|||
|
RetuData = (RetuData|Byte_5th)<<8;
|
|||
|
RetuData = (RetuData|Byte_6th);
|
|||
|
RetuData = RetuData&0xfffff;
|
|||
|
ct[1] =RetuData; //<2F>¶<EFBFBD>
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
void AHT20_Read_CTdata_crc(uint32_t *ct) //CRCУ<43><D0A3><EFBFBD><EFBFBD>ȡAHT20<32><30><EFBFBD>¶Ⱥ<C2B6>ʪ<EFBFBD><CAAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
volatile uint8_t Byte_1th=0;
|
|||
|
volatile uint8_t Byte_2th=0;
|
|||
|
volatile uint8_t Byte_3th=0;
|
|||
|
volatile uint8_t Byte_4th=0;
|
|||
|
volatile uint8_t Byte_5th=0;
|
|||
|
volatile uint8_t Byte_6th=0;
|
|||
|
volatile uint8_t Byte_7th=0;
|
|||
|
uint32_t RetuData = 0;
|
|||
|
uint16_t cnt = 0;
|
|||
|
// uint8_t CRCDATA=0;
|
|||
|
uint8_t CTDATA[6]={0};//<2F><><EFBFBD><EFBFBD>CRC<52><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
AHT20_SendAC();//<2F><>AHT20<32><30><EFBFBD><EFBFBD>AC<41><43><EFBFBD><EFBFBD>
|
|||
|
Delay_1ms(80);//<2F><>ʱ80ms<6D><73><EFBFBD><EFBFBD>
|
|||
|
cnt = 0;
|
|||
|
while(((AHT20_Read_Status()&0x80)==0x80))//ֱ<><D6B1>״̬bit[7]Ϊ0<CEAA><30><EFBFBD><EFBFBD>ʾΪ<CABE><CEAA><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD>Ϊ1<CEAA><31><EFBFBD><EFBFBD>ʾæ״̬
|
|||
|
{
|
|||
|
SensorDelay_us(1508);
|
|||
|
if(cnt++>=100)
|
|||
|
{
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
I2C_Start();
|
|||
|
|
|||
|
AHT20_WR_Byte(0x71);
|
|||
|
Receive_ACK();
|
|||
|
CTDATA[0]=Byte_1th = AHT20_RD_Byte();//״̬<D7B4>֣<EFBFBD><D6A3><EFBFBD>ѯ<EFBFBD><D1AF>״̬Ϊ0x98,<2C><>ʾΪæ״̬<D7B4><CCAC>bit[7]Ϊ1<CEAA><31>״̬Ϊ0x1C<31><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0x0C<30><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0x08<30><38>ʾΪ<CABE><CEAA><EFBFBD><EFBFBD>״̬<D7B4><CCAC>bit[7]Ϊ0
|
|||
|
Send_ACK();
|
|||
|
CTDATA[1]=Byte_2th = AHT20_RD_Byte();//ʪ<><CAAA>
|
|||
|
Send_ACK();
|
|||
|
CTDATA[2]=Byte_3th = AHT20_RD_Byte();//ʪ<><CAAA>
|
|||
|
Send_ACK();
|
|||
|
CTDATA[3]=Byte_4th = AHT20_RD_Byte();//ʪ<><CAAA>/<2F>¶<EFBFBD>
|
|||
|
Send_ACK();
|
|||
|
CTDATA[4]=Byte_5th = AHT20_RD_Byte();//<2F>¶<EFBFBD>
|
|||
|
Send_ACK();
|
|||
|
CTDATA[5]=Byte_6th = AHT20_RD_Byte();//<2F>¶<EFBFBD>
|
|||
|
Send_ACK();
|
|||
|
Byte_7th = AHT20_RD_Byte();//CRC<52><43><EFBFBD><EFBFBD>
|
|||
|
Send_NOT_ACK(); //ע<><D7A2>: <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>NAK
|
|||
|
Stop_I2C();
|
|||
|
|
|||
|
if(Calc_CRC8(CTDATA,6)==Byte_7th)
|
|||
|
{
|
|||
|
RetuData = (RetuData|Byte_2th)<<8;
|
|||
|
RetuData = (RetuData|Byte_3th)<<8;
|
|||
|
RetuData = (RetuData|Byte_4th);
|
|||
|
RetuData =RetuData >>4;
|
|||
|
ct[0] = RetuData;//ʪ<><CAAA>
|
|||
|
RetuData = 0;
|
|||
|
RetuData = (RetuData|Byte_4th)<<8;
|
|||
|
RetuData = (RetuData|Byte_5th)<<8;
|
|||
|
RetuData = (RetuData|Byte_6th);
|
|||
|
RetuData = RetuData&0xfffff;
|
|||
|
ct[1] =RetuData; //<2F>¶<EFBFBD>
|
|||
|
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
ct[0]=0x00;
|
|||
|
ct[1]=0x00;//У<><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD>Ը<EFBFBD><D4B8><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
|
|||
|
}//CRC<52><43><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
void JH_Reset_REG(uint8_t addr)
|
|||
|
{
|
|||
|
|
|||
|
uint8_t Byte_first,Byte_second,Byte_third,Byte_fourth;
|
|||
|
I2C_Start();
|
|||
|
AHT20_WR_Byte(0x70);//ԭ<><D4AD><EFBFBD><EFBFBD>0x70
|
|||
|
Receive_ACK();
|
|||
|
AHT20_WR_Byte(addr);
|
|||
|
Receive_ACK();
|
|||
|
AHT20_WR_Byte(0x00);
|
|||
|
Receive_ACK();
|
|||
|
AHT20_WR_Byte(0x00);
|
|||
|
Receive_ACK();
|
|||
|
Stop_I2C();
|
|||
|
|
|||
|
Delay_1ms(5);//<2F><>ʱ5ms<6D><73><EFBFBD><EFBFBD>
|
|||
|
I2C_Start();
|
|||
|
AHT20_WR_Byte(0x71);//
|
|||
|
Receive_ACK();
|
|||
|
Byte_first = AHT20_RD_Byte();
|
|||
|
Send_ACK();
|
|||
|
Byte_second = AHT20_RD_Byte();
|
|||
|
Send_ACK();
|
|||
|
Byte_third = AHT20_RD_Byte();
|
|||
|
Send_NOT_ACK();
|
|||
|
Stop_I2C();
|
|||
|
|
|||
|
Delay_1ms(10);//<2F><>ʱ10ms<6D><73><EFBFBD><EFBFBD>
|
|||
|
I2C_Start();
|
|||
|
AHT20_WR_Byte(0x70);///
|
|||
|
Receive_ACK();
|
|||
|
AHT20_WR_Byte(0xB0|addr);////<2F>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Receive_ACK();
|
|||
|
AHT20_WR_Byte(Byte_second);
|
|||
|
Receive_ACK();
|
|||
|
AHT20_WR_Byte(Byte_third);
|
|||
|
Receive_ACK();
|
|||
|
Stop_I2C();
|
|||
|
|
|||
|
Byte_second=0x00;
|
|||
|
Byte_third =0x00;
|
|||
|
}
|
|||
|
|
|||
|
void AHT20_Start_Init(void)
|
|||
|
{
|
|||
|
JH_Reset_REG(0x1b);
|
|||
|
JH_Reset_REG(0x1c);
|
|||
|
JH_Reset_REG(0x1e);
|
|||
|
}
|
|||
|
|
|||
|
//int32_t main(void)
|
|||
|
//{
|
|||
|
// uint32_t CT_data[2];
|
|||
|
// volatile int c1,t1;
|
|||
|
// /***********************************************************************************/
|
|||
|
// /**///<2F>ϵ<EFBFBD><CFB5><EFBFBD>ʼ<EFBFBD><CABC>SDA<44><41>SCL<43><4C>IO<49><4F>
|
|||
|
// /***********************************************************************************/
|
|||
|
// Init_I2C_Sensor_Port();
|
|||
|
// /***********************************************************************************/
|
|||
|
// /**///<2F>ٸ<EFBFBD><D9B8>ϵ磬<CFB5><E7A3AC>ƷоƬ<D0BE>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫʱ<D2AA>䣬<EFBFBD><E4A3AC>ʱ100~500ms,<2C><><EFBFBD><EFBFBD>500ms
|
|||
|
// /***********************************************************************************/
|
|||
|
// Delay_1ms(500);
|
|||
|
// /***********************************************************************************/
|
|||
|
// /**///<2F><><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD>һ<EFBFBD>η<EFBFBD>0x71<37><31>ȡ״̬<D7B4>֣<EFBFBD><D6A3>ж<EFBFBD>״̬<D7B4><CCAC><EFBFBD>Ƿ<EFBFBD>Ϊ0x18,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0x18,<2C><><EFBFBD>мĴ<D0BC><C4B4><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
// /***********************************************************************************/
|
|||
|
// if((AHT20_Read_Status()&0x18)!=0x18)
|
|||
|
// {
|
|||
|
// AHT20_Start_Init(); //<2F><><EFBFBD>³<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
|
|||
|
// Delay_1ms(10);
|
|||
|
// }
|
|||
|
//
|
|||
|
// /***********************************************************************************/
|
|||
|
// /**///<2F>۸<EFBFBD><DBB8>ݿͻ<DDBF><CDBB>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><F3B7A2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>ʪ<EFBFBD><CAAA><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD>ǰwhile<6C><65>1<EFBFBD><31>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>ʪ<EFBFBD><CAAA><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ο<EFBFBD>
|
|||
|
// /***********************************************************************************/
|
|||
|
// while(1)
|
|||
|
// {
|
|||
|
// AHT20_Read_CTdata(CT_data); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>CRCУ<43>飬ֱ<E9A3AC>Ӷ<EFBFBD>ȡAHT20<32><30><EFBFBD>¶Ⱥ<C2B6>ʪ<EFBFBD><CAAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>Ƽ<EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1S<31><53>һ<EFBFBD><D2BB>
|
|||
|
// //AHT20_Read_CTdata_crc(CT_data); //crcУ<63><D0A3><EFBFBD><EFBFBD>ȡAHT20<32><30><EFBFBD>¶Ⱥ<C2B6>ʪ<EFBFBD><CAAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//
|
|||
|
|
|||
|
// c1 = CT_data[0]*100*10/1024/1024; //<2F><><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ʪ<EFBFBD><CAAA>ֵc1<63><31><EFBFBD>Ŵ<EFBFBD><C5B4><EFBFBD>10<31><30><EFBFBD><EFBFBD>
|
|||
|
// t1 = CT_data[1]*200*10/1024/1024-500;//<2F><><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5>¶<EFBFBD>ֵt1<74><31><EFBFBD>Ŵ<EFBFBD><C5B4><EFBFBD>10<31><30><EFBFBD><EFBFBD>
|
|||
|
// ////<2F><>һ<EFBFBD><D2BB><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ݣ<EFBFBD>
|
|||
|
// /***********************************************************************************/
|
|||
|
// /**///<2F><>ʱ1.5s<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1.5s<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>
|
|||
|
// /***********************************************************************************/
|
|||
|
// delay_ms(1500);
|
|||
|
// }
|
|||
|
|
|||
|
// }
|