硬件准备
需要以下硬件:
- 树莓派
- 面包板
- 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脚注意一定要接地
- 调节电位器可以调节液晶对比度
代码
#!/usr/bin/python## based on code from lrvick and LiquidCrystal# lrvic - https://github.com/lrvick/raspi-hd44780/blob/master/hd44780.py# LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp#from time import sleepfrom datetime import datetimefrom time import sleepclass Adafruit_CharLCD:# commandsLCD_CLEARDISPLAY = 0x01LCD_RETURNHOME = 0x02LCD_ENTRYMODESET = 0x04LCD_DISPLAYCONTROL = 0x08LCD_CURSORSHIFT = 0x10LCD_FUNCTIONSET = 0x20LCD_SETCGRAMADDR = 0x40LCD_SETDDRAMADDR = 0x80# flags for display entry modeLCD_ENTRYRIGHT = 0x00LCD_ENTRYLEFT = 0x02LCD_ENTRYSHIFTINCREMENT = 0x01LCD_ENTRYSHIFTDECREMENT = 0x00# flags for display on/off controlLCD_DISPLAYON = 0x04LCD_DISPLAYOFF = 0x00LCD_CURSORON = 0x02LCD_CURSOROFF = 0x00LCD_BLINKON = 0x01LCD_BLINKOFF = 0x00# flags for display/cursor shiftLCD_DISPLAYMOVE = 0x08LCD_CURSORMOVE = 0x00# flags for display/cursor shiftLCD_DISPLAYMOVE = 0x08LCD_CURSORMOVE = 0x00LCD_MOVERIGHT = 0x04LCD_MOVELEFT = 0x00# flags for function setLCD_8BITMODE = 0x10LCD_4BITMODE = 0x00LCD_2LINE = 0x08LCD_1LINE = 0x00LCD_5x10DOTS = 0x04LCD_5x8DOTS = 0x00def __init__(self, pin_rs=8, pin_e=10, pins_db=[11,12,13,15], GPIO = None):# Emulate the old behavior of using RPi.GPIO if we haven't been given# an explicit GPIO interface to useif not GPIO:import RPi.GPIO as GPIOGPIO.setwarnings(False)self.GPIO = GPIOself.pin_rs = pin_rsself.pin_e = pin_eself.pins_db = pins_dbself.GPIO.setmode(GPIO.BOARD)self.GPIO.setup(self.pin_e, GPIO.OUT)self.GPIO.setup(self.pin_rs, GPIO.OUT)for pin in self.pins_db:self.GPIO.setup(pin, GPIO.OUT)self.write4bits(0x33) # initializationself.write4bits(0x32) # initializationself.write4bits(0x28) # 2 line 5x7 matrixself.write4bits(0x0C) # turn cursor off 0x0E to enable cursorself.write4bits(0x06) # shift cursor rightself.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFFself.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTSself.displayfunction |= self.LCD_2LINE""" Initialize to default text direction (for romance languages) """self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENTself.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry modeself.clear()def begin(self, cols, lines):if (lines > 1):self.numlines = linesself.displayfunction |= self.LCD_2LINEself.currline = 0def home(self):self.write4bits(self.LCD_RETURNHOME) # set cursor position to zeroself.delayMicroseconds(3000) # this command takes a long time!def clear(self):self.write4bits(self.LCD_CLEARDISPLAY) # command to clear displayself.delayMicroseconds(3000) # 3000 microsecond sleep, clearing the display takes a long timedef setCursor(self, col, row):self.row_offsets = [ 0x00, 0x40, 0x14, 0x54 ]if ( row > self.numlines ):row = self.numlines - 1 # we count rows starting w/0self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))def noDisplay(self):""" Turn the display off (quickly) """self.displaycontrol &= ~self.LCD_DISPLAYONself.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)def display(self):""" Turn the display on (quickly) """self.displaycontrol |= self.LCD_DISPLAYONself.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)def noCursor(self):""" Turns the underline cursor on/off """self.displaycontrol &= ~self.LCD_CURSORONself.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)def cursor(self):""" Cursor On """self.displaycontrol |= self.LCD_CURSORONself.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)def noBlink(self):""" Turn on and off the blinking cursor """self.displaycontrol &= ~self.LCD_BLINKONself.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)def noBlink(self):""" Turn on and off the blinking cursor """self.displaycontrol &= ~self.LCD_BLINKONself.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)def DisplayLeft(self):""" These commands scroll the display without changing the RAM """self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)def scrollDisplayRight(self):""" These commands scroll the display without changing the RAM """self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT);def leftToRight(self):""" This is for text that flows Left to Right """self.displaymode |= self.LCD_ENTRYLEFTself.write4bits(self.LCD_ENTRYMODESET | self.displaymode);def rightToLeft(self):""" This is for text that flows Right to Left """self.displaymode &= ~self.LCD_ENTRYLEFTself.write4bits(self.LCD_ENTRYMODESET | self.displaymode)def autoscroll(self):""" This will 'right justify' text from the cursor """self.displaymode |= self.LCD_ENTRYSHIFTINCREMENTself.write4bits(self.LCD_ENTRYMODESET | self.displaymode)def noAutoscroll(self):""" This will 'left justify' text from the cursor """self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENTself.write4bits(self.LCD_ENTRYMODESET | self.displaymode)def write4bits(self, bits, char_mode=False):""" Send command to LCD """self.delayMicroseconds(1000) # 1000 microsecond sleepbits=bin(bits)[2:].zfill(8)self.GPIO.output(self.pin_rs, char_mode)for pin in self.pins_db:self.GPIO.output(pin, False)for i in range(4):if bits[i] == "1":self.GPIO.output(self.pins_db[::-1][i], True)self.pulseEnable()for pin in self.pins_db:self.GPIO.output(pin, False)for i in range(4,8):if bits[i] == "1":self.GPIO.output(self.pins_db[::-1][i-4], True)self.pulseEnable()def delayMicroseconds(self, microseconds):seconds = microseconds / float(1000000) # divide microseconds by 1 million for secondssleep(seconds)def pulseEnable(self):self.GPIO.output(self.pin_e, False)self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450nsself.GPIO.output(self.pin_e, True)self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450nsself.GPIO.output(self.pin_e, False)self.delayMicroseconds(1) # commands need > 37us to settledef message(self, text):""" Send string to LCD. Newline wraps to second line"""for char in text:if char == '\n':self.write4bits(0xC0) # next lineelse:self.write4bits(ord(char),True)if __name__ == '__main__':lcd = Adafruit_CharLCD()lcd.noBlink()while True:sleep(1)lcd.clear()lcd.message(datetime.now().strftime(' %I : %M : %S \n%a %b %d %Y'))
完成后效果
未经允许不得转载:网购经验网 » 在1602液晶屏上显示当前时间–电子钟








评论前必须登录!
登陆 注册