Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
How it is working i have book of pic16f877a ..
40 bits data right?
Here is the code want to display on 7 segmentLast time I looked you were coding in C, so don't know why you are struggling with a Basic project.
If you simply google on "dht11 with Pic micro" there are lots of dht11 projects written in C with good explantions of how it works; here's just 3 I found straight away.
http://embedded-lab.com/blog/?p=4333
**broken link removed**
**broken link removed**
bit Data at RA0_bit;
sbit DataDir at TRISA0_bit;
char message1[] = "Temp = 00.0 C";
char message2[] = "RH = 00.0 %";
unsigned short TOUT = 0, CheckSum, i;
unsigned short T_Byte1, T_Byte2, RH_Byte1, RH_Byte2;
void StartSignal(){
DataDir = 0; // Data port is output
Data = 0;
Delay_ms(25); // Low for at least 18us
Data = 1;
Delay_us(30); // High for 20-40 us
DataDir = 1; // Data port is input
}
unsigned short CheckResponse(){
TOUT = 0;
TMR2 = 0;
T2CON.TMR2ON = 1; // Start TMR2 while waiting for sensor response
while(!Data && !TOUT); // If there's no response within 256us, the Timer2 overflows
if (TOUT) return 0; // and exit
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);
TMR2 = 0;
T2CON.TMR2ON = 1; // Start TMR2 from 0 when a low to high data pulse
while(Data); // is detected, and wait until it falls low again.
T2CON.TMR2ON = 0; // Stop the TMR2 when the data pulse falls low.
if(TMR2 > 40) num |= 1<<(7-i); // If time > 40us, Data is 1
}
return num;
}
void interrupt(){
if(PIR1.TMR2IF){
TOUT = 1;
T2CON.TMR2ON = 0; // stop timer
PIR1.TMR2IF = 0; // Clear TMR0 interrupt flag
}
}
void main() {
unsigned short check;
TRISB = 0b00000000;
PORTB = 0;
TRISA = 0b00100001;
CMCON = 7;
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);
do {
Delay_ms(1000);
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))
{
message1[7] = T_Byte1/10 + 48;
message1[8] = T_Byte1%10 + 48;
message1[10] = T_Byte2/10 + 48;
message2[7] = RH_Byte1/10 + 48;
message2[8] = RH_Byte1%10 + 48;
message2[10] = RH_Byte2/10 + 48;
message1[11] = 223; // Degree symbol
Lcd_Cmd(_Lcd_Clear);
Lcd_Out(1, 1, message1);
Lcd_Out(2, 1, message2);
}
else{
Lcd_Cmd(_Lcd_Clear);
Lcd_Out(1, 1, "Checksum Error!");
Lcd_Out(2, 1, "Trying Again ...");
}
}
}while(1);
}
Here is the code want to display on 7 segment .Last time I looked you were coding in C, so don't know why you are struggling with a Basic project.
If you simply google on "dht11 with Pic micro" there are lots of dht11 projects written in C with good explantions of how it works; here's just 3 I found straight away.
http://embedded-lab.com/blog/?p=4333
**broken link removed**
**broken link removed**
bit Data at RA0_bit;
sbit DataDir at TRISA0_bit;
char message1[] = "Temp = 00.0 C";
char message2[] = "RH = 00.0 %";
unsigned short TOUT = 0, CheckSum, i;
unsigned short T_Byte1, T_Byte2, RH_Byte1, RH_Byte2;
void StartSignal(){
DataDir = 0; // Data port is output
Data = 0;
Delay_ms(25); // Low for at least 18us
Data = 1;
Delay_us(30); // High for 20-40 us
DataDir = 1; // Data port is input
}
unsigned short CheckResponse(){
TOUT = 0;
TMR2 = 0;
T2CON.TMR2ON = 1; // Start TMR2 while waiting for sensor response
while(!Data && !TOUT); // If there's no response within 256us, the Timer2 overflows
if (TOUT) return 0; // and exit
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);
TMR2 = 0;
T2CON.TMR2ON = 1; // Start TMR2 from 0 when a low to high data pulse
while(Data); // is detected, and wait until it falls low again.
T2CON.TMR2ON = 0; // Stop the TMR2 when the data pulse falls low.
if(TMR2 > 40) num |= 1<<(7-i); // If time > 40us, Data is 1
}
return num;
}
void interrupt(){
if(PIR1.TMR2IF){
TOUT = 1;
T2CON.TMR2ON = 0; // stop timer
PIR1.TMR2IF = 0; // Clear TMR0 interrupt flag
}
}
void main() {
unsigned short check;
TRISB = 0b00000000;
PORTB = 0;
TRISA = 0b00100001;
CMCON = 7;
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);
do {
Delay_ms(1000);
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))
{
message1[7] = T_Byte1/10 + 48;
message1[8] = T_Byte1%10 + 48;
message1[10] = T_Byte2/10 + 48;
message2[7] = RH_Byte1/10 + 48;
message2[8] = RH_Byte1%10 + 48;
message2[10] = RH_Byte2/10 + 48;
message1[11] = 223; // Degree symbol
Lcd_Cmd(_Lcd_Clear);
Lcd_Out(1, 1, message1);
Lcd_Out(2, 1, message2);
}
else{
Lcd_Cmd(_Lcd_Clear);
Lcd_Out(1, 1, "Checksum Error!");
Lcd_Out(2, 1, "Trying Again ...");
}
}
}while(1);
}
OK, are you using Oshonsoft IDE? If so there isn't much difference between PIC and AVR versions of the program to read the DHT11/22.
I used Servoin to "catch" the positive going pulses with an 8 bit loop. Then I evaluated them for their length to see if they are 1 or 0, then worked out the Humidity/temperature.
I didn't use a 16 bit loop because Oshonsoft IDE doesn't seem to work with variables in the bit location for a Word. I.e. Word.j = x(j). Whereas Byte.j = x(j) does work.
I didn't bother with the check sum, that's only of importance where temperature/humidity is in a critical location, if it fails a warning can be given.
Do you want to work it out for yourself, or do you want me to list my program?
What code?? Basic code is included in post #7Hello,
Can u help me with code.
Hello,
Can u help me with code.