Advertisement
Guest User

DHT11

a guest
Oct 10th, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.75 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <stdio.h>
  5.  
  6. #include "dht.h"
  7.  
  8. // Trying to set up the DHT11 so it can be used in this project
  9. void read() {
  10.   // The bits are for to get the temprature values in.
  11.   uint8_t bits[5];
  12.     uint8_t i, j = 0;
  13.  
  14.   // Setting up the DDRD as OUTPUT and PORTD as HIGH
  15.   DDRD |= (1 << DDD5);
  16.   PORTD |= (1 << PORTD5);
  17.   _delay_ms(100);
  18.  
  19.   /*
  20.    * Makes an request to get the data from the DHT11
  21.    * Set the the PORTD5 as LOW
  22.   */
  23.   PORTD &= ~(1 << PORTD5);
  24.   _delay_ms(18);
  25.   // Setting it back to high and DDRD as an input
  26.   PORTD |= (1 << PORTD5);
  27.   _delay_us(1);
  28.   DDRD &= ~(1 << DDD5);
  29.  
  30.   /*
  31.    * Checking to see if the ACK is happening
  32.   */
  33.   _delay_us(40);
  34.   if ((PIND & (1 << PIND5))) {
  35.     return;
  36.   }
  37.   _delay_us(80);
  38.   if (!(PIND & (1 << PIND5))) {
  39.     return;
  40.   }
  41.   _delay_us(80);
  42.  
  43.  
  44.   // Reading the data and starts with bits 0 up to 4
  45.   for (j = 0; j < 5; j++) {
  46.         uint8_t result = 0;
  47.     // I read every bit in the system to be able to get it in to the bits.
  48.         for (i = 0; i < 8; i++) {
  49.             while (!(PIND & (1 << PIND5)))
  50.                 ; //wait for an HIGH input
  51.         // delay for 30us
  52.             _delay_us(30);
  53.       //if the input is HIGH then I will put the data in result
  54.             if (PIND & (1 << PIND5))
  55.                 result |= (1 << (7 - i));
  56.             while (PIND & (1 << PIND5))
  57.                 ; //wait until input get LOW
  58.         }
  59.     //Put the result in the bit
  60.         bits[j] = result;
  61.     }
  62.  
  63.   // Resets the pins
  64.   DDRD |= (1 << DDD5);
  65.   PORTD |= (1 << PORTD5);
  66.   _delay_ms(100);
  67.  
  68.  
  69.   //checks the sum and gets the temprature
  70.   if (bits[0] + bits[1] + bits[2] + bits[3] == bits[4]) {
  71.     printf("Temperature: %d\n", bits[2]);
  72.   }
  73.   //printf("Temprature: %d\n", temprature);
  74.   _delay_ms(2000);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement