B4R Question Characters on LCD (solved)

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

with InlineC - Example using I2C LCD 20x4 (for tests, but should work on 16x2 also)
B4X:
Sub Process_Globals    Public Serial1 As Serial
    Private lcd As LiquidCrystal_I2C
    Private CharDegree As Byte = 0
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    lcd.Initialize(0x27, 20, 4)
    lcd.Backlight = True   
    'Special Chars
    RunNative("createChar", Null)
    lcd.SetCursor(0,0)
    lcd.Write("25")
    RunNative("writeChar", CharDegree)

End Sub

#if C
Byte degree[8] = {
B00111,
B00101,
B00111,
B00000,
B00000,
B00000,
B00000,
B00000
};

//Create the special chars:0=degee
//Use:
//RunNative("createChar", Null)
void createChar(B4R::Object* o) {
   b4r_main::_lcd->lcd->createChar(0, degree);
}

//Write a special chart to the display: 0=degree
//Use:
//lcd.SetCursor(0,1)
//RunNative("writeChar", 0)
void writeChar(B4R::Object* o) {
    b4r_main::_lcd->lcd->write((Byte)o->toULong());
}
#end if
 
Last edited:
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Addition to Post #2.
If want to use quotes as well, then add more special chars. Each char has 5x8 pixels which are set accordingly.
B4X:
Sub Process_Globals    Public Serial1 As Serial
    Private lcd As LiquidCrystal_I2C
    Private CharDegree = 0, CharQuotes = 1 As Byte
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    lcd.Initialize(0x27, 20, 4)
     lcd.Backlight = True
    'Special Chars
    RunNative("createChar", Null)
    lcd.SetCursor(0,0)
    RunNative("writeChar", CharQuotes)
    lcd.Write("25")
    RunNative("writeChar", CharDegree)
    RunNative("writeChar", CharQuotes)
End Sub

#if C
Byte degree[8] = {
B00111,
B00101,
B00111,
B00000,
B00000,
B00000,
B00000,
B00000
};

Byte quotes[8] = {
B00101,
B00101,
B00101,
B00000,
B00000,
B00000,
B00000,
B00000
};

//Create the special chars:0=degee,1=quotes
//Use:
//RunNative("createChar", Null)
void createChar(B4R::Object* o) {
   b4r_main::_lcd->lcd->createChar(0, degree);
   b4r_main::_lcd->lcd->createChar(1, quotes);
}

//Write a special chart to the display: 0=degree,1=quotes
//Use:
//lcd.SetCursor(0,1)
//RunNative("writeChar", 0)
void writeChar(B4R::Object* o) {
    b4r_main::_lcd->lcd->write((Byte)o->toULong());
}
#end if
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
Addition to Post #2.
If want to use quotes as well, then add more special chars. Each char has 8bit which are set accordingly.
B4X:
Sub Process_Globals    Public Serial1 As Serial
    Private lcd As LiquidCrystal_I2C
    Private CharDegree = 0, CharQuotes = 1 As Byte
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    lcd.Initialize(0x27, 20, 4)
     lcd.Backlight = True
    'Special Chars
    RunNative("createChar", Null)
    lcd.SetCursor(0,0)
    RunNative("writeChar", CharQuotes)
    lcd.Write("25")
    RunNative("writeChar", CharDegree)
    RunNative("writeChar", CharQuotes)
End Sub

#if C
Byte degree[8] = {
B00111,
B00101,
B00111,
B00000,
B00000,
B00000,
B00000,
B00000
};

Byte quotes[8] = {
B00101,
B00101,
B00101,
B00000,
B00000,
B00000,
B00000,
B00000
};

//Create the special chars:0=degee,1=quotes
//Use:
//RunNative("createChar", Null)
void createChar(B4R::Object* o) {
   b4r_main::_lcd->lcd->createChar(0, degree);
   b4r_main::_lcd->lcd->createChar(1, quotes);
}

//Write a special chart to the display: 0=degree,1=quotes
//Use:
//lcd.SetCursor(0,1)
//RunNative("writeChar", 0)
void writeChar(B4R::Object* o) {
    b4r_main::_lcd->lcd->write((Byte)o->toULong());
}
#end if

Thank you. I have found a simpler solution:
IMG_20170130_110640.jpg

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Public lcd As LiquidCrystal_I2C
    Private b() As Byte = Array As Byte(223,0)
    Private bc As ByteConverter
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    lcd.Initialize(0x3F, 16,2)
    lcd.Backlight = True
    lcd.SetCursor(0,0)
    lcd.Write("25")
    lcd.SetCursor(2,0)
    lcd.Write(bc.StringFromBytes(b))

End Sub
 
Last edited:
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Very Good!

The solution I shared enables to define your own special chars beyond the onces defined in character tables.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
One more addition (if I may) for those who would ask where 223 comes from.

To write a character from the character table of the driver:
The LCD1602 uses the HD44780 Dot Matrix Liquid Crystal Display Controller/Driver.
The datasheet contains the character tables.
Example Degree Character °:
Ref page 17 of the data sheet.
The Degree Character ° is located at position upper 4 bits 1101 and lower 4 bits 1111.
The 8 bits 1101 1111 are HEX DF and DEC 223.
To write the character to the LCD use lcd.Write(Array As Byte(223)) as shared in post #6.
 
Last edited:
Upvote 0
Top