// 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