BV4218 Programs

The BV4218 can be driven by any I2C host. Presented here is some sample code for driving this controller. Below is a PIC32-Basic example, for a Forth example see http://www.pin1.org/forthlib

Using PIC32 Basic

Here is an example of code that uses PIC32 Basic to drive the display and keypad of the BV4218 connected to a 20 x 2 line display. The code can be downloaded in the downloads section. The constants may be changed for differing address and line configurations. Error checking has been purposely left out to make the code clearer.

I2ctest is used to see if the display is connected and has the correct address. There are two basic commands for writing to the display put character and put string. Positioning of the cursor is done by sending the appropriate value as a command. This is normally found by trial and error but is usually either 0x20,0x40,0x80 or 0xC0. The function and constantans can be changed or extended to cater for differing displays.

The keypad section also contains two main functions, one to see if there is a key in the key buffer (lcd20_key?) and the other to get the key (lcd20_key). The latter of these will remain in the function until a key is pressed. From experience this is the most useful behaviour. The last function will map a keypad to give the correct values when a key is presses, i.e. the ones marked on top of the keypad.

// Example of using a 20 x 2 I2C LCD display
// with PIC Basic
//
constant LCD20ADR 0x42
constant LCD20LINE0 0x80
constant LCD20LINE1 0xc0

// initialise and check address
function lcd20_init
    i2copen 100000
    if i2ctest(LCD20ADR) = 0 then
        print "Device not connected at address ";hex$(LCD20ADR)
    endif
endf

// ----------- LCD functions ----------------------
//
// put character to display at current location
// use: lcd20_putc('a')
// lcd20_putc(97)
function lcd20_putc(c)
    i2cwrite LCD20ADR 2 c
endf

// prints a string to display at current position
function lcd20_puts(s$)
dim j
    for j = 1 to len(s$)
        lcd20_putc(asc(s$,j))
    next
endf

// clear screen and home cursor
function lcd20_cls
    i2cwrite LCD20ADR 1 1
endf

// set line and current position, line is 0 or 1 on
// a 2 line display and postion os 0 to 19
// use: lcd20_lp(1,5)
function lcd20_lp(line,pos)
dim l
    if line = 0 then
        l=LCD20LINE0
    else
        l=LCD20LINE1
    endif
    i2cwrite LCD20ADR 1 l+pos // set line & pos
endf

// -------------- Keypad Functions ----------------
//
// returns 0 if there are no keys in the buffer
// otherwise it returns the number of keys in the buffer
function lcd20_key?
dim d[1]
    i2cread LCD20ADR 0x10, d[1];1
    result d[1]
endf

// stays in the routine until a key is recieved
// and then returns that key
function lcd20_key
dim d[1]
    while lcd20_key? = 0
        wait 50 // so as not to overload the i2c bus
    wend
    i2cread LCD20ADR 0x11, d[1];1
    result d[1]
endf

// set up the keys on the keypad
function lcd20_mapkeys
dim km, in, d[16], j, k
    // get keymap address
    i2cread LCD20ADR 0x12, d[1];1
    km=d[1]
    print "this can only be done after a factory reset"
    print "press each key in assending order"
    for j = 1 to 13 // 0-12
        k=lcd20_key
        print "key ";j;" map value ";k+1
        d[k+1]=j
    next
    // now write out key map file
    i2cwrite LCD20ADR 0x91 km d[1] d[2] d[3] d[4] d[5] d[6] d[7] d[8] d[9] d[10] d[11] d[12]
endf