Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

HDT22 sensor program not working

Status
Not open for further replies.

okbro

Member
hi,
I have humidity/temperature sensor and LCD connected to ATmega32(below is schematic and code). The code does not work, the values does not get displayed on LCD. Can anybody point out what is wrong in the code? thanks

hum.jpg


Codes:
main.c
C:
#ifndef F_CPU
#deifne F_CPU 8000000UL
#endif

#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdio.h>
#include "lcd.h"
#include "hdt11.h"

uint8_t I_RH,D_RH,I_Temp,D_Temp,CheckSum;

int main()
{
   // Write your code here
    lcdinit();

    char data[5];

    char humi[]={"Humidity: "};
    char temp[] = {"Temp: "};
   
    lcdgoto(1,1);
    lcdstr(humi);
    lcdgoto(1,2);
    lcdstr(temp);

   while (1){

        request();        /* send start pulse */
        response();        /* receive response */

        I_RH=receive_data();    /* store first eight bit in I_RH */
        D_RH=receive_data();    /* store next eight bit in D_RH */
        I_Temp=receive_data();    /* store next eight bit in I_Temp */
        D_Temp=receive_data();    /* store next eight bit in D_Temp */
        CheckSum=receive_data();/* store next eight bit in CheckSum */

        _delay_ms(10);

        if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)
        {
            lcdgoto(1,1);
            lcdstr("Error");
        }
       
        else
        {  
            itoa(I_RH,data,10);
            lcdgoto(10,1);
            lcdstr(data);
            lcdstr(".");
           
            itoa(D_RH,data,10);
            lcdstr(data);
            lcdstr("%");

            itoa(I_Temp,data,10);
            lcdgoto(10,2);
            lcdstr(data);
            lcdstr(".");
           
            itoa(D_Temp,data,10);
            lcdstr(data);
            lcdchar(0xDF);
            lcdstr("C ");
           
            itoa(CheckSum,data,10);
            lcdstr(data);
            lcdstr(" ");
        }
     
    _delay_ms(10);
      }

   return 0;
}

hdt11.c

C:
#include "hdt11.h"

uint8_t c=0;

void request()                /* Microcontroller send start pulse/request */
{
    DDRC |= (1<<PC3);
    PORTC &= ~(1<<PC3);    /* set to low pin */
    _delay_ms(18);            /* wait for 20ms */
    PORTC |= (1<<PC3);    /* set to high pin */
}

void response()                /* receive response from DHT11 */
{
    DDRC &= ~(1<<PC3);
    while(PINC & (1<<PC3));
    while((PINC & (1<<PC3))==0);
    while(PINC & (1<<PC3));
}

uint8_t receive_data()            /* receive data */
{  

    for (int q=0; q<8; q++)
    {
        while((PINC & (1<<PC3)) == 0);  /* check received bit 0 or 1 */
        _delay_us(30);
        if(PINC & (1<<PC3))/* if high pulse is greater than 30ms */
        c = (c<<1)|(0x01);    /* then its logic HIGH */
        else            /* otherwise its logic LOW */
        c = (c<<1);
        while(PINC & (1<<PC3));
    }
    return c;
}
 

Attachments

  • hum.jpg
    hum.jpg
    53.9 KB · Views: 298
hi nigel,
i know arduino, but can we use arduino libraries with ATmega32 microcontroller and other AVR microcontrollers?
 
hi nigel,
i know arduino, but can we use arduino libraries with ATmega32 microcontroller and other AVR microcontrollers?

You can use them on anything, if you port the code - I often port the libraries to standard C for use with PIC controllers.

However, the ATMega32 is supported in the Arduino IDE, so if you use that for programming there's no changes at all needed - or as long as your programming system uses C++ there's very little to change anyway.
 
porting is quite difficult, i looked at the library code. configuring arduino IDE for atmega32 is whole different story and there is memory space issue too. wanted to know what could be wrong in the code i posted.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top