网站:bbs.21ic.com
Write_AM2301_PIN_Init();
RESET_AM2301_PIN();
HAL_Delay(2);
SET_AM2301_PIN();
rt_hw_us_delay(30);
Read_AM2301_PIN_Init();
Sensor_AnswerFlag=0;
=GPIO_PIN_RESET) =
{
Sensor_AnswerFlag=1;
Sys_CNT=0;
while(Read_AM2301_PIN()==GPIO_PIN_RESET)
{
if(++Sys_CNT>3000)
{
Sensor_ErrorFlag=1;
return 0;
}
}
Sys_CNT=0;
while(Read_AM2301_PIN()==GPIO_PIN_SET)
{
if(++Sys_CNT>3000)
{
Sensor_ErrorFlag=1;
return 0;
}
}
for(i=0;i<5;i++)
{
AM2301_Data<i> = Read_AM2301_Data();</i>
<i> }</i>
unsigned char Read_AM2301_Data(void)
{
unsigned char i,cnt,buffer,tmp;
//要读取8次
for (i = 0; i < 8; i++)
{
cnt=0;
//判断低电平是否结束
while(!Read_AM2301_PIN())
{
if(++cnt>=3000)
break;
}
//低电平结束后,进入高电平,开始计时30us
rt_hw_us_delay(30);
tmp=0;
//如果此时还是高电平,那么肯定是大于28us,确定是1来了,赋值1
if(Read_AM2301_PIN())
tmp=1;
cnt=0;
//等待高电平结束,号进入下一位的读取
while(Read_AM2301_PIN())
{
if(++cnt>=2000)
break;
}
//移位写入刚刚得到的1个BIT
buffer<<=1;
buffer|=tmp;
}
return buffer;
}
unsigned char Read_AM2301_PIN(void)
{
return HAL_GPIO_ReadPin(AM2301_PORT, AM2301_PIN);
}
void SET_AM2301_PIN(void)
{
HAL_GPIO_WritePin(AM2301_PORT, AM2301_PIN,GPIO_PIN_SET);
}
void RESET_AM2301_PIN(void)
{
HAL_GPIO_WritePin(AM2301_PORT, AM2301_PIN,GPIO_PIN_RESET);
}
//读传感器 端口位定义,可修改
//*
unsigned char Sensor_AnswerFlag; //收到起始标志位
unsigned char Sensor_ErrorFlag; //读取传感器错误标志
unsigned int Sys_CNT;
unsigned char AM2301_Data[5]={0x00,0x00,0x00,0x00,0x00};
void Read_AM2301_PIN_Init(void)
{
AM2301_GPIO_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = AM2301_PIN;
HAL_GPIO_Init(AM2301_PORT, &GPIO_InitStruct);
}
void Write_AM2301_PIN_Init(void)
{
AM2301_GPIO_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = AM2301_PIN;
HAL_GPIO_Init(AM2301_PORT, &GPIO_InitStruct);
}
unsigned char Read_AM2301_PIN(void)
{
return HAL_GPIO_ReadPin(AM2301_PORT, AM2301_PIN);
}
void SET_AM2301_PIN(void)
{
HAL_GPIO_WritePin(AM2301_PORT, AM2301_PIN,GPIO_PIN_SET);
}
void RESET_AM2301_PIN(void)
{
HAL_GPIO_WritePin(AM2301_PORT, AM2301_PIN,GPIO_PIN_RESET);
}
unsigned char Read_AM2301_Data(void)
{
unsigned char i,cnt,buffer,tmp;
for (i = 0; i < 8; i++)
{
cnt=0;
while(!Read_AM2301_PIN())
{
if(++cnt>=3000)
break;
}
rt_hw_us_delay(30);
tmp=0;
if(Read_AM2301_PIN())
tmp=1;
cnt=0;
while(Read_AM2301_PIN())
{
if(++cnt>=2000)
break;
}
buffer<<=1;
buffer|=tmp;
}
return buffer;
}
unsigned char Read_Sensor(void)
{
unsigned char i;
Write_AM2301_PIN_Init();
RESET_AM2301_PIN();
// rt_thread_mdelay(2);
HAL_Delay(2);
SET_AM2301_PIN();
rt_hw_us_delay(30);
SET_AM2301_PIN();
Read_AM2301_PIN_Init();
Sensor_AnswerFlag=0;
if(Read_AM2301_PIN()==GPIO_PIN_RESET)
{
Sensor_AnswerFlag=1;
Sys_CNT=0;
while(Read_AM2301_PIN()==GPIO_PIN_RESET)
{
if(++Sys_CNT>3000)
{
Sensor_ErrorFlag=1;
return 0;
}
}
Sys_CNT=0;
while(Read_AM2301_PIN()==GPIO_PIN_SET)
{
if(++Sys_CNT>3000)
{
Sensor_ErrorFlag=1;
return 0;
}
}
for(i=0;i<5;i++)
{
AM2301_Data[i] = Read_AM2301_Data();
}
}
else
{
Sensor_AnswerFlag=0;
}
return 1;
}
本文系21ic论坛网友gaoyang9992006原创