B4R Code Snippet Define LCD custom characters for HD44780 controllers and compatible.

Hi everybody,
here is a code snippet to define custom chars in HD44780 controllers:

B4X:
#if C
#include <LiquidCrystal.h>
byte  psensc[8]= {
    B00100,
    B00100,
    B00100,
    B00100,
    B00100,
    B00100,
    B00100,
    B00000
    };
    byte puntooffc[8] ={
    B00000,
    B11111,
    B10001,
    B10001,
    B10001,
    B10001,
    B11111,
    B00000
    };
  
    byte infc[8] ={
    B00000,
    B00000,
    B00000,
    B01011,
    B10101,
    B11010,
    B00000,
    B00000
    };
    byte frecciadxc[8] ={
    B00000,
    B00100,
    B00010,
    B11111,
    B00010,
    B00100,
    B00000,
    B00000
    };
    byte frecciasxc[8] ={
    B00000,
    B00100,
    B01000,
    B11111,
    B01000,
    B00100,
    B00000,
    B00000
    };

void Initchar (B4R::Object* o) {
LiquidCrystal lcd(9,8,7,6,5,4); //adapt to you hardware cabling
lcd.begin (20,4);
lcd.createChar(1, psensc);
lcd.createChar(2, infc);
lcd.createChar(3, frecciadxc);
lcd.createChar(4, frecciasxc);
lcd.createChar(5, puntooffc);
};
#end if

To print use
B4X:
RunNative("Initchar",Null)
lcd.print(chr(1))
lcd.print(chr(2))
.....
.....

Have fun!

Mauro Zanin
 

Kevin

Well-Known Member
Licensed User
Edit: Never mind the questions. I hope that I've got it sorted out but cannot test for a couple days until I get the module delivered.

Thanks for this info! I haven't yet received my LCD but am trying to prepare for it. In my case I will need a degree symbol and I found code for this in a review on the module I purchased.

A couple questions:
  1. Will this work even if I am using the rLiquidCrystal_I2C library as it is? The code sample seems to also be initializing the module. Could I just do that prior to the inline C?
  2. Ideally, I would like to just run this inline code once in order to create the character with the LCD panel and save that character to a local B4R variable, but assuming I can do this, I don't know what type to declare it as.
B4X:
Sub Process_Globals
    Private lcd As LiquidCrystal_I2C
    Private MyChar as ????  '<---- What type?
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    lcd.Initialize(0x27, 20, 4) 'address may be different
    lcd.Backlight = True
 
    'Create a degree symbol for LCD
    #if C
    #include <LiquidCrystal.h>
    byte degreeSymbol[8] = {
    0b00110,
    0b01001,
    0b01001,
    0b00110,
    0b00000,
    0b00000,
    0b00000,
    0b00000
    };

    void Initchar (B4R::Object* o) {
    LiquidCrystal lcd(9,8,7,6,5,4); //adapt to you hardware cabling         // <----- Do I need this?
    lcd.begin (20,4);        // <----- Do I need this?
    lcd.createChar(0, degreeSymbol);
    #End if
 
    RunNative("Initchar",Null)
    MyChar = chr(0)
End Sub

Sub Example
     lcd.Print ("Temperature = 80.5 " & MyChar & "F") ' I know I cannot use '&' to build text in B4R but I am working on that...

End Sub
 
Last edited:

tigrot

Well-Known Member
Licensed User
The issue is that if you print a chr function you get the correct result. If you print a variable you get a converted result.
That is:
B4X:
Dim Mychar as byte=char(1)
Lcd.print(mychar)
A "1" is printed on lcd.
You could write:
B4X:
Dim Mychar as int=1
Lcd.print(char(mychar))
And you get the right
 

Kevin

Well-Known Member
Licensed User
The issue is that if you print a chr function you get the correct result. If you print a variable you get a converted result.
That is:
B4X:
Dim Mychar as byte=char(1)
Lcd.print(mychar)
A "1" is printed on lcd.
You could write:
B4X:
Dim Mychar as int=1
Lcd.print(char(mychar))
And you get the right

Unfortunately this throws a warning that undeclared variable 'char' is used before it is assigned any value. I was thinking I should just store the entire byte (as a byte) and write that to the LCD but can't seem to work that out either.

Regarding your previous post, I think I figured that part out and have it referencing LiquidCrystal_I2C.h.
 

Similar Threads

Top