2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > (七)树莓派系列教程:树莓派4B连接LCD1602液晶显示屏并且显示内容

(七)树莓派系列教程:树莓派4B连接LCD1602液晶显示屏并且显示内容

时间:2019-03-26 23:31:26

相关推荐

(七)树莓派系列教程:树莓派4B连接LCD1602液晶显示屏并且显示内容

树莓派连接LCD1602液晶显示屏,并显示内容

一、效果图

所需要硬件:

LCD1602液晶显示屏IIC转接LCD1602模块(转成IIC通讯)

二、设置树莓派,启动IIC通讯功能

因为树莓派刚烧录完系统后IIC功能是没有启动的,在命令行分别输入以下命令:

raspi-config ; 进入树莓派设置界面>>>Interfacing Options ; 设置>>>P5 I2C ; 选择I2C选项>>>enable ; 启动I2C功能

下载对应缺的包,例如:

sudo apt-get install i2c-toolssudo apt-get updatesudo apt-get install python3-smbus

设置好了,重启一次树莓派。

三、连线

树莓派-------液晶显示屏VCC----------3.3VGND-----------地SDA----------SDASCL-----------SCL

四、代码

方案一:(一个LCD602程序—LCD1602.py)

'''1602/1602Aauthor:ZengXiaojiedescription:直接实例化 My1602 对象然后调用对象的 print_lcd(param1, param2, str) 方法ex:print_lcd(0, 0, 'Hello, world!')param1:从第几位开始,共16位(0-15),超出不显示,除非移动屏幕(其实1602一行有40位)param2:第几行,共有两行,0为第一行,1位第二行。str:要显示的数据每次打印字符时,建议适当清屏。'''import timeimport smbusimport sysclass My1602(object):BUS = smbus.SMBus(1)LCD_ADDR = 0x27BLEN = 1# '''# 开关灯def turn_light(self, key):self.BLEN = keyif key == 1:self.BUS.write_byte(self.LCD_ADDR, 0x08)else:self.BUS.write_byte(self.LCD_ADDR, 0x00)# '''def write_word(self, addr, data):temp = dataif self.BLEN == 1:temp |= 0x08else:temp &= 0xF7self.BUS.write_byte(addr, temp)# 写命令def send_command(self, comm):# 发送7-4位数据buf = comm & 0xF0buf |= 0x04 # RS = 0, RW = 0, EN = 1self.write_word(self.LCD_ADDR, buf)time.sleep(0.002)buf &= 0xFBself.write_word(self.LCD_ADDR, buf)# 发送3-0位数据buf = (comm & 0x0F) << 4buf |= 0x04 # RS = 0, RW = 0, EN = 1self.write_word(self.LCD_ADDR, buf)time.sleep(0.002)buf &= 0xFBself.write_word(self.LCD_ADDR, buf)# 写数据def send_data(self, data):# 发送7-4位数据buf = data & 0xF0buf |= 0x05 # RS = 1, RW = 0, EN = 1self.write_word(self.LCD_ADDR, buf)time.sleep(0.002)buf &= 0xFBself.write_word(self.LCD_ADDR, buf)# 发送3-0位数据buf = (data & 0x0F) << 4buf |= 0x05 # RS = 1, RW = 0, EN = 1self.write_word(self.LCD_ADDR, buf)time.sleep(0.002)buf &= 0xFBself.write_word(self.LCD_ADDR, buf)# 初始化def __init__(self):try:self.send_command(0x33)time.sleep(0.005)self.send_command(0x32)time.sleep(0.005)self.send_command(0x28)time.sleep(0.005)self.send_command(0x0C)time.sleep(0.005)self.send_command(0x01)self.BUS.write_byte(self.LCD_ADDR, 0x08)except:return Noneelse:return None# 清屏def clear_lcd(self):self.send_command(0x01) # 清屏# 显示字符def print_lcd(self, x, y, str):if x < 0:x = 0if x > 15:x = 15if y < 0:y = 0if y > 1:y = 1addr = 0x80 + 0x40 * y + xself.send_command(addr)for chr in str:self.send_data(ord(chr))if __name__ == '__main__':my1602 = My1602()# turn_light(0) # 关闭背景灯光my1602.print_lcd(0, 0, 'Hello, world!')my1602.print_lcd(8, 1, 'by zxj')

方案二:(两个python程序:1.LCD1602驱动程序,2.调用驱动程序)

程序1—LCD1602.py(LCD主控制程序):

import LCD1602 as LCDimport timeLCD.init_lcd()time.sleep(2)LCD.print_lcd(0,0,'start')

程序2—LCD1602.py(LCD驱动程序1):

import timeimport smbusimport logximport loggingBUS = smbus.SMBus(1)LCD_ADDR = 0x27BLEN = 1 #turn on/off background lightdef turn_light(key):global BLENBLEN = keyif key ==1 :BUS.write_byte(LCD_ADDR ,0x08)logging.info('LCD executed turn on BLight')else:BUS.write_byte(LCD_ADDR ,0x00)logging.info('LCD executed turn off BLight')def write_word(addr, data):global BLENtemp = dataif BLEN == 1:temp |= 0x08else:temp &= 0xF7BUS.write_byte(addr ,temp)def send_command(comm):# Send bit7-4 firstlybuf = comm & 0xF0buf |= 0x04# RS = 0, RW = 0, EN = 1write_word(LCD_ADDR ,buf)time.sleep(0.002)buf &= 0xFB# Make EN = 0write_word(LCD_ADDR ,buf)# Send bit3-0 secondlybuf = (comm & 0x0F) << 4buf |= 0x04# RS = 0, RW = 0, EN = 1write_word(LCD_ADDR ,buf)time.sleep(0.002)buf &= 0xFB# Make EN = 0write_word(LCD_ADDR ,buf)def send_data(data):# Send bit7-4 firstlybuf = data & 0xF0buf |= 0x05# RS = 1, RW = 0, EN = 1write_word(LCD_ADDR ,buf)time.sleep(0.002)buf &= 0xFB# Make EN = 0write_word(LCD_ADDR ,buf)# Send bit3-0 secondlybuf = (data & 0x0F) << 4buf |= 0x05# RS = 1, RW = 0, EN = 1write_word(LCD_ADDR ,buf)time.sleep(0.002)buf &= 0xFB# Make EN = 0write_word(LCD_ADDR ,buf)def init_lcd():try:send_command(0x33) # Must initialize to 8-line mode at firsttime.sleep(0.005)send_command(0x32) # Then initialize to 4-line modetime.sleep(0.005)send_command(0x28) # 2 Lines & 5*7 dotstime.sleep(0.005)send_command(0x0C) # Enable display without cursortime.sleep(0.005)send_command(0x01) # Clear Screenlogging.info('LCD init over')BUS.write_byte(LCD_ADDR ,0x08)logging.info('LCD turning on BLight')except:return Falseelse:return Truedef clear_lcd():send_command(0x01) # Clear Screendef print_lcd(x, y, str):if x < 0:x = 0if x > 15:x = 15if y <0:y = 0if y > 1:y = 1# Move cursoraddr = 0x80 + 0x40 * y + xsend_command(addr)for chr in str:send_data(ord(chr))if __name__ == '__main__':init_lcd()print_lcd(0, 0, 'Hello, world!')print_lcd(8, 1, 'by Jerry')

程序3—logx.py(LCD驱动程序2):

import logging#print on log filelogging.basicConfig(level=logging.INFO,format='%(asctime)s <%(levelname)s> [line:%(lineno)d] %(filename)s : %(message)s',datefmt='%Y-%m-%d %H:%M:%S',filename='trace.log',filemode='a')#w a#print on screemconsole = logging.StreamHandler()console.setLevel(logging.DEBUG)formatter = logging.Formatter('%(asctime)s <%(levelname)s> [line:%(lineno)d] %(filename)s : %(message)s')console.setFormatter(formatter)logging.getLogger('').addHandler(console)if __name__ == '__main__':#CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSETlogging.critical('This is critical message')logging.error('This is error message')logging.warning('This is warning message')logging.info('This is info message')logging.debug('This is debug message')#logging.notset('This is notset message')

五、运行即可,运行成功如下:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。