| 1 | |
| 2 | == 16x2 I2C液晶モジュール(水平タイプ) の使い方== |
| 3 | |
| 4 | [https://www.switch-science.com/catalog/1892/ 16x2 I2C液晶モジュール]は[https://www.switch-science.com/catalog/1407/ I2C接続の小型LCD搭載ボード(5V版)]のサイズ違いなだけのように思えますが、[[BR]] |
| 5 | 内蔵しているコントローラが違うため初期化の手順やI2Cスレーブアドレスも違います。 |
| 6 | |
| 7 | こちらが[https://www.switch-science.com/catalog/1892/ 16x2 I2C液晶モジュール]のサンプルスケッチです。 |
| 8 | |
| 9 | {{{ |
| 10 | #include <Wire.h> |
| 11 | |
| 12 | #define I2Cadr 0x3A // 固定 |
| 13 | byte VLCD = 27; // コントラスト(0~63) |
| 14 | |
| 15 | void setup() { |
| 16 | pinMode(16, OUTPUT); |
| 17 | digitalWrite(16, LOW); |
| 18 | pinMode(17, OUTPUT); |
| 19 | digitalWrite(17, HIGH); |
| 20 | |
| 21 | Wire.begin(); |
| 22 | delay(100); |
| 23 | lcd_cmd(0x34); |
| 24 | delay(5); |
| 25 | lcd_cmd(0x34); |
| 26 | delay(5); |
| 27 | lcd_cmd(0x34); |
| 28 | delay(40); |
| 29 | |
| 30 | Wire.beginTransmission(I2Cadr); |
| 31 | Wire.write(0x00); // CO = 0,RS = 0 |
| 32 | Wire.write(0x35); |
| 33 | Wire.write(0x41); |
| 34 | Wire.write(0x80 | VLCD); |
| 35 | Wire.write(0xC0 | VLCD); |
| 36 | Wire.write(0x34); |
| 37 | Wire.endTransmission(); |
| 38 | |
| 39 | lcd_cmd(0x01); |
| 40 | delay(400); |
| 41 | |
| 42 | lcd_cmd(0x0C); |
| 43 | lcd_cmd(0x06); |
| 44 | |
| 45 | delay(500); |
| 46 | } |
| 47 | |
| 48 | void loop() { |
| 49 | |
| 50 | lcd_setCursor(0, 0); |
| 51 | lcd_printStr("Switch "); |
| 52 | lcd_setCursor(0, 1); |
| 53 | lcd_printStr(" Science"); |
| 54 | delay(500); |
| 55 | lcd_setCursor(0, 0); |
| 56 | lcd_printStr(" Switch"); |
| 57 | lcd_setCursor(0, 1); |
| 58 | lcd_printStr("Science "); |
| 59 | delay(500); |
| 60 | } |
| 61 | |
| 62 | void lcd_cmd(unsigned char x) { |
| 63 | Wire.beginTransmission(I2Cadr); |
| 64 | Wire.write(0b00000000); // CO = 0,RS = 0 |
| 65 | Wire.write(x); |
| 66 | Wire.endTransmission(); |
| 67 | } |
| 68 | |
| 69 | void lcd_data(unsigned char x){ |
| 70 | Wire.beginTransmission(I2Cadr); |
| 71 | Wire.write(0b01000000); // CO = 0, RS = 1 |
| 72 | Wire.write(x ^ 0x80); |
| 73 | Wire.endTransmission(); |
| 74 | } |
| 75 | |
| 76 | // 文字の表示 |
| 77 | void lcd_printStr(const char *s) { |
| 78 | Wire.beginTransmission(I2Cadr); |
| 79 | while (*s) { |
| 80 | if (*(s + 1)) { |
| 81 | Wire.write(0b11000000); // CO = 1, RS = 1 |
| 82 | Wire.write(*s ^ 0x80); |
| 83 | } else { |
| 84 | Wire.write(0b01000000); // CO = 0, RS = 1 |
| 85 | Wire.write(*s ^ 0x80); |
| 86 | } |
| 87 | s++; |
| 88 | } |
| 89 | Wire.endTransmission(); |
| 90 | } |
| 91 | |
| 92 | // 表示位置の指定 |
| 93 | void lcd_setCursor(unsigned char x, unsigned char y) { |
| 94 | lcd_cmd(0x80 | (y * 0x40 + x)); |
| 95 | } |
| 96 | |
| 97 | }}} |