Library for LCD ACM1602NI connected using I2C on Nucleo F401 is not to be found. Using I2C class offered by mbed, I cannot program for this LCD on Nucleo F401. So I programmed for this LCD to control by direct accsessing registors for I2C in STM32F401RE. Nucleo F401 で使用できる,I2C 接続の LCD ACM1602NI 用のライブラリ.このライブラリでは,I2C クラスのコンストラクタとクロック周波数設定のメンバ関数は使っているが,その他のメンバ関数などは使っていない.その理由は,タイミングの関係だと思うが,I2C クラスのメンバ関数では正常に動くプログラムを,どうしても作れないことによる.しょうがないので,MCU の I2C 関係のレジスタに直接アクセスするようなプログラムを作ったたところ,動くようになった.Nucleo の F401RE 以外については確認していない.動かない場合は,I2C 用のレジスタのアドレスを,使っている MCU に応じて書きかえる必要がある.

Dependents:   ACM1602NI_Nucleo_Demo

Committer:
MikamiUitOpen
Date:
Mon Dec 01 03:47:45 2014 +0000
Revision:
3:b2a573e31cc9
4

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MikamiUitOpen 3:b2a573e31cc9 1 //------------------------------------------------------
MikamiUitOpen 3:b2a573e31cc9 2 // Class for LCD, ACM1602NI
MikamiUitOpen 3:b2a573e31cc9 3 //
MikamiUitOpen 3:b2a573e31cc9 4 // 2014/08/27, Copyright (c) 2014 MIKAMI, Naoki
MikamiUitOpen 3:b2a573e31cc9 5 //------------------------------------------------------
MikamiUitOpen 3:b2a573e31cc9 6
MikamiUitOpen 3:b2a573e31cc9 7 #include "ACM1602NI.hpp"
MikamiUitOpen 3:b2a573e31cc9 8
MikamiUitOpen 3:b2a573e31cc9 9 namespace Mikami
MikamiUitOpen 3:b2a573e31cc9 10 {
MikamiUitOpen 3:b2a573e31cc9 11 // Constructor
MikamiUitOpen 3:b2a573e31cc9 12 Acm1602Ni::Acm1602Ni(PinName sda, PinName scl, uint32_t clock,
MikamiUitOpen 3:b2a573e31cc9 13 bool cursor, bool blink)
MikamiUitOpen 3:b2a573e31cc9 14 : i2c_(sda, scl)
MikamiUitOpen 3:b2a573e31cc9 15 {
MikamiUitOpen 3:b2a573e31cc9 16 uint32_t i2cAddr = 0;
MikamiUitOpen 3:b2a573e31cc9 17 if ( ((sda == PB_9) || (sda == PB_7)) &&
MikamiUitOpen 3:b2a573e31cc9 18 ((scl == PB_8) || (scl == PB_6)) )
MikamiUitOpen 3:b2a573e31cc9 19 i2cAddr = I2C_1; // I2C1 will be used
MikamiUitOpen 3:b2a573e31cc9 20 if ( (sda == PB_3) && (scl == PB_10) )
MikamiUitOpen 3:b2a573e31cc9 21 i2cAddr = I2C_2; // I2C2 will be used
MikamiUitOpen 3:b2a573e31cc9 22 if ( ((sda == PC_9) || (sda == PB_4)) &&
MikamiUitOpen 3:b2a573e31cc9 23 (scl == PA_8) )
MikamiUitOpen 3:b2a573e31cc9 24 i2cAddr = I2C_3; // I2C3 will be used
MikamiUitOpen 3:b2a573e31cc9 25
MikamiUitOpen 3:b2a573e31cc9 26 i2c_cr1_ = (uint32_t *)i2cAddr;
MikamiUitOpen 3:b2a573e31cc9 27 i2c_dr_ = (uint32_t *)(i2cAddr + 0x10);
MikamiUitOpen 3:b2a573e31cc9 28 i2c_sr1_ = (uint32_t *)(i2cAddr + 0x14);
MikamiUitOpen 3:b2a573e31cc9 29 i2c_sr2_ = (uint32_t *)(i2cAddr + 0x18);
MikamiUitOpen 3:b2a573e31cc9 30
MikamiUitOpen 3:b2a573e31cc9 31 connected_ = Clear(); // Clear display
MikamiUitOpen 3:b2a573e31cc9 32 if (!connected_) return;
MikamiUitOpen 3:b2a573e31cc9 33
MikamiUitOpen 3:b2a573e31cc9 34 if (clock != 100000) i2c_.frequency(clock);
MikamiUitOpen 3:b2a573e31cc9 35
MikamiUitOpen 3:b2a573e31cc9 36 WriteCmd(0x38); // data length:8-bit, 2-line, 5×8 dots
MikamiUitOpen 3:b2a573e31cc9 37 WriteCmd(0x0C | (cursor << 1) | blink);
MikamiUitOpen 3:b2a573e31cc9 38 WriteCmd(0x06); // cursor direction: rightward
MikamiUitOpen 3:b2a573e31cc9 39 }
MikamiUitOpen 3:b2a573e31cc9 40
MikamiUitOpen 3:b2a573e31cc9 41 // All clear
MikamiUitOpen 3:b2a573e31cc9 42 bool Acm1602Ni::Clear()
MikamiUitOpen 3:b2a573e31cc9 43 {
MikamiUitOpen 3:b2a573e31cc9 44 bool ok = WriteCmd(0x01);
MikamiUitOpen 3:b2a573e31cc9 45 wait_ms(50);
MikamiUitOpen 3:b2a573e31cc9 46 return ok;
MikamiUitOpen 3:b2a573e31cc9 47 }
MikamiUitOpen 3:b2a573e31cc9 48
MikamiUitOpen 3:b2a573e31cc9 49 // Write string from specified position
MikamiUitOpen 3:b2a573e31cc9 50 void Acm1602Ni::WriteString(const char str[], uint8_t x, uint8_t y)
MikamiUitOpen 3:b2a573e31cc9 51 {
MikamiUitOpen 3:b2a573e31cc9 52 SetXY(x, y);
MikamiUitOpen 3:b2a573e31cc9 53 for (int n=0; n<16; n++)
MikamiUitOpen 3:b2a573e31cc9 54 if (str[n] == 0) break;
MikamiUitOpen 3:b2a573e31cc9 55 else WriteChar(str[n]);
MikamiUitOpen 3:b2a573e31cc9 56 }
MikamiUitOpen 3:b2a573e31cc9 57
MikamiUitOpen 3:b2a573e31cc9 58 //------------------------------------------------------------------------------
MikamiUitOpen 3:b2a573e31cc9 59 // Following: private functions
MikamiUitOpen 3:b2a573e31cc9 60
MikamiUitOpen 3:b2a573e31cc9 61 // Send command and data
MikamiUitOpen 3:b2a573e31cc9 62 bool Acm1602Ni::LcdTx(uint8_t cmdData, uint8_t data)
MikamiUitOpen 3:b2a573e31cc9 63 {
MikamiUitOpen 3:b2a573e31cc9 64 if (!Start()) return false;
MikamiUitOpen 3:b2a573e31cc9 65 TxDR(cmdData); // "cmdData" defines king of "data" in next statement
MikamiUitOpen 3:b2a573e31cc9 66 TxDR(data); // Send command or RAM dataspecify specified by "cmdData"
MikamiUitOpen 3:b2a573e31cc9 67 wait_us(500); // indispensable
MikamiUitOpen 3:b2a573e31cc9 68 SetCR1(0x200); // Generate stop condition
MikamiUitOpen 3:b2a573e31cc9 69 return true;
MikamiUitOpen 3:b2a573e31cc9 70 }
MikamiUitOpen 3:b2a573e31cc9 71
MikamiUitOpen 3:b2a573e31cc9 72 // Preparation for send of command and data
MikamiUitOpen 3:b2a573e31cc9 73 bool Acm1602Ni::Start()
MikamiUitOpen 3:b2a573e31cc9 74 {
MikamiUitOpen 3:b2a573e31cc9 75 const uint8_t WAIT = 20;
MikamiUitOpen 3:b2a573e31cc9 76 const uint8_t LENGTH = 10;
MikamiUitOpen 3:b2a573e31cc9 77 uint8_t n;
MikamiUitOpen 3:b2a573e31cc9 78
MikamiUitOpen 3:b2a573e31cc9 79 // wait for I2C not busy
MikamiUitOpen 3:b2a573e31cc9 80 for (n=0; n<LENGTH; n++)
MikamiUitOpen 3:b2a573e31cc9 81 {
MikamiUitOpen 3:b2a573e31cc9 82 wait_us(WAIT);
MikamiUitOpen 3:b2a573e31cc9 83 if (!Check(i2c_sr2_, 0x02)) break;
MikamiUitOpen 3:b2a573e31cc9 84 }
MikamiUitOpen 3:b2a573e31cc9 85 if (n == LENGTH) return false;
MikamiUitOpen 3:b2a573e31cc9 86
MikamiUitOpen 3:b2a573e31cc9 87 // Generate start condition
MikamiUitOpen 3:b2a573e31cc9 88 SetCR1(0x100);
MikamiUitOpen 3:b2a573e31cc9 89 // Confirm start condition and master mode
MikamiUitOpen 3:b2a573e31cc9 90 for (n=0; n<LENGTH; n++)
MikamiUitOpen 3:b2a573e31cc9 91 {
MikamiUitOpen 3:b2a573e31cc9 92 wait_us(WAIT);
MikamiUitOpen 3:b2a573e31cc9 93 if (Check(i2c_sr1_, 0x01) && Check(i2c_sr2_, 0x03)) break;
MikamiUitOpen 3:b2a573e31cc9 94 }
MikamiUitOpen 3:b2a573e31cc9 95 if (n == LENGTH) return false;
MikamiUitOpen 3:b2a573e31cc9 96
MikamiUitOpen 3:b2a573e31cc9 97 // Send slave address
MikamiUitOpen 3:b2a573e31cc9 98 TxDR(LCD_ADDRESS_);
MikamiUitOpen 3:b2a573e31cc9 99 // Confirm on transmit mode
MikamiUitOpen 3:b2a573e31cc9 100 for (n=0; n<LENGTH; n++)
MikamiUitOpen 3:b2a573e31cc9 101 {
MikamiUitOpen 3:b2a573e31cc9 102 wait_us(WAIT);
MikamiUitOpen 3:b2a573e31cc9 103 if (Check(i2c_sr1_, 0x82) && Check(i2c_sr2_, 0x07)) break;
MikamiUitOpen 3:b2a573e31cc9 104 }
MikamiUitOpen 3:b2a573e31cc9 105 if (n == LENGTH) return false;
MikamiUitOpen 3:b2a573e31cc9 106
MikamiUitOpen 3:b2a573e31cc9 107 return true;
MikamiUitOpen 3:b2a573e31cc9 108 }
MikamiUitOpen 3:b2a573e31cc9 109 }