#define AM2302_L LPC_GPIO0->DATA &= ~(1<<1)
#define AM2302_I GPIOSetDir(0, 1,0)
#define AM2302_O GPIOSetDir(0, 1,1)
#define AM2302_D LPC_GPIO0->DATA & (1<<1)
#define TMR32_0_TC LPC_TMR32B0->TC
void Timer32_0_Start(void)
{
LPC_TMR32B0->TCR = 2;
LPC_TMR32B0->TCR = 1;
}
/*us延时函数 */
void Timer32_0_Delay_us(uint32_t Timer_Count)
{
Timer32_0_Start();
while(TMR32_0_TC < Timer_Count);
}
uint32_t AM2302_READ(void)
{
uint64_t data_temp = 0;
uint32_t timer_temp = 0;
uint32_t data = 0;
uint8_t crc1 = 0;
uint8_t crc2 = 0;
uint8_t AM2302 = 0;
uint8_t Counter = 0;
AM2302_O; //引脚设置为输出
AM2302_L; //拉低
Timer32_0_Delay_us(1000); //延时1ms (如果是DHT11改为20000)
Timer32_0_Start(); //启动us定时器
AM2302_I; //引脚设置为输入
while(TMR32_0_TC < 5000) //5ms内结束
{
if( AM2302_D )
{
if(AM2302 == 0)
{
if( AM2302_D )
{AM2302 = 1;}
}
}
else
{
if(AM2302 == 1)
{
if( !(AM2302_D) )
{
AM2302 = 0;
if( (TMR32_0_TC - timer_temp) > 98 ) //单个CLK大于98us认为是1
{data_temp |= ( 0x20000000000 >> Counter );}
timer_temp = TMR32_0_TC;
if(++Counter == 42)
{TMR32_0_TC = 5000;}
}
}
}
}
if(Counter == 42)
{
data = data_temp >> 8;
crc1 = data_temp;
crc2 = (data>>24) + (data>>16) + (data>>8) + data;
if(crc1 != crc2)
{data = 0;}
}
else
{
data = 0x80000000;
}
return data;
}
|