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

Discussion in 'B4R Code Snippets' started by tigrot, Nov 30, 2017.

Similar threads

B4R Tutorial Arduino + Raspberry Pi
B4R Question Sending command to LCD HD44780.
B4R Tutorial LCD Clock example
B4R Library rAdafruitSH1106 - SH1106 lcd
B4R Library LiquidCrystal_I2C
  1. tigrot

    tigrot Well-Known Member Licensed User

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

    Code:
    #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
    Code:
    RunNative("Initchar",Null)
    lcd.print(
    chr(1))
    lcd.print(
    chr(2))
    .....
    .....
    Have fun!

    Mauro Zanin
     
    Peter Simpson and Erel like this.
  2. tigrot

    tigrot Well-Known Member Licensed User

    The trick is to look the shape of '1' in the data for each char!
     
    spairo likes this.
  3. Kevin

    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.
    Code:
    Sub Process_Globals
        
    Private lcd As LiquidCrystal_I2C
        
    Private MyChar as ????  '<---- What type?
    End Sub

    Private Sub AppStart
        Serial1.Initialize(
    115200)
        lcd.Initialize(
    0x27204'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: Jun 11, 2018
  4. tigrot

    tigrot Well-Known Member Licensed User

    I think that you must reference the library for this kind of lcd. This is the parallel 4 bits version.
    Ciao
    Mauro
     
  5. tigrot

    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:
    Code:
    Dim Mychar as byte=char(1)
    Lcd.print(mychar)
    A "1" is printed on lcd.
    You could write:
    Code:
    Dim Mychar as int=1
    Lcd.print(char(mychar))
    And you get the right
     
  6. Kevin

    Kevin Well-Known Member Licensed User

    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.
     
    tigrot likes this.
  7. tigrot

    tigrot Well-Known Member Licensed User

    Ops use chr(mychar)...
     
    Kevin likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice