Moderators: adafruit_support_bill, adafruit
rustybikes wrote:I know I'm late to the party here, but I have also seen the problem where line 0 and 1 get swapped. Adding another data-point, the behaviour is different when powered via USB vs. external. It's been a while since I poked at this, but IIRC, on USB they're reversed. On external, they display as intended. Or maybe the other way round. I don't remember.
@techman - did you ever get the backpack working? I've a project that really really needs OLED, but I'm limited to SPI (maybe i2c) by the number of pins used on the Ardu by the shield in use.
void setup(void) {
lcd.begin(16,2); // initialize the lcd
// Initialization sequence is not quite as documented by Winstar.
// Documented sequence only works on initial power-up. An additional
// step is required to handle a warm-restart.
//
// In the data sheet, the timing specs are all zeros(!). These have been tested to
// reliably handle both warm & cold starts
//
lcd.write4bits(0x03); // Missing step from doc. Thanks to Elco Jacobs
delayMicroseconds(5000);
lcd.write4bits(0x02);
delayMicroseconds(5000);
lcd.write4bits(0x02);
delayMicroseconds(5000);
lcd.write4bits(0x08);
delayMicroseconds(5000);
lcd.command(0x08); // Turn Off
delayMicroseconds(5000);
lcd.command(0x01); // Clear Display
delayMicroseconds(5000);
lcd.command(0x06); // Set Entry Mode
delayMicroseconds(5000);
lcd.command(0x02); // Home Cursor
delayMicroseconds(5000);
lcd.command(0x0C); // Turn On - enable cursor & blink
delayMicroseconds(5000);
lcd.home();
lcd.print("Booting...");
delay(1000);
lcd.print("Booted...");
}
Yes I was able to get it working with the backpack. I took the adafruit_LiquidCrystal library, made the write4bits public, and added the following. It's was brute force.
/*
Demonstration sketch for Adafruit i2c/SPI LCD backpack
using 74HC595 SPI expander
( http://www.ladyada.net/products/i2cspilcdbackpack/index.html )
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
* 5V to Arduino 5V pin
* GND to Arduino GND pin
* CLK to Digital 2
* DAT to Digital 3
* LAT to Digital 4
*/
// include the library code:
#include "Wire.h"
#include "LiquidCrystal.h"
// Connect via SPI. Data pin is #3, Clock is #2 and Latch is #4
LiquidCrystal lcd(3, 2, 4);
void setup() {
// set up the LCD's number of rows and columns:
lcd.begin(16,2); // initialize the lcd
// Initialization sequence is not quite as documented by Winstar.
// Documented sequence only works on initial power-up. An additional
// step is required to handle a warm-restart.
//
// In the data sheet, the timing specs are all zeros(!). These have been tested to
// reliably handle both warm & cold starts
//
lcd.write4bits(0x03); // Missing step from doc. Thanks to Elco Jacobs
delayMicroseconds(5000);
lcd.write4bits(0x02);
delayMicroseconds(5000);
lcd.write4bits(0x02);
delayMicroseconds(5000);
lcd.write4bits(0x08);
delayMicroseconds(5000);
lcd.command(0x08); // Turn Off
delayMicroseconds(5000);
lcd.command(0x01); // Clear Display
delayMicroseconds(5000);
lcd.command(0x06); // Set Entry Mode
delayMicroseconds(5000);
lcd.command(0x02); // Home Cursor
delayMicroseconds(5000);
lcd.command(0x0C); // Turn On - enable cursor & blink
delayMicroseconds(5000);
lcd.home();
lcd.print("Booting...");
delay(1000);
lcd.print("Booted...");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000);
lcd.setBacklight(HIGH);
//delay(500); // Seizure-inducing. Plus, it's not needed for OLED.
//lcd.setBacklight(LOW);
//delay(500);
}

adafruit_support wrote:These displays are tricky to work with. I've got them to work without using the R/W line, but not very consistently. And once they get out of sync, the only way to reliably recover is to power cycle them.
#!/usr/bin/python
#
# based on code from lrvick and LiquidCrystal
# lrvic - https://github.com/lrvick/raspi-hd44780/blob/master/hd44780.py
# LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp
#
# Changes based on the Adafruit_CharacterOLED library
import RPi.GPIO as GPIO
from time import sleep
class Adafruit_CharLCD:
# commands
LCD_CLEARDISPLAY = 0x01
LCD_RETURNHOME = 0x02
LCD_ENTRYMODESET = 0x04
LCD_DISPLAYCONTROL = 0x08
LCD_CURSORSHIFT = 0x10
LCD_FUNCTIONSET = 0x20
LCD_SETCGRAMADDR = 0x40
LCD_SETDDRAMADDR = 0x80
# flags for display entry mode
LCD_ENTRYRIGHT = 0x00
LCD_ENTRYLEFT = 0x02
LCD_ENTRYSHIFTINCREMENT = 0x01
LCD_ENTRYSHIFTDECREMENT = 0x00
# flags for display on/off control
LCD_DISPLAYON = 0x04
LCD_DISPLAYOFF = 0x00
LCD_CURSORON = 0x02
LCD_CURSOROFF = 0x00
LCD_BLINKON = 0x01
LCD_BLINKOFF = 0x00
# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00
# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00
LCD_MOVERIGHT = 0x04
LCD_MOVELEFT = 0x00
# flags for function set
LCD_8BITMODE = 0x10
LCD_4BITMODE = 0x00
LCD_2LINE = 0x08
LCD_1LINE = 0x00
LCD_5x10DOTS = 0x04
LCD_5x8DOTS = 0x00
def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22]):
self.pin_rs = pin_rs
self.pin_e = pin_e
self.pins_db = pins_db
GPIO.setmode(GPIO.BCM)
# set the control pins as outputs
GPIO.setup(self.pin_e, GPIO.OUT)
GPIO.setup(self.pin_rs, GPIO.OUT)
# set the data pins as outputs
for pin in self.pins_db:
GPIO.setup(pin, GPIO.OUT)
# pull the control pins low
GPIO.output(self.pin_rs, False)
GPIO.output(self.pin_e, False)
# special setup sauce
self.write4bits(0x03)
self.delayMicroseconds(5000)
self.write4bits(0x02)
self.delayMicroseconds(5000)
self.write4bits(0x02)
self.delayMicroseconds(5000)
self.write4bits(0x08)
self.delayMicroseconds(5000)
# turn off
self.write4bits(0x08)
self.delayMicroseconds(5000)
# clear display
self.write4bits(0x01)
self.delayMicroseconds(5000)
# set entry mode
self.write4bits(0x06)
self.delayMicroseconds(5000)
# home cursor
self.write4bits(0x02)
self.delayMicroseconds(5000)
# turn on - enable cursor and blink
self.write4bits(0x0c)
self.delayMicroseconds(5000)
def begin(self, cols, lines):
if (lines > 1):
self.numlines = lines
self.displayfunction |= self.LCD_2LINE
self.currline = 0
def home(self):
self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero
self.delayMicroseconds(2000) # this command takes a long time!
def clear(self):
self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display
self.delayMicroseconds(2000) # 2000 microsecond sleep, clearing the display takes a long time
def setCursor(self, col, row):
self.row_offsets = [ 0x00, 0x40, 0x14, 0x54 ]
if ( row > self.numlines ):
row = self.numlines - 1 # we count rows starting w/0
self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))
def noDisplay(self):
""" Turn the display off (quickly) """
self.displaycontrol &= ~self.LCD_DISPLAYON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def display(self):
""" Turn the display on (quickly) """
self.displaycontrol |= self.LCD_DISPLAYON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def noCursor(self):
""" Turns the underline cursor on/off """
self.displaycontrol &= ~self.LCD_CURSORON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def cursor(self):
""" Cursor On """
self.displaycontrol |= self.LCD_CURSORON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def noBlink(self):
""" Turn on and off the blinking cursor """
self.displaycontrol &= ~self.LCD_BLINKON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def noBlink(self):
""" Turn on and off the blinking cursor """
self.displaycontrol &= ~self.LCD_BLINKON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def DisplayLeft(self):
""" These commands scroll the display without changing the RAM """
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)
def scrollDisplayRight(self):
""" These commands scroll the display without changing the RAM """
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT);
def leftToRight(self):
""" This is for text that flows Left to Right """
self.displaymode |= self.LCD_ENTRYLEFT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode);
def rightToLeft(self):
""" This is for text that flows Right to Left """
self.displaymode &= ~self.LCD_ENTRYLEFT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
def autoscroll(self):
""" This will 'right justify' text from the cursor """
self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
def noAutoscroll(self):
""" This will 'left justify' text from the cursor """
self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
def write4bits(self, bits, char_mode=False):
""" Send command to LCD """
self.delayMicroseconds(1000) # 1000 microsecond sleep
bits=bin(bits)[2:].zfill(8)
GPIO.output(self.pin_rs, char_mode)
for pin in self.pins_db:
GPIO.output(pin, False)
for i in range(4):
if bits[i] == "1":
GPIO.output(self.pins_db[::-1][i], True)
self.pulseEnable()
for pin in self.pins_db:
GPIO.output(pin, False)
for i in range(4,8):
if bits[i] == "1":
GPIO.output(self.pins_db[::-1][i-4], True)
# added 50 us delay
self.delayMicroseconds(50)
self.pulseEnable()
def delayMicroseconds(self, microseconds):
seconds = microseconds / 1000000 # divide microseconds by 1 million for seconds
sleep(seconds)
def pulseEnable(self):
GPIO.output(self.pin_e, False)
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
GPIO.output(self.pin_e, True)
# changed to 50 us
self.delayMicroseconds(50) # 1 microsecond pause - enable pulse must be > 450ns
GPIO.output(self.pin_e, False)
self.delayMicroseconds(1) # commands need > 37us to settle
def message(self, text):
""" Send string to LCD. Newline wraps to second line"""
for char in text:
if char == '\n':
self.write4bits(0xC0) # next line
else:
self.write4bits(ord(char),True)
if __name__ == '__main__':
lcd = Adafruit_CharLCD()
lcd.clear()
lcd.message(" Adafruit 16x2\n Standard LCD")
I've got the r/w pin grounded since in the Arduino library it never seems to leave the LOW state.
#!/usr/bin/python
#
# based on code from lrvick and LiquidCrystal
# lrvic - https://github.com/lrvick/raspi-hd44780/blob/master/hd44780.py
# LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp
#
import RPi.GPIO as GPIO
from time import sleep
class Adafruit_CharOLED:
# commands
LCD_CLEARDISPLAY = 0x01
LCD_RETURNHOME = 0x02
LCD_ENTRYMODESET = 0x04
LCD_DISPLAYCONTROL = 0x08
LCD_CURSORSHIFT = 0x10
LCD_FUNCTIONSET = 0x20
LCD_SETCGRAMADDR = 0x40
LCD_SETDDRAMADDR = 0x80
# flags for display entry mode
LCD_ENTRYRIGHT = 0x00
LCD_ENTRYLEFT = 0x02
LCD_ENTRYSHIFTINCREMENT = 0x01
LCD_ENTRYSHIFTDECREMENT = 0x00
# flags for display on/off control
LCD_DISPLAYON = 0x04
LCD_DISPLAYOFF = 0x00
LCD_CURSORON = 0x02
LCD_CURSOROFF = 0x00
LCD_BLINKON = 0x01
LCD_BLINKOFF = 0x00
# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00
# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00
LCD_MOVERIGHT = 0x04
LCD_MOVELEFT = 0x00
# flags for function set
LCD_8BITMODE = 0x10
LCD_4BITMODE = 0x00
LCD_2LINE = 0x08
LCD_1LINE = 0x00
LCD_5x10DOTS = 0x04
LCD_5x8DOTS = 0x00
def __init__(self, pin_rw=18, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22]):
self.pin_rw = pin_rw
self.pin_rs = pin_rs
self.pin_e = pin_e
self.pins_db = pins_db
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.pin_rw, GPIO.OUT)
GPIO.setup(self.pin_e, GPIO.OUT)
GPIO.setup(self.pin_rs, GPIO.OUT)
GPIO.output(self.pin_rs, False)
GPIO.output(self.pin_e, False)
GPIO.output(self.pin_rw, False)
self.busy_pin = 22
for pin in self.pins_db:
GPIO.setup(pin, GPIO.OUT)
# copied from Adafruit_CharacterOLED.cpp
self.write4bits(0x03, True)
self.delayMicroseconds(5000)
self.write4bits(0x02, True)
self.delayMicroseconds(5000)
self.write4bits(0x02, True)
self.delayMicroseconds(5000)
self.write4bits(0x08, True)
self.delayMicroseconds(5000)
self.write4bits(0x08)
self.delayMicroseconds(5000)
self.write4bits(0x01)
self.delayMicroseconds(5000)
self.write4bits(0x06)
self.delayMicroseconds(5000)
self.write4bits(0x02)
self.delayMicroseconds(5000)
self.write4bits(0x0c)
self.delayMicroseconds(5000)
# self.write4bits(0x33) # initialization
# self.write4bits(0x32) # initialization
# self.write4bits(0x28) # 2 line 5x7 matrix
# self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor
# self.write4bits(0x06) # shift cursor right
self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF
self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS
self.displayfunction |= self.LCD_2LINE
""" Initialize to default text direction (for romance languages) """
self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode
self.clear()
def begin(self, cols, lines):
if (lines > 1):
self.numlines = lines
self.displayfunction |= self.LCD_2LINE
self.currline = 0
def waitForReady(self):
self.busy = True
GPIO.setup(self.busy_pin, GPIO.IN)
GPIO.output(self.pin_rs, False)
GPIO.output(self.pin_rw, True)
while True:
GPIO.output(self.pin_e, False)
GPIO.output(self.pin_e, True)
self.delayMicroseconds(10)
self.busy = GPIO.input(self.busy_pin)
GPIO.output(self.pin_e, False)
self.pulseEnable()
if (self.busy == False):
break
GPIO.setup(self.busy_pin, GPIO.OUT)
GPIO.output(self.pin_rw, False)
def home(self):
self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero
self.delayMicroseconds(2000) # this command takes a long time!
def clear(self):
self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display
self.delayMicroseconds(2000) # 2000 microsecond sleep, clearing the display takes a long time
def setCursor(self, col, row):
self.row_offsets = [ 0x00, 0x40, 0x14, 0x54 ]
if ( row > self.numlines ):
row = self.numlines - 1 # we count rows starting w/0
self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))
def noDisplay(self):
""" Turn the display off (quickly) """
self.displaycontrol &= ~self.LCD_DISPLAYON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def display(self):
""" Turn the display on (quickly) """
self.displaycontrol |= self.LCD_DISPLAYON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def noCursor(self):
""" Turns the underline cursor on/off """
self.displaycontrol &= ~self.LCD_CURSORON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def cursor(self):
""" Cursor On """
self.displaycontrol |= self.LCD_CURSORON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def noBlink(self):
""" Turn on and off the blinking cursor """
self.displaycontrol &= ~self.LCD_BLINKON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def noBlink(self):
""" Turn on and off the blinking cursor """
self.displaycontrol &= ~self.LCD_BLINKON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def DisplayLeft(self):
""" These commands scroll the display without changing the RAM """
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)
def scrollDisplayRight(self):
""" These commands scroll the display without changing the RAM """
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT);
def leftToRight(self):
""" This is for text that flows Left to Right """
self.displaymode |= self.LCD_ENTRYLEFT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode);
def rightToLeft(self):
""" This is for text that flows Right to Left """
self.displaymode &= ~self.LCD_ENTRYLEFT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
def autoscroll(self):
""" This will 'right justify' text from the cursor """
self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
def noAutoscroll(self):
""" This will 'left justify' text from the cursor """
self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
def write4bits(self, bits, char_mode=False):
""" Send command to LCD """
self.delayMicroseconds(1000) # 1000 microsecond sleep
bits=bin(bits)[2:].zfill(8)
GPIO.output(self.pin_rs, char_mode)
for pin in self.pins_db:
GPIO.output(pin, False)
for i in range(4):
if bits[i] == "1":
GPIO.output(self.pins_db[::-1][i], True)
self.pulseEnable()
for pin in self.pins_db:
GPIO.output(pin, False)
for i in range(4,8):
if bits[i] == "1":
GPIO.output(self.pins_db[::-1][i-4], True)
self.pulseEnable()
self.waitForReady()
def delayMicroseconds(self, microseconds):
seconds = microseconds / 1000000 # divide microseconds by 1 million for seconds
sleep(seconds)
def pulseEnable(self):
GPIO.output(self.pin_e, False)
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
GPIO.output(self.pin_e, True)
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
GPIO.output(self.pin_e, False)
self.delayMicroseconds(1) # commands need > 37us to settle
def message(self, text):
""" Send string to LCD. Newline wraps to second line"""
for char in text:
if char == '\n':
self.write4bits(0xC0) # next line
else:
self.write4bits(ord(char),True)
if __name__ == '__main__':
lcd = Adafruit_CharOLED()
lcd.clear()
lcd.message(" Adafruit 16x2\n Standard LCD")uhclem wrote:On a cold power-up it does not work right but after that it does. I still have to figure out why there is no space between the characters.
Here's my code (don't forget this is python meant for python GPIO library on the Raspberry Pi):
Return to General Project help
Users browsing this forum: No registered users and 12 guests