【树莓派教程】——I2c驱动 1602 LCD

2016-01-091.6万
AI 快速预览详细 收起
本教程详细介绍了如何在树莓派上通过I2C接口驱动1602 LCD显示屏。首先,需要启用树莓派的I2C功能,然后安装必要的工具如i2c-tools和python-smbus。接着,通过i2cdetect命令查看设备地址,最后提供了一个完整的示例代码,展示了如何初始化LCD并显示文本。这个过程不仅解决了默认I2C关闭的问题,还提供了具体的配置文件修改和代码示例,帮助用户快速实现LCD显示。


【树莓派教程】——I2c驱动 1602 LCD图1

1.1602LCD 就是16(字符)x2(行)
2.树莓派默认I2c是关闭的,在/dev 中我们不能发现设备
vi /etc/modprobe.d/raspi-blacklist.conf
  1. # blacklist spi and i2c by default (many users don't need them)
  2. #blacklist spi-bcm2708
  3. #blacklist i2c-bcm2708
复制代码
注释掉代表使用
vi  /etc/modules
  1. # /etc/modules: kernel modules to load at boot time.
  2. #
  3. # This file contains the names of kernel modules that should be loaded
  4. # at boot time, one per line. Lines beginning with "#" are ignored.
  5. snd-bcm2835
  6. i2c-bcm2708
  7. i2c-dev
复制代码

3.查看设备

lsmod


4.安装工具
apt-get install i2c-tools python-smbus
5.查看设备地址
i2cdetect -y 1
设备地址就是0x27
6示例代码logx.py
  1. import logging
  2. #print on log file
  3. logging.basicConfig(level=logging.INFO,
  4.                 format='%(asctime)s <%(levelname)s> [line:%(lineno)d] %(filename)s : %(message)s',
  5.                 datefmt='%Y-%m-%d %H:%M:%S',
  6.                 filename='trace.log',
  7.                 filemode='a')#w a
  8. #print on screem                                
  9. console = logging.StreamHandler()
  10. console.setLevel(logging.DEBUG)
  11. formatter = logging.Formatter('%(asctime)s <%(levelname)s> [line:%(lineno)d] %(filename)s : %(message)s')
  12. console.setFormatter(formatter)
  13. logging.getLogger('').addHandler(console)
  14. if __name__ == '__main__':                                
  15.         #CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET
  16.         logging.critical('This is critical message')
  17.         logging.error('This is error message')
  18.         logging.warning('This is warning message')
  19.         logging.info('This is info message')
  20.         logging.debug('This is debug message')
  21.         #logging.notset('This is notset message')
复制代码

LCD1602.py

  1. import time
  2. import smbus
  3. import logx
  4. import logging
  5. BUS = smbus.SMBus(1)
  6. LCD_ADDR = 0x27
  7. BLEN = 1 #turn on/off background light
  8. def turn_light(key):
  9.         global BLEN
  10.         BLEN = key
  11.         if key ==1 :
  12.                 BUS.write_byte(LCD_ADDR ,0x08)
  13.                 logging.info('LCD executed turn on BLight')
  14.         else:
  15.                 BUS.write_byte(LCD_ADDR ,0x00)
  16.                 logging.info('LCD executed turn off BLight')
  17. def write_word(addr, data):
  18.         global BLEN
  19.         temp = data
  20.         if BLEN == 1:
  21.                 temp |= 0x08
  22.         else:
  23.                 temp &= 0xF7
  24.         BUS.write_byte(addr ,temp)
  25. def send_command(comm):
  26.         # Send bit7-4 firstly
  27.         buf = comm & 0xF0
  28.         buf |= 0x04               # RS = 0, RW = 0, EN = 1
  29.         write_word(LCD_ADDR ,buf)
  30.         time.sleep(0.002)
  31.         buf &= 0xFB               # Make EN = 0
  32.         write_word(LCD_ADDR ,buf)
  33.         
  34.         # Send bit3-0 secondly
  35.         buf = (comm & 0x0F) << 4
  36.         buf |= 0x04               # RS = 0, RW = 0, EN = 1
  37.         write_word(LCD_ADDR ,buf)
  38.         time.sleep(0.002)
  39.         buf &= 0xFB               # Make EN = 0
  40.         write_word(LCD_ADDR ,buf)
  41. def send_data(data):
  42.         # Send bit7-4 firstly
  43.         buf = data & 0xF0
  44.         buf |= 0x05               # RS = 1, RW = 0, EN = 1
  45.         write_word(LCD_ADDR ,buf)
  46.         time.sleep(0.002)
  47.         buf &= 0xFB               # Make EN = 0
  48.         write_word(LCD_ADDR ,buf)
  49.         
  50.         # Send bit3-0 secondly
  51.         buf = (data & 0x0F) << 4
  52.         buf |= 0x05               # RS = 1, RW = 0, EN = 1
  53.         write_word(LCD_ADDR ,buf)
  54.         time.sleep(0.002)
  55.         buf &= 0xFB               # Make EN = 0
  56.         write_word(LCD_ADDR ,buf)
  57. def init_lcd():
  58.         try:
  59.                 send_command(0x33) # Must initialize to 8-line mode at first
  60.                 time.sleep(0.005)
  61.                 send_command(0x32) # Then initialize to 4-line mode
  62.                 time.sleep(0.005)
  63.                 send_command(0x28) # 2 Lines & 5*7 dots
  64.                 time.sleep(0.005)
  65.                 send_command(0x0C) # Enable display without cursor
  66.                 time.sleep(0.005)
  67.                 send_command(0x01) # Clear Screen
  68.                 logging.info('LCD init over')
  69.                 BUS.write_byte(LCD_ADDR ,0x08)
  70.                 logging.info('LCD turning on BLight')
  71.         except:
  72.                 return False
  73.         else:
  74.                 return True
  75. def clear_lcd():
  76.         send_command(0x01) # Clear Screen
  77. def print_lcd(x, y, str):
  78.         if x < 0:
  79.                 x = 0
  80.         if x > 15:
  81.                 x = 15
  82.         if y <0:
  83.                 y = 0
  84.         if y > 1:
  85.                 y = 1
  86.         # Move cursor
  87.         addr = 0x80 + 0x40 * y + x
  88.         send_command(addr)
  89.         
  90.         for chr in str:
  91.                 send_data(ord(chr))
  92. if __name__ == '__main__':
  93.         init_lcd()
  94.         print_lcd(0, 0, 'Hello, DFRobot!!!')
  95.         print_lcd(8, 1, 'By lingfeng')
复制代码

lcdtest.py

  1. #!/user/bin/env python
  2. import smbus
  3. import time
  4. import sys
  5. import LCD1602 as LCD
  6. import logx
  7. import logging
  8. if __name__ == '__main__':  
  9.         LCD.init_lcd()
  10.         time.sleep(2)
  11.         LCD.print_lcd(0,0,'Hello DFRobot!')
  12.         LCD.print_lcd(1,1,'local time')
  13.         logging.info('wait 2 seconds')
  14.         LCD.turn_light(1)
  15.         logging.info('turn on Light')
  16.         time.sleep(5)
  17.         while True:
  18.                 nowtime = time.strftime('%m-%d %H:%M:%S',time.localtime(time.time()))
  19.                 hourtime = time.strftime('%H',time.localtime(time.time()))
  20.                 mintime = time.strftime('%M',time.localtime(time.time()))
  21.                 sectime = time.strftime('%S',time.localtime(time.time()))
  22.                 LCD.print_lcd(1,1,nowtime)
  23.                 if mintime == '59':
  24.                         if sectime == '00':
  25.                                 LCD.turn_light(1)
  26.                         elif sectime  == '59':
  27.                                 LCD.turn_light(0)
  28.                 time.sleep(1)        
复制代码

上图难,上图难于上青天~~~网速好了再发图



创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(10)
丄帝De咗臂
丄帝De咗臂
这么多代码呢
qq815076543322
qq815076543322
评论内容
dsweiliang
dsweiliang
能显示中文么
凌风清羽
凌风清羽
作者
回复dsweiliang
当道理上是这样的,我下次可以发一个自定义显示图形的帖子
凌风清羽
凌风清羽
作者
回复dsweiliang
当道理上是这样的,我下次可以发一个自定义显示图形的帖子
共4条回复,已加载2条,点击查看更多共4条回复,已加载全部
凌风清羽
凌风清羽
作者
以后无论是拍照还是截图都要留出给DF放水印的位置
凌风清羽
凌风清羽
作者
以后无论是拍照还是截图都要留出给DF放水印的位置
Ash1
Ash1回复凌风清羽

哈哈哈哈 悟出经验了
凌风清羽
凌风清羽
作者
吼吼吼,终于有图了~~~
孙毅
孙毅
另外,你的1602的屏幕什么样子,我最近买了几个 IIC的 OLED,点亮进行中。。。
孙毅
孙毅回复凌风清羽
哦哦哦~~~了解了
凌风清羽
凌风清羽
作者
回复孙毅
上不了图啊,就是DF那款,只是单独拆下来的1602 ,最便宜的那种
https://www.dfrobot.com.cn/goods-372.html
孙毅
孙毅
图片看不到啊。。。。我上传的时候,速度还能接受啊,就是有时候自己拍的照片大小可能会超过限制,得自己找软件重新保存下才行。
dsweiliang
dsweiliang
图挂了
凌风清羽
凌风清羽
作者
想问一下大家发帖的时候图片上传的速度怎么样~~~
- 没有更多了 -