您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > STM32+DHT11单总线数据采集源程序
STM32+DHT11单总线通信源码/****************************************Copyright(c)**************************************************************--------------FileInfo---------------------------------------------------------------------------------**FileName:userApi-dht11.h**LastmodifiedDate:2016.02.16**LastVersion:v1.0**Description:Driverfordht11****--------------------------------------------------------------------------------------------------------**CreatedBy:chengyong**Createddate:**Version:v1.0**Descriptions:***********************************************************************************************************/#includestdint.h#includeuserApi-gpio.h#defineDHT11_DATA_PORTGPIOA#defineDHT11_DATA_PINGPIO_Pin_0#defineDHT11_DataPinInputSet(x)userApiGpio_Init(DHT11_DATA_PORT,DHT11_DATA_PIN,PIN_INPUT,PIN_PUSH_PULL,PIN_PULL_UP,x)#defineDHT11_DataPinOutputSet(x)userApiGpio_Init(DHT11_DATA_PORT,DHT11_DATA_PIN,PIN_OUTPUT,PIN_PUSH_PULL,PIN_PULL_UP,x);#defineDHT11_ReadDataInputPin()userApiGpio_Read(DHT11_DATA_PORT,DHT11_DATA_PIN)externuint8_tDHT11_ReadValue(void);/****************************************Copyright(c)**************************************************************--------------FileInfo---------------------------------------------------------------------------------**FileName:userApi-dht11.c**LastmodifiedDate:2016.02.16**LastVersion:v1.0**Description:Driverfordht11****--------------------------------------------------------------------------------------------------------**CreatedBy:chengyong**Createddate:**Version:v1.0**Descriptions:***********************************************************************************************************/#includeuserApi-dht11.h#includeuserApi-delay.huint8_tDHT11_Temp;uint8_tDHT11_Huri;uint8_tuserApiDht11_BitValue(uint8_tbit){switch(bit){case0:return0x01;case1:return0x02;case2:return0x04;case3:return0x08;case4:return0x10;case5:return0x20;case6:return0x40;case7:return0x80;default:break;}return0;}uint8_tuserApiDht11_DataCrc(uint8_t*rxBuf){uint16_tCheckSum=0;uint8_tCrcValue=0;CrcValue=rxBuf[4];CheckSum=rxBuf[0]+rxBuf[1]+rxBuf[2]+rxBuf[3];if(CheckSum==CrcValue){returntrue;}returnfalse;}uint8_tuserApiDht11_DataRx(uint8_t*rxBuf){uint8_tbyteIdx=0,bitIdx=0;for(byteIdx=0;byteIdx5;byteIdx++){rxBuf[byteIdx]=0;for(bitIdx=0;bitIdx8;bitIdx++){while(!DHT11_ReadDataInputPin());//50us低电平userApiDelay_Multi10us(5);//延时50usif(DHT11_ReadDataInputPin()){rxBuf[byteIdx]|=userApiDht11_BitValue(7-bitIdx);while(DHT11_ReadDataInputPin());//等待该位数据信号结束}}}returntrue;}uint8_tDHT11_ReadValue(void){uint8_ttimeout1=30,timeout2=30;uint8_tDHT11_Buffer[5];DHT11_DataPinOutputSet(0);//拉低数据口userApiDelay_TimerHwDelayMs(20);//延时18msDHT11_DataPinInputSet(0);//释放数据口,设置为输入userApiDelay_Multi10us(2);//延时20uswhile(!DHT11_ReadDataInputPin())//检测应答信号80us低电平{userApiDelay_Delay5us();timeout1--;if(timeout1==0){returnfalse;}}while(DHT11_ReadDataInputPin())//检测开始发送数据信号80us高电平{userApiDelay_Delay5us();timeout2--;if(timeout2==0){returnfalse;}}if(true!=userApiDht11_DataRx(DHT11_Buffer)){returnfalse;}if(true!=userApiDht11_DataCrc(DHT11_Buffer)){returnfalse;}DHT11_Temp=DHT11_Buffer[0];DHT11_Huri=DHT11_Buffer[2];returntrue;}
本文标题:STM32+DHT11单总线数据采集源程序
链接地址:https://www.777doc.com/doc-2860897 .html