LCD_DT161A.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #!/usr/bin/env python2
  2. __author__ = 'bnj'
  3. #--------------------------------------
  4. # ___ ___ _ ____
  5. # / _ \/ _ \(_) __/__ __ __
  6. # / , _/ ___/ /\ \/ _ \/ // /
  7. # /_/|_/_/ /_/___/ .__/\_, /
  8. # /_/ /___/
  9. #
  10. # lcd_16x2.py
  11. # 20x4 LCD Test Script with
  12. # backlight control and text justification
  13. #
  14. # Author : Matt Hawkins
  15. # Date : 06/04/2015
  16. #
  17. # http://www.raspberrypi-spy.co.uk/
  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. #import
  38. import RPi.GPIO as GPIO
  39. import time
  40. # Define GPIO to LCD mapping
  41. LCD_RS = 7
  42. LCD_E = 8
  43. LCD_D0 = 26
  44. LCD_D1 = 19
  45. LCD_D2 = 13
  46. LCD_D3 = 6
  47. LCD_D4 = 25
  48. LCD_D5 = 24
  49. LCD_D6 = 23
  50. LCD_D7 = 18
  51. LED_ON = 15
  52. # Define some device constants
  53. LCD_WIDTH = 16 # Maximum characters per line
  54. LCD_CHR = True
  55. LCD_CMD = False
  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. # Timing constants
  61. E_PULSE = 0.0005
  62. E_DELAY = 0.0005
  63. # Mode Constant
  64. MODE_INCR = 0x02
  65. MODE_DECR = 0x00
  66. MODE_SHIFT = 0x01
  67. MODE_NO_SHIFT = 0x00
  68. # Display Constant
  69. DISPLAY_ON = 0x04
  70. DISPLAY_OFF = 0x00
  71. CURSOR_ON = 0x02
  72. CURSOR_OFF = 0x00
  73. BLINK_ON = 0x01
  74. BLINK_OFF = 0x00
  75. # Shift Constant
  76. SHIFT_DISPLAY = 0x08
  77. SHIFT_CURSOR = 0x00
  78. SHIFT_RIGHT = 0x04
  79. SHIFT_LEFT = 0x00
  80. # Set function constant
  81. FUNC_4BITS_INT = 0x00
  82. FUNC_8BITS_INT = 0x10
  83. FUNC_1_LINE = 0x00
  84. FUNC_2_LINE = 0x08
  85. FUNC_5x10_DOTS = 0x04
  86. FUNC_5x7_DOTS = 0x00
  87. class LCD_DT161A:
  88. CMD_CLEAR = 0x01
  89. CMD_RETURN = 0x02
  90. CMD_MODE = 0x04
  91. CMD_DISPLAY = 0x08
  92. CMD_SHIFT = 0x10
  93. CMD_FUNCTION = 0x20
  94. CMD_CG_RAM = 0x40
  95. CMD_DD_RAM = 0x80
  96. def __init__(self):
  97. GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
  98. GPIO.setup(LCD_E, GPIO.OUT) # E
  99. GPIO.setup(LCD_RS, GPIO.OUT) # RS
  100. GPIO.setup(LCD_D0, GPIO.OUT) # DB4
  101. GPIO.setup(LCD_D1, GPIO.OUT) # DB5
  102. GPIO.setup(LCD_D2, GPIO.OUT) # DB6
  103. GPIO.setup(LCD_D3, GPIO.OUT) # DB7
  104. GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  105. GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  106. GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  107. GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  108. GPIO.setup(LED_ON, GPIO.OUT) # Backlight enable
  109. def write_byte(self, bits, mode):
  110. """ Send byte to data pins
  111. bits = data
  112. mode = True for character
  113. False for command
  114. """
  115. GPIO.output(LCD_RS, mode) # RS
  116. # High bits
  117. GPIO.output(LCD_D0, False)
  118. GPIO.output(LCD_D1, False)
  119. GPIO.output(LCD_D2, False)
  120. GPIO.output(LCD_D3, False)
  121. GPIO.output(LCD_D4, False)
  122. GPIO.output(LCD_D5, False)
  123. GPIO.output(LCD_D6, False)
  124. GPIO.output(LCD_D7, False)
  125. if bits&0x10==0x10:
  126. GPIO.output(LCD_D4, True)
  127. if bits&0x20==0x20:
  128. GPIO.output(LCD_D5, True)
  129. if bits&0x40==0x40:
  130. GPIO.output(LCD_D6, True)
  131. if bits&0x80==0x80:
  132. GPIO.output(LCD_D7, True)
  133. # Low bits
  134. if bits&0x01==0x01:
  135. GPIO.output(LCD_D0, True)
  136. if bits&0x02==0x02:
  137. GPIO.output(LCD_D1, True)
  138. if bits&0x04==0x04:
  139. GPIO.output(LCD_D2, True)
  140. if bits&0x08==0x08:
  141. GPIO.output(LCD_D3, True)
  142. # Toggle 'Enable' pin
  143. self.lcd_toggle_enable()
  144. def write_cmd(self, cmd):
  145. self.write_byte(cmd, LCD_CMD)
  146. def lcd_toggle_enable(self):
  147. """ Toggle enable """
  148. time.sleep(E_DELAY)
  149. GPIO.output(LCD_E, True)
  150. time.sleep(E_PULSE)
  151. GPIO.output(LCD_E, False)
  152. time.sleep(E_DELAY)
  153. # Basic LCD Command
  154. def clear(self):
  155. self.write_cmd(self.CMD_CLEAR)
  156. def return_home(self):
  157. self.write_cmd(self.CMD_RETURN)
  158. def set_entry_mode(self, move_direction, shifting):
  159. self.write_cmd(self.CMD_MODE + move_direction + shifting)
  160. def display_mode(self,display, cursor, blinking):
  161. self.write_cmd(self.CMD_DISPLAY + display + cursor + blinking)
  162. def shift(self, display_cursor, way):
  163. self.write_cmd(self.CMD_SHIFT + display_cursor + way)
  164. def set_function(self, nb_bits, nb_line, font_size):
  165. self.write_cmd(self.CMD_FUNCTION + nb_bits + nb_line + font_size)
  166. def set_CGRAM_address(self, address):
  167. self.write_cmd(self.CMD_CG_RAM + address)
  168. def set_DDRAM_address(self, address):
  169. self.write_cmd(self.CMD_DD_RAM + address)
  170. def get_busy_address(self):
  171. return (1,1)
  172. def write(self, data):
  173. pass
  174. def read(self):
  175. return 0
  176. # High Level Functions
  177. def write_string(self, message, line, style):
  178. """ Send string to display
  179. style=1 Left justified
  180. style=2 Centred
  181. style=3 Right justified """
  182. if style==1:
  183. message = message.ljust(LCD_WIDTH," ")
  184. elif style==2:
  185. message = message.center(LCD_WIDTH," ")
  186. elif style==3:
  187. message = message.rjust(LCD_WIDTH," ")
  188. self.write_byte(0x00, LCD_CMD)
  189. for i in range(LCD_WIDTH/2):
  190. self.write_byte(ord(message[i]),LCD_CHR)
  191. self.write_byte(0xC0, LCD_CMD)
  192. for i in range(LCD_WIDTH/2,LCD_WIDTH):
  193. self.write_byte(ord(message[i]),LCD_CHR)
  194. def lcd_init(lcd):
  195. # Initialise display
  196. lcd.write_byte(0x30,LCD_CMD) # 110011 Initialise
  197. lcd.write_byte(0x30,LCD_CMD) # 110011 Initialise
  198. lcd.write_byte(0x30,LCD_CMD) # 110010 Initialise
  199. lcd.set_function(FUNC_8BITS_INT, FUNC_2_LINE, FUNC_5x7_DOTS) # 101000 Data length, number of lines, font size
  200. lcd.display_mode(DISPLAY_OFF,CURSOR_OFF,BLINK_OFF) # Display On,Cursor On, Blink On
  201. lcd.clear()
  202. lcd.set_entry_mode(MODE_INCR,MODE_NO_SHIFT) # Cursor move direction
  203. lcd.display_mode(DISPLAY_ON,CURSOR_ON,BLINK_ON) # Display On,Cursor On, Blink On
  204. lcd.clear() # Clear display
  205. time.sleep(E_DELAY)
  206. if __name__ == '__main__':
  207. lcd = LCD_DT161A()
  208. # Initialise display
  209. lcd_init(lcd)
  210. time.sleep(10) # 3 second delay
  211. # Send some centred test
  212. lcd.write_string("----------------",LCD_LINE_1,2)
  213. time.sleep(3) # 3 second delay
  214. lcd.write_string("Rasbperry Pi",LCD_LINE_1,2)
  215. time.sleep(3) # 3 second delay
  216. lcd.write_string("Model B",LCD_LINE_1,2)
  217. time.sleep(3) # 3 second delay
  218. lcd.write_string("----------------",LCD_LINE_1,2)
  219. time.sleep(3) # 3 second delay
  220. lcd.write_string("EWFT",LCD_LINE_1,3)
  221. time.sleep(3) # 3 second delay
  222. lcd.write_string(".org",LCD_LINE_1,3)
  223. time.sleep(3) # 3 second delay
  224. lcd.write_string("",LCD_LINE_1,2)
  225. time.sleep(3) # 3 second delay
  226. lcd.write_string("20x4 LCD Module ",LCD_LINE_1,2)
  227. time.sleep(3) # 20 second delay
  228. # Blank display
  229. lcd.clear()
  230. time.sleep(3) # 3 second delay
  231. lcd.clear()
  232. lcd.write_string("Goodbye!",LCD_LINE_1,2)
  233. GPIO.cleanup()