11.6:完善多任务和消息队列机制。
This commit is contained in:
129
STM32/Environment_Node/Drivers/DHT11.c
Normal file
129
STM32/Environment_Node/Drivers/DHT11.c
Normal file
@@ -0,0 +1,129 @@
|
||||
#include "DHT11.h"
|
||||
|
||||
//<2F><>λDHT11
|
||||
void vDht11Rst(void)
|
||||
{
|
||||
vDht11Mode(OUT); //SET OUTPUT
|
||||
DHT11_Low; //<2F><><EFBFBD><EFBFBD>DQ
|
||||
vTaskDelay(20); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>18~30ms
|
||||
DHT11_High; //DQ=1
|
||||
vDelayUs(13); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>10~35us
|
||||
}
|
||||
|
||||
//<2F>ȴ<EFBFBD>DHT11<31>Ļ<EFBFBD>Ӧ
|
||||
//<2F><><EFBFBD><EFBFBD>1:δ<><CEB4><EFBFBD>DHT11<31>Ĵ<EFBFBD><C4B4><EFBFBD>
|
||||
//<2F><><EFBFBD><EFBFBD>0:<3A><><EFBFBD><EFBFBD>
|
||||
uint8_t vDht11Check(void)
|
||||
{
|
||||
uint8_t retry=0;
|
||||
vDht11Mode(IN);//SET INPUT
|
||||
while (GPIO_ReadInputDataBit(DHT11_GPIO_PORT,DHT11_GPIO_PIN)&&retry<100)//DHT11<31><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>40~80us
|
||||
{
|
||||
retry++;
|
||||
vDelayUs(1);
|
||||
};
|
||||
if(retry>=100)return 1;
|
||||
else retry=0;
|
||||
while (!GPIO_ReadInputDataBit(DHT11_GPIO_PORT,DHT11_GPIO_PIN)&&retry<100)//DHT11<31><31><EFBFBD>ͺ<EFBFBD><CDBA><EFBFBD><EFBFBD>ٴ<EFBFBD><D9B4><EFBFBD><EFBFBD><EFBFBD>40~80us
|
||||
{
|
||||
retry++;
|
||||
vDelayUs(1);
|
||||
};
|
||||
if(retry>=100)return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//<2F><>DHT11<31><31>ȡһ<C8A1><D2BB>λ
|
||||
//<2F><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>1/0
|
||||
uint8_t vDht11ReadBit(void)
|
||||
{
|
||||
uint8_t retry=0;
|
||||
while(GPIO_ReadInputDataBit(DHT11_GPIO_PORT,DHT11_GPIO_PIN)&&retry<100)//<2F>ȴ<EFBFBD><C8B4><EFBFBD>Ϊ<EFBFBD>͵<EFBFBD>ƽ
|
||||
{
|
||||
retry++;
|
||||
vDelayUs(1);
|
||||
}
|
||||
retry=0;
|
||||
while(!GPIO_ReadInputDataBit(DHT11_GPIO_PORT,DHT11_GPIO_PIN)&&retry<100)//<2F>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD>ߵ<EFBFBD>ƽ
|
||||
{
|
||||
retry++;
|
||||
vDelayUs(1);
|
||||
}
|
||||
vDelayUs(40);//<2F>ȴ<EFBFBD>40us
|
||||
if(GPIO_ReadInputDataBit(DHT11_GPIO_PORT,DHT11_GPIO_PIN))return 1;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
//<2F><>DHT11<31><31>ȡһ<C8A1><D2BB><EFBFBD>ֽ<EFBFBD>
|
||||
//<2F><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
uint8_t vDht11ReadByte(void)
|
||||
{
|
||||
uint8_t i,dat;
|
||||
dat=0;
|
||||
for (i=0;i<8;i++)
|
||||
{
|
||||
dat<<=1;
|
||||
dat|=vDht11ReadBit();
|
||||
}
|
||||
return dat;
|
||||
}
|
||||
|
||||
//<2F><>DHT11<31><31>ȡһ<C8A1><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
//temp:<3A>¶<EFBFBD>ֵ(<28><>Χ:0~50<35><30>)
|
||||
//humi:ʪ<><CAAA>ֵ(<28><>Χ:20%~90%)
|
||||
//<2F><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>0,<2C><><EFBFBD><EFBFBD>;1,<2C><>ȡʧ<C8A1><CAA7>
|
||||
uint8_t vDht11ReadData(uint8_t *pucTemp,uint8_t *pucHumi)
|
||||
{
|
||||
uint8_t ucBuffer[5];
|
||||
uint8_t i;
|
||||
vDht11Rst();
|
||||
if(vDht11Check()==0)
|
||||
{
|
||||
for(i=0;i<5;i++)//<2F><>ȡ40λ<30><CEBB><EFBFBD><EFBFBD>
|
||||
{
|
||||
ucBuffer[i]=vDht11ReadByte();
|
||||
}
|
||||
if((ucBuffer[0]+ucBuffer[1]+ucBuffer[2]+ucBuffer[3])==ucBuffer[4])
|
||||
{
|
||||
*pucHumi=ucBuffer[0];
|
||||
*pucTemp=ucBuffer[2];
|
||||
}
|
||||
}
|
||||
else return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//<2F><>ʼ<EFBFBD><CABC>DHT11<31><31>IO<49><4F> DQ ͬʱ<CDAC><CAB1><EFBFBD><EFBFBD>DHT11<31>Ĵ<EFBFBD><C4B4><EFBFBD>
|
||||
//<2F><><EFBFBD><EFBFBD>1:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
//<2F><><EFBFBD><EFBFBD>0:<3A><><EFBFBD><EFBFBD>
|
||||
uint8_t vDht11Init(void)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
RCC_APB2PeriphClockCmd(DHT11_GPIO_CLK, ENABLE); //ʹ<><CAB9>PA<50>˿<EFBFBD>ʱ<EFBFBD><CAB1>
|
||||
GPIO_InitStructure.GPIO_Pin = DHT11_GPIO_PIN; //PG11<31>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD>
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(DHT11_GPIO_PORT, &GPIO_InitStructure); //<2F><>ʼ<EFBFBD><CABC>IO<49><4F>
|
||||
GPIO_SetBits(DHT11_GPIO_PORT,DHT11_GPIO_PIN); //PG11 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
vDht11Rst(); //<2F><>λDHT11
|
||||
return vDht11Check();//<2F>ȴ<EFBFBD>DHT11<31>Ļ<EFBFBD>Ӧ
|
||||
}
|
||||
|
||||
void vDht11Mode(uint8_t ucMode)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
|
||||
if(ucMode)
|
||||
{
|
||||
GPIO_InitStructure.GPIO_Pin = DHT11_GPIO_PIN;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_InitStructure.GPIO_Pin = DHT11_GPIO_PIN;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
||||
}
|
||||
GPIO_Init(DHT11_GPIO_PORT, &GPIO_InitStructure);
|
||||
}
|
||||
Reference in New Issue
Block a user