Advertisement
Guest User

lcd_driver.py

a guest
Mar 11th, 2016
1,567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. import i2c_lib
  2. from time import *
  3.  
  4. # LCD Address
  5. ADDRESS = 0x3f
  6.  
  7. # commands
  8. LCD_CLEARDISPLAY = 0x01
  9. LCD_RETURNHOME = 0x02
  10. LCD_ENTRYMODESET = 0x04
  11. LCD_DISPLAYCONTROL = 0x08
  12. LCD_CURSORSHIFT = 0x10
  13. LCD_FUNCTIONSET = 0x20
  14. LCD_SETCGRAMADDR = 0x40
  15. LCD_SETDDRAMADDR = 0x80
  16.  
  17. # flags for display entry mode
  18. LCD_ENTRYRIGHT = 0x00
  19. LCD_ENTRYLEFT = 0x02
  20. LCD_ENTRYSHIFTINCREMENT = 0x01
  21. LCD_ENTRYSHIFTDECREMENT = 0x00
  22.  
  23. # flags for display on/off control
  24. LCD_DISPLAYON = 0x04
  25. LCD_DISPLAYOFF = 0x00
  26. LCD_CURSORON = 0x02
  27. LCD_CURSOROFF = 0x00
  28. LCD_BLINKON = 0x01
  29. LCD_BLINKOFF = 0x00
  30.  
  31. # flags for display/cursor shift
  32. LCD_DISPLAYMOVE = 0x08
  33. LCD_CURSORMOVE = 0x00
  34. LCD_MOVERIGHT = 0x04
  35. LCD_MOVELEFT = 0x00
  36.  
  37. # flags for function set
  38. LCD_8BITMODE = 0x10
  39. LCD_4BITMODE = 0x00
  40. LCD_2LINE = 0x08
  41. LCD_1LINE = 0x00
  42. LCD_5x10DOTS = 0x04
  43. LCD_5x8DOTS = 0x00
  44.  
  45. # flags for backlight control
  46. LCD_BACKLIGHT = 0x08
  47. LCD_NOBACKLIGHT = 0x00
  48.  
  49. En = 0b00000100 # Enable bit
  50. Rw = 0b00000010 # Read/Write bit
  51. Rs = 0b00000001 # Register select bit
  52.  
  53. class lcd:
  54. #initializes objects and lcd
  55. def __init__(self):
  56. self.lcd_device = i2c_lib.i2c_device(ADDRESS)
  57.  
  58. self.lcd_write0(0x03)
  59. self.lcd_write0(0x03)
  60. self.lcd_write0(0x03)
  61. self.lcd_write0(0x02)
  62.  
  63. self.lcd_write0(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE)
  64. self.lcd_write0(LCD_DISPLAYCONTROL | LCD_DISPLAYON)
  65. self.lcd_write0(LCD_CLEARDISPLAY)
  66. self.lcd_write0(LCD_ENTRYMODESET | LCD_ENTRYLEFT)
  67. sleep(0.2)
  68.  
  69. # clocks EN to latch command
  70. def lcd_strobe0(self, data):
  71. self.lcd_device.write_cmd(data | En | LCD_NOBACKLIGHT)
  72. sleep(.0005)
  73. self.lcd_device.write_cmd(((data & ~En | LCD_NOBACKLIGHT)))
  74. sleep(.0001)
  75.  
  76. def lcd_strobe1(self, data):
  77. self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT)
  78. sleep(.0005)
  79. self.lcd_device.write_cmd(((data & ~En | LCD_BACKLIGHT)))
  80. sleep(.0001)
  81.  
  82. def lcd_write_four_bits0(self, data):
  83. self.lcd_device.write_cmd(data | LCD_NOBACKLIGHT)
  84. self.lcd_strobe0(data)
  85.  
  86. def lcd_write_four_bits1(self, data):
  87. self.lcd_device.write_cmd(data | LCD_BACKLIGHT)
  88. self.lcd_strobe1(data)
  89.  
  90. # write a command to lcd
  91. def lcd_write0(self, cmd, mode=0):
  92. self.lcd_write_four_bits0(mode | (cmd & 0xF0))
  93. self.lcd_write_four_bits0(mode | ((cmd << 4) & 0xF0))
  94.  
  95. def lcd_write1(self, cmd, mode=0):
  96. self.lcd_write_four_bits1(mode | (cmd & 0xF0))
  97. self.lcd_write_four_bits1(mode | ((cmd << 4) & 0xF0))
  98.  
  99. # put string function
  100. def lcd_display_string0(self, string, line):
  101. if line == 1:
  102. self.lcd_write0(0x80)
  103. if line == 2:
  104. self.lcd_write0(0xC0)
  105. if line == 3:
  106. self.lcd_write0(0x94)
  107. if line == 4:
  108. self.lcd_write0(0xD4)
  109.  
  110. for char in string:
  111. self.lcd_write0(ord(char), Rs)
  112.  
  113. def lcd_display_string1(self, string, line):
  114. if line == 1:
  115. self.lcd_write1(0x80)
  116. if line == 2:
  117. self.lcd_write1(0xC0)
  118. if line == 3:
  119. self.lcd_write1(0x94)
  120. if line == 4:
  121. self.lcd_write1(0xD4)
  122.  
  123. for char in string:
  124. self.lcd_write1(ord(char), Rs)
  125.  
  126. # clear lcd and set to home
  127. def lcd_clear0(self):
  128. self.lcd_write0(LCD_CLEARDISPLAY)
  129. self.lcd_write0(LCD_RETURNHOME)
  130.  
  131. def lcd_clear1(self):
  132. self.lcd_write1(LCD_CLEARDISPLAY)
  133. self.lcd_write1(LCD_RETURNHOME)
  134.  
  135. def lcd_write_char0(self, charvalue, mode=1):
  136. self.lcd_write_four_bits0(mode | (charvalue & 0xF0))
  137. self.lcd_write_four_bits0(mode | ((charvalue << 4) & 0xF0))
  138.  
  139. def lcd_write_char1(self, charvalue, mode=1):
  140. self.lcd_write_four_bits1(mode | (charvalue & 0xF0))
  141. self.lcd_write_four_bits1(mode | ((charvalue << 4) & 0xF0))
  142.  
  143. def lcd_load_custom_chars0(self, fontdata):
  144. self.lcd_write0(0x40);
  145. for char in fontdata:
  146. for line in char:
  147. self.lcd_write_char0(line)
  148.  
  149. def lcd_load_custom_chars1(self, fontdata):
  150. self.lcd_write1(0x40);
  151. for char in fontdata:
  152. for line in char:
  153. self.lcd_write_char1(line)
  154.  
  155. # define precise positioning (addition from the forum)
  156. def lcd_display_string_pos0(self, string, line, pos):
  157. if line == 1:
  158. pos_new = pos
  159. elif line == 2:
  160. pos_new = 0x40 + pos
  161. elif line == 3:
  162. pos_new = 0x14 + pos
  163. elif line == 4:
  164. pos_new = 0x54 + pos
  165.  
  166. self.lcd_write0(0x80 + pos_new)
  167.  
  168. for char in string:
  169. self.lcd_write0(ord(char), Rs)
  170.  
  171. # define precise positioning (addition from the forum)
  172. def lcd_display_string_pos1(self, string, line, pos):
  173. if line == 1:
  174. pos_new = pos
  175. elif line == 2:
  176. pos_new = 0x40 + pos
  177. elif line == 3:
  178. pos_new = 0x14 + pos
  179. elif line == 4:
  180. pos_new = 0x54 + pos
  181.  
  182. self.lcd_write1(0x80 + pos_new)
  183.  
  184. for char in string:
  185. self.lcd_write1(ord(char), Rs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement