Dht11

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Dht11

954 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CR7 on Mon Mar 11 06:31:22 MST 2013
I am currently using LPC1769 and I want to program the code for DHT11 Humidity sensor.Could anyone help me about the coding? Or anyone has written the program about this sensor? Please help.

The DHT11 datasheet can be found here: http://www.micro4you.com/files/sensor/DHT11.pdf

Thank you very much.
0 Kudos
1 Reply

764 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by tbelo on Wed Oct 21 10:03:01 MST 2015
Here you are.
For IRC 12Mhz at port 1.4 on lpc1115.


getHumidity(&sensors_data);

#define DHT_MAXTIMINGS10000
#define DHT_BREAKTIME20
#define DHT_MAXCOUNT32000


void getHumidity (sensors* output){
uint16_t counter = 0;
uint8_t laststate = 1;
uint16_t i=0,k  = 0;
uint8_t j = 0;
uint8_t checksum = 0;
uint8_t data[100];
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
uint8_t temperature;
uint8_t humidity;

LPC_IOCON->PIO1_4 = 0xD0;// PIO1.4, PULL UP
LPC_GPIO1->DIR |= (1<<4);//output
LPC_GPIO1->DATA |= (0x1<<4);//high
delayms(250);
// Wake up device, 250ms of high


LPC_GPIO1->DATA &= ~(0x1<<4);//low
delayms(20);
// Hold low for 20ms

//LPC_GPIO1->DIR |= (1<<4);//output
LPC_GPIO1->DATA |= (0x1<<4);//high
delayus(1);
// High for 40ns


LPC_GPIO1->DIR &= ~(1<<4);//input
while (((LPC_GPIO1->DATA & (0x1<<4))==1) && (i < DHT_MAXCOUNT)) {
for (k = 0; k<6; k++) {}
i++;
}

for (i = 0; i < DHT_MAXTIMINGS; i++)
{
// Count high time (in approx us)
counter = 0;
while ((LPC_GPIO1->DATA & (0x1<<4)) == laststate)
{
counter++;
for (k = 0; k<6; k++) {}
if (counter == 1000)
break;
}

laststate = (LPC_GPIO1->DATA & (0x1<<4));
if (counter == 1000)
break;
// store data after 3 reads
// ignore first 3 transitions
if ((i>3) && (i%2 == 0)) {
// shove each bit into the storage bytes
//data[j/8] will point to the same byte for 8 consecutive values of j
//in each loop, this byte will be shifted left by one bit
data[j/8] <<= 1;
if (counter > DHT_BREAKTIME)
data[j/8] |= 1;
j++;
}
}


if (j >= 39) {
checksum = (data[0] + data[1] + data[2] + data[3]) & 0xFF;
//if (data[4] == checksum) {
// checksum is valid

humidity =  data[0];
temperature = data[2];

//citoa ((uint8_t)humidity, 10, output->humidityC);
//citoa (temperature, 10, output->temperatureC);

} else {
// output->humidityC[0]='\0';
}

LPC_IOCON->PIO1_4 = 0xC0;// PIO1.4, PULL UP
LPC_GPIO1->DIR  &= ~(1<<4);
LPC_GPIO3->MASKED_ACCESS[1<<4] = 0xFFFFFFFF;
}



void delayus (uint16_t ms){
    SysTick->LOAD = 479;
    SysTick->VAL = 0;
    SysTick->CTRL = 7;
    Wait(ms);
}

void delayms (uint16_t ms){
    SysTick->LOAD = 11999;//(12000-1);
    SysTick->VAL = 0;
    SysTick->CTRL = 7;
    Wait(ms);
}

0 Kudos