Class library for LCD character display ACM1602NI using I2C on Nucleo. Nucleo 用 I2C 接続の LCD キャラクタ・ディスプレー ACM1602Ni 用のクラス・ライブラリ.
Dependents: UIT2_VariableFIR_LPFHPF UIT2_VariableFIR_LPF UIT2_InputSW_LCD ADDA_Prototype_PollingSW ... more
Diff: ACM1602NI.cpp
- Revision:
- 0:d8048b36d982
- Child:
- 1:7c6b2df4e60b
diff -r 000000000000 -r d8048b36d982 ACM1602NI.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ACM1602NI.cpp Mon Oct 20 03:01:24 2014 +0000 @@ -0,0 +1,111 @@ +//------------------------------------------------------- +// Class for LCD, ACM1602NI +// +// 2014/09/13, Copyright (c) 2014 MIKAMI, Naoki +//------------------------------------------------------- + +#include "ACM1602NI.hpp" + +namespace Mikami +{ + // Constructor + Acm1602Ni::Acm1602Ni(PinName sda, PinName scl, uint32_t clock, + bool cursor, bool blink) + : i2c_(sda, scl), myI2c_((I2C_TypeDef*)NULL) + { + if ( ((sda == PB_9) || (sda == PB_7)) && + ((scl == PB_8) || (scl == PB_6)) ) + myI2c_ = (I2C_TypeDef*)I2C_1; // I2C1 will be used + if ( (sda == PB_3) && (scl == PB_10) ) + myI2c_ = (I2C_TypeDef*)I2C_2; // I2C2 will be used + if ( ((sda == PC_9) || (sda == PB_4)) && + (scl == PA_8) ) + myI2c_ = (I2C_TypeDef*)I2C_3; // I2C3 will be used + + connected_ = Clear(); // Clear display + if (!connected_) return; + + if (clock != 100000) i2c_.frequency(clock); + + WriteCmd(0x38); // data length:8-bit, 2-line, 5×8 dots + WriteCmd(0x0C | (cursor << 1) | blink); + WriteCmd(0x06); // cursor direction: rightward + } + + // All clear + bool Acm1602Ni::Clear() + { + bool ok = WriteCmd(0x01); + wait_ms(50); + return ok; + } + + // Write string from specified position + void Acm1602Ni::WriteString(const char str[], + uint8_t x, uint8_t y) + { + SetXY(x, y); + for (int n=0; n<16; n++) + if (str[n] == 0) break; + else WriteChar(str[n]); + } + + //--------------------------------------------------- + // Following functions: private + + // Send command and data + bool Acm1602Ni::LcdTx(uint8_t cmdData, uint8_t data) + { + if (!Start()) return false; + // defines king of "data" in next statement + TxDR(cmdData); + TxDR(data); + wait_us(500); // indispensable + // Generate stop condition + SetCR1(I2C_CR1_STOP); + return true; + } + + // Preparation for send of command and data + bool Acm1602Ni::Start() + { + const uint8_t WAIT = 20; + const uint8_t LENGTH = 10; + + // wait for I2C not busy + for (int n=0; n<LENGTH; n++) + { + wait_us(WAIT); + if (!Check(myI2c_->SR2, I2C_SR2_BUSY)) break; + if (n == LENGTH-1) return false; + } + + // Generate start condition + SetCR1(I2C_CR1_START); + // Confirm start condition and master mode + for (int n=0; n<LENGTH; n++) + { + wait_us(WAIT); + if (Check(myI2c_->SR1, I2C_SR1_SB) && + Check(myI2c_->SR2, I2C_SR2_MSL | + I2C_SR2_BUSY)) break; + if (n == LENGTH-1) return false; + } + + // Send slave address + TxDR(LCD_ADDRESS_); + // Confirm on transmit mode + for (int n=0; n<LENGTH; n++) + { + wait_us(WAIT); + if (Check(myI2c_->SR1, I2C_SR1_TXE | + I2C_SR1_ADDR) && + Check(myI2c_->SR2, I2C_SR2_MSL | + I2C_SR2_BUSY | + I2C_SR2_TRA)) break; + if (n == LENGTH-1) return false; + } + + return true; + } +}