Display user-defined characters on the I2C 1602 LCD with Raspberry Pi 3 & Java

Introduction

To display characters on the 1602 LCD connected to the Raspberry Pi from a program written in Java, you can use the general-purpose library provided by Pi4J. For more information on this, please refer to the following articles.

Display characters on I2C 1602 LCD with Raspberry Pi 3 & Java

The 1602 LCD not only displays the characters prepared on the ROM, but also has the function of creating a character pattern (** user-defined character **) from the program and displaying it.

This feature is not implemented in the general library class introduced in the above article, so I tried to extend this class to use user-defined characters.

1602 LCD user-defined character function

The 1602 LCD has a memory area called CGRAM (Character Generator RAM), and up to 8 5 x 8 dot character patterns can be registered in this area. Each registered character pattern can be displayed by specifying the character code 0 to 7 (even if you specify 8 to 15, the same character pattern as 0 to 7 is displayed).

(Quoted from Datasheet)

program

To enable the use of user-defined characters, I created the I2C1602Lcd class, which inherits from the com.pi4j.component.lcd.impl.I2CLcdDisplay class in the generic library.

I2C1602Lcd.java


import com.pi4j.component.lcd.impl.I2CLcdDisplay;

public class I2C1602Lcd extends I2CLcdDisplay {
    protected final boolean LCD_CHR = true;
    protected final boolean LCD_CMD = false;

    public I2C1602Lcd(int i2cBus, int i2cAddress) throws Exception {
        super(2, 16, i2cBus, i2cAddress, 3, 0, 1, 2, 7, 6, 5, 4);
    }

    public void setCharacterPattern(byte characterCode, byte[] characterPattern) throws Exception {
        if (characterCode > 0x08) {
            throw new RuntimeException("Invalid character code.");
        }
        if (characterPattern.length != 8) {
            throw new RuntimeException("Wrong character pattern length.");
        }

        lcd_byte(0x40 | characterCode << 3, LCD_CMD);

        for (byte pattern : characterPattern) {
            lcd_byte(pattern, LCD_CHR);
        }
    }
}

To display characters on the LCD, first create an instance of the I2C1602Lcd class created this time. The constructor has the following format. For i2cBus, specify the I2C bus number, and for i2cAddress, specify the address of the module you are using, depending on your environment.

I2C1602Lcd(int i2cBus, int i2cAddress) throws Exception

Because it inherits from the Pi4J generic library class I2CLcdDisplay, the methods in that class can still be used in instances of the I2C1602Lcd class.

Specify the character code and character pattern of user-defined characters with the setCharacterPattern method of the I2C1602Lcd class.

void setCharacterPattern(byte characterCode, byte[] characterPattern) throws Exception

The first parameter, characterCode, specifies the character code (0-7) to assign to the user-defined character.

The second parameter, characterPattern, specifies a byte array with eight elements containing the character pattern. The figure below is an example of the character pattern to be displayed and the value to be set for each element of the array.

userdefinedfontsample.png

The following is a simple demo program using the class created this time. Registers three types of weather marks as user-defined characters and displays them on the LCD.

I2C1602LcdChPatternDemo.java


public class I2C1602LcdChPatternDemo {
    private static final int LCD_ROW_1 = 0;
    private static final int LCD_ROW_2 = 1;

    private static final byte[] patternSunnyMark  = {0x04, 0x11, 0x0E, 0x1F, 0x0E, 0x11, 0x04, 0x00};
    private static final byte[] patternCloudyMark = {0x0C, 0x1E, 0x0C, 0x00, 0x06, 0x0F, 0x06, 0x00};
    private static final byte[] patternRainyMark  = {0x0E, 0x1F, 0x0E, 0x00, 0x15, 0x00, 0x15, 0x00};

    public static void main(String[] args) throws Exception {
        I2C1602Lcd lcd = new I2C1602Lcd(1, 0x27);

        lcd.setCharacterPattern((byte)0x00, patternSunnyMark );
        lcd.setCharacterPattern((byte)0x01, patternCloudyMark);
        lcd.setCharacterPattern((byte)0x07, patternRainyMark );

        lcd.write(LCD_ROW_1, "%1X  %1X  %1X  %1X  %1X  %1X", 0x00, 0x01, 0x07, 0x08, 0x09, 0x0F);
        lcd.write(LCD_ROW_2, "%c  %c  %c  %c  %c  %c"      , 0x00, 0x01, 0x07, 0x08, 0x09, 0x0F);
    }
}

Compile and run

To actually try the user-defined characters, put the above two files in a suitable directory and compile.

pi@raspberrypi:~ $ pi4j -c I2C1602LcdChPatternDemo.java
--------------------------------------------
Pi4J - Compiling: I2C1602LcdChPatternDemo.java
--------------------------------------------
+ javac -classpath '.:classes:*:classes:/opt/pi4j/lib/*' -d . I2C1602LcdChPatternDemo.java
pi@raspberrypi:~ $

After compiling, execute it with root privileges.

pi@raspberrypi:~ $ sudo pi4j I2C1602LcdChPatternDemo
+ java -classpath '.:classes:*:classes:/opt/pi4j/lib/*' I2C1602LcdChPatternDemo
pi@raspberrypi:~ $

lcd.png

Recommended Posts

Display user-defined characters on the I2C 1602 LCD with Raspberry Pi 3 & Java
Display characters on I2C 1602 LCD with Raspberry Pi 3 & Java
Launched Redmine with Docker on Raspberry Pi 3
How to run javafx with Raspberry Pi Posted on 2020/07/12
[Android] Display images and characters on the ViewPager tab
Display the list in setDetails on the screen with spring-security
Read temperature / humidity with Java from Raspberry Pi 3 & DHT11
How to reduce the load on the program even a little when combining characters with JAVA
The key to running Docker on Raspberry Pi 4 (Ubuntu server 20.04)
Install Docker on Raspberry Pi
Put Ubuntu 20.04.1 on Raspberry Pi 4
Minecraft server on Raspberry Pi 4
Seasonal display with Java switch
Graph the sensor information of Raspberry Pi in Java and check it with a web browser
I tried to display the calendar on the Eclipse console using Java.
Read barometric pressure and temperature with Java from Raspberry Pi 3 & BMP180
Install MariaDB on Raspberry Pi OS
Install Java with zip on Windows
Try putting CentOS 8 on Raspberry Pi 3
Programming with ruby (on the way)
Note: setting javaMail on Raspberry Pi
Consideration on the 2017 Java Persistence Framework (1)
[Java] Display the bit string stored in the byte type variable on the console