Advertisement
Guest User

Untitled

a guest
Jun 20th, 2015
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.82 KB | None | 0 0
  1. #!/usr/bin/python
  2. #--------------------------------------
  3. #    ___  ___  _ ____
  4. #   / _ \/ _ \(_) __/__  __ __
  5. #  / , _/ ___/ /\ \/ _ \/ // /
  6. # /_/|_/_/  /_/___/ .__/\_, /
  7. #                /_/   /___/
  8. #
  9. #  lcd_16x2.py
  10. #  20x4 LCD Test Script with
  11. #  backlight control and text justification
  12. #
  13. # Author : Matt Hawkins
  14. # Date   : 06/04/2015
  15. #
  16. # http://www.raspberrypi-spy.co.uk/
  17. #
  18. #--------------------------------------
  19.  
  20. # The wiring for the LCD is as follows:
  21. # 1 : GND
  22. # 2 : 5V
  23. # 3 : Contrast (0-5V)*
  24. # 4 : RS (Register Select)
  25. # 5 : R/W (Read Write)       - GROUND THIS PIN
  26. # 6 : Enable or Strobe
  27. # 7 : Data Bit 0             - NOT USED
  28. # 8 : Data Bit 1             - NOT USED
  29. # 9 : Data Bit 2             - NOT USED
  30. # 10: Data Bit 3             - NOT USED
  31. # 11: Data Bit 4
  32. # 12: Data Bit 5
  33. # 13: Data Bit 6
  34. # 14: Data Bit 7
  35. # 15: LCD Backlight +5V**
  36. # 16: LCD Backlight GND
  37.  
  38. #import
  39. import RPi.GPIO as GPIO
  40. import time
  41.  
  42. # Define GPIO to LCD mapping
  43. LCD_RS = 7
  44. LCD_E  = 8
  45. LCD_D4 = 25
  46. LCD_D5 = 24
  47. LCD_D6 = 23
  48. LCD_D7 = 18
  49. LED_ON = 15
  50.  
  51. # Define some device constants
  52. LCD_WIDTH = 20    # Maximum characters per line
  53. LCD_CHR = True
  54. LCD_CMD = False
  55.  
  56. LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
  57. LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
  58. LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
  59. LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
  60.  
  61. # Timing constants
  62. E_PULSE = 0.0005
  63. E_DELAY = 0.0005
  64.  
  65. def main():
  66.   # Main program block
  67.  
  68.   GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  69.   GPIO.setup(LCD_E, GPIO.OUT)  # E
  70.   GPIO.setup(LCD_RS, GPIO.OUT) # RS
  71.   GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  72.   GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  73.   GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  74.   GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  75.   GPIO.setup(LED_ON, GPIO.OUT) # Backlight enable
  76.  
  77.   # Initialise display
  78.   lcd_init()
  79.  
  80.   # Toggle backlight on-off-on
  81.   lcd_backlight(True)
  82.   time.sleep(0.5)
  83.   lcd_backlight(False)
  84.   time.sleep(0.5)
  85.   lcd_backlight(True)
  86.   time.sleep(0.5)
  87.  
  88.   while True:
  89.  
  90.     # Send some centred test
  91.     lcd_string("--------------------",LCD_LINE_1,2)
  92.     lcd_string("Rasbperry Pi",LCD_LINE_2,2)
  93.     lcd_string("Model B",LCD_LINE_3,2)
  94.     lcd_string("--------------------",LCD_LINE_4,2)
  95.  
  96.     time.sleep(3) # 3 second delay
  97.  
  98.     lcd_string("Raspberrypi-spy",LCD_LINE_1,3)
  99.     lcd_string(".co.uk",LCD_LINE_2,3)
  100.     lcd_string("",LCD_LINE_3,2)
  101.     lcd_string("20x4 LCD Module Test",LCD_LINE_4,2)
  102.  
  103.     time.sleep(3) # 20 second delay
  104.  
  105.     # Blank display
  106.     lcd_byte(0x01, LCD_CMD)
  107.  
  108.     time.sleep(3) # 3 second delay
  109.  
  110. def lcd_init():
  111.   # Initialise display
  112.   lcd_byte(0x33,LCD_CMD) # 110011 Initialise
  113.   lcd_byte(0x32,LCD_CMD) # 110010 Initialise
  114.   lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
  115.   lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
  116.   lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
  117.   lcd_byte(0x01,LCD_CMD) # 000001 Clear display
  118.   time.sleep(E_DELAY)
  119.  
  120. def lcd_byte(bits, mode):
  121.   # Send byte to data pins
  122.   # bits = data
  123.   # mode = True  for character
  124.   #        False for command
  125.  
  126.   GPIO.output(LCD_RS, mode) # RS
  127.  
  128.   # High bits
  129.   GPIO.output(LCD_D4, False)
  130.   GPIO.output(LCD_D5, False)
  131.   GPIO.output(LCD_D6, False)
  132.   GPIO.output(LCD_D7, False)
  133.   if bits&0x10==0x10:
  134.     GPIO.output(LCD_D4, True)
  135.   if bits&0x20==0x20:
  136.     GPIO.output(LCD_D5, True)
  137.   if bits&0x40==0x40:
  138.     GPIO.output(LCD_D6, True)
  139.   if bits&0x80==0x80:
  140.     GPIO.output(LCD_D7, True)
  141.  
  142.   # Toggle 'Enable' pin
  143.   lcd_toggle_enable()
  144.  
  145.   # Low bits
  146.   GPIO.output(LCD_D4, False)
  147.   GPIO.output(LCD_D5, False)
  148.   GPIO.output(LCD_D6, False)
  149.   GPIO.output(LCD_D7, False)
  150.   if bits&0x01==0x01:
  151.     GPIO.output(LCD_D4, True)
  152.   if bits&0x02==0x02:
  153.     GPIO.output(LCD_D5, True)
  154.   if bits&0x04==0x04:
  155.     GPIO.output(LCD_D6, True)
  156.   if bits&0x08==0x08:
  157.     GPIO.output(LCD_D7, True)
  158.  
  159.   # Toggle 'Enable' pin
  160.   lcd_toggle_enable()
  161.  
  162. def lcd_toggle_enable():
  163.   # Toggle enable
  164.   time.sleep(E_DELAY)
  165.   GPIO.output(LCD_E, True)
  166.   time.sleep(E_PULSE)
  167.   GPIO.output(LCD_E, False)
  168.   time.sleep(E_DELAY)
  169.  
  170. def lcd_string(message,line,style):
  171.   # Send string to display
  172.   # style=1 Left justified
  173.   # style=2 Centred
  174.   # style=3 Right justified
  175.  
  176.   if style==1:
  177.     message = message.ljust(LCD_WIDTH," ")
  178.   elif style==2:
  179.     message = message.center(LCD_WIDTH," ")
  180.   elif style==3:
  181.     message = message.rjust(LCD_WIDTH," ")
  182.  
  183.   lcd_byte(line, LCD_CMD)
  184.  
  185.   for i in range(LCD_WIDTH):
  186.     lcd_byte(ord(message[i]),LCD_CHR)
  187.  
  188. def lcd_backlight(flag):
  189.   # Toggle backlight on-off-on
  190.   GPIO.output(LED_ON, flag)
  191.  
  192. if __name__ == '__main__':
  193.  
  194.   try:
  195.     main()
  196.   except KeyboardInterrupt:
  197.     pass
  198.   finally:
  199.     lcd_byte(0x01, LCD_CMD)
  200.     lcd_string("Goodbye!",LCD_LINE_1,2)
  201.     GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement