1602 キャラクタディスプレイ データ4bit接続用ライブラリ STM32F103RB CUCLEOボードを想定
CharLcd4bit.cpp@0:0e84781e64cf, 2017-10-06 (annotated)
- Committer:
- tshl
- Date:
- Fri Oct 06 05:01:05 2017 +0000
- Revision:
- 0:0e84781e64cf
?????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tshl | 0:0e84781e64cf | 1 | #include "CharLcd4bit.h" |
tshl | 0:0e84781e64cf | 2 | |
tshl | 0:0e84781e64cf | 3 | #define WAIT_E() wait_ms(1) |
tshl | 0:0e84781e64cf | 4 | #define CHAR_MAX_PER_LINE 16 |
tshl | 0:0e84781e64cf | 5 | #define LINE_MAX 2 |
tshl | 0:0e84781e64cf | 6 | |
tshl | 0:0e84781e64cf | 7 | /* コンストラクタ */ |
tshl | 0:0e84781e64cf | 8 | CharLcd4bit::CharLcd4bit( |
tshl | 0:0e84781e64cf | 9 | PinName rs, |
tshl | 0:0e84781e64cf | 10 | PinName rw, |
tshl | 0:0e84781e64cf | 11 | PinName e, |
tshl | 0:0e84781e64cf | 12 | PinName db4, |
tshl | 0:0e84781e64cf | 13 | PinName db5, |
tshl | 0:0e84781e64cf | 14 | PinName db6, |
tshl | 0:0e84781e64cf | 15 | PinName db7) : |
tshl | 0:0e84781e64cf | 16 | RS(rs), |
tshl | 0:0e84781e64cf | 17 | RW(rw), |
tshl | 0:0e84781e64cf | 18 | E(e), |
tshl | 0:0e84781e64cf | 19 | DB4(db4), |
tshl | 0:0e84781e64cf | 20 | DB5(db5), |
tshl | 0:0e84781e64cf | 21 | DB6(db6), |
tshl | 0:0e84781e64cf | 22 | DB7(db7) |
tshl | 0:0e84781e64cf | 23 | { |
tshl | 0:0e84781e64cf | 24 | |
tshl | 0:0e84781e64cf | 25 | } |
tshl | 0:0e84781e64cf | 26 | |
tshl | 0:0e84781e64cf | 27 | /* LCD初期化 */ |
tshl | 0:0e84781e64cf | 28 | void CharLcd4bit::Initialize(void) |
tshl | 0:0e84781e64cf | 29 | { |
tshl | 0:0e84781e64cf | 30 | lcd8(0x03, false);//8bit mode |
tshl | 0:0e84781e64cf | 31 | lcd8(0x03, false);//8bit mode |
tshl | 0:0e84781e64cf | 32 | lcd8(0x03, false);//8bit mode |
tshl | 0:0e84781e64cf | 33 | lcd8(0x02, false);//4bit mode |
tshl | 0:0e84781e64cf | 34 | lcd4(0x08);//2行表示モード |
tshl | 0:0e84781e64cf | 35 | lcd4(0x0c);//表示設定 |
tshl | 0:0e84781e64cf | 36 | lcd4(0x06);//entry mode |
tshl | 0:0e84781e64cf | 37 | lcd4(0x01);//clear |
tshl | 0:0e84781e64cf | 38 | } |
tshl | 0:0e84781e64cf | 39 | |
tshl | 0:0e84781e64cf | 40 | /* 1行出力 */ |
tshl | 0:0e84781e64cf | 41 | void CharLcd4bit::WriteLine(int line, char *str) |
tshl | 0:0e84781e64cf | 42 | { |
tshl | 0:0e84781e64cf | 43 | char *ad=0; |
tshl | 0:0e84781e64cf | 44 | |
tshl | 0:0e84781e64cf | 45 | // 文字位置指定 |
tshl | 0:0e84781e64cf | 46 | SetPosition(line, 0); |
tshl | 0:0e84781e64cf | 47 | |
tshl | 0:0e84781e64cf | 48 | ad = str; |
tshl | 0:0e84781e64cf | 49 | for(int i = 0; i < CHAR_MAX_PER_LINE; i++) |
tshl | 0:0e84781e64cf | 50 | { |
tshl | 0:0e84781e64cf | 51 | if(*ad) |
tshl | 0:0e84781e64cf | 52 | { |
tshl | 0:0e84781e64cf | 53 | WriteChar(*ad); |
tshl | 0:0e84781e64cf | 54 | ad++; |
tshl | 0:0e84781e64cf | 55 | } |
tshl | 0:0e84781e64cf | 56 | else |
tshl | 0:0e84781e64cf | 57 | { |
tshl | 0:0e84781e64cf | 58 | // 文字数が足りない時は空白で埋める |
tshl | 0:0e84781e64cf | 59 | WriteChar(' '); |
tshl | 0:0e84781e64cf | 60 | } |
tshl | 0:0e84781e64cf | 61 | } |
tshl | 0:0e84781e64cf | 62 | } |
tshl | 0:0e84781e64cf | 63 | |
tshl | 0:0e84781e64cf | 64 | /* 1行出力 */ |
tshl | 0:0e84781e64cf | 65 | void CharLcd4bit::WriteStr(char *str) |
tshl | 0:0e84781e64cf | 66 | { |
tshl | 0:0e84781e64cf | 67 | char *ad=0; |
tshl | 0:0e84781e64cf | 68 | |
tshl | 0:0e84781e64cf | 69 | ad = str; |
tshl | 0:0e84781e64cf | 70 | while(*ad) |
tshl | 0:0e84781e64cf | 71 | { |
tshl | 0:0e84781e64cf | 72 | WriteChar(*ad); |
tshl | 0:0e84781e64cf | 73 | ad++; |
tshl | 0:0e84781e64cf | 74 | } |
tshl | 0:0e84781e64cf | 75 | } |
tshl | 0:0e84781e64cf | 76 | |
tshl | 0:0e84781e64cf | 77 | /* 1文字出力 */ |
tshl | 0:0e84781e64cf | 78 | void CharLcd4bit::WriteChar(char c) |
tshl | 0:0e84781e64cf | 79 | { |
tshl | 0:0e84781e64cf | 80 | lcd8(c >> 4, true); |
tshl | 0:0e84781e64cf | 81 | lcd8(c, true); |
tshl | 0:0e84781e64cf | 82 | } |
tshl | 0:0e84781e64cf | 83 | |
tshl | 0:0e84781e64cf | 84 | /* 文字位置指定 */ |
tshl | 0:0e84781e64cf | 85 | void CharLcd4bit::SetPosition(int line, int position) |
tshl | 0:0e84781e64cf | 86 | { |
tshl | 0:0e84781e64cf | 87 | if(line==1) |
tshl | 0:0e84781e64cf | 88 | lcd4(0x80 + position); /*1行目*/ |
tshl | 0:0e84781e64cf | 89 | else if(line==2) |
tshl | 0:0e84781e64cf | 90 | lcd4(0xc0 + position); /*2行目*/ |
tshl | 0:0e84781e64cf | 91 | } |
tshl | 0:0e84781e64cf | 92 | |
tshl | 0:0e84781e64cf | 93 | void CharLcd4bit::Clear() |
tshl | 0:0e84781e64cf | 94 | { |
tshl | 0:0e84781e64cf | 95 | lcd4(0x01); |
tshl | 0:0e84781e64cf | 96 | } |
tshl | 0:0e84781e64cf | 97 | |
tshl | 0:0e84781e64cf | 98 | /* 8bitモード出力 -> 4bit接続に変換して出力 */ |
tshl | 0:0e84781e64cf | 99 | void CharLcd4bit::lcd8(char c, bool isData) |
tshl | 0:0e84781e64cf | 100 | { |
tshl | 0:0e84781e64cf | 101 | char temp = c; |
tshl | 0:0e84781e64cf | 102 | |
tshl | 0:0e84781e64cf | 103 | DB4 = temp & 0x01; |
tshl | 0:0e84781e64cf | 104 | temp = temp >> 1; |
tshl | 0:0e84781e64cf | 105 | |
tshl | 0:0e84781e64cf | 106 | DB5 = temp & 0x01; |
tshl | 0:0e84781e64cf | 107 | temp = temp >> 1; |
tshl | 0:0e84781e64cf | 108 | |
tshl | 0:0e84781e64cf | 109 | DB6 = temp & 0x01; |
tshl | 0:0e84781e64cf | 110 | temp = temp >> 1; |
tshl | 0:0e84781e64cf | 111 | |
tshl | 0:0e84781e64cf | 112 | DB7 = temp & 0x01; |
tshl | 0:0e84781e64cf | 113 | |
tshl | 0:0e84781e64cf | 114 | if(isData) RS = 1; |
tshl | 0:0e84781e64cf | 115 | E=1; |
tshl | 0:0e84781e64cf | 116 | WAIT_E(); |
tshl | 0:0e84781e64cf | 117 | E=0; |
tshl | 0:0e84781e64cf | 118 | if(isData) RS = 0; |
tshl | 0:0e84781e64cf | 119 | WAIT_E(); |
tshl | 0:0e84781e64cf | 120 | } |
tshl | 0:0e84781e64cf | 121 | |
tshl | 0:0e84781e64cf | 122 | /* 4bitモード出力 */ |
tshl | 0:0e84781e64cf | 123 | void CharLcd4bit::lcd4(char data) |
tshl | 0:0e84781e64cf | 124 | { |
tshl | 0:0e84781e64cf | 125 | lcd8(data >> 4, false); |
tshl | 0:0e84781e64cf | 126 | lcd8(data, false); |
tshl | 0:0e84781e64cf | 127 | } |