Sarit
Newbie level 2
- Joined
- Oct 24, 2014
- Messages
- 2
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 79
Hey there, I've been trying to interface a DHT11 sensor to a pic18F4550 and coding with xC8 compiler. Actually I've modified it from a website. I've successfully loaded a hex file into the MCU but the LCD only displays zero humidity and zero temperature. I've checked and my circuit has no problem. The DHT11 sensor is also working, I guessed the problem may be with the software... Can someone point out where I'm missing it. Thanks.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 /* MCU Definitions */ #include <p18cxxx.h> #include <string.h> #include <plib/delays.h> #include <stdio.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 // PLL Prescaler Selection bits (Divide by 5 (20 MHz oscillator input)) #pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2]) #pragma config USBDIV = 2 // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes from the 96 MHz PLL divided by 2) // CONFIG1H #pragma config FOSC = INTOSCIO_EC// Oscillator Selection bits (Internal oscillator, port function on RA6, EC used by USB (INTIO)) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled) #pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled) // CONFIG2L #pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config BOR = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software) #pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting) #pragma config VREGEN = OFF // USB Voltage Regulator Enable bit (USB voltage regulator disabled) // CONFIG2H #pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit)) #pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768) // CONFIG3H #pragma config CCP2MX = ON // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1) #pragma config PBADEN = OFF // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset) #pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation) #pragma config MCLRE = OFF // MCLR Pin Enable bit (RE3 input pin enabled; MCLR pin disabled) // CONFIG4L #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset) #pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled) #pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled) #pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode)) // CONFIG5L #pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected) #pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected) #pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected) #pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected) // CONFIG5H #pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected) #pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is not code-protected) // CONFIG6L #pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected) #pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected) #pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected) #pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected) // CONFIG6H #pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected) #pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected) #pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is not write-protected) // CONFIG7L #pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks) #pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks) #pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks) #pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks) // CONFIG7H #pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks) #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[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); } //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(n); //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); }
Last edited by a moderator: