在1602液晶屏上显示当前时间–电子钟

硬件准备

需要以下硬件:

  • 树莓派
  • 面包板
  • 1602液晶屏一块
  • 10K电位器
  • 杜邦线
  • 排针
  • 面包板电源

1602 LCD液晶屏

LCD1602液晶屏提供了16列x2行的ASCII字符显示能力,工作电压5V,提供4位数据与8位数据两种工作模式,因为Raspberry Pi的GPIO口数量很有限,所以使用4位数据模式。LCD1602液晶屏模块提供了16个引脚,我们只需接其中的12个即可–请参考GPIO命名规则

  • VSS,接地,RPi PIN 6
  • VDD,接5V电源,PRi PIN 2
  • VO,液晶对比度调节,接电位器中间的引脚
  • RS,寄存器选择,接GPIO 14,RPi PIN 8
  • RW,读写选择,接地,表示写模式,PRi PIN 6
  • EN,使能信号,接GPIO 15,RPi PIN 10
  • D0,数据位0,4位工作模式下不用,不接
  • D1,数据位1,4位工作模式下不用,不接
  • D2,数据位2,4位工作模式下不用,不接
  • D3,数据位3,4位工作模式下不用,不接
  • D4,数据位4,接GPIO 17,RPi PIN 11
  • D5,数据位5,接GPIO 18,RPi PIN 12
  • D6,数据位6,接GPIO 27,RPi PIN 13
  • D7,数据位7,接GPIO 22,RPi PIN 15
  • A,液晶屏背光+,接5V,RPi PIN 2
  • K,液晶屏背光-,接地,RPi PIN 6

注意事项

  • 电源VDD最后接上
  • 排针焊接在液晶屏时注意不要虚焊,也可以用万用表测量一下
  • RW脚注意一定要接地
  • 调节电位器可以调节液晶对比度

代码

  1. #!/usr/bin/python
  2. #
  3. # based on code from lrvick and LiquidCrystal
  4. # lrvic - https://github.com/lrvick/raspi-hd44780/blob/master/hd44780.py
  5. # LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp
  6. #
  7. from time import sleep
  8. from datetime import datetime
  9. from time import sleep
  10. class Adafruit_CharLCD:
  11. # commands
  12. LCD_CLEARDISPLAY = 0x01
  13. LCD_RETURNHOME = 0x02
  14. LCD_ENTRYMODESET = 0x04
  15. LCD_DISPLAYCONTROL = 0x08
  16. LCD_CURSORSHIFT = 0x10
  17. LCD_FUNCTIONSET = 0x20
  18. LCD_SETCGRAMADDR = 0x40
  19. LCD_SETDDRAMADDR = 0x80
  20. # flags for display entry mode
  21. LCD_ENTRYRIGHT = 0x00
  22. LCD_ENTRYLEFT = 0x02
  23. LCD_ENTRYSHIFTINCREMENT = 0x01
  24. LCD_ENTRYSHIFTDECREMENT = 0x00
  25. # flags for display on/off control
  26. LCD_DISPLAYON = 0x04
  27. LCD_DISPLAYOFF = 0x00
  28. LCD_CURSORON = 0x02
  29. LCD_CURSOROFF = 0x00
  30. LCD_BLINKON = 0x01
  31. LCD_BLINKOFF = 0x00
  32. # flags for display/cursor shift
  33. LCD_DISPLAYMOVE = 0x08
  34. LCD_CURSORMOVE = 0x00
  35. # flags for display/cursor shift
  36. LCD_DISPLAYMOVE = 0x08
  37. LCD_CURSORMOVE = 0x00
  38. LCD_MOVERIGHT = 0x04
  39. LCD_MOVELEFT = 0x00
  40. # flags for function set
  41. LCD_8BITMODE = 0x10
  42. LCD_4BITMODE = 0x00
  43. LCD_2LINE = 0x08
  44. LCD_1LINE = 0x00
  45. LCD_5x10DOTS = 0x04
  46. LCD_5x8DOTS = 0x00
  47. def __init__(self, pin_rs=8, pin_e=10, pins_db=[11,12,13,15], GPIO = None):
  48. # Emulate the old behavior of using RPi.GPIO if we haven't been given
  49. # an explicit GPIO interface to use
  50. if not GPIO:
  51. import RPi.GPIO as GPIO
  52. GPIO.setwarnings(False)
  53. self.GPIO = GPIO
  54. self.pin_rs = pin_rs
  55. self.pin_e = pin_e
  56. self.pins_db = pins_db
  57. self.GPIO.setmode(GPIO.BOARD)
  58. self.GPIO.setup(self.pin_e, GPIO.OUT)
  59. self.GPIO.setup(self.pin_rs, GPIO.OUT)
  60. for pin in self.pins_db:
  61. self.GPIO.setup(pin, GPIO.OUT)
  62. self.write4bits(0x33) # initialization
  63. self.write4bits(0x32) # initialization
  64. self.write4bits(0x28) # 2 line 5x7 matrix
  65. self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor
  66. self.write4bits(0x06) # shift cursor right
  67. self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF
  68. self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS
  69. self.displayfunction |= self.LCD_2LINE
  70. """ Initialize to default text direction (for romance languages) """
  71. self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT
  72. self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode
  73. self.clear()
  74. def begin(self, cols, lines):
  75. if (lines > 1):
  76. self.numlines = lines
  77. self.displayfunction |= self.LCD_2LINE
  78. self.currline = 0
  79. def home(self):
  80. self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero
  81. self.delayMicroseconds(3000) # this command takes a long time!
  82. def clear(self):
  83. self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display
  84. self.delayMicroseconds(3000) # 3000 microsecond sleep, clearing the display takes a long time
  85. def setCursor(self, col, row):
  86. self.row_offsets = [ 0x00, 0x40, 0x14, 0x54 ]
  87. if ( row > self.numlines ):
  88. row = self.numlines - 1 # we count rows starting w/0
  89. self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))
  90. def noDisplay(self):
  91. """ Turn the display off (quickly) """
  92. self.displaycontrol &= ~self.LCD_DISPLAYON
  93. self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
  94. def display(self):
  95. """ Turn the display on (quickly) """
  96. self.displaycontrol |= self.LCD_DISPLAYON
  97. self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
  98. def noCursor(self):
  99. """ Turns the underline cursor on/off """
  100. self.displaycontrol &= ~self.LCD_CURSORON
  101. self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
  102. def cursor(self):
  103. """ Cursor On """
  104. self.displaycontrol |= self.LCD_CURSORON
  105. self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
  106. def noBlink(self):
  107. """ Turn on and off the blinking cursor """
  108. self.displaycontrol &= ~self.LCD_BLINKON
  109. self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
  110. def noBlink(self):
  111. """ Turn on and off the blinking cursor """
  112. self.displaycontrol &= ~self.LCD_BLINKON
  113. self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
  114. def DisplayLeft(self):
  115. """ These commands scroll the display without changing the RAM """
  116. self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)
  117. def scrollDisplayRight(self):
  118. """ These commands scroll the display without changing the RAM """
  119. self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT);
  120. def leftToRight(self):
  121. """ This is for text that flows Left to Right """
  122. self.displaymode |= self.LCD_ENTRYLEFT
  123. self.write4bits(self.LCD_ENTRYMODESET | self.displaymode);
  124. def rightToLeft(self):
  125. """ This is for text that flows Right to Left """
  126. self.displaymode &= ~self.LCD_ENTRYLEFT
  127. self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
  128. def autoscroll(self):
  129. """ This will 'right justify' text from the cursor """
  130. self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT
  131. self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
  132. def noAutoscroll(self):
  133. """ This will 'left justify' text from the cursor """
  134. self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT
  135. self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
  136. def write4bits(self, bits, char_mode=False):
  137. """ Send command to LCD """
  138. self.delayMicroseconds(1000) # 1000 microsecond sleep
  139. bits=bin(bits)[2:].zfill(8)
  140. self.GPIO.output(self.pin_rs, char_mode)
  141. for pin in self.pins_db:
  142. self.GPIO.output(pin, False)
  143. for i in range(4):
  144. if bits[i] == "1":
  145. self.GPIO.output(self.pins_db[::-1][i], True)
  146. self.pulseEnable()
  147. for pin in self.pins_db:
  148. self.GPIO.output(pin, False)
  149. for i in range(4,8):
  150. if bits[i] == "1":
  151. self.GPIO.output(self.pins_db[::-1][i-4], True)
  152. self.pulseEnable()
  153. def delayMicroseconds(self, microseconds):
  154. seconds = microseconds / float(1000000) # divide microseconds by 1 million for seconds
  155. sleep(seconds)
  156. def pulseEnable(self):
  157. self.GPIO.output(self.pin_e, False)
  158. self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
  159. self.GPIO.output(self.pin_e, True)
  160. self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
  161. self.GPIO.output(self.pin_e, False)
  162. self.delayMicroseconds(1) # commands need > 37us to settle
  163. def message(self, text):
  164. """ Send string to LCD. Newline wraps to second line"""
  165. for char in text:
  166. if char == '\n':
  167. self.write4bits(0xC0) # next line
  168. else:
  169. self.write4bits(ord(char),True)
  170. if __name__ == '__main__':
  171. lcd = Adafruit_CharLCD()
  172. lcd.noBlink()
  173. while True:
  174. sleep(1)
  175. lcd.clear()
  176. lcd.message(datetime.now().strftime(' %I : %M : %S \n%a %b %d %Y'))

 

完成后效果

未经允许不得转载:树莓派 » 在1602液晶屏上显示当前时间–电子钟

赞 (1)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址