// BASIC driven lcd display // updated for v1.445 // // Example lcd connections on port D // D1 LCD data line 4 pin 11 // D2 LCD data line 5 pin 12 // D3 LCD data line 6 pin 13 // D4 LCD data line 7 pin 14 // D5 LCD RS pin 4 // D6 LCD RW pin 5 // D7 LCD E pin 6 // LCD pin 1 GND // LCD pin 2 + 5V // LCD pin 3 GND (contrast) // constant LCD_PORT$ "d" constant LCD_RS_BIT 5 constant LCD_RW_BIT 6 constant LCD_E_BIT 7 constant LCD_BSY_MASK 0x10 // set to mask off all but bit 7 constant LCD_IO_MASK 0xfe // this should be all of the 7 port bits // D4 to D7 on LCD must be contiguous, data_start is the data bit of D4 on // the LCD, 5 will be the next bit etc. e.g. if lcd data lines are connected // to say RB4-7 then LCD_DATA_START will be 4 constant LCD_DATA_START 1 // value contains, in its lowest 4 bits the nibble to put on to the // data lines 1 to 4. As the data lines can be any 4 contiguous bits // the data needs to be shifted function lcd_nib(value) dim x portb LCD_PORT$+"os" LCD_E_BIT // set LCDe high x=lshift(0xf,LCD_DATA_START) // shift '1111' to data pos portw LCD_PORT$+"oc" x // clear nibble x=and(value,0xf) // mask off high nibble x=lshift(x,LCD_DATA_START) // move data up from base bit portw LCD_PORT$+"os" x // write to port portb LCD_PORT$+"oc" LCD_E_BIT // set LCDe low endf // outputs 8 bits as two nibbles function lcd_out(value) dim x x=rshift(value,4) // high nibble first lcd_nib(x) lcd_nib(value) wait 1 // uses small delay, testing busy flag would be better portb LCD_PORT$+"os" LCD_RS_BIT // default high endf function lcd_data(value) portb LCD_PORT$+"oc" LCD_RW_BIT // write lcd_out(value) endf function lcd_cmd(value) portb LCD_PORT$+"oc" LCD_RS_BIT // LCDrs+LCDrw portb LCD_PORT$+"oc" LCD_RW_BIT // LCDrs+LCDrw lcd_out(value) endf function lcd_init portw LCD_PORT$+"tc" LCD_IO_MASK // set bits 1-7 as o/p portb LCD_PORT$+"oc" LCD_RS_BIT // set RS,RW & E to low portb LCD_PORT$+"oc" LCD_RW_BIT portb LCD_PORT$+"oc" LCD_E_BIT // initialise for 4 bit operation lcd_nib(3): wait 60 lcd_nib(3): wait 60 lcd_nib(2) portb LCD_PORT$+"os" LCD_RS_BIT // LCDrs // put rs high lcd_cmd(0x28): wait 60 lcd_cmd(0x0e): wait 60 lcd_cmd(6): wait 60 lcd_cmd(1): wait 260 endf // ======= Start here for lcd interface ============ // sends string to lcd display function lcd_type(a$) dim j for j=1 to len(a$) lcd_data(asc(a$,j)) next endf