baileychic
Advanced Member level 3
I am having issues driving a 16x2 LCD.
Find the attached PDF. I have to write code for that LCD.
I am trying 4-bit mode.
Here is my code. All I get is black boxes on line 1. Line 2 is blank.
I ran LED blink and it runs fine and perfect.
Find the attached PDF. I have to write code for that LCD.
I am trying 4-bit mode.
Here is my code. All I get is black boxes on line 1. Line 2 is blank.
I ran LED blink and it runs fine and perfect.
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 #define _XTAL_FREQ 4000000UL // PIC18F4520 Configuration Bit Settings // 'C' source line config statements // CONFIG1H #pragma config OSC = XT // Oscillator Selection bits (XT oscillator) #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled) #pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled) // CONFIG2L #pragma config PWRT = ON // Power-up Timer Enable bit (PWRT enabled) #pragma config BOREN = ON // Brown-out Reset Enable bits (Brown-out Reset enabled and controlled by software (SBOREN is enabled)) #pragma config BORV = 2 // Brown Out Reset Voltage bits () // CONFIG2H #pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit)) #pragma config WDTPS = 1024 // Watchdog Timer Postscale Select bits (1:1024) // CONFIG3H #pragma config CCP2MX = PORTC // 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 Timer1 Oscillator Enable bit (Timer1 configured for higher power operation) #pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input 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 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) not code-protected) #pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) not code-protected) #pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) not code-protected) #pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) not code-protected) // CONFIG5H #pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected) #pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected) // CONFIG6L #pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) not write-protected) #pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) not write-protected) #pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) not write-protected) #pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) not write-protected) // CONFIG6H #pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected) #pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected) #pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected) // CONFIG7L #pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks) #pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks) #pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks) #pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks) // CONFIG7H #pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include <xc.h> #include <stdio.h> #include <stdlib.h> #define _LCD_FIRST_ROW 0x80 //Move cursor to the 1st row #define _LCD_SECOND_ROW 0xC0 //Move cursor to the 2nd row #define _LCD_THIRD_ROW 0x94 //Move cursor to the 3rd row #define _LCD_FOURTH_ROW 0xD4 //Move cursor to the 4th row #define _LCD_CLEAR 0x01 //Clear display #define _LCD_RETURN_HOME 0x02 //Return cursor to home position, returns a //shifted display to its original position. //Display data RAM is unaffected. #define _LCD_CURSOR_OFF 0x0C //Turn off cursor #define _LCD_UNDERLINE_ON 0x0E //Underline cursor on #define _LCD_BLINK_CURSOR_ON 0x0F //Blink cursor on #define _LCD_MOVE_CURSOR_LEFT 0x10 //Move cursor left without changing //display data RAM #define _LCD_MOVE_CURSOR_RIGHT 0x14 //Move cursor right without changing //display data RAM #define _LCD_TURN_ON 0x0C //Turn Lcd display on #define _LCD_TURN_OFF 0x08 //Turn Lcd display off #define _LCD_SHIFT_LEFT 0x18 //Shift display left without changing //display data RAM #define _LCD_SHIFT_RIGHT 0x1E //Shift display right without changing //display data RAM #define LCD_RS LATCbits.LATC0 #define LCD_EN LATCbits.LATC3 #define LCD_D4 LATCbits.LATC4 #define LCD_D5 LATCbits.LATC5 #define LCD_D6 LATCbits.LATC6 #define LCD_D7 LATCbits.LATC7 #define LCD_RS_Direction TRISCbits.TRISC0 #define LCD_EN_Direction TRISCbits.TRISC3 #define LCD_D4_Direction TRISCbits.TRISC4 #define LCD_D5_Direction TRISCbits.TRISC5 #define LCD_D6_Direction TRISCbits.TRISC6 #define LCD_D7_Direction TRISCbits.TRISC7 #define EN_DELAY 500 #define LCD_STROBE {LCD_EN = 1; __delay_us(EN_DELAY); LCD_EN = 0; __delay_us(EN_DELAY);}; #define LED_North LATAbits.LATA0 #define LED_East LATAbits.LATA1 #define LED_West LATAbits.LATA2 #define LED_South LATAbits.LATA3 const char txt1[] = "Project-1"; const char txt2[] = "Dam Gate Control"; char txt[32]; // copy const to ram string char * CopyConst2Ram(char *dest, const char *src){ char *d; d = dest; while(*src) { *dest++ = *src++; } return d; } void LCD_Cmd(char out_char) { LCD_RS = 0; LCD_EN = 1; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; //LCD_STROBE __delay_us(EN_DELAY); LCD_EN = 0; LCD_EN = 1; LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; __delay_us(EN_DELAY); LCD_EN = 0; //LCD_STROBE if(out_char == 0x01)__delay_ms(2); } void LCD_Chr(char row, char column, char out_char) { /* switch(row){ case 1: LCD_Cmd(0x00 + (column - 1)); break; case 2: LCD_Cmd(0x40 + (column - 1)); break; case 3: LCD_Cmd(0x54 + (column - 1)); break; case 4: LCD_Cmd(0x68 + (column - 1)); break; } */ switch(row){ case 1: LCD_Cmd(0x80 + (column - 1)); break; case 2: LCD_Cmd(0xC0 + (column - 1)); break; case 3: LCD_Cmd(0x94 + (column - 1)); break; case 4: LCD_Cmd(0xD4 + (column - 1)); break; } LCD_RS = 1; /* LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_EN = 1; LCD_STROBE */ LCD_EN = 1; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; //LCD_STROBE __delay_us(EN_DELAY); LCD_EN = 0; LCD_EN = 1; LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; __delay_us(EN_DELAY); LCD_EN = 0; //LCD_STROBE } void LCD_Chr_Cp(char out_char) { LCD_RS = 1; /* LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_EN = 1; LCD_STROBE */ LCD_EN = 1; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; //LCD_STROBE __delay_us(EN_DELAY); LCD_EN = 0; LCD_EN = 1; LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; __delay_us(EN_DELAY); LCD_EN = 0; //LCD_STROBE } void LCD_Init() { __delay_ms(200); LCD_RS_Direction = 0; LCD_EN_Direction = 0; LCD_D4_Direction = 0; LCD_D5_Direction = 0; LCD_D6_Direction = 0; LCD_D7_Direction = 0; LCD_RS = 0; LCD_EN = 0; LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; __delay_ms(60); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE __delay_ms(60); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE __delay_ms(60); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE __delay_ms(60); LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE __delay_ms(20); LCD_Cmd(0x28); LCD_Cmd(0x06); } void LCD_Out(char row, char col, char *text) { while(*text) LCD_Chr(row, col++, *text++); } void LCD_Out_Cp(char *text) { while(*text) LCD_Chr_Cp(*text++); } int main(int argc, char** argv) { // Write your code here CMCON = 0x07; ADCON1 = 0x0F; TRISA = 0x30; TRISC = 0x00; LCD_Init(); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Cmd(_LCD_CLEAR); LCD_Out(1,1,CopyConst2Ram(txt,txt1)); LCD_Out(2,1,CopyConst2Ram(txt,txt2)); while(1) { LED_North = ~LED_North; __delay_ms(500); } return (EXIT_SUCCESS); }