Vote count:
0
I got a code to interface DHT11 temperature humidity sensor with a pic18f4550 from http://www.expkits.com. I've modified it to work with xC8 compiler. The hex file builds successfully but on the LCD both temperature and humidity reads zero. I've tried all tricks to verify the DHT sensor is in good condition and I somehow feel that the problem may be with the code. I've attached my code below. I'm a beginner in systems programming, I will appreciate any help, thanks in advance.
#include <p18cxxx.h>
#include <plib/delays.h>
#include <stdio.h>
#include <string.h>
#include <plib/xlcd.h>
// PIC18F4550 Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1L
#pragma config PLLDIV = 5
#pragma config CPUDIV = OSC1_PLL2
#pragma config USBDIV = 2
// CONFIG1H
#pragma config FOSC = INTOSCIO_EC
#pragma config FCMEN = OFF
#pragma config IESO = OFF
// CONFIG2L
#pragma config PWRT = OFF
#pragma config BOR = OFF
#pragma config BORV = 3
#pragma config VREGEN = OFF
// CONFIG2H
#pragma config WDT = OFF
#pragma config WDTPS = 32768
// CONFIG3H
#pragma config CCP2MX = ON
#pragma config PBADEN = OFF
#pragma config LPT1OSC = OFF
#pragma config MCLRE = OFF
// CONFIG4L
#pragma config STVREN = ON
#pragma config LVP = OFF
#define _XTAL_FREQ 4000000 // SET THIS TO SUIT YOUR FREQUENCY
/* DHT11 I/O Macros */
#ifndef DHT11
#define DHT11_TRIS TRISDbits.RD1
#define DHT11_PIN PORTDbits.RD1
#endif
/* Globals */
char dht_dat[10];
unsigned char GlobalErr = 0;
unsigned char buf[20];
unsigned char buf1[20];
unsigned char buf2[20];
unsigned char buf3[20];
/* Function Prototypes */
void dht_init(void);
unsigned char dht_poll(void);
void read_dht(void);
/* Functions */
void dht_init() {
Delay10TCYx(25);
DHT11_TRIS = 0;
DHT11_PIN = 1;
}
unsigned char dht_poll() {
unsigned char i = 0;
unsigned char result = 0;
DHT11_TRIS = 1;
for (i = 0; i < 8; i++) {
while (DHT11_PIN == 0);
Delay1TCYx(5);
if (DHT11_PIN == 1) {
result |= (1 << (7 - i));
}
while (DHT11_PIN == 1;
}
return result;
}
void read_dht() {
unsigned char dht_in;
unsigned char i;
GlobalErr = 0;
dht_init();
DHT11_PIN = 0;
Delay100TCYx(45);
DHT11_PIN = 1;
Delay1TCYx(5);
DHT11_TRIS = 1;
dht_in = DHT11_PIN;
if (dht_in) {
GlobalErr = 1;
return;
}
Delay1TCYx(20);
dht_in = DHT11_PIN;
if (!dht_in) {
GlobalErr = 2;
return;
}
Delay1TCYx(20);
for (i = 0; i < 5; i++) {
dht_dat[i] = dht_poll();
}
DHT11_PIN = 1;
DHT11_TRIS = 0;
}
/* _user_putc()
* Check for more information :
* C18 Standart Libraries "Output Streams"
*/
int _user_putc(char c) {
putcXLCD(c);
}
void init_XLCD(void) //Initialize LCD display
{
OpenXLCD(FOUR_BIT & LINES_5X7);
while(BusyXLCD());
WriteCmdXLCD(0x06);
WriteCmdXLCD(0x0C);
}
/* Main Application */
void main(void) {
ADCON1 |= 0x0F; // ADC All Digital
CMCON = 0x07; // Comparators off
Delay1KTCYx(75); // 300ms
init_XLCD(); // Init LCD
Delay100TCYx(75); // 30ms
putrsXLCD("......WAIT......");
while(BusyXLCD());
Delay10KTCYx(50);
WriteCmdXLCD(0x01);
Delay100TCYx(75);
dht_init();
/*Main Loop*/
while (1) {
memset(dht_dat, 0x0, sizeof (dht_dat));
read_dht();
sprintf(buf2,"HUMI RH :%d.%d %% ", dht_dat[0], dht_dat[1]);
sprintf(buf3,"TEMP :%d.%d %cC ", dht_dat[2], dht_dat[3], 223);
putrsXLCD(buf2);
while(BusyXLCD());
WriteCmdXLCD(0xC0);
while(BusyXLCD());
putrsXLCD(buf3);
Delay10KTCYx(25);
//Delay10KTCYx(250);
//Delay10KTCYx(250);
} /* Main Loop Ends */
}/* Main Applicaiton Ends */
void DelayFor18TCY(void){
Delay10TCYx(20);
}
void DelayPORXLCD(void){
Delay1KTCYx(30);
}
void DelayXLCD(void){
Delay1KTCYx(10);
}
asked 1 min ago
Interfacing DHT11 temperature humidity sensor with pic
Aucun commentaire:
Enregistrer un commentaire