CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

INTERFACE DHT11 PIC16F876 PLACE I HAVE PROBLEM MESUREMENT

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
GAMCH96



Joined: 13 Aug 2025
Posts: 3

View user's profile Send private message

INTERFACE DHT11 PIC16F876 PLACE I HAVE PROBLEM MESUREMENT
PostPosted: Wed Aug 13, 2025 9:10 am     Reply with quote

// Interfacing PIC16F877A with DHT11 sensor
// http://ccspicc.blogspot.com/
// electronnote@gmail.com

//LCD module connections PUERTO B
#define LCD_RS_PIN PIN_B0
#define LCD_RW_PIN PIN_B1
#define LCD_ENABLE_PIN PIN_B2
#define LCD_DATA4 PIN_B3
#define LCD_DATA5 PIN_B4
#define LCD_DATA6 PIN_B5
#define LCD_DATA7 PIN_B6
//End LCD module connections

#include <16F876.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 8000000)
#use rs232 (BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7) // Tx a 9600 BAUDS VIA RS-232 STANDAR
#include <lcd4x20b.c>
#use fast_io(A)
// Connection pin between PIC16F877A and DHT11 (RHT01) sensor ENTRADA RA0
#BIT Data_Pin = 0x05.0 // Pin mapped to PORTA.0
#BIT Data_Pin_Direction = 0x85.0 // Pin direction mapped to TRISA.0

char message1[] = "Temp = 00.0 C";
char message2[] = "RH = 00.0 %";
short Time_out ;
unsigned int8 T_byte1, T_byte2, RH_byte1, RH_byte2, CheckSum ;
unsigned int16 Temp, RH;

void start_signal()

{
Data_Pin_Direction = 0; // Configure connection pin as output
Data_Pin = 0; // Connection pin output low
delay_ms(25);
Data_Pin = 1; // Connection pin output high
delay_us(30);
Data_Pin_Direction = 1; // Configure connection pin as input
}
short check_response()

{

delay_us(40);
if(!Data_Pin)
{ // Read and test if connection pin is low
delay_us(80);
if(Data_Pin)
{ // Read and test if connection pin is high
delay_us(50);
return 1;
}
}
}
unsigned int8 Read_Data()
{
unsigned int8 i, k, _data = 0; // k is used to count 1 bit reading duration
if(Time_out)
break;
for(i = 0; i < 8; i++)
{
k = 0;
while(!Data_Pin)
{ // Wait until pin goes high
k++;
if (k > 100)

{

Time_out = 1; break;

}
delay_us(1);
}
delay_us(30);
if(!Data_Pin)
bit_clear(_data, (7 - i)); // Clear bit (7 - i)
else
{
bit_set(_data, (7 - i)); // Set bit (7 - i)
while(Data_Pin)
{ // Wait until pin goes low
k++;
if (k > 100) {Time_out = 1; break;}
delay_us(1);
}
}
}
return _data;
}
void main()

{

setup_adc_ports(NO_ANALOGS); // Configure AN pins as digital
lcd_init(); // Initialize LCD module
lcd_putc('\f'); // LCD clear
while(TRUE)
{
delay_ms(1000);
Time_out = 0;
Start_signal();
if(check_response())
{ // If there is response from sensor
RH_byte1 = Read_Data(); // read RH byte1
RH_byte2 = Read_Data(); // read RH byte2
T_byte1 = Read_Data(); // read T byte1
T_byte2 = Read_Data(); // read T byte2
Checksum = Read_Data(); // read checksum
if(Time_out)
{ // If reading takes long time
lcd_putc('\f'); // LCD clear
lcd_gotoxy(5, 1); // Go to column 5 row 1
lcd_putc("Time out!");
}
else
{
if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF)){
RH = RH_byte1;
RH = (RH << 8) | RH_byte2;
Temp = T_byte1;
Temp = (Temp << 8) | T_byte2;
if (Temp > 0X8000){
message1[6] = '-';
Temp = Temp & 0X7FFF; }
else

message1[6] = ' ';
message1[7] = (Temp / 10) % 10 + 48;
message1[8] = (Temp / 10) % 10 + 48;
message1[10] = Temp % 10 + 48;

message1[6] = ' ';
message2[7] = (RH / 10) % 10 + 48;
message2[8] = (RH / 10) % 10 + 48;
message2[10] = RH % 10 + 48;
message1[11] = 223;

// Degree symbol
lcd_putc('\f'); // LCD clear
lcd_gotoxy(1, 1); // Go to column 1 row 1
printf(lcd_putc, message1); // Display message1
lcd_gotoxy(1, 2); // Go to column 1 row 2
printf(lcd_putc, message2); // Display message2

// Envio RS232
// Envio RS232
printf("%c%c.%c,",message1[7],message1[8],message1[10]);
printf("%c%c.%c,",message2[7],message2[8],message2[10]);
printf("\n\r"); // Display message2


}
}
}
}
}

However, as you can see in the photograph, the result is incorrect. The intended result is to obtain the temperature and humidity values, as shown below, with a variation in the LCD text:
Temp = 22.7 C
RH = 66.3%

I'm trying to figure out what I'm doing incorrectly in the conversion operations. Is it something I can't understand, or am I using the printf(lcd_putc,)-… command incorrectly? I don't know???

Please... Any help you can give me on the correct way to write the code submitted for your consideration, I would appreciate it.

Without further ado, please accept greetings from Mexico.

Thank you for your attention.

Gustavo Adolfo Martinez Chavez.
Ttelmah



Joined: 11 Mar 2010
Posts: 19928

View user's profile Send private message

PostPosted: Thu Aug 14, 2025 2:15 am     Reply with quote

Of course it is..... Sad

Look at your code:

message1[7] = (Temp / 10) % 10 + 48;
message1[8] = (Temp / 10) % 10 + 48;

message2[7] = (RH / 10) % 10 + 48;
message2[8] = (RH / 10) % 10 + 48;

You have exactly the same maths being performed for the 7th and
eight characters. Result the same value in each character....

You need:
/100
/10
/1 (not actually needed).

For the three characters.
GAMCH96



Joined: 13 Aug 2025
Posts: 3

View user's profile Send private message

PostPosted: Thu Aug 14, 2025 11:45 am     Reply with quote

Thank you, I am so grateful.. Laughing Razz
GAMCH96



Joined: 13 Aug 2025
Posts: 3

View user's profile Send private message

PostPosted: Thu Aug 14, 2025 12:00 pm     Reply with quote

Thank you for you help...

And the end code.....



//LCD module connections PUERTO B
#define LCD_RS_PIN PIN_B0
#define LCD_RW_PIN PIN_B1
#define LCD_ENABLE_PIN PIN_B2
#define LCD_DATA4 PIN_B3
#define LCD_DATA5 PIN_B4
#define LCD_DATA6 PIN_B5
#define LCD_DATA7 PIN_B6
//End LCD module connections

#include <16F876.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 8000000)
#use rs232 (BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7) // Tx a 9600 BAUDS VIA RS-232 STANDAR
#include <lcd4x20b.c>
#use fast_io(A)
// Connection pin between PIC16F877A and DHT11 (RHT01) sensor ENTRADA RA0
#BIT Data_Pin = 0x05.0 // Pin mapped to PORTA.0
#BIT Data_Pin_Direction = 0x85.0 // Pin direction mapped to TRISA.0

char message1[] = "Temp = 00.0 C";
char message2[] = "RH = 00.0 %";
short Time_out ;
unsigned int8 T_byte1, T_byte2, RH_byte1, RH_byte2, CheckSum ;
unsigned int16 Temp, RH;

void start_signal()

{
Data_Pin_Direction = 0; // Configure connection pin as output
Data_Pin = 0; // Connection pin output low
delay_ms(25);
Data_Pin = 1; // Connection pin output high
delay_us(30);
Data_Pin_Direction = 1; // Configure connection pin as input
}
short check_response()

{

delay_us(40);
if(!Data_Pin)
{ // Read and test if connection pin is low
delay_us(80);
if(Data_Pin)
{ // Read and test if connection pin is high
delay_us(50);
return 1;
}
}
}
unsigned int8 Read_Data()
{
unsigned int8 i, k, _data = 0; // k is used to count 1 bit reading duration
if(Time_out)
break;
for(i = 0; i < 8; i++)
{
k = 0;
while(!Data_Pin)
{ // Wait until pin goes high
k++;
if (k > 100)

{

Time_out = 1; break;

}
delay_us(1);
}
delay_us(30);
if(!Data_Pin)
bit_clear(_data, (7 - i)); // Clear bit (7 - i)
else
{
bit_set(_data, (7 - i)); // Set bit (7 - i)
while(Data_Pin)
{ // Wait until pin goes low
k++;
if (k > 100) {Time_out = 1; break;}
delay_us(1);
}
}
}
return _data;
}
void main()

{

setup_adc_ports(NO_ANALOGS); // Configure AN pins as digital
lcd_init(); // Initialize LCD module
lcd_putc('\f'); // LCD clear
while(TRUE)
{
delay_ms(1000);
Time_out = 0;
Start_signal();
if(check_response())
{ // If there is response from sensor
RH_byte1 = Read_Data(); // read RH byte1
RH_byte2 = Read_Data(); // read RH byte2
T_byte1 = Read_Data(); // read T byte1
T_byte2 = Read_Data(); // read T byte2
Checksum = Read_Data(); // read checksum
if(Time_out)
{ // If reading takes long time
lcd_putc('\f'); // LCD clear
lcd_gotoxy(5, 1); // Go to column 5 row 1
lcd_putc("Time out!");
}
else
{
if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF)){
RH = RH_byte1;
RH = (RH << 8) | RH_byte2;
Temp = T_byte1;
Temp = (Temp << 8) | T_byte2;
if (Temp > 0X8000){
message1[6] = '-';
Temp = Temp & 0X7FFF; }
else

message1[6] = ' ';
message1[7] = (Temp / 10) % 10 + 48;
Temp=Temp%100;
message1[8] = (Temp / 10) % 10 + 48;
Temp=Temp%10;
message1[10] = Temp % 10 + 48;

message1[6] = ' ';
message2[7] = (RH / 10) % 10 + 48;
RH=RH%100;
message2[8] = (RH / 10) % 10 + 48;
RH=RH%10;
message2[10] = RH % 10 + 48;
message1[11] = 223;

// Degree symbol
lcd_putc('\f'); // LCD clear
lcd_gotoxy(1, 1); // Go to column 1 row 1
printf(lcd_putc, message1); // Display message1
lcd_gotoxy(1, 2); // Go to column 1 row 2
printf(lcd_putc, message2); // Display message2

// Envio RS232
// Envio RS232
printf("%c%c.%c,",message1[7],message1[8],message1[10]);
printf("%c%c.%c,",message2[7],message2[8],message2[10]);
printf("\n\r"); // Display message2

delay_ms(2000);
}
}
}
}
} Smile Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group