i2c LCD driver using i2c from jPi4J
Have fun
now with BIG Numer support
BIG Number:
Have fun
now with BIG Numer support
B4X:
lcd.LCDInit(1,0x27,20)
lcd.Delay(50)
lcd.clear
lcd.goto(4,0)
lcd.print("Hallo LCD")
BIG Number:
B4X:
Dim hr, mn, sc, sc1, sc2 As Int
Dim ax As Int = 1
Dim sc0, sc1 As Int
Dim mn0, mn1 As Int
Dim hr0, hr1 As Int
Dim now As Long
lcd.Define_Big_Numbers
Do While ax < 2
hr = DateTime.GetHour(DateTime.Now)
mn = DateTime.GetMinute(DateTime.Now)
sc = DateTime.GetSecond(DateTime.Now)
sc0 = sc Mod 10
sc1 = sc/10
mn0 = mn Mod 10
mn1 = mn/10
hr0 = hr Mod 10
hr1 = hr/10
If sc <> sc2 Then
lcd.PrintNum(hr1, 2)
lcd.PrintNum(hr0, 6)
lcd.PrintNum(mn1, 12)
lcd.PrintNum(mn0, 16)
If sc0 mod 2 = 1 Then
lcd.goto(2, 10)
lcd.put(111)
lcd.goto(3, 10)
lcd.put(111)
Else
lcd.goto(2, 10)
lcd.put(0x20)
lcd.goto(3, 10)
lcd.put(0x20)
End If
sc2 = sc
EndIf
Loop
B4X:
#Region PCF8574x Info
' INPUTS i2c-bus Slave Address
' PCF8574 PCF8574A
'A2 A1 A0
'L L L 0x20 0x38
'L L H 0x21 0x39
'L H L 0x22 0x3A
'L H H 0x23 0x3B
'H L L 0x24 0x3C
'H L H 0x25 0x3D
'H H L 0x26 0x3E
'H H H 0x27 0x3F
' Wirering PCF8574
' P.0= RS
' P.1= R/W
' P.2= E
' P.3= LCD-Light
' P.4= D4
' P.5= D5
' P.6= D6
' P.7= D7
#End Region
Sub Process_Globals
Dim LCD_Backlight As Byte = 0x08
'Dim Addr() As Byte
'Addr = Array As Byte(0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, _ 'PCF8574 - Adresses
'0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x4E, 0x3F) 'PCF8574A - Adresses
Dim Line() As Int
Line = Array As Int(0x80, 0xC0, 0x94, 0xD4)
'Line = Array As Int(0x00, 0x40, 0x14, 0x54)
'Line = Array As Int(0x80, 0xA0, 0xC0, 0xe0)
Dim CharsperLine As Byte = 20 'Characters per line
Dim row1() As Int
Dim row2() As Int
Dim row3() As Int
Dim row4() As Int
row1 = Array As Int(0x00, 0x01, 0x02 , 0x00, 0x01, 0x20 , 0x00, 0x01, 0x02 , 0x00, 0x01, 0x02 , 0x01, 0x20, 0x20 , 0x01, 0x01, 0x01 , 0x00, 0x01, 0x02 , 0x01, 0x01, 0x01 , 0x00, 0x01, 0x02 , 0x00, 0x01, 0x02)
row2 = Array As Int(0xFF, 0x20, 0xFF , 0x20, 0xFF, 0x20 , 0x00, 0x01, 0x03 , 0x20, 0x00, 0x03 , 0xFF, 0x01, 0xFF , 0xFF, 0x01, 0x02 , 0xFF, 0x01, 0x02 , 0x20, 0x00, 0x20 , 0xFF, 0x01, 0xFF , 0xFF, 0x01, 0xFF)
row3 = Array As Int(0xFF, 0x20, 0xFF , 0x20, 0xFF, 0x20 , 0xFF, 0x20, 0x20 , 0x20, 0x05, 0x04 , 0x20, 0x20, 0xFF , 0x20, 0x20, 0xFF , 0xFF, 0x20, 0xFF , 0x20, 0xFF, 0x20 , 0xFF, 0x20, 0xFF , 0x20, 0x20, 0xFF)
row4 = Array As Int(0x05, 0x06, 0x07 , 0x06, 0x06, 0x06 , 0x06, 0x06, 0x06 , 0x05, 0x06, 0x07 , 0x20, 0x20, 0x06 , 0x06, 0x06, 0x07 , 0x05, 0x06, 0x07 , 0x20, 0x06, 0x20 , 0x05, 0x06, 0x07 , 0x05, 0x06, 0x07)
Public bus As JavaObject
Public device As JavaObject
End Sub
'I2C bus, PCF I2C address,
Sub LCDInit(i2cbus As Byte, LCD_I2c_Address As Byte, NrCols As Byte)
'NrCols ignored for this init
bus = i2c.GetBus(i2cbus)
device = i2c.GetDevice(bus, LCD_I2c_Address)
LCD_Backlight = Bit.And(LCD_Backlight, 8)
Delay(100)
'Note: Initialisation depends on the used display, please update for your needs !
i2c.write(device, Bit.Or(0x30, LCD_Backlight))
i2c.write(device, Bit.Or(0x30, LCD_Backlight))
i2c.write(device, Bit.Or(0x34, LCD_Backlight)) ' Function Set 8 Bit
i2c.write(device, Bit.Or(0x30, LCD_Backlight))
Delay(50)
i2c.write(device, Bit.Or(0x30, LCD_Backlight))
i2c.write(device, Bit.Or(0x34, LCD_Backlight)) ' Function Set 8 Bit
i2c.write(device, Bit.Or(0x30, LCD_Backlight))
i2c.write(device, Bit.Or(0x30, LCD_Backlight))
i2c.write(device, Bit.Or(0x34, LCD_Backlight)) ' Function Set 8 Bit
i2c.write(device, Bit.Or(0x30, LCD_Backlight))
i2c.write(device, Bit.Or(0x20, LCD_Backlight))
i2c.write(device, Bit.Or(0x24, LCD_Backlight)) ' Function Set 4 Bit
i2c.write(device, Bit.Or(0x20, LCD_Backlight))
i2c.write(device, Bit.Or(0x24, LCD_Backlight)) ' Function Set
i2c.write(device, Bit.Or(0x20, LCD_Backlight))
i2c.write(device, Bit.Or(0x84, LCD_Backlight)) ' 2 Rows, 5x7 Points
i2c.write(device, Bit.Or(0x80, LCD_Backlight))
i2c.write(device, Bit.Or(0x04, LCD_Backlight)) ' Display Control
i2c.write(device, Bit.Or(0x00, LCD_Backlight))
i2c.write(device, Bit.Or(0xC4, LCD_Backlight)) ' Cursor Off, display On
i2c.write(device, Bit.Or(0xC0, LCD_Backlight))
i2c.write(device, Bit.Or(0x04, LCD_Backlight)) ' Display Clear
i2c.write(device, Bit.Or(0x00, LCD_Backlight))
i2c.write(device, Bit.Or(0x14, LCD_Backlight)) ' Display Clear
i2c.write(device, Bit.Or(0x10, LCD_Backlight))
Delay(50)
i2c.write(device, Bit.Or(0x04, LCD_Backlight)) ' Entry - Mode
i2c.write(device, Bit.Or(0x00, LCD_Backlight))
i2c.write(device, Bit.Or(0x64, LCD_Backlight)) ' inkrement, Cursor - shift
i2c.write(device, Bit.Or(0x60, LCD_Backlight))
End Sub
Sub reset
'Note: Reset depends on the used display, please update for your needs !
'Note: this is not the same a POR
i2c.write(device, Bit.Or(0x30, LCD_Backlight))
i2c.write(device, Bit.Or(0x30, LCD_Backlight))
i2c.write(device, Bit.Or(0x34, LCD_Backlight))
i2c.write(device, Bit.Or(0x30, LCD_Backlight))
End Sub
'define Chars
'Addr= 0 to 7 (ASCII 0-7)
'data size 8 Byte
'rowdata= 5Bit (Bit4 to Bit0
'Byte 0 to 7 = row 0 to 7
Sub defineChar(myAddr As Int, charr() As Int)
'Do clear the LCD once after all characters are written to RAM
Dim i As Int
myAddr = Bit.Or((Bit.ShiftLeft(Bit.And(myAddr, 0x7), 3)), 0x40)
i2c.write(device, Bit.Or(0x04, LCD_Backlight))
i2c.write(device, Bit.Or(0x00, LCD_Backlight))
i2c.write(device, Bit.Or(0x64 , LCD_Backlight))
i2c.write(device, Bit.Or(0x60 , LCD_Backlight))
i2c.write(device, Bit.Or(Bit.Or(Bit.And(myAddr, 0xf0), LCD_Backlight), 0x4))'HighNibble
i2c.write(device, Bit.Or(Bit.And(myAddr, 0xf0), LCD_Backlight))
i2c.write(device, Bit.Or(Bit.Or(Bit.Shiftleft(myAddr, 4), LCD_Backlight), 4))'LowNibble
i2c.write(device, Bit.Or(Bit.ShiftLeft(myAddr, 4), LCD_Backlight))
For i = 0 To 7
i2c.write(device, Bit.Or(Bit.Or(Bit.And(charr(i), 0xf0), LCD_Backlight), 0x05))'HighNibble
i2c.write(device, Bit.Or(Bit.Or(Bit.And(charr(i), 0xf0), LCD_Backlight), 0x01))
i2c.write(device, Bit.Or(Bit.Or(Bit.ShiftLeft(charr(i), 4), LCD_Backlight), 0x05))'LowNibble
i2c.write(device, Bit.Or(Bit.Or(Bit.ShiftLeft(charr(i), 4), LCD_Backlight), 0x01))
Next
i2c.write(device, Bit.Or(0x84, LCD_Backlight))
i2c.write(device, Bit.Or(0x80, LCD_Backlight))
i2c.write(device, Bit.Or(0x04, LCD_Backlight))
i2c.write(device, Bit.Or(0x00, LCD_Backlight))
Delay(30)
End Sub
'0= off, 1= on
Sub setBacklight( state As Byte)
LCD_Backlight = Bit.Shiftleft(state, 3)
i2c.write(device, LCD_Backlight)
End Sub
'0= off, 2= underline, 3= Block, flashing
Sub setcursor( cursor As Int)
WriteCmd(Bit.Or(Bit.And(cursor, 0x3), 0x0C))
End Sub
'put char @ set position
Sub put( inp As Int)
i2c.write(device, Bit.Or(Bit.Or(Bit.And(inp, 0xf0), LCD_Backlight), 0x5)) ' HighNibble
i2c.write(device, Bit.Or(Bit.Or(Bit.And(inp, 0xf0), LCD_Backlight), 0x1))
i2c.write(device, Bit.Or(Bit.Or(Bit.ShiftLeft(inp, 4), LCD_Backlight), 0x5)) ' LowNibble
i2c.write(device, Bit.Or(Bit.Or(Bit.ShiftLeft(inp, 4), LCD_Backlight), 0x1))
End Sub
'command byte / Mode: command-mode (RS=Low)
Sub WriteCmd ( cmd As Byte)
i2c.write(device, Bit.Or(Bit.Or(Bit.And(cmd, 0xF0), LCD_Backlight), 0x4)) ' HighNibble
i2c.write(device, Bit.Or(Bit.And(cmd , 0xF0), LCD_Backlight))
i2c.write(device, Bit.Or(Bit.Or(Bit.ShiftLeft(cmd, 4) , LCD_Backlight) , 0x4))' LowNibble
i2c.write(device, Bit.Or(Bit.ShiftLeft(cmd, 4), LCD_Backlight))
End Sub
'print text @ set position
Sub print(s As String)
Dim m() As Byte
Dim value As Int
Dim en, md, i As Int
en = Bit.Or(LCD_Backlight, 0x5)
md = Bit.Or(LCD_Backlight, 0x1)
m = s.GetBytes("UTF8")
For i = 0 To m.Length - 1
value = m(i)
'Log(value)
i2c.write(device, Bit.Or(Bit.And(value, 0xf0), en))' HighNibble
i2c.write(device, Bit.Or(Bit.And(value, 0xf0), md))
i2c.write(device, Bit.Or(Bit.ShiftLeft(value, 4), en))'LowNibble
i2c.write(device, Bit.Or(Bit.ShiftLeft(value, 4), md))
Next
Delay(2)
End Sub
'clear display
Sub clear()
WriteCmd(0x01)
Delay(2)
End Sub
'set cursor to 1,0
Sub home()
WriteCmd(0x02)
Delay(2)
End Sub
'delete row 1 to 4
Sub ClearRow(row As Int)
Dim i, l As Int
l = Line(row - 1)
Bit.Or(Bit.Or(Bit.And(l, 0xf0), LCD_Backlight), 0x4) 'HighNibble
Bit.Or(Bit.And(l, 0xf0), LCD_Backlight)
Bit.Or(Bit.Or(Bit.ShiftLeft(l, 4), LCD_Backlight), 0x4)'LowNibble
Bit.Or(Bit.ShiftLeft(l, 4), LCD_Backlight)
For i = 0 To CharsperLine - 1
WriteCmd(Bit.Or(0x25, LCD_Backlight))
WriteCmd(Bit.Or(0x21, LCD_Backlight))
WriteCmd(Bit.Or(0x05, LCD_Backlight))
WriteCmd(Bit.Or(0x01, LCD_Backlight))
Next
End Sub
'row 1 to 4
Sub setLine(row As Int)
Dim l As Int
l = Line(row - 1)
i2c.write(device, Bit.Or(Bit.Or(Bit.And(l, 0xf0), LCD_Backlight), 0x4)) 'HighNibble
i2c.write(device, Bit.Or(Bit.And(l, 0xf0), LCD_Backlight))
i2c.write(device, Bit.Or(Bit.Or(Bit.ShiftLeft(l, 4), LCD_Backlight), 0x4))'LowNibble
i2c.write(device, Bit.Or(Bit.ShiftLeft(l, 4), LCD_Backlight))
End Sub
'Row 1 to 4, Col 0 - 19(15)
Sub goto( myLine As Int, col As Int)
' Log(myLine&" "&col)
myLine = Line(myLine - 1) + col
i2c.write(device, Bit.Or(Bit.Or(Bit.And(myLine, 0xf0), LCD_Backlight), 0x4)) 'HighNibble
i2c.write(device, Bit.Or(Bit.And(myLine, 0xf0), LCD_Backlight))
i2c.write(device, Bit.Or(Bit.Or(Bit.ShiftLeft(myLine, 4), LCD_Backlight), 0x4))'LowNibble
i2c.write(device, Bit.Or(Bit.ShiftLeft(myLine, 4), LCD_Backlight))
End Sub
'move cursor left
Sub cursorleft()
i2c.write(device, Bit.Or(0x14, LCD_Backlight))'HighNibble
i2c.write(device, Bit.Or(0x10, LCD_Backlight))
i2c.write(device, Bit.Or(0x04, LCD_Backlight))'LowNibble
i2c.write(device, Bit.Or(0x00, LCD_Backlight))
End Sub
'move cursor right
Sub cursorright()
i2c.write(device, Bit.Or(0x14, LCD_Backlight))'HighNibble
i2c.write(device, Bit.Or(0x10, LCD_Backlight))
i2c.write(device, Bit.Or(0x44, LCD_Backlight))'LowNibble
i2c.write(device, Bit.Or(0x40, LCD_Backlight))
End Sub
'Define Chars for Progressbar (0 to 3)
Sub def_bargraph()
Dim chars(8) As Int
Dim x, j As Int
Dim b As Int
b = 0
For x = 0 To 3
For j = 0 To 7
b = Bit.ShiftRight(240, x)
chars(j) = b
Next
defineChar(x, chars)
Next
End Sub
'draw Progressbar
Sub bargraph(len As Int, maxlen As Int)
Dim i, j, En, Md As Int
En = Bit.Or(LCD_Backlight, 0x5)
Md = Bit.Or(LCD_Backlight, 0x1)
If len > maxlen Then len = maxlen
For i = 0 To len - 1 Step 5
j = len - i
If j > 4 Then
j = 0xFF
Else If j >= 1 Then
j = j - 1
Else
j = 0x20
End If
'Log(j)
i2c.write(device, Bit.Or(Bit.And(j, 0xf0), En))'HighNibble
i2c.write(device, Bit.Or(Bit.And(j, 0xf0), Md))
i2c.write(device, Bit.Or(Bit.ShiftLeft(j, 4), En))
i2c.write(device, Bit.Or(Bit.ShiftLeft(j, 4), Md))
Next
Do While i < maxlen
i2c.write(device, Bit.Or(0x20, En))'HighNibble
i2c.write(device, Bit.Or(0x20, Md))
i2c.write(device, En)'LowNibble
i2c.write(device, Md)
i = i + 5
Loop
End Sub
'wipe Diplay left To right }
Sub LCDwipeOffLR(fill As Boolean)
Dim i, y As Byte
y = 0x14
If fill = True Then
y = 0xff
End If
For i = 19 To 0 Step - 1
Delay(50)
goto(1, i)
put(y)
goto(2, i)
put(y)
goto(3, i)
put(y)
goto(4, i)
put(y)
Next
End Sub
'wipe Diplay right To left
Sub LCDwipeOffRL(fill As Boolean)
Dim i, y As Byte
y = 0x14
If fill = True Then
y = 0xff
End If
For i = 0 To 19
Delay(50)
goto(1, i)
put(y)
goto(2, i)
put(y)
goto(3, i)
put(y)
goto(4, i)
put(y)
Next
End Sub
'read the CG or DD RAM
Sub ReadRam () As Int
' do WriteCmd() 1st
' cmd=0x40 to 0x7F = CG-RAM
' cmd=0x80 to 0xFF = DD-RAM
Delay(50)
Dim data As Int
i2c.write(device, Bit.Or(0xF3 , LCD_Backlight))
Delay(1)
i2c.write(device, Bit.Or(0xF7 , LCD_Backlight))
Delay(1)
data = Bit.And(i2c.read(device), 0xF0) ' High - Nibble
i2c.write(device, Bit.Or(0xF3, LCD_Backlight))
Delay(1)
i2c.write(device, Bit.Or(0xF7, LCD_Backlight))
data = Bit.Or(data, Bit.ShiftRight(i2c.read(device), 4)) ' Low - Nibble
i2c.write(device, Bit.Or(0xF3 , LCD_Backlight))
Delay(1)
i2c.write(device, Bit.Or(0xF0 , LCD_Backlight))
Return data
End Sub
'0= Left, 1= Right
Sub shift(dir As Int)
WriteCmd(24 + Bit.Shiftleft(dir, 2))
End Sub
' some char examples:
'ascii= ascii-Code 0 to 7
Sub def_batterie( ascii As Int)
Dim chars(8) As Int
chars = Array As Int (0x0e, 0x1b, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1f)
defineChar(ascii, chars)
End Sub
'ascii= ascii-Code 0 to 7
Sub def_hourglass( ascii As Int)
Dim chars(8) As Int
chars = Array As Int (0x1f, 0x11, 0x0a, 0x04, 0xa, 0x11, 0x1f, 0x00)
defineChar(ascii, chars)
End Sub
'ascii= ascii-Code 0 to 7
Sub def_arr_down( ascii As Int)
Dim chars(8) As Int
chars = Array As Int (0x04, 0x04, 0x04, 0x04, 0x15, 0x0E, 0x04, 0x00)
defineChar(ascii, chars)
End Sub
'ascii= ascii-Code 0 to 7
Sub def_arr_up( ascii As Int)
Dim chars(8) As Int
chars = Array As Int (0x04, 0x0e, 0x15, 0x04, 0x14, 0x04, 0x04, 0x00)
defineChar(ascii, chars)
End Sub
Sub Delay (DurationMs As Int)
Dim target As Long = DateTime.Now + DurationMs
Do While DateTime.Now < target
Loop
End Sub
Sub Define_Big_Numbers
Dim chars(8) As Int
chars = Array As Int (0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x0F)
defineChar(0, chars)
Delay(20)
chars = Array As Int (0x00, 0x00, 0x00, 0x00, 0x1F, 0x1F, 0x1F, 0x1F)
defineChar(1, chars)
Delay(20)
chars = Array As Int (0x00, 0x00, 0x00, 0x00, 0x10, 0x18, 0x1C, 0x1E)
defineChar(2, chars)
Delay(20)
chars = Array As Int (0x1F, 0x1F, 0x1F, 0x1F, 0x1E, 0x1C, 0x18, 0x10)
defineChar(3, chars)
Delay(20)
chars = Array As Int (0x10, 0x18, 0x1C, 0x1E, 0x1F, 0x1F, 0x1F, 0x1F)
defineChar(4, chars)
Delay(20)
chars = Array As Int (0x0F, 0x07, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00)
defineChar(5, chars)
Delay(20)
chars = Array As Int (0x1F, 0x1F, 0x1F, 0x1F, 0x00, 0x00, 0x00, 0x00)
defineChar(6, chars)
Delay(20)
chars = Array As Int (0x1E, 0x1C, 0x18, 0x10, 0x00, 0x00, 0x00, 0x00)
defineChar(7, chars)
End Sub
Sub PrintNum(digit As Byte, leftAdjust As Byte)
For row = 1 To 4
goto(row, leftAdjust)
For col = digit * 3 To digit * 3 + 2
Select row
Case 1
put(row1(col))
Case 2
put(row2(col))
Case 3
put(row3(col))
Case 4
put(row4(col))
End Select
Next
Next
End Sub
Last edited: