// // raspCLCD: Raspberry Pi CLCD (Character LCD) module (using 4bit parallel) // xkozima@myu.ac.jp #include "raspCLCD.h" // 4ビットのコマンド送信 void CLCD::sendCommand4 (unsigned char command) { // RS=0, DB7-4=command gpio_clear(CLCD_RS); if (command & 0x08) gpio_set(CLCD_DB7); else gpio_clear(CLCD_DB7); if (command & 0x04) gpio_set(CLCD_DB6); else gpio_clear(CLCD_DB6); if (command & 0x02) gpio_set(CLCD_DB5); else gpio_clear(CLCD_DB5); if (command & 0x01) gpio_set(CLCD_DB4); else gpio_clear(CLCD_DB4); // Eを発行 gpio_set(CLCD_E); usleep(1); // min 140ns gpio_clear(CLCD_E); usleep(1); } // 8ビットのコマンド送信 void CLCD::sendCommand8 (unsigned char command) { // 上位4ビット sendCommand4(command >> 4); // 下位4ビット sendCommand4(command & 0x0f); } // 8ビットのデータ送信 void CLCD::sendData8 (unsigned char data) { // RS=1, DB7-4=data gpio_set(CLCD_RS); // 上位4ビット if (data & 0x80) gpio_set(CLCD_DB7); else gpio_clear(CLCD_DB7); if (data & 0x40) gpio_set(CLCD_DB6); else gpio_clear(CLCD_DB6); if (data & 0x20) gpio_set(CLCD_DB5); else gpio_clear(CLCD_DB5); if (data & 0x10) gpio_set(CLCD_DB4); else gpio_clear(CLCD_DB4); // Eを発行 gpio_set(CLCD_E); usleep(1); // min 140ns gpio_clear(CLCD_E); usleep(1); // 下位4ビット if (data & 0x08) gpio_set(CLCD_DB7); else gpio_clear(CLCD_DB7); if (data & 0x04) gpio_set(CLCD_DB6); else gpio_clear(CLCD_DB6); if (data & 0x02) gpio_set(CLCD_DB5); else gpio_clear(CLCD_DB5); if (data & 0x01) gpio_set(CLCD_DB4); else gpio_clear(CLCD_DB4); // Eを発行 gpio_set(CLCD_E); usleep(1); // min 140ns gpio_clear(CLCD_E); usleep(1); // wait 43us usleep(43); } // キャラクタ液晶ディスプレー初期化(最初に1回呼び出すこと) void CLCD::init () { // 出力端子の設定 gpio_init(); gpio_configure(CLCD_RS, GPIO_OUTPUT); gpio_configure(CLCD_E, GPIO_OUTPUT); gpio_configure(CLCD_DB7, GPIO_OUTPUT); gpio_configure(CLCD_DB6, GPIO_OUTPUT); gpio_configure(CLCD_DB5, GPIO_OUTPUT); gpio_configure(CLCD_DB4, GPIO_OUTPUT); // 初期化シーケンス // 電源投入後は 15ms 以上待つ (Vcc=5V) usleep(15000); // 8-bit で起動(1): 0011**** (8-bit) + 4.1ms sendCommand4(0x3); usleep(4100); // 8-bit で起動(2): 0011**** (8-bit) + 100us sendCommand4(0x3); usleep(100); // 8-bit で起動(3): 0011**** (8-bit) + 39us sendCommand4(0x3); usleep(39); // 次に 4-bit に設定: 0010 (4-bit) + 39us (ここから4-bit) sendCommand4(0x2); usleep(39); // 機能設定: 0010,1000 (4-bit, 2-line, 5x8font) + 39us sendCommand8(0x28); usleep(39); // 表示設定: 0000,1100 (Display-on, no-Cursor, no-Blinking) + 39us sendCommand8(0x0c); usleep(39); // 表示クリア: 0000,0001 (clear display) + 1.53ms sendCommand8(0x01); usleep(1530); // 描画モード: 0000,0110 (cursor++, no-displayShift) sendCommand8(0x06); usleep(39); } // 表示クリア(空白で埋める) void CLCD::clear () { // 0000, 0001 (clear display) sendCommand8(0x01); usleep(1530); } // カーソル位置の設定(左上(0,0),左下(0,1),右上(15,0),右下(15,1)) void CLCD::locate (int x, int y) { if (y % 2 == 0) sendCommand4(0x8); else sendCommand4(0xc); sendCommand4(x & 0xf); usleep(39); } // 文字列の書き込み(カーソル位置から) void CLCD::print (const char *buf) { for (int i = 0; i < strlen(buf); i++) sendData8(buf[i]); } //