グラフィックLCDを使ってみよう

01 HelloWorld



文字を表示させるmain.cppを見ると簡単のように見えますが、Glcdクラスで全てを吸収しています。
グラフィックディスプレイモジュールを直接mbedでコントロールするのが普通かと思いますが、ここでは、PIC16F690をコントローラとして作り、これにmbedからシリアルでコマンドおよびデータを送るようにしています。ですから、高速動作はあまり望めませんが、プログラムの練習には良いかもしれません。
グラフィックディスプレイモジュールはフォントを内蔵していませんから、文字を画像として表示する必要があります。

文字を表示させる
#include "mbed.h"
#include "Glcd.h"

int main() {
  Glcd LCD;
  while(true){
    for(int y=0; y<8; y++){
      LCD.print(0, y, "Hello world!");
      wait_ms(200);
    }
    LCD.clearAll();
  }
}
 1 
 2 
 3 
 4 
 5 グラフィックLCDのオブジェクト
 6 永久に表示続ける
 7 画面は8行
 8 x軸は128ドット(0-127)
 9 
10 
11 一画面描いたら画面クリア
12 
13 


Glcd.h
#ifndef GLCD_H
#define GLCD_H
#include "mbed.h"

class Glcd
{
  public:
    Glcd();
    Glcd(PinName tx, PinName rx);
    void clearAll();
    void draw(int x, int y, unsigned char dt[], int n);
    void clear(int x, int y, int n);
    void drawChar(int x, int y, unsigned char ch);
    void print(int x, int y, char *str);
  protected:
    Serial *GLCD; // tx, rx
  private:
};

#endif // GLCD_H
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 画面を全てクリアする
11 グラフィックデータを転送する
12 x,yからn×8ドットクリアする。文字フォント用
13 x,yに英文字を表示させる
14 x,yに英文字列を表示させる
15 
16 シリアルのオブジェクトへのポインタ
17 
18 
19 
20 

PIC16F690へのコマンドを含んでいますから、PICが無い場合には動作しません。PICへのコマンドの部分をその動作を行うメソッドを作れば動作します。そのためには、この資料を読んで作ります。
Glcd.cpp
#include "Glcd.h"

Glcd::Glcd()
{
  //ctor
  GLCD = new Serial(p9, p10); // tx, rx
//  GLCD->baud(19200);
  GLCD->baud(38400);
  do {
      GLCD->putc(0xa5);
  }while(GLCD->getc()!=0xa5);
  clearAll();
}

Glcd::Glcd(PinName tx, PinName rx)
{
  GLCD = new Serial(tx, rx); // tx, rx
  GLCD->baud(38400);
  do {
      GLCD->putc(0xa5);
  }while(GLCD->getc()!=0xa5);
  clearAll();
}
void Glcd::draw(int x, int y, unsigned char dt[], int n)
{
  GLCD->putc(0x08); // start
  GLCD->putc(y);    // page
  GLCD->putc(x);    // col
  GLCD->putc(n);    // # of byte
  for(int i=0; i<n; i++)
    GLCD->putc(dt[i]);
  GLCD->getc();
}

void Glcd::clear(int x, int y, int n)
{
  GLCD->putc(0x08); // start
  GLCD->putc(y);    // page
  GLCD->putc(x);    // col
  GLCD->putc(n);    // # of byte
  for(int i=0; i<n; i++)
    GLCD->putc(0);
  GLCD->getc();
}

void Glcd::drawChar(int x, int y, unsigned char ch)
{
  GLCD->putc(0x02); // char
  GLCD->putc(y); // page
  GLCD->putc(x); // col
  GLCD->putc(ch);
  GLCD->getc();
}

void Glcd::print(int x, int y, char *str)
{
  int n = strlen(str);
  for(int i=0; i<n; i++){
    drawChar(x+i*5, y, (unsigned char)str[i]);
  }
}

void Glcd::clearAll()
{
  GLCD->putc(1);
  GLCD->getc();
}
 1 
 2 
 3 
 4 
 5 
 6 デフォールトは9600bps
 7 19200bpsに変更する場合
 8 38400bpsに変更する場合
 9 これを超えるスピードはうまくいかない
10 PICと同期させるためのマジックナンバー
11 同期すると同様のマジックナンバーが送り返される
12 画面クリアする
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 n×8ドットのグラフィックデータを送る
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