こんにちは!駆け出しエンジニアのまっきーです。
今回は。「I2Cを使ってLCDに文字を表示させる!②(C言語)」で作成したソースコードをもとに、LCDのライブラリを作成します。
ライブラリ作成の手順
- ヘッダーファイルの作成(拡張子.h)
- ライブラリのソースコード作成(拡張子.c)
- ライブラリを作成(拡張子.a)
- ビルドの設定
これだけの手順でライブラリを作成することができます。
ライブラリには
- ・静的ライブラリ:includeするだけですぐ使える
- ・共有ライブラリ:ライブラリをリンクする作業が必要
というように、2種類あります。今回は静的ライブラリの実装をします。
実際にやってみる
これから作成するファイルは、同じディレクトリに保存してください。(pi_practiceと仮定します)
ヘッダーファイルの作成
ファイル名を「lcdPi.h」として保存をします。
ここで定義した関数を自由に使えるようになります!
1 2 3 4 5 6 7 |
//LCD表示に必要な関数 extern void write_word(int data); extern void send_command(int comm); extern void send_data(int data); extern void init(); extern void clear(); extern void write(int x, int y, char data[]); |
「extern宣言」は複数ファイルによるシステムにおいて、グローバル変数を共有するための仕組みです。
ライブラリのソースコード作成
ファイル名を「lcdPi.c」として保存します。以下の2点に注意して下さい。
- ・先ほど作成した"lcdPi.h"をincludeすること
- ・main関数は入れないこと
ヘッダーファイルを囲むカッコには次のような意味があります。
「<>」デフォルトのディレクトリ(usr/include)から参照
「""」ソースコードの置かれているカレントディレクトリから参照
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
#include <stdio.h> #include <wiringPi.h> #include <wiringPiI2C.h> #include <string.h> #include "lcdPi.h" int LCDAddr = 0x27;//I2c address of LCD, some LCD i2c address might be 0x27 int BLEN = 1;//1--open backlight.0--close backlight int fd;//linux file descriptor //send an 16 bits data to LCD buffer void write_word(int data){ int temp = data; if ( BLEN == 1) temp |= 0x08; else temp &= 0xF7; wiringPiI2CWrite(fd, temp); } //send control command to lcd void send_command(int comm){ int buf; // Send bit7-4 firstly buf = comm & 0xF0; buf |= 0x04; // RS = 0, RW = 0, EN = 1 write_word(buf); delay(2); buf &= 0xFB; // Make EN = 0 write_word(buf); // Send bit3-0 secondly buf = (comm & 0x0F)<<4; buf |= 0x04; // RS = 0, RW = 0, EN = 1 write_word(buf); delay(2); buf &= 0xFB; // Make EN = 0 write_word(buf); } //send character to lcd void send_data(int data){ int buf; // Send bit7-4 firstly buf = data & 0xF0; buf |= 0x05; // RS = 1, RW = 0, EN = 1 write_word(buf); delay(2); buf &= 0xFB; // Make EN = 0 write_word(buf); // Send bit3-0 secondly buf = (data & 0x0F) << 4; buf |= 0x05; // RS = 1, RW = 0, EN = 1 write_word(buf); delay(2); buf &= 0xFB; // Make EN = 0 write_word(buf); } //initialize the lcd void init(){ fd = wiringPiI2CSetup(LCDAddr);//追加しました send_command(0x33); // Must initialize to 8-line mode at first delay(5); send_command(0x32); // Then initialize to 4-line mode delay(5); send_command(0x28); // 2 Lines & 5*7 dots delay(5); send_command(0x0C); // Enable display without cursor delay(5); send_command(0x01); // Clear Screen wiringPiI2CWrite(fd, 0x08); } //clear screen void clear(){ send_command(0x01); //clear Screen } //Print the message on the lcd void write(int x, int y, char data[]){ int addr, i; int tmp; if (x < 0) x = 0; if (x > 15) x = 15; if (y < 0) y = 0; if (y > 1) y = 1; // Move cursor addr = 0x80 + 0x40 * y + x; send_command(addr); tmp = strlen(data); for (i = 0; i < tmp; i++){ send_data(data[i]); } } |
ライブラリを作成(拡張子.a)
図に従って操作していきます。使用するエディタは「Geany」です。
まず、lcdPi.cをコンパイルします。

ターミナルの場合はこのコマンドでコンパイルできます。(ディレクトリに注意)
1 |
gcc -Wall -o lcdPi lcdPi.c -lwiringPi |
次に、ターミナルを開き以下のコマンドを入力します。
1 |
cd pi_practice(ソースコードのあるディレクトリへ) |
1 |
ar rcs liblcdPi.a lcdPi.o |
- ・ar :書庫(アーカイブ)ファイルを作成する
- ・r :アーカイブにメンバを追加
- ・c :アーカイブの作成
- ・s :シンボルテーブルを作成
ビルドの設定
「Geany」を使っている方は、以下の手順でビルド設定を変更してください
ビルドのビルドコマンドを設定を選択

Buildコマンドを以下のように書き換える

1 |
gcc -Wall -o"%e""%f" -lwiringPi -lpthread -g -o0 liblcdPi.a |
ビルドをターミナルで行う方はこちら↓
1 |
./lcdPi liblcdPi.a |
以上でライブラリの作成は終了です。
では、実際に使ってみます。使用するライブラリ関数の仕様です。

ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> #include <wiringPi.h> #include <wiringPiI2C.h> #include <string.h> #include "lcdPi.h" int main(void){ init(); while(1){ write(0,0,"ohayo"); delay(3000); clear(); } return 0; } |
o | h | a | y | o | |||||||||||||
こんな感じでLCDに文字が表示されます。
まとめ
ライブラリ作成の手順を復習します。
- ヘッダーファイルの作成(拡張子.h)
- ライブラリのソースコード作成(拡張子.c)
- ライブラリを作成(拡張子.a)
- ビルドの設定
一度ライブラリを作ってしまえば、関数を呼び出すだけで簡単に使えて便利ですね!
汎用性を高くしないといけないところが難しいですね。今回のを例にすると、スレイブアドレス(0x27)をライブラリに含めてしまいました。
これだと、ほかのアドレスの時に対応できないですね。改善方法は模索中です。もしわかる方がいたら教えてください。
今回はこれで終わります!ありがとうございました。