10335浏览
查看: 10335|回复: 17

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

[复制链接]
本帖最后由 凌风清羽 于 2016-1-25 20:00 编辑

【树莓派教程】——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)        
复制代码

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



凌风清羽  中级技匠
 楼主|

发表于 2016-1-9 15:14:29

想问一下大家发帖的时候图片上传的速度怎么样~~~
回复

使用道具 举报

dsweiliang  初级技神

发表于 2016-1-9 15:54:22

图挂了
回复

使用道具 举报

孙毅  初级技匠

发表于 2016-1-9 16:36:53

图片看不到啊。。。。我上传的时候,速度还能接受啊,就是有时候自己拍的照片大小可能会超过限制,得自己找软件重新保存下才行。
回复

使用道具 举报

孙毅  初级技匠

发表于 2016-1-9 16:40:23

另外,你的1602的屏幕什么样子,我最近买了几个 IIC的 OLED,点亮进行中。。。
回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-1-9 16:42:19

孙毅 发表于 2016-1-9 16:40
另外,你的1602的屏幕什么样子,我最近买了几个 IIC的 OLED,点亮进行中。。。 ...

上不了图啊,就是DF那款,只是单独拆下来的1602 ,最便宜的那种
https://www.dfrobot.com.cn/goods-372.html
回复

使用道具 举报

孙毅  初级技匠

发表于 2016-1-9 16:45:01

凌风清羽 发表于 2016-1-9 16:42
上不了图啊,就是DF那款,只是单独拆下来的1602 ,最便宜的那种
https://www.dfrobot.com.cn/goods-372.ht ...

哦哦哦~~~了解了
回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-1-9 17:35:32

吼吼吼,终于有图了~~~
回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-1-9 17:37:53

以后无论是拍照还是截图都要留出给DF放水印的位置
回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-1-9 17:37:55

以后无论是拍照还是截图都要留出给DF放水印的位置
回复

使用道具 举报

dsweiliang  初级技神

发表于 2016-1-9 20:18:01

能显示中文么
回复

使用道具 举报

大连林海  初级技神

发表于 2016-1-9 20:41:50

回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-1-9 22:27:29


没有中文字库啊
回复

使用道具 举报

dsweiliang  初级技神

发表于 2016-1-10 12:31:39


有字库就行?
回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-1-10 14:58:33


当道理上是这样的,我下次可以发一个自定义显示图形的帖子
回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-1-10 14:58:34


当道理上是这样的,我下次可以发一个自定义显示图形的帖子
回复

使用道具 举报

丄帝De咗臂  高级技匠

发表于 2016-1-10 19:58:44

这么多代码呢
回复

使用道具 举报

Ash  管理员

发表于 2016-1-11 15:48:16

凌风清羽 发表于 2016-1-9 17:37
以后无论是拍照还是截图都要留出给DF放水印的位置

哈哈哈哈 悟出经验了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail