patulong po.
thermal sensor po kasi using dht11 with pic 18f46k22, eh yung codes na nakita namin sa internet using xc8 compiler, tinry namin sa mp lab, andaming syntax error, pano po ba gagawin dun? di ako magaling sa coding eh, c language.
#define _XTAL_FREQ 4000000 //Chip FREQUENCY
/* DHT11 I/O Macros */
#ifndef DHT11
#define DHT11_TRIS TRISDbits.TRISD1
#define DHT11_PIN PORTDbits.RD1
#endif
/* Globals */
char dht_dat[10]; // Output data will be here
unsigned char GlobalErr = 0;
unsigned char Buffer[20];
unsigned char Buffer1[20];
unsigned char t[20];
unsigned char n[20];
/* Function Prototypes */
void dht_init(void);
unsigned char dht_poll(void);
void read_dht(void);
//a simple delay function
void Wait(unsigned int delay)//delay for 100ms
{
for(;delay;delay--)
__delay_us(100);
}
/* Functions */
void dht_init() {
Delay1KTCYx(5); // 1ms
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);
Delay10TCYx(15); //30us
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; //port value is low
Delay1KTCYx(90); // 18ms
DHT11_PIN = 1; //port value is high
Delay100TCYx(1); // 20-40us
DHT11_TRIS = 1; //make pin input
dht_in = DHT11_PIN;
if (dht_in) {
GlobalErr = 1;
return;
}
Delay100TCYx(4); // 80us
dht_in = DHT11_PIN;
if (!dht_in) {
GlobalErr = 2;
return;
}
Delay100TCYx(4); // 80us
for (i = 0; i < 5; i++) {
dht_dat
= 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);
}
//Function to Initialise the LCD module
void init_XLCD(void) //Initialize LCD display
{
OpenXLCD(FOUR_BIT & LINES_5X7); //configure LCD in 4-bit Data Interface mode
//and 5x7 characters, multiple line display
while(BusyXLCD()); //Check if the LCD controller is not busy
//before writing some commands?
WriteCmdXLCD(0x06); // move cursor right, don?t shift display
WriteCmdXLCD(0x0C); //turn display on without cursor
}
/* Main Application */
void main(void) {
unsigned char dht11_crc = 0;
ADCON1 |= 0x0F; // ADC All Digital
CMCON = 0x07; // Comparators off
//Let the LCD Module start up
Wait(1);
//Initialize the LCD Module
init_XLCD();
Wait(8); // Some Delay
dht_init(); // DHT11 PreInit
/*Main Loop*/
while (1) {
memset(dht_dat, 0x0, sizeof (dht_dat));
read_dht(); // Check dht_dat variable after this funciton
dht11_crc = dht_dat[0] + dht_dat[1] + dht_dat[2] + dht_dat[3]; // check check_sum
if (dht_dat[4] != dht11_crc || GlobalErr>0) {
sprintf(t,"CRC ERR :%02x!%02X ", dht_dat[4], dht11_crc);
sprintf(n,"Resp. Err:%d ", GlobalErr);
putrsXLCD(t); //Display humidity on the screen
while(BusyXLCD());
WriteCmdXLCD(0xC0);
while(BusyXLCD());
putsXLCD; //Display the Temperature on the screen
} else {
sprintf(Buffer,"HUMI RH :%d.%d %% ", dht_dat[0], dht_dat[1]);
sprintf(Buffer1,"TEMP :%d.%d %cC ", dht_dat[2], dht_dat[3], 223);
putrsXLCD(Buffer); //Display "Voltage" on the screen
while(BusyXLCD());
WriteCmdXLCD(0xC0);
while(BusyXLCD());
putsXLCD(Buffer1); //Display the Temperature on the screen
putrsXLCD(" "); // Clear after comma
WriteCmdXLCD(0x02);
}
}
Delay10KTCYx(250);
Delay10KTCYx(250); // Min. 1sn
//Delay10KTCYx(250);
//Delay10KTCYx(250); // 2sn Ideal Delay
} /* Main Loop Ends */
/* Main Applicaiton Ends */
void DelayFor18TCY(void){
Delay10TCYx(20);
}
void DelayPORXLCD(void){
Delay1KTCYx(30);
}
void DelayXLCD(void){
Delay1KTCYx(10);
}