2009/09/22

I2C LCD用ライブラリを作ってみた

ビーコン菅原の組み込みがらみ | Arduinoで I2C LCD」を参考にして、ストロベリーリナックスのI2C低電圧キャラクタ液晶モジュール(16x2行)を動かしてみた。
I2C LCD on arduino
ピン2本だけでLCDが制御できてしまうのは楽チンだ。
ついでにアイコンの表示制御機能も追加した上で、ライブラリ化してみた。
スケッチはとにかく簡潔にしたいのである...;-)

I2C LCD(ST7032i)用ライブラリ:NsLCD-0.1.zip

ちなみに、ライブラリを置く場所は、IDEのバージョンが0017なら、「Sketchbook location」直下の「libraries」(無ければ自分で作ればよい)、IDEのバージョンが0016以前なら、IDEインストール場所の「Arduino\hardware\libraries」。

NsLCDを使ったスケッチはこんな感じになる。
#include <Wire.h>
#include <NsLCD.h>

// NsLCD library for I2C LCD(ST7032i) sample sketch
// YKO[2009/09/21]

void setup()
{
// initialize I2C LCD
Wire.begin( 0 ); // I2C master mode
NsLCD.init();
}

void loop()
{
NsLCD.clear();

NsLCD.moveCursor(0,0);
NsLCD.print("cyclic landscape");

NsLCD.moveCursor(0,1);
NsLCD.print(" by YKO");

NsLCD.displayAntennaIcon(true);
NsLCD.displayTelIcon(true);
NsLCD.displaySoundIcon(true);
NsLCD.displayOutputIcon(true);
NsLCD.displayUpDownIcon(true,true);
NsLCD.displayKeyIcon(true);
NsLCD.displayMuteIcon(true);
NsLCD.displayBatteryIcon(true,3);
NsLCD.displayNumIcon(true);

delay(1000);

NsLCD.home();
for(int i=0;i<16;i++) NsLCD.write('@');

NsLCD.displayAntennaIcon(false);
NsLCD.displayTelIcon(false);
NsLCD.displaySoundIcon(false);
NsLCD.displayOutputIcon(false);
NsLCD.displayUpDownIcon(false,false);
NsLCD.displayKeyIcon(false);
NsLCD.displayMuteIcon(false);
NsLCD.displayBatteryIcon(true,0);
NsLCD.displayNumIcon(false);

delay(1000);
}
使い方のポイントは以下の通り。
  • init()する前に、必ずI2Cライブラリの方でWire.begin(0)すること。

  • init()では500msecほど、clear()では200msecほど、それぞれ待ち時間が発生する。

  • moveCursor()でカーソルを動かして、print()で文字列、write()で文字、を表示。

  • display??Icon()で各アイコンの表示状態を制御。

とりあえず問題無く動いてるみたい。

0 件のコメント: