不韋 呂 / UIT_ACM1602NI

Dependents:   UIT2_VariableFIR_LPFHPF UIT2_VariableFIR_LPF UIT2_InputSW_LCD ADDA_Prototype_PollingSW ... more

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 //  2016/04/01, Copyright (c) 2016 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         if (clock != 100000) i2c_.frequency(clock);
00026 
00027         wait_ms(40);
00028         connected_ = Clear();      // Clear display
00029         if (!connected_)
00030         {
00031             fprintf(stderr, "\r\nLCD ACM1602NI not connected\r\n");
00032             return;
00033         }
00034 
00035         WriteCmd(0x38); // data length:8-bit, 2-line, 5×8 dots
00036         WriteCmd(0x0C | (cursor << 1) | blink);
00037         WriteCmd(0x06); // cursor direction: rightward
00038     }
00039 
00040     // All clear
00041     bool Acm1602Ni::Clear()
00042     {
00043         bool ok = WriteCmd(0x01);
00044         wait_ms(50);
00045         return ok;
00046     }
00047 
00048     // Write string
00049     void Acm1602Ni::WriteString(const char str[])
00050     {
00051         for (int n=0; n<16; n++)
00052             if (str[n] == 0) break;
00053             else             WriteChar(str[n]);
00054     }
00055 
00056     // Write string from specified position
00057     void Acm1602Ni::WriteStringXY(const char str[],
00058                                   uint8_t x, uint8_t y)
00059     {
00060         SetXY(x, y);
00061         WriteString(str);
00062     }
00063 
00064     //---------------------------------------------------
00065     // Following member functions: private
00066 
00067     // Send command and data
00068     bool Acm1602Ni::LcdTx(uint8_t cmdData, uint8_t data)
00069     {
00070         // Generate start condition
00071         i2c_.start();
00072 
00073         // Send slave address
00074         TxDR(LCD_ADDRESS_);
00075 
00076         if (!IsTxMode()) return false;
00077         
00078         // Define kind of "data" in next statement
00079         TxDR(cmdData);
00080         TxDR(data);
00081         wait_us(500);   // indispensable
00082         
00083         // Generate stop condition
00084         i2c_.stop();      
00085 
00086         return true;
00087     }
00088 
00089     // Check on transmitter mode
00090     bool Acm1602Ni::IsTxMode()
00091     {
00092         for (int n=0; n<10; n++)
00093         {
00094             wait_us(10);      
00095             if (CheckSR12(I2C_SR1_TXE | I2C_SR1_ADDR,
00096                           I2C_SR2_MSL | I2C_SR2_BUSY
00097                                       | I2C_SR2_TRA))
00098                 return true;
00099         }
00100         
00101         return false;
00102     }
00103 }