I2C 接続の LCD AQM1602XA-RN-GBW 用のライブラリ. Library for LCD 1602XA-RN-GBW connected using I2C interface.

Dependents:   UIT2_MovingAv_Intr UIT2_VariableFIR UIT2_VowelSynthesizer UIT2_ALE_LeakyLMS ... more

Revision:
12:21830002e12d
Parent:
11:f650f1e809a9
Child:
13:4975e1c1d743
--- a/AQM1602.cpp	Sat Jul 22 01:56:41 2017 +0000
+++ b/AQM1602.cpp	Sat Oct 28 12:57:52 2017 +0000
@@ -1,10 +1,17 @@
 //---------------------------------------------------------------
 //  Class for LCD, AQM1602XA-RN-GBW
-//      Use mbed Rev.129 for Nucleo-F401/446/746 and DISCO-F746
-//  mbed Rev.130 以降で起こる,割り込み処理での動作不良を修正
-//  DISCO-F746 では動作不良は修正できないようだ
+//
+//  Default pin assignments for Nucleo
+//      D14  SDA ---- to pin4 of LCD module
+//      D15  SCL ---- to pin3 of LCD module
 //
-//  2017/07/21, Copyright (c) 2016 MIKAMI, Naoki
+//  動作を確認したボード: Nucleo-F401RE, Nucleo-F411RE, Nucleo-F446RE,
+//                      LPC1768
+//  動作が不良なボード:  Nucleo-F302R8, Nucle0-F746ZG, Disco-f746NG
+//
+//  このプログラムを作った際の mbed のリビジョン:Rev.154
+//
+//  2017/10/28, Copyright (c) 2017 MIKAMI, Naoki
 //---------------------------------------------------------------
 
 #include "AQM1602.hpp"
@@ -23,23 +30,21 @@
     {   Init(clock, cursor, blink); }
 
     // All clear
-    bool Aqm1602::Clear()
+    void Aqm1602::Clear()
     {
-        bool ok = WriteCmd(0x01);
+        WriteCmd(0x01);
         wait_ms(50);
-        return ok;
     }
 
     // Write string
-    void Aqm1602::WriteString(const char str[])
+    void Aqm1602::WriteString(const string str)
     {
-        for (int n=0; n<N_CHR; n++)
-            if (str[n] == 0) break;
-            else             WriteChar(str[n]);
+        int length = min(str.length(), (size_t)N_CHR);
+        for (int n=0; n<length; n++) WriteChar(str[n]);
     }
 
     // Write string from specified position
-    void Aqm1602::WriteStringXY(const char str[],
+    void Aqm1602::WriteStringXY(const string str,
                                 uint8_t x, uint8_t y)
     {
         SetXY(x, y);
@@ -71,8 +76,11 @@
     {
         if (clock != 100000) i2c_.frequency(clock);
 
-        wait_ms(40);
-        connected_ = Clear();   // Clear display
+        wait_ms(100);
+
+        // 初期化のためのコマンド送信手順は,秋月電子の AE-AQM1602A(KIT) の
+        // データシートの記載に従った
+        connected_ = WriteCmd(0x38); // data length:8-bit, 2-line, 5×8 dots
         if (!connected_)
         {
             fprintf(stderr, "\r\nLCD AQM1602 not connected\r\n");
@@ -87,7 +95,10 @@
         wait_ms(200);
 
         WriteCmd(0x38); // data length:8-bit, 2-line, 5×8 dots
+//        WriteCmd(0x01); // clear,これを入れると正常に動作しない
         WriteCmd(0x0C | (cursor << 1) | blink);
+
+        Clear();
     }
 
     // Send command and data
@@ -100,13 +111,13 @@
         wait_us(30);
         return (rt == 0) ? true : false;
 */
-        // mbed Rev.130 以降は下記を使う
+        // 以下は Rev.12 で変更したもの
         i2c_.start();
-        int ack = i2c_.write(LCD_ADDRESS_);
-        ack &= i2c_.write(cmdData);
-        ack &= i2c_.write(data);
+        if (1 != i2c_.write(LCD_ADDRESS_)) return false;
+        if (1 != i2c_.write(cmdData)) return false;
+        if (1 != i2c_.write(data)) return false;
         i2c_.stop();
         wait_us(30);
-        return (ack == 1) ? true : false;
+        return true;
     }
 }