This code uses the HiPi module which seemed to me to require I load the whole GUI (I am using minibian) so I converted a Python demo program to Perl as an illustration of how to use the SMBus module to write to an I2C LCD (16x2).
Here is the code:
#!/usr/bin/perl
# Example program which writes to the I2C LCD at address 0x3f. This w
+as
# a direct port of the program published by Matt Hawkins on the site
# www.raspberrypi-spy.co.uk. Port performed by Steve Stock.
#
use strict;
use warnings;
use Time::HiRes ('usleep');
use Device::SMBus;
my $I2C_ADDR=0x3f;
my $DEVICE_PATH = '/dev/i2c-1';
my $dev = Device::SMBus->new(I2CBusDevicePath => $DEVICE_PATH,
I2CDeviceAddress => $I2C_ADDR);
my $LCD_WIDTH = 16;
my $LCD_CHR=1;
my $LCD_CMD=0;
my $FILL_CHAR=" ";
my $LCD_LINE_1=0x80;
my $LCD_LINE_2=0xc0;
my $LCD_LINE_3=0x94;
my $LCD_LINE_4=0xd4;
my $LCD_BACKLIGHT=0x08;
my $LCD_BACKLIGHT_OFF=0x00;
my $ENABLE=0b00000100;
my $E_PULSE=500;
my $E_DELAY=500;
sub lcd_toggle_enable {
my ($bits)=@_;
usleep($E_PULSE);
my $rc = $dev->writeByteData(0x00,$bits | $ENABLE);
$rc = $dev->writeByteData(0x00,$bits & ~$ENABLE);
}
sub lcd_byte {
my ($bits,$mode) = @_;
my $bits_high=0;my $bits_low=0;
$bits_high = $mode | ($bits & 0xf0) | $LCD_BACKLIGHT;
$bits_low = $mode | (($bits << 4) & 0xf0) | $LCD_BACKLIGHT;
my $rc = $dev->writeByteData(0x00,$bits_high);
lcd_toggle_enable($bits_high);
$rc = $dev->writeByteData(0x00,$bits_low);
lcd_toggle_enable($bits_low);
usleep($E_PULSE);
}
sub lcd_init {
lcd_byte(0x33,$LCD_CMD);
lcd_byte(0x32,$LCD_CMD);
lcd_byte(0x06,$LCD_CMD);
lcd_byte(0x0c,$LCD_CMD);
lcd_byte(0x28,$LCD_CMD);
lcd_byte(0x01,$LCD_CMD);
usleep($E_PULSE);
}
sub lcd_string {
my ($message, $line) = @_;
lcd_byte($line, $LCD_CMD);
for (0..$LCD_WIDTH) {$message=$message.$FILL_CHAR};
for (my $i=0;$i<($LCD_WIDTH);$i++) {lcd_byte(ord(substr($message,$i,
+1)),$LCD_CHR);}
}
# MAIN
while (1==1) {
lcd_init();
lcd_string("RPiSpy <",$LCD_LINE_1);
lcd_string("I2C LCD <",$LCD_LINE_2);
sleep(5);
lcd_string("> RPiSpy",$LCD_LINE_1);
lcd_string("> I2C LCD",$LCD_LINE_2);
sleep(5);
}
|