0% found this document useful (0 votes)
2 views

capdh

The document contains a program for interfacing a DHT11 temperature and humidity sensor with a PIC16F877A microcontroller using mikroC. It includes the setup for LCD display connections, initialization, and functions to read data from the sensor, process it, and display the results. The program also handles errors such as checksum mismatches and sensor response failures.

Uploaded by

Hichem Guedri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

capdh

The document contains a program for interfacing a DHT11 temperature and humidity sensor with a PIC16F877A microcontroller using mikroC. It includes the setup for LCD display connections, initialization, and functions to read data from the sensor, process it, and display the results. The program also handles errors such as checksum mismatches and sensor response failures.

Uploaded by

Hichem Guedri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

/*******************************************************************************

Program for, "DHT11 interfacing with PIC16F877A"


Program written by_ Engr. Mithun K. Das
MCU: PIC16F877A; X-tal: 8MHz; mikroC pro for PIC v7.6.0
Date: 15-05-2020
*******************************************************************************/

// LCD module connections


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 of LCD initialization

sbit Data at RB1_bit;


sbit DataDir at TRISB1_bit;

char message[] = "00.0";


unsigned short TOUT = 0, CheckSum, i;
unsigned int humidity,value;
unsigned int T_Byte1, T_Byte2, RH_Byte1, RH_Byte2;

void StartSignal()
{
DataDir = 0; // Data port is output
Data = 0;
Delay_ms(25);
Data = 1;
Delay_us(30);
DataDir = 1; // Data port is input
}

unsigned short CheckResponse()


{
TOUT = 0;
TMR2 = 0;
T2CON.TMR2ON = 1; // start timer
while(!Data && !TOUT);
if (TOUT) return 0;
else
{
TMR2 = 0;
while(Data && !TOUT);
if (TOUT) return 0;
else
{
T2CON.TMR2ON = 0;
return 1;
}
}
}

unsigned short ReadByte()


{
unsigned short num = 0, t;
DataDir = 1;
for (i=0; i<8; i++)
{
while(!Data);
Delay_us(40);
if(Data) num |= 1<<(7-i);
while(Data);
}
return num;
}

void Interrupt() iv 0x0004 ics ICS_AUTO


{
if(PIR1.TMR2IF)
{
TOUT = 1;
T2CON.TMR2ON = 0; // stop timer
PIR1.TMR2IF = 0; // Clear TMR0 interrupt flag
}
}

void main()
{
// port initialization
TRISB = 0b00000010;
PORTB = 0;

CMCON = 7;// comparator off


ADCON1 = 0x07;// ADC off

INTCON.GIE = 1; //Enable global interrupt


INTCON.PEIE = 1; //Enable peripheral interrupt
// Configure Timer2 module
PIE1.TMR2IE = 1; // Enable Timer2 interrupt
T2CON = 0; // Prescaler 1:1, and Timer2 is off initially
PIR1.TMR2IF =0; // Clear TMR INT Flag bit
TMR2 = 0;

Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"DHT11 WITH");
Lcd_Out(2,1,"PIC16F877A");
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);

while(1)
{

unsigned short check;


PIE1.TMR2IE = 1; // Enable Timer2 interrupt
ADCON1 = 0x07; // all digital selected
StartSignal();
check = CheckResponse();
if (!check)
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1, 1, "No response");
Lcd_Out(2, 1, "from the sensor");
}
else
{
RH_Byte1 = ReadByte();
RH_Byte2 = ReadByte();
T_Byte1 = ReadByte();
T_Byte2 = ReadByte();
CheckSum = ReadByte();
// Check for error in Data reception
if (CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF))
{
Lcd_Out(1,1,"HUMIDITY:");
Lcd_Out(2,1,"TEMP:");

message[0] = RH_Byte1/10 + 48;


message[1] = RH_Byte1%10 + 48;
message[3] = RH_Byte2%10 + 48;
Lcd_Out(1,11, message);
Lcd_Out(1,16,"%");

message[0] = T_Byte1/10 + 48;


message[1] = T_Byte1%10 + 48;
message[3] = T_Byte2%10 + 48;
Lcd_Out(2,11, message);
Lcd_Chr_CP(223);
Lcd_Out(2,16,"C");

Delay_ms(2000);
}
else
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1, 1, "Checksum Error!");
Lcd_Out(2, 1, "Trying Again...");
}
}
PIE1.TMR2IE = 0; // disable Timer2 interrupt

}// while(1)
}// void

You might also like