2013年11月2日土曜日

Raspberry Pi + I2C LCD module

Raspberry Pi に I2C で秋月電子の LCD ディスプレイをつないでみる。
そのときの記録。

使用した LCD ディスプレイは「I2C接続小型LCDモジュールピッチ変換キット」。
これは、「I2C接続小型LCDモジュール 8x2行」と「I2C接続小型LCDモジュール用ピッチ変換基板」をセットにしたもの。LCDモジュールだけではブレッドボードに取り付けるのが大変なので、ピッチ変換基板も必要となる。

I2C の準備はこちら「Raspberry Pi + I2C」を参照。

Python でソフトを作る。
ソースはこれ。日付と時刻を表示する。

#!/usr/bin/env python
# coding: UTF-8

'''
$ sudo apt-get install python-smbus
$ sudo apt-get install i2c-tools

comment out this line in /etc/modprobe.d/raspi-blacklist.conf
blacklist i2c-bcm2708

add the following lines to /etc/modules  
 i2c-dev 
 i2c-bcm2708
and then reboot.

search all addr.
$ sudo i2cdetect -y 1
'''

import smbus
import RPi.GPIO as GPIO
import time
import math
import datetime

class st7032i:
    def __init__(self, addr = 0x3e, ch = 1, contrast = 0x20):
        self.addr = addr
        self.ch = ch
        self.bus = smbus.SMBus(ch)
        self.contrast = contrast
        self.reset()
        
    def reset(self):
        contrast_h = 0x70 | (self.contrast & 0x0f)
        contrast_l = 0x54 | ((self.contrast >> 4) & 0x03)
        self.bus.write_i2c_block_data(self.addr, 0, [0x38, 0x39, 0x14, contrast_h, contrast_l, 0x6c])

        time.sleep(0.25)
        self.bus.write_i2c_block_data(self.addr, 0, [0x0c, 0x01, 0x06])
        time.sleep(0.05)
        
    def clear(self):
        self.bus.write_i2c_block_data(self.addr, 0, [0x01])
        
    def mov_to(self, row = 0, col = 0):
        self.bus.write_i2c_block_data(self.addr, 0, [0x80 + 0x40 * row + col])
        
    def put_str(self, the_str):
        self.bus.write_i2c_block_data(self.addr, 0x40, map(ord, the_str))
        
if __name__ == '__main__':
  try:
    my_lcd = st7032i(0x3e, 1)
    while True:
        time_str = datetime.datetime.now().strftime("%H:%m:%S")
        date_str = datetime.datetime.now().strftime("%y/%m/%d")
        my_lcd.clear()
        my_lcd.mov_to(0, 0)
        my_lcd.put_str(date_str)
        my_lcd.mov_to(1, 0)
        my_lcd.put_str(time_str)
        print date_str + ' ' + time_str
        time.sleep(1)
  except:
    print "Error accessing default I2C bus"


回路は、LCD モジュールに I2C の信号と電源(3.3V)を供給するだけ。

0 件のコメント:

コメントを投稿