今回はキャラクタLCDに文字表示するところに挑戦。
ネットでいろいろなプログラムの断片をかき集めいろいろ試行錯誤しましたが
最終的にはオリジナルキャラクタの登録にも成功しました。
液晶画面
上段右側のスマイルマークとベルマークは本プログラムで登録したものです。
プログラムを実行すると下段の数値部分が0から99にカウントアップします。
i2c_CharacterLcd.py
from machine import I2C
import utime
def putstr(string):
StringBuff = bytearray([0x40]) #Co=0,RS=1 Data
for char in string:
StringBuff += char
print(StringBuff)
i2c.writeto(DEFAULT_I2C_ADDR,StringBuff)
def putcmd(command):
CommandBuff = bytearray([0x00,command]) #Co=0,RS=0 Control byte
print(CommandBuff)
i2c.writeto(DEFAULT_I2C_ADDR,CommandBuff)
def custom_char(location,charmap):
#"""Write a character to one of the 8 CGRAM locations, available
#as chr(0) through chr(7).
location &= 0x07
#CommandBuff = chr(0x00)+ chr(0x40|(location << 3))
CommandBuff = bytearray([0x00,(0x40|(location << 3))])
i2c.writeto(DEFAULT_I2C_ADDR,CommandBuff)
utime.sleep_ms(50)
CommandBuff = chr(0x40) #Co=0,RS=1 Data
for i in range(8):
CommandBuff += chr(charmap[i])
i2c.writeto(DEFAULT_I2C_ADDR,CommandBuff)
if __name__ == "__main__":
DEFAULT_I2C_ADDR = 0x3E
print(DEFAULT_I2C_ADDR)
i2c = I2C(I2C.I2C3, freq=300*1000, sda=7, scl=6) # cube
#オリジナルキャラクタの定義
happy = bytearray([0x00,0x0A,0x00,0x04,0x00,0x11,0x0E,0x00])
bell = bytearray([0x04,0x0e,0x0e,0x0e,0x1f,0x00,0x04,0x00])
putcmd(0x38)#ファンクションリセット
utime.sleep_ms(5)
putcmd(0x39)#ファンクションセット
utime.sleep_ms(5)
putcmd(0x14)#発信周波数
utime.sleep_ms(5)
#液晶のコントラスト
putcmd(0x70|0x03)#下位4ビットがコントラスト下位4ビット デフォルト0x03
utime.sleep_ms(5)
putcmd(0x54|0x02)#下位2ビットがコントラスト上位2ビット デフォルト0x02
utime.sleep_ms(1)
putcmd(0x6C)
utime.sleep_ms(300)
putcmd(0x38)#ファンクションリセット
utime.sleep_ms(5)
putcmd(0x01)
utime.sleep_ms(1)
putcmd(0x0C)
utime.sleep_ms(1)
custom_char(0x0,happy)
custom_char(0x1,bell)
putcmd(0x80)#SET DDRAM ADDRESS 0x00
utime.sleep_ms(1)
#文字とオリジナルキャラクタの表示
putstr('keita '+chr(0)+chr(1))#文字と登録したオリジナルキャラクタを表示
putcmd(0x80|0x40)#SET DDRAM ADDRESS 0x40
putstr('Count=')
for num in range(100):
putcmd(0x80|0x46)#DDRAMアドレスを下段の左から7つめ0x46にセット
utime.sleep_ms(100)
putstr(str('{:3d}'.format(num)))
utime.sleep_ms(100)
i2c.deinit()