Welcome! Log In Create A New Profile

Advanced

Library for BV502 and 1602 LCD using i2c interface

Posted by Acuario 
Library for BV502 and 1602 LCD using i2c interface
January 12, 2015 07:12AM
Hi,
for anyone who may find this useful the code below is for driving a 2 line 16 character lcd display using an i2c interface based on the PCF8574 8 bit i/o expander.

// Driver code for 1602 LCD using i2c interface
// Default A0=A1=A2=0 giving I2C address = 0x20
// P0 = LCD D4
// P1 = LCD D5
// P2 = LCD D6
// P3 = LCD D7
// P4 = LCD E
// P5 = LCD R/W
// P6 = LCD RS
// P7 = LCD backlight (-ve logic)
constant D4 0x10
constant D5 0x20
constant D6 0x40
constant D7 0x80
constant E 0x4
constant RW 0x2
constant RS 0x1
constant BL 0x8

constant BLINK 1
constant CURSOR 2
constant DATA 1
constant COMMAND 0
constant ON 1
constant OFF 0
constant data_pins {0x10,0x20,0x40,0x80}
constant BL_ON 0x8
constant BL_OFF 0
dim backLightState //Store backlight state

// *******************************************************************
// Pulse the data to the lcd
// *******************************************************************
function lcd_pulse(nibble)
i2c_putc(nibble | E)
i2c_putc(nibble & ~ E) // latch it in
endf

// *******************************************************************
// writes nibble to the 4 bits of the lcd port
// *******************************************************************
function lcd_write4(value, mode)
dim pinValue = 0
dim i

// Map the value to LCD pin mapping
// --------------------------------
for i = 0 to 3
if (( value & 0x1 ) = 1 )
pinValue = pinValue + data_pins(i)
endif
value = ( value >> 1 )
next

pinValue = pinValue | mode | backLightState
lcd_pulse ( pinValue )
endf

// *******************************************************************
// sends data (8 bytes) to the 4 bit lcd port
// *******************************************************************
function lcd_write(d, mode)
lcd_write4(d >> 4, mode) // high nibble first
lcd_write4(d, mode)
wait(10) // no LCD WR line do delay needed - may vary
endf

// *******************************************************************
// initiaalise display
// *******************************************************************
function lcd_init()
lcd_write(3, COMMAND) // still in 8 bit mode
wait(20)
lcd_write(3, COMMAND) // again
wait(20)
lcd_write(2, COMMAND) // 4 bit mode
wait(20)
lcd_write(0x28, COMMAND);
wait(10);
lcd_write(0x08, COMMAND); // display off
wait(10);
lcd_write(0x0c, COMMAND); // display on, blink off, cursor off
wait(10);
lcd_write(0x06, COMMAND); // entry mode
wait(10);
endf

// =============================================================================
// Lcd commands
// *****************************************************************************
// Turn display backlight on/off
// *****************************************************************************
function lcd_backlight(state)
if state = ON
backLightState = BL_ON
i2c_putc(BL)
else
backLightState = BL_OFF
i2c_putc(0)
endif
endf

// *****************************************************************************
// clear display
// *****************************************************************************
function lcd_cls()
lcd_write(1, COMMAND)
endf

// *******************************************************************
// Go to the specified position
// *******************************************************************
function lcd_goto(line, posn)
if line = 1 then
posn = 0x80 + posn //Line 2
else
posn = 0xc0 + posn //Line 2
endif
lcd_write(posn, COMMAND);
endf

// *******************************************************************
// Cursor control
// *******************************************************************
function lcd_setCursor(state)
lcd_write(0x0c + state, COMMAND)
endf

// *******************************************************************
// sends a string to the line and position on the display
// *******************************************************************
function lcd_puts(line, posn, text$[40])
dim j, k=strlen(text$)

lcd_goto(line, posn)

for j = 0 to k-1
lcd_write(asc(text$,j), DATA)
next
endf

// *******************************************************************
// Function to test display
// *******************************************************************
function display()
i2c_open(100000)
i2c_start(0x40)
backLightState = BL_ON //IMPORTANT backlight needs to be set here as used in command
wait(20)
lcd_init()
lcd_cls()
lcd_puts(1,0,"Hello world")
wait (2000)
lcd_cls()
lcd_puts(2,5,"Next")
lcd_setCursor(CURSOR + BLINK)
lcd_goto(1,6)
i2c_stop()
endf
Re: Library for BV502 and 1602 LCD using i2c interface
January 12, 2015 09:36AM
Thanks,
an interesting use for an I/O expander.
Re: Library for BV502 and 1602 LCD using i2c interface
January 12, 2015 06:33PM
It saves a lot of wiring and a lot of precious i/o pins :-)
Sorry, only registered users may post in this forum.

Click here to login