Python driving 44870 LCD


87 posts   Page 2 of 4   1, 2, 3, 4
by kghunt » Wed Jul 18, 2012 10:52 pm
I made my own change to the class so I can write to each line separately.

I added this to end and removed the wrap around if statement in message.
Code: Select all
def Writeline(self,message,line):
          if line == 1:
                  self.cmd(128)
                  self.message(message)
          if line == 2:
                  self.cmd(192)
                  self.message(message)


I find this better to update just a line at a time so if I have a clock on line 1 I can update it without changing line 2.
Posts: 348
Joined: Sun Mar 04, 2012 9:28 am
by texy » Thu Jul 19, 2012 5:56 am
Good stuff. What about lines 3 and 4 ;-)
Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2210
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by kghunt » Thu Jul 19, 2012 8:50 am
I did consider adding lines 3 and 4 but my main use is 2 line although I do have a couple of 4 lines kicking around maybe add it in for good measure.
Posts: 348
Joined: Sun Mar 04, 2012 9:28 am
by Reider » Fri Jul 20, 2012 8:12 am
Just a simple change;

Code: Select all
def Writeline(self,message,line):
          if line == 1:
                  self.cmd(128)
                  self.message(message)
          if line == 2:
                  self.cmd(192)
                  self.message(message)
          if line == 3:
                  self.cmd(148)
                  self.message(message)
          if line == 4:
                  self.cmd(212)
                  self.message(message)


Things are much better now we can address 2 lines and/or 4 lines on an LCD. I`ve started working on some LCD screens (programs) now. The first one being to read a 4 line text file from the desktop and add the respective output lines to the LCD window. Change the file contents and the LCD view changes content too, without making up a fresh Python program for the new contents.

Steve
Posts: 75
Joined: Sun Mar 04, 2012 12:00 pm
by kghunt » Fri Jul 20, 2012 8:18 am
I am way ahead of you. My app gets the idle display details from an ini file and outputs it to the LCD. If my keypad is pressed the program changes and the prompts change to match then when I present a Mifare card to my reader it calculates the UID of the card does a look up then displays the persons name on the screen followed by a message ie "has clocked in". Then the data from that transaction is passed to another process which builds and XML file then posts it to a web service for processing.

:D

Look for PiClock in the forums.
Posts: 348
Joined: Sun Mar 04, 2012 9:28 am
by kghunt » Fri Jul 20, 2012 10:19 am
Reider wrote:Just a simple change;

Code: Select all
def Writeline(self,message,line):
          if line == 1:
                  self.cmd(128)
                  self.message(message)
          if line == 2:
                  self.cmd(192)
                  self.message(message)
          if line == 3:
                  self.cmd(148)
                  self.message(message)
          if line == 4:
                  self.cmd(212)
                  self.message(message)


Things are much better now we can address 2 lines and/or 4 lines on an LCD. I`ve started working on some LCD screens (programs) now. The first one being to read a 4 line text file from the desktop and add the respective output lines to the LCD window. Change the file contents and the LCD view changes content too, without making up a fresh Python program for the new contents.

Steve


this is what I went for. It pads strings out to 16 characters if they are less. That way if you write a message to the LCD and its shorter than the previous message then you don't get part of the old message still on the screen.

Code: Select all
    def Writeline(self, message, line):
      message = message.ljust(16)
      message = message [:16]
      if line == 1:
         self.cmd(128)
         self.message(message)
      elif line == 2:
         self.cmd(192)
         self.message(message)
      elif line == 3:
         self.cmd(148)
         self.message(message)
      elif line == 4:
         self.cmd(212)
         self.message(message)
Posts: 348
Joined: Sun Mar 04, 2012 9:28 am
by paaland » Mon Jul 23, 2012 6:52 pm
I need some help here. I bought a 44870 LCD off Ebay. Specs are here:
http://www.fcb-electronics.com/pdf/FCB162-LCD.pdf

I'm trying to use the h44870.py provided on this forum. I've got the LCD connected as follows:
Code: Select all
LCD           E (6) | RS (4) | DB4 (11) | DB5 (12) | DB6 (13) | DB7 (14) |
Raspi.BCM        23 | 24     | 4        | 17       | 21       | 22       |
Raspi.GPIO       16 | 18     | 7        | 11       | 13       | 15       |


This seems ok to run the lcd in 4-bit mode. But when I run the code nothing happens (expect that the backlight for the LCD is on).

I've also connected LCD pins 1 and 16 to 0V (GRND) and 1 and 15 to V5+.

Any help is appreciated.
Dragon 32>BBC Model B>PC>PC>PC>PC>Raspberry PI (circle complete)
User avatar
Posts: 68
Joined: Mon Mar 12, 2012 2:30 pm
Location: Norway
by kghunt » Mon Jul 23, 2012 7:06 pm
paaland wrote:
I've also connected LCD pins 1 and 16 to 0V (GRND) and 1 and 15 to V5+.

Any help is appreciated.


Have you connected rw to ground? Also you say above you have connected pin 1 to 5v and ground?

On mine I connected db0 - 3 to gnd also. Plus you'll need at least a 2k resistor between pin 2 and + or the contrast will be too high to see anything.
Posts: 348
Joined: Sun Mar 04, 2012 9:28 am
by texy » Mon Jul 23, 2012 7:07 pm
Are DB0 - DB3 and the R/W line also tied to 0volts?
Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2210
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by kghunt » Mon Jul 23, 2012 7:09 pm
texy wrote:Are DB0 - DB3 and the R/W line also tied to 0volts?
Texy



I'm the winner :-)
Posts: 348
Joined: Sun Mar 04, 2012 9:28 am
by paaland » Mon Jul 23, 2012 7:19 pm
@kghunt and @texy

db0-db3 and r/w are all unconnected. I'll try to set them to grd just in case.
Also contrast is unconnected, I assumed a default value of something visible. I'll try a 2k resistor between pin 2 and Vcc.

The statement "I've also connected LCD pins 1 and 16 to 0V (GRND) and 1 and 15 to V5+" was ofcause a typo. It's 1 and 16 to 0V and 2 and 15 to Vcc.

Thank you both for your assistance.
Dragon 32>BBC Model B>PC>PC>PC>PC>Raspberry PI (circle complete)
User avatar
Posts: 68
Joined: Mon Mar 12, 2012 2:30 pm
Location: Norway
by texy » Mon Jul 23, 2012 7:25 pm
kghunt wrote:
texy wrote:Are DB0 - DB3 and the R/W line also tied to 0volts?
Texy



I'm the winner :-)


....by 1 seconds, lol :lol:

Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2210
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by paaland » Mon Jul 23, 2012 8:34 pm
Thank you both for your help. I added the extra ground connectors and the 2K resistor, but nothing helped. Then I decided to start from scratch with the new Raspbian image. Turned out the problem was the RPi.GPIO python library I was using. Version 0.3.0a had a major bug where the high and low's were inverted on outputs. Upgraded to 0.3.1a and all is now working.

Image
Dragon 32>BBC Model B>PC>PC>PC>PC>Raspberry PI (circle complete)
User avatar
Posts: 68
Joined: Mon Mar 12, 2012 2:30 pm
Location: Norway
by kghunt » Mon Jul 23, 2012 8:40 pm
Awesome. Out of interest how much did you pay for your LCD mine was £1.66

I have another that I bought with a USB controller attached (£16) and also a 4 line that I harvested from an embedded device but that one doesn't have a back light.

The price of these displays varies a lot.

I'm considering importing a job lot of these displays for resale.
Posts: 348
Joined: Sun Mar 04, 2012 9:28 am
by paaland » Mon Jul 23, 2012 8:57 pm
I paid $9 (usd) including shipping from the US to Norway. Can't complain since it's much cheaper than purchasing locally.
Dragon 32>BBC Model B>PC>PC>PC>PC>Raspberry PI (circle complete)
User avatar
Posts: 68
Joined: Mon Mar 12, 2012 2:30 pm
Location: Norway
by paaland » Tue Jul 24, 2012 7:44 am
For anyone else interested in this project I've made this schematic overview of the connections involved.

Image

http://i303.photobucket.com/albums/nn127/paaland/44870LCD-RaspiGPIO.png
Dragon 32>BBC Model B>PC>PC>PC>PC>Raspberry PI (circle complete)
User avatar
Posts: 68
Joined: Mon Mar 12, 2012 2:30 pm
Location: Norway
by kghunt » Tue Jul 24, 2012 8:01 am
Awesome although on mine I have trim pots on VD (pin 3) and K (pin 16) so I can vary the contrast and brightness.

This should go on the elinux RPi wiki.
Posts: 348
Joined: Sun Mar 04, 2012 9:28 am
by paaland » Tue Jul 24, 2012 9:29 am
There's an even better drawing that someone else made here:
http://pbambridge.brunelweb.net/blog/wp ... CD2_bb.png

Read the full post at http://pbambridge.brunelweb.net/blog/?p=64
Dragon 32>BBC Model B>PC>PC>PC>PC>Raspberry PI (circle complete)
User avatar
Posts: 68
Joined: Mon Mar 12, 2012 2:30 pm
Location: Norway
by texy » Wed Aug 08, 2012 5:05 pm
kghunt wrote:Awesome. Out of interest how much did you pay for your LCD mine was £1.66

I have another that I bought with a USB controller attached (£16) and also a 4 line that I harvested from an embedded device but that one doesn't have a back light.

The price of these displays varies a lot.

I'm considering importing a job lot of these displays for resale.


So how many of these did you import ;-)

Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2210
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by kghunt » Wed Aug 08, 2012 5:54 pm
Just 10 initially i am selling them for 4.50 which should cover taxes and shipping with a small margin on top. I have sold a few to friends who have pis and kept a few for my own projects. I just need some stepper motors now then my kit box will be pretty awesome.
Posts: 348
Joined: Sun Mar 04, 2012 9:28 am
by jhd » Thu Aug 16, 2012 11:08 pm
hi kghunt

could you share a sample for 1602 text writing please
Site FR RaspBerry Pi: http://www.zem.fr
Posts: 33
Joined: Thu Aug 02, 2012 1:11 pm
Location: france (44)
by texy » Sun Nov 04, 2012 7:02 pm
With thanks to Matt Hawkins of http://www.raspberrypi-spy.co.uk fame, here is updated code which implements RPi.GPIO to drive the LCD and wiringpi to
drive the backlight LED via hardware PWM :

Code: Select all
#!/usr/bin/python
#
# HD44780 LCD Test Script for
# Raspberry Pi
#
# Author : Matt Hawkins + Texy
# Site   : http://www.raspberrypi-spy.co.uk
#
# Date   : 28/10/2012
#
# uses RPi.GPIO to drive the LCD and
# wiringpi to drive the backlight LED via hardware PWM

#import
import RPi.GPIO as GPIO
import time
import wiringpi

# Define GPIO to LCD mapping
LCD_RS = 17
LCD_E  = 4
LCD_D4 = 24
LCD_D5 = 22
LCD_D6 = 23
LCD_D7 = 21
LCD_LED = 18

# Define some device constants
LCD_WIDTH = 16    # Maximum characters per line
LCD_CHR = True
LCD_CMD = False

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line

# Timing constants
E_PULSE = 0.00005
E_DELAY = 0.00005

def main():
  # Main program block
  GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  GPIO.setwarnings(False)
  GPIO.setup(LCD_E, GPIO.OUT)  # E
  GPIO.setup(LCD_RS, GPIO.OUT) # RS
  GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  wiringpi.wiringPiSetupGpio()
  wiringpi.pinMode(LCD_LED, 2) # LED set up as PWM
  wiringpi.pwmWrite(LCD_LED, 128)
  # Initialise display
  lcd_init()

  # Send some test
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("wiringpi and")
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("RPi.GPIO 2gether")

def lcd_init():
  # Initialise display
  lcd_byte(0x33,LCD_CMD)
  lcd_byte(0x32,LCD_CMD)
  lcd_byte(0x28,LCD_CMD)
  lcd_byte(0x0C,LCD_CMD) 
  lcd_byte(0x06,LCD_CMD)
  lcd_byte(0x01,LCD_CMD) 

def lcd_string(message):
  # Send string to display

  message = message.ljust(LCD_WIDTH," ") 

  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)

def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = data
  # mode = True  for character
  #        False for command

  GPIO.output(LCD_RS, mode) # RS

  # High bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x10==0x10:
    GPIO.output(LCD_D4, True)
  if bits&0x20==0x20:
    GPIO.output(LCD_D5, True)
  if bits&0x40==0x40:
    GPIO.output(LCD_D6, True)
  if bits&0x80==0x80:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  time.sleep(E_DELAY)   
  GPIO.output(LCD_E, True) 
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False) 
  time.sleep(E_DELAY)     

  # Low bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x01==0x01:
    GPIO.output(LCD_D4, True)
  if bits&0x02==0x02:
    GPIO.output(LCD_D5, True)
  if bits&0x04==0x04:
    GPIO.output(LCD_D6, True)
  if bits&0x08==0x08:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  time.sleep(E_DELAY)   
  GPIO.output(LCD_E, True) 
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False) 
  time.sleep(E_DELAY)   

if __name__ == '__main__':
  main()


Output displayed here on one of my new LCD add on boards :
Image

Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2210
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by alexeames » Sun Nov 04, 2012 10:40 pm
Ooh I need hardware PWM in python.

Code: Select all
 wiringpi.wiringPiSetupGpio()
wiringpi.pinMode(LCD_LED, 2) # LED set up as PWM
wiringpi.pwmWrite(LCD_LED, 128)


I couldn't find this info anywhere Texy. Where is it documented please?
My Pi uses 2 watts - what what? ---- HiRes early production Pi photos RS Front Back | Farnell Front Back
User avatar
Posts: 2018
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by texy » Sun Nov 04, 2012 10:44 pm
It's isn't. At least on GitHub. Read through the wiringpi thread where gadgetoid explains it, but my code shows the principles.
Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2210
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by alexeames » Mon Nov 05, 2012 8:15 am
texy wrote:It's isn't. At least on GitHub. Read through the wiringpi thread where gadgetoid explains it, but my code shows the principles.
Texy


Great, thanks :) Early tests are promising, i.e. it works :lol:
My Pi uses 2 watts - what what? ---- HiRes early production Pi photos RS Front Back | Farnell Front Back
User avatar
Posts: 2018
Joined: Sat Mar 03, 2012 11:57 am
Location: UK