Advertisement
Guest User

Sunfounder1

a guest
May 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. #
  4. # based on code from lrvick and LiquidCrystal
  5. # lrvic - https://github.com/lrvick/raspi-hd44780/blob/master/hd44780.py
  6. # LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp
  7. #
  8.  
  9. from time import sleep
  10.  
  11. class Adafruit_CharLCD:
  12.  
  13. # commands
  14. LCD_CLEARDISPLAY = 0x01
  15. LCD_RETURNHOME = 0x02
  16. LCD_ENTRYMODESET = 0x04
  17. LCD_DISPLAYCONTROL = 0x08
  18. LCD_CURSORSHIFT = 0x10
  19. LCD_FUNCTIONSET = 0x20
  20. LCD_SETCGRAMADDR = 0x40
  21. LCD_SETDDRAMADDR = 0x80
  22.  
  23. # flags for display entry mode
  24. LCD_ENTRYRIGHT = 0x00
  25. LCD_ENTRYLEFT = 0x02
  26. LCD_ENTRYSHIFTINCREMENT = 0x01
  27. LCD_ENTRYSHIFTDECREMENT = 0x00
  28.  
  29. # flags for display on/off control
  30. LCD_DISPLAYON = 0x04
  31. LCD_DISPLAYOFF = 0x00
  32. LCD_CURSORON = 0x02
  33. LCD_CURSOROFF = 0x00
  34. LCD_BLINKON = 0x01
  35. LCD_BLINKOFF = 0x00
  36.  
  37. # flags for display/cursor shift
  38. LCD_DISPLAYMOVE = 0x08
  39. LCD_CURSORMOVE = 0x00
  40.  
  41. # flags for display/cursor shift
  42. LCD_DISPLAYMOVE = 0x08
  43. LCD_CURSORMOVE = 0x00
  44. LCD_MOVERIGHT = 0x04
  45. LCD_MOVELEFT = 0x00
  46.  
  47. # flags for function set
  48. LCD_8BITMODE = 0x10
  49. LCD_4BITMODE = 0x00
  50. LCD_2LINE = 0x08
  51. LCD_1LINE = 0x00
  52. LCD_5x10DOTS = 0x04
  53. LCD_5x8DOTS = 0x00
  54.  
  55.  
  56.  
  57. def __init__(self, pin_rs=27, pin_e=22, pins_db=[25, 24, 23, 18], GPIO = None):
  58. # Emulate the old behavior of using RPi.GPIO if we haven't been given
  59. # an explicit GPIO interface to use
  60. if not GPIO:
  61. import RPi.GPIO as GPIO
  62. self.GPIO = GPIO
  63. self.pin_rs = pin_rs
  64. self.pin_e = pin_e
  65. self.pins_db = pins_db
  66.  
  67. self.GPIO.setmode(GPIO.BCM)
  68. self.GPIO.setup(self.pin_e, GPIO.OUT)
  69. self.GPIO.setup(self.pin_rs, GPIO.OUT)
  70.  
  71. for pin in self.pins_db:
  72. self.GPIO.setup(pin, GPIO.OUT)
  73.  
  74. self.write4bits(0x33) # initialization
  75. self.write4bits(0x32) # initialization
  76. self.write4bits(0x28) # 2 line 5x7 matrix
  77. self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor
  78. self.write4bits(0x06) # shift cursor right
  79.  
  80. self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF
  81.  
  82. self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS
  83. self.displayfunction |= self.LCD_2LINE
  84.  
  85. """ Initialize to default text direction (for romance languages) """
  86. self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT
  87. self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode
  88.  
  89. self.clear()
  90.  
  91.  
  92. def begin(self, cols, lines):
  93.  
  94. if (lines > 1):
  95. self.numlines = lines
  96. self.displayfunction |= self.LCD_2LINE
  97. self.currline = 0
  98.  
  99.  
  100. def home(self):
  101.  
  102. self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero
  103. self.delayMicroseconds(3000) # this command takes a long time!
  104.  
  105.  
  106. def clear(self):
  107.  
  108. self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display
  109. self.delayMicroseconds(3000) # 3000 microsecond sleep, clearing the display takes a long time
  110.  
  111.  
  112. def setCursor(self, col, row):
  113.  
  114. self.row_offsets = [ 0x00, 0x40, 0x14, 0x54 ]
  115.  
  116. if ( row > self.numlines ):
  117. row = self.numlines - 1 # we count rows starting w/0
  118.  
  119. self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))
  120.  
  121.  
  122. def noDisplay(self):
  123. """ Turn the display off (quickly) """
  124.  
  125. self.displaycontrol &= ~self.LCD_DISPLAYON
  126. self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
  127.  
  128.  
  129. def display(self):
  130. """ Turn the display on (quickly) """
  131.  
  132. self.displaycontrol |= self.LCD_DISPLAYON
  133. self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
  134.  
  135.  
  136. def noCursor(self):
  137. """ Turns the underline cursor on/off """
  138.  
  139. self.displaycontrol &= ~self.LCD_CURSORON
  140. self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
  141.  
  142.  
  143. def cursor(self):
  144. """ Cursor On """
  145.  
  146. self.displaycontrol |= self.LCD_CURSORON
  147. self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
  148.  
  149.  
  150. def noBlink(self):
  151. """ Turn on and off the blinking cursor """
  152.  
  153. self.displaycontrol &= ~self.LCD_BLINKON
  154. self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
  155.  
  156.  
  157. def noBlink(self):
  158. """ Turn on and off the blinking cursor """
  159.  
  160. self.displaycontrol &= ~self.LCD_BLINKON
  161. self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
  162.  
  163.  
  164. def DisplayLeft(self):
  165. """ These commands scroll the display without changing the RAM """
  166.  
  167. self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)
  168.  
  169.  
  170. def scrollDisplayRight(self):
  171. """ These commands scroll the display without changing the RAM """
  172.  
  173. self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT);
  174.  
  175.  
  176. def leftToRight(self):
  177. """ This is for text that flows Left to Right """
  178.  
  179. self.displaymode |= self.LCD_ENTRYLEFT
  180. self.write4bits(self.LCD_ENTRYMODESET | self.displaymode);
  181.  
  182.  
  183. def rightToLeft(self):
  184. """ This is for text that flows Right to Left """
  185. self.displaymode &= ~self.LCD_ENTRYLEFT
  186. self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
  187.  
  188.  
  189. def autoscroll(self):
  190. """ This will 'right justify' text from the cursor """
  191.  
  192. self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT
  193. self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
  194.  
  195.  
  196. def noAutoscroll(self):
  197. """ This will 'left justify' text from the cursor """
  198.  
  199. self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT
  200. self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
  201.  
  202.  
  203. def write4bits(self, bits, char_mode=False):
  204. """ Send command to LCD """
  205.  
  206. self.delayMicroseconds(1000) # 1000 microsecond sleep
  207.  
  208. bits=bin(bits)[2:].zfill(8)
  209.  
  210. self.GPIO.output(self.pin_rs, char_mode)
  211.  
  212. for pin in self.pins_db:
  213. self.GPIO.output(pin, False)
  214.  
  215. for i in range(4):
  216. if bits[i] == "1":
  217. self.GPIO.output(self.pins_db[::-1][i], True)
  218.  
  219. self.pulseEnable()
  220.  
  221. for pin in self.pins_db:
  222. self.GPIO.output(pin, False)
  223.  
  224. for i in range(4,8):
  225. if bits[i] == "1":
  226. self.GPIO.output(self.pins_db[::-1][i-4], True)
  227.  
  228. self.pulseEnable()
  229.  
  230.  
  231. def delayMicroseconds(self, microseconds):
  232. seconds = microseconds / float(1000000) # divide microseconds by 1 million for seconds
  233. sleep(seconds)
  234.  
  235.  
  236. def pulseEnable(self):
  237. self.GPIO.output(self.pin_e, False)
  238. self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
  239. self.GPIO.output(self.pin_e, True)
  240. self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
  241. self.GPIO.output(self.pin_e, False)
  242. self.delayMicroseconds(1) # commands need > 37us to settle
  243.  
  244.  
  245. def message(self, text):
  246. """ Send string to LCD. Newline wraps to second line"""
  247.  
  248. for char in text:
  249. if char == '\n':
  250. self.write4bits(0xC0) # next line
  251. else:
  252. self.write4bits(ord(char),True)
  253.  
  254. def loop():
  255. lcd = Adafruit_CharLCD()
  256. while True:
  257. lcd.clear()
  258. lcd.message(" LCD 1602 Test \n123456789ABCDEF")
  259. sleep(2)
  260. lcd.clear()
  261. lcd.message(" SUNFOUNDER \nHello World ! :)")
  262. sleep(2)
  263. lcd.clear()
  264. lcd.message("Welcom to --->\n sunfounder.com")
  265. sleep(2)
  266.  
  267. if __name__ == '__main__':
  268. loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement