在线时间954 小时
UID336767
注册时间2011-12-29
NXP金币721
TA的每日心情 | 开心 2018-7-23 21:04 |
---|
签到天数: 103 天 连续签到: 1 天 [LV.6]常住居民II
金牌会员
 
- 积分
- 16702
- 最后登录
- 1970-1-1
|

楼主 |
发表于 2016-11-11 23:55:58
|
显示全部楼层
dht11.c文件
- /*
- * DHT11.c
- *
- * Created on: 2016年11月11日
- * Author: LKL0305
- */
- #include "dht11.h"
- #define DHT11_SET_IN() Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, 0, dht11_Pin)
- #define DHT11_SET_OUT() Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 0, dht11_Pin)
- #define DHT11_DQ_OUT(x) Chip_GPIO_SetPinState(LPC_GPIO_PORT, 0, dht11_Pin, x)
- #define DHT11_DQ_IN() Chip_GPIO_GetPinState(LPC_GPIO_PORT, 0, dht11_Pin)
- #define MEAN_NUM 10
- thTypedef_t temphumTypedef;
- uint8_t dht11_Pin;
- uint8_t DHT11_ReadData(uint8_t *temperature, uint8_t *humidity);
- uint8_t DHT11_ReadByte(void);
- uint8_t DHT11_ReadBit(void);
- uint8_t DHT11_Check(void);
- void DHT11_Rst(void);
- void DHT11_Rst(void)
- {
- DHT11_SET_OUT();
- DHT11_DQ_OUT(LOW);
- Delay_Ms(20);
- DHT11_DQ_OUT(HIGH);
- Delay_Us(30);
- }
- uint8_t DHT11_Check(void)
- {
- uint8_t retry = 0;
- DHT11_SET_IN();
- while (DHT11_DQ_IN() && (retry < 100))
- {
- retry++;
- Delay_Us(1);
- }
- if (retry >= 100)
- {
- return 1;
- }
- else
- {
- retry = 0;
- }
- while (!DHT11_DQ_IN() && (retry < 100))
- {
- retry++;
- Delay_Us(1);
- }
- if(retry >= 100)
- {
- return 1;
- }
- return 0;
- }
- uint8_t DHT11_ReadBit(void)
- {
- uint8_t retry=0;
- while (DHT11_DQ_IN() && (retry < 100))
- {
- retry++;
- Delay_Us(1);
- }
- retry = 0;
- while (!DHT11_DQ_IN() && (retry < 100))
- {
- retry++;
- Delay_Us(1);
- }
- Delay_Us(30);
- if (DHT11_DQ_IN())
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- uint8_t DHT11_ReadByte(void)
- {
- uint8_t i, dat;
- dat = 0;
- for (i = 0; i < 8; i++)
- {
- dat <<= 1;
- dat |= DHT11_ReadBit();
- }
- return dat;
- }
- uint8_t DHT11_ReadData(uint8_t *temperature, uint8_t *humidity)
- {
- uint8_t buf[5];
- uint8_t i;
- DHT11_Rst();
- if (0 == DHT11_Check())
- {
- for (i = 0; i < 5; i++)
- {
- buf[i] = DHT11_ReadByte();
- }
- if (buf[4] == (buf[0] + buf[1] + buf[2] + buf[3]))
- {
- *humidity = buf[0];
- *temperature = buf[2];
- }
- }
- else
- {
- return 1;
- }
- return 0;
- }
- uint8_t DHT11_Read(uint8_t *temperature, uint8_t *humidity)
- {
- uint8_t curTem = 0, curHum = 0;
- uint16_t temMeans = 0, humMeans = 0;
- uint8_t curI = 0;
- uint8_t ret = 0;
- ret = DHT11_ReadData(&curTem, &curHum);
- if (1 != ret)
- {
- if (MEAN_NUM > temphumTypedef.curI)
- {
- temphumTypedef.thBufs[temphumTypedef.curI][0] = curTem;
- temphumTypedef.thBufs[temphumTypedef.curI][1] = curHum;
- temphumTypedef.curI++;
- }
- else
- {
- temphumTypedef.curI = 0;
- temphumTypedef.thBufs[temphumTypedef.curI][0] = curTem;
- temphumTypedef.thBufs[temphumTypedef.curI][1] = curHum;
- temphumTypedef.curI++;
- }
- }
- else
- {
- return 1;
- }
- if (MEAN_NUM <= temphumTypedef.curI)
- {
- temphumTypedef.thAmount = MEAN_NUM;
- }
- if (0 == temphumTypedef.thAmount)
- {
- for (curI = 0; curI < temphumTypedef.curI; curI++)
- {
- temMeans += temphumTypedef.thBufs[curI][0];
- humMeans += temphumTypedef.thBufs[curI][1];
- }
- temMeans = temMeans / temphumTypedef.curI;
- humMeans = humMeans / temphumTypedef.curI;
- *temperature = temMeans;
- *humidity = humMeans;
- }
- else if (MEAN_NUM == temphumTypedef.thAmount)
- {
- for(curI = 0; curI < temphumTypedef.thAmount; curI++)
- {
- temMeans += temphumTypedef.thBufs[curI][0];
- humMeans += temphumTypedef.thBufs[curI][1];
- }
- temMeans = temMeans / temphumTypedef.thAmount;
- humMeans = humMeans / temphumTypedef.thAmount;
- *temperature = (uint8_t)temMeans;
- *humidity = (uint8_t)humMeans;
- }
- return 0;
- }
- uint8_t DHT11_Init(uint8_t Pin)
- {
- dht11_Pin = Pin;
- Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_GPIO);
- DHT11_SET_OUT();
- DHT11_DQ_OUT(HIGH);
- DHT11_Rst();
- memset((uint8_t *)&temphumTypedef, 0, sizeof(thTypedef_t));
- return DHT11_Check();
- }
复制代码 |
|