I have a problem with the code to read data from DHT22 and show on LCD 16x2.... All i can show is temp and humi and it does not have any data about temp or humi..
This is my code
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// end LCD module connections
// DHT22 pin connection (here data pin is connected to pin RB0)
#define DHT22_PIN RA0_bit
#define DHT22_PIN_DIR TRISA0_bit
char dht22_read_byte()
{
char i = 8, dht22_byte = 0;
while(i--)
{
while( !DHT22_PIN );
delay_us(40);
if( DHT22_PIN )
{
dht22_byte |= (1 << i); // set bit i
while( DHT22_PIN );
}
}
return(dht22_byte);
}
// read humidity (in hundredths rH%) and temperature (in hundredths °Celsius) from sensor
void dht22_read(int *dht22_humi, int *dht22_temp)
{
// send start signal
DHT22_PIN = 0; // connection pin output low
DHT22_PIN_DIR = 0; // configure connection pin as output
delay_ms(25); // wait 25 ms
DHT22_PIN = 1; // connection pin output high
delay_us(30); // wait 30 us
DHT22_PIN_DIR = 1; // configure connection pin as input
// check sensor response
while( DHT22_PIN );
while(!DHT22_PIN );
while( DHT22_PIN );
// read data
*dht22_humi = dht22_read_byte(); // read humidity byte 1
*dht22_humi = (*dht22_humi << | dht22_read_byte(); // read humidity byte 2
*dht22_temp = dht22_read_byte(); // read temperature byte 1
*dht22_temp = (*dht22_temp << | dht22_read_byte(); // read temperature byte 2
dht22_read_byte(); // read checksum (skipped)
if(*dht22_temp & 0x8000) { // if temperature is negative
*dht22_temp &= 0x7FFF;
*dht22_temp *= -1;
}
}
void main()
{
OSCCON = 0x70; // set internal oscillator to 8MHz
ADCON1 = 0x0C;
CMCON = 7;
delay_ms(1000); // wait a second
Lcd_Init(); // initialize LCD module
Lcd_Cmd(_LCD_CURSOR_OFF); // cursor off
Lcd_Cmd(_LCD_CLEAR); // clear LCD
Lcd_Out(1, 1, "Temp =");
Lcd_Out(2, 1, "Humi =");
while(1)
{
int humidity, temperature;
char temp_a[8], humi_a[8];
// read humidity (in hundredths rH%) and temperature (in hundredths °C) from the DHT22 sensor
dht22_read(&humidity, &temperature);
if(temperature < 0)
sprintf(temp_a, "-%02u.%01u%cC", abs(temperature)/10, abs(temperature) % 10, 223);
else
sprintf(temp_a, " %02u.%01u%cC", temperature/10, temperature % 10, 223);
if(humidity >= 1000) // if humidity >= 100.0 rH%
sprintf(humi_a, "%03u.%01u %%", humidity/10, humidity % 10);
else
sprintf(humi_a, " %02u.%01u %%", humidity/10, humidity % 10);
Lcd_Out(1, 7, temp_a);
Lcd_Out(2, 7, humi_a);
//delay_ms(1000); // wait a second
}
}
DHT22 with 18f4550
- IvanJeremic
- mikroElektronika team
- Posts: 316
- Joined: 05 Sep 2022 14:32
Re: DHT22 with 18f4550
Hi,
You need to convert the values into a string.
There are conversion libraries in both MikroC and Necto Studio (Necto is free for non-commercial purposes https://www.mikroe.com/necto).
Regards,
Ivan.
You need to convert the values into a string.
There are conversion libraries in both MikroC and Necto Studio (Necto is free for non-commercial purposes https://www.mikroe.com/necto).
Regards,
Ivan.
-
- Posts: 1557
- Joined: 24 Jun 2007 19:27
- Location: 01800 St Maurice de Gourdans France
- Contact:
Re: DHT22 with 18f4550
hello,
you try to put 2 bytes into a variable using an integer pointer ....so at same adress
test this:
define variables as (global) integer value
no need to pass parameter to function void dht22_read
sprintf is allready used for that ..IvanJeremic wrote: You need to convert the values into a string.
you try to put 2 bytes into a variable using an integer pointer ....so at same adress
test this:
define variables as (global) integer value
no need to pass parameter to function void dht22_read
Code: Select all
int dht22_humi;
int dht22_temp;
// read humidity (in hundredths rH%) and temperature (in hundredths °Celsius) from sensor
void dht22_read()
{
// send start signal
DHT22_PIN = 0; // connection pin output low
DHT22_PIN_DIR = 0; // configure connection pin as output
delay_ms(25); // wait 25 ms
DHT22_PIN = 1; // connection pin output high
delay_us(30); // wait 30 us
DHT22_PIN_DIR = 1; // configure connection pin as input
// check sensor response
while( DHT22_PIN );
while(!DHT22_PIN );
while( DHT22_PIN );
// read data
*( dht22_humi) = dht22_read_byte(); // read humidity byte 1
*( dht22_humi+1) = dht22_read_byte(); // read humidity byte 2
*( dht22_temp) = dht22_read_byte(); // read temperature byte 1
*(dht22_temp+1) = dht22_read_byte(); // read temperature byte 2
dht22_read_byte(); // read checksum (skipped)
if(*dht22_temp & 0x8000) { // if temperature is negative
*dht22_temp &= 0x7FFF;
*dht22_temp *= -1;
}
}