Library for LCD ACM1602NI connected using I2C interface on Nucleo F401/411. Nucleo F401/411RE で使える I2C 接続の LCD ACM1602NI 用のライブラリ.

Dependents:   ACM1602NI_NucleoF4_Demo ACM1602NI_NucleoF4_Demo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ACM1602NI.cpp Source File

ACM1602NI.cpp

00001 //-------------------------------------------------------
00002 //  Class for LCD, ACM1602Ni
00003 //
00004 //  2014/12/14, Copyright (c) 2014 MIKAMI, Naoki
00005 //-------------------------------------------------------
00006 
00007 #include "ACM1602NI.hpp"
00008 
00009 namespace Mikami
00010 {
00011     // Constructor
00012     Acm1602Ni::Acm1602Ni(PinName sda, PinName scl, uint32_t clock,
00013                          bool cursor, bool blink)
00014         : i2c_(sda, scl), myI2c_((I2C_TypeDef*)NULL)
00015     {
00016         if ( ((sda == PB_9) || (sda == PB_7)) &&
00017              ((scl == PB_8) || (scl == PB_6)) )
00018                 myI2c_ = (I2C_TypeDef*)I2C_1;   // I2C1 will be used
00019         if ( (sda == PB_3) && (scl == PB_10) )
00020                 myI2c_ = (I2C_TypeDef*)I2C_2;   // I2C2 will be used
00021         if ( ((sda == PC_9) || (sda == PB_4)) &&
00022              (scl == PA_8) )
00023                 myI2c_ = (I2C_TypeDef*)I2C_3;   // I2C3 will be used
00024         
00025         connected_ = Clear();      // Clear display
00026         if (!connected_)
00027         {
00028             fprintf(stderr, "\r\nLCD device not connected\r\n");
00029             return;
00030         }
00031         if (clock != 100000) i2c_.frequency(clock);
00032 
00033         WriteCmd(0x38); // data length:8-bit, 2-line, 5×8 dots
00034         WriteCmd(0x0C | (cursor << 1) | blink);
00035         WriteCmd(0x06); // cursor direction: rightward
00036     }
00037 
00038     // All clear
00039     bool Acm1602Ni::Clear()
00040     {
00041         bool ok = WriteCmd(0x01);
00042         wait_ms(50);
00043         return ok;
00044     }
00045 
00046     // Write string
00047     void Acm1602Ni::WriteString(const char str[])
00048     {
00049         for (int n=0; n<16; n++)
00050             if (str[n] == 0) break;
00051             else             WriteChar(str[n]);
00052     }
00053 
00054     // Write string from specified position
00055     void Acm1602Ni::WriteStringXY(const char str[],
00056                                 uint8_t x, uint8_t y)
00057     {
00058         SetXY(x, y);
00059         WriteString(str);
00060     }
00061 
00062     //---------------------------------------------------
00063     // Following functions: private
00064 
00065     // Send command and data
00066     bool Acm1602Ni::LcdTx(uint8_t cmdData, uint8_t data)
00067     {
00068         if (!Start()) return false;
00069         // defines kind of "data" in next statement
00070         TxDR(cmdData);
00071         TxDR(data);
00072         wait_us(500);   // indispensable
00073         // Generate stop condition
00074         SetCR1(I2C_CR1_STOP);
00075         return true;
00076     }
00077 
00078     // Preparation for send of command and data
00079     bool Acm1602Ni::Start()
00080     {
00081         const uint8_t WAIT = 30;
00082         const uint8_t LENGTH = 10;
00083 
00084         // wait for I2C not busy
00085         for (int n=0; n<LENGTH; n++)
00086         {
00087             wait_us(WAIT);      
00088             if (!CheckSR2(I2C_SR2_BUSY)) break;
00089             if (n == LENGTH-1) return false;
00090         }
00091 
00092         // Generate start condition
00093         SetCR1(I2C_CR1_START);
00094         // Confirm start condition and master mode
00095         for (int n=0; n<LENGTH; n++)
00096         {
00097             wait_us(WAIT);
00098             if (CheckSR12(I2C_SR1_SB, I2C_SR2_MSL |
00099                                       I2C_SR2_BUSY)) break;
00100             if (n == LENGTH-1) return false;
00101         }
00102 
00103         // Send slave address
00104         TxDR(LCD_ADDRESS_);
00105         // Confirm on transmit mode
00106         for (int n=0; n<LENGTH; n++)
00107         {
00108             wait_us(WAIT);      
00109             if (CheckSR12(I2C_SR1_TXE | I2C_SR1_ADDR,
00110                           I2C_SR2_MSL | I2C_SR2_BUSY
00111                                       | I2C_SR2_TRA)) break;
00112             if (n == LENGTH-1) return false;
00113         }
00114 
00115         return true;
00116     }
00117 }