Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <avr/io.h>
- #include <util/delay.h>
- #include <stdio.h>
- #include "dht.h"
- // Trying to set up the DHT11 so it can be used in this project
- void read() {
- // The bits are for to get the temprature values in.
- uint8_t bits[5];
- uint8_t i, j = 0;
- // Setting up the DDRD as OUTPUT and PORTD as HIGH
- DDRD |= (1 << DDD5);
- PORTD |= (1 << PORTD5);
- _delay_ms(100);
- /*
- * Makes an request to get the data from the DHT11
- * Set the the PORTD5 as LOW
- */
- PORTD &= ~(1 << PORTD5);
- _delay_ms(18);
- // Setting it back to high and DDRD as an input
- PORTD |= (1 << PORTD5);
- _delay_us(1);
- DDRD &= ~(1 << DDD5);
- /*
- * Checking to see if the ACK is happening
- */
- _delay_us(40);
- if ((PIND & (1 << PIND5))) {
- return;
- }
- _delay_us(80);
- if (!(PIND & (1 << PIND5))) {
- return;
- }
- _delay_us(80);
- // Reading the data and starts with bits 0 up to 4
- for (j = 0; j < 5; j++) {
- uint8_t result = 0;
- // I read every bit in the system to be able to get it in to the bits.
- for (i = 0; i < 8; i++) {
- while (!(PIND & (1 << PIND5)))
- ; //wait for an HIGH input
- // delay for 30us
- _delay_us(30);
- //if the input is HIGH then I will put the data in result
- if (PIND & (1 << PIND5))
- result |= (1 << (7 - i));
- while (PIND & (1 << PIND5))
- ; //wait until input get LOW
- }
- //Put the result in the bit
- bits[j] = result;
- }
- // Resets the pins
- DDRD |= (1 << DDD5);
- PORTD |= (1 << PORTD5);
- _delay_ms(100);
- //checks the sum and gets the temprature
- if (bits[0] + bits[1] + bits[2] + bits[3] == bits[4]) {
- printf("Temperature: %d\n", bits[2]);
- }
- //printf("Temprature: %d\n", temprature);
- _delay_ms(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement