xabia
Newbie level 3
hi all i am using c18 picdem z lcd18
i cant get my lcd working display out
i had do a online search but simply cant work
Attached is my datasheet for the LCDView attachment LCD.pdf
Appreciate your help.
thx
i cant get my lcd working display out
i had do a online search but simply cant work
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 #include <p8h> #include <portb.h> #include <delays.h> #pragma config OSC = XT #pragma config WDT = OFF #pragma config LVP = OFF #pragma config DEBUG = ON #pragma config PBADEN = OFF // PORTB<4:0> pins are configured as digital I/O on Reset) #define TRIS_RW TRISDbits.TRISD5 /* TRIS for RW */ #define TRIS_RS TRISDbits.TRISD4 /* TRIS for RS */ #define TRIS_E TRISDbits.TRISD6 /* TRIS for E */ #define RW_PIN LATDbits.LATD5 /* PORT for RW */ #define RS_PIN LATDbits.LATD4 /* PORT for RS */ #define E_PIN LATDbits.LATD6 /* PORT for E */ #define DB7 LATDbits.LATD3 #define DB6 LATDbits.LATD2 #define DB5 LATDbits.LATD1 #define DB4 LATDbits.LATD0 // PORTD0 - DB4 // PORTD1 - DB5 // PORTD2 - DB6 // PORTD3 - DB7 // PORTD4 - RS // PORTD5 - RW // PORTD6 - EN #define SWITCH PORTBbits.RB4 #define SWITCH_1 PORTBbits.RB5 #define LED1 PORTAbits.RA0 #define LED2 PORTAbits.RA1 void lcd_send(far rom char *str); void delay_20us(unsigned int); void sendChar(unsigned char); void clr_screen(void); void clr_scr(unsigned char); void send_cmd(unsigned char, unsigned char); void welcome_title(void); void send_lcd(unsigned char, unsigned char); void write_data(far rom char *str, unsigned char); void lcd_init(void); void line_no(int); void nop(void); void clear_lcd(int); void latch_E(void); void delay_ms(unsigned int duration); void display_lcd(void); /* global variables */ void main(void) { INTCON2bits.RBPU=0; //Enable pull up for PB ADCON1=0x0f; //Digital output LATB=0b00000000; //Clear portb LATA=0b00000000; //Clear porta TRISB=0b00110000; //PORTB BIT4 and 5 AS INPUT, THE OTHERS ARE OUTPUT TRISA=0b00000000; //PORTA OUTPUT, LED TRISD=0x00; //PORTD is output, LCD TRISE=0x00; //PORTE PSP not active LATD=0x00; // initialisation start Delay10TCYx(200); lcd_init(); delay_ms(15); //initialisation end welcome_title(); while(1) { if(SWITCH==0 & SWITCH_1==0) { //LED1=0, LED2=0; //Clear both LED LATA = 0x00; } else if(SWITCH==0) //RB4 button { //LED1=1, LED2=0; //RA0 LED true LATA = 0x01; clr_scr(3); } else if(SWITCH_1==0) //RB5 button { //RA1 LED true LATA = 0x02; write_data("AA", 1); } } } void lcd_init(void) // 4bit mode { //function set LATD=0b00000000; Delay10TCYx(500); send_cmd(0b00110000, 0); Delay10TCYx(500); send_cmd(0b00110000, 0); Delay10TCYx(500); send_cmd(0b00110000, 0); // 2 lines, 5x8 dots Delay10TCYx(500); send_cmd(0b00110000, 0); // 2 lines, 5x8 dots send_cmd(0b00110000, 0); // 2 lines, 5x8 dots // display on/off control send_cmd(0x28, 0); //send_cmd(0b11100000, 0); // Display on, cursor on, blink off send_cmd(0x0F, 0); // Display on, cursor off, blink off // Entry mode send_cmd(0x01, 0); send_cmd(0x0C, 0);// increment mode, shift off } void enable(void) { E_PIN = 1; Delay10TCYx(1); E_PIN = 0; } void send_cmd(unsigned char b, unsigned char c) { unsigned int tempdata=b; RS_PIN = c; //Command mode = 0 //To write address = 1 LATD = ((LATD & 0xF0) | ((b >> 4) & 0x0F)) ; latch_E(); delay_ms(10); } void lcd_send(far rom char *str) { int index = 0; while (str[index] != '\0') { sendChar(str[index]); index++; } } void clr_screen(void) { int index = 0; //for (index=0; index<16; index++) while (index != 16) { sendChar(' '); // write blank index++; } } void clr_scr(unsigned char b) { unsigned char index =0; //send_lcd (0x80,0); if ((b == 1) || (b == 3)) { line_no(1); while (index != 16) { sendChar(' '); // write blank index++; } } if ((b == 2) || (b ==3) ) { //send_lcd (0xC0,0); line_no(2); index=0; while (index != 16) { sendChar(' '); // write blank index++; } } } void sendChar(unsigned char b) { RS_PIN = 1; DB7 = (b >> 7) & 1; DB6 = (b >> 6) & 1; DB5 = (b >> 5) & 1; DB4 = (b >> 4) & 1; latch_E(); DB7 = (b >> 3) & 1; DB6 = (b >> 2) & 1; DB5 = (b >> 1) & 1; DB4 = (b & 1); latch_E(); Delay10TCYx(80); } void line_no(int b) { if (b == 2) { send_lcd(0b11000000,0); //1100 0000 - move cursor to 2nd line } else { send_lcd(0b10000000,0); //1000 0000 - move cursor to 1st line } } void latch_E(void) { E_PIN=1; Delay10TCYx(1); E_PIN=0; } /* write data on lcd */ void send_lcd(unsigned char b,unsigned char rs_con) { unsigned int tempdata = b; RS_PIN = rs_con; // set RS //0 = set address //1 = write data RW_PIN = 0; // clear RW - write mode LATD = ((LATD & 0xF0) | ((b >> 4) & 0x0F)) ; //DelayFor18TCY(); latch_E(); LATD = (LATD & 0xF0) | (tempdata & 0x0F); Delay100TCYx(2); latch_E(); Delay1KTCYx(2); // alternate to check busy flag.. if lcd doesnt work properly, inc/dec this delay } /* Welcome*/ void welcome_title(void) { unsigned char index; unsigned char a[16] = "Hello"; unsigned char b[16] = "123"; index=0; //send_lcd (0x80,0); line_no(1); while (a[index] != '\0') { send_lcd (a[index],1); index++; } //send_lcd (0xC0,0); line_no(2); index=0; while (b[index] != '\0') { send_lcd (b[index],1); index++; } } void write_data(far rom char *str, unsigned char c) { unsigned char index; index =0; //send_lcd (0x80,0); if (c == 2) { clr_scr(2); line_no(2); } else { clr_scr(1); line_no(1); } while (str[index] != '\0') { send_lcd (str[index],1); index++; } } void delay_ms(unsigned int duration) // delay in miliseconds for 4.0MHZ crystal { unsigned int i; for(;duration!=0;duration--) { for(i=0;i<=50;i++) { _asm nop nop nop _endasm } _asm nop nop _endasm } } void nop(void) { _asm nop _endasm } void delay_20us(unsigned int duration) // delay in 20u for 4.0MHZ crystal { unsigned int i; for(;duration!=0;duration--) { _asm nop _endasm } }
Attached is my datasheet for the LCDView attachment LCD.pdf
Appreciate your help.
thx
Last edited by a moderator: