You can output chars to AQM1602 with printf(). ex. lcd.printf("iter: %5.5d.\r\n", iter);

Dependents:   aqm1602 FCAS-M101V1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AQM1602.cpp Source File

AQM1602.cpp

00001 #include "AQM1602.h"
00002 
00003 AQM1602::AQM1602(PinName sda, PinName scl, bool pw3v3, char address)
00004     : p_i2c(new I2C(sda, scl)), i2c(*p_i2c), addr(address), vdd3v3(pw3v3)
00005 {
00006     init();
00007 }
00008 AQM1602::AQM1602(I2C &_i2c, bool pw3v3, char address)
00009     : p_i2c(NULL), i2c(_i2c), addr(address), vdd3v3(pw3v3)
00010 {
00011     init();
00012 }
00013 AQM1602::~AQM1602()
00014 {
00015     if(p_i2c != NULL)
00016         delete p_i2c;
00017 }
00018 
00019 bool AQM1602::cmd(char chr)
00020 {
00021     buf[0]= 0x00;
00022     buf[1]= chr;
00023     // if write success:0, other: err code.
00024     int status= i2c.write(addr, buf, 2);
00025     wait_ms(3);
00026 
00027     if(status == 0)
00028         return true;
00029     else
00030         return false;
00031 }
00032 
00033 int AQM1602::_putc(int val)     // for printf()
00034 {
00035     if (val == '\r') {
00036         this->cr();
00037     } else if (val == '\n') {
00038         this->lf();
00039     } else {
00040         locate(col, row);
00041         buf[0]= 0x40;
00042         buf[1]= val;
00043         i2c.write(addr, buf, 2);
00044 
00045         col++;
00046         if (col >= 16) {
00047             this->cr();
00048             this->lf();
00049         }
00050     }
00051     wait_ms(1);
00052     return val;
00053 }
00054 
00055 // for "Stream"
00056 int AQM1602::_getc()
00057 {
00058     return -1;
00059 }
00060 
00061 void AQM1602::locate(int _col, int _row)
00062 {
00063     col= _col;
00064     row= _row;
00065     cmd(0x80 + row * 0x40 + col);
00066     return;
00067 }
00068 
00069 void AQM1602::init()
00070 {
00071     col= row= 0;
00072     buf[0]= 0x00;
00073     buf[1]= 0x00;
00074     buf[2]= 0x00;
00075     wait4cmd= 3;
00076 //    valContrast= 0x23;
00077 
00078     wait_ms(wait4cmd);
00079     // Function set = 0x38; 0b0011 1000
00080     // Function set = 0x39; 0b0011 1001
00081     this->clearIS();
00082     this->setIS();
00083 
00084     // ** must be IS=1. **
00085     // Internal OSC frequency = 0x14; 0b0001 0100
00086     this->cmd(0x14);
00087     wait_ms(wait4cmd);
00088 
00089     // Power/ICON/Contrast control = 0x56; 0b0101 0110
00090     // Contrast set = 0x70; 0b0111 0011
00091     this->setContrast(0x23, false);
00092 
00093     // Follower control = 0x6C; 0b0110 1100
00094     this->cmd(0x6C);
00095     wait_ms(220);
00096 
00097     this->clearIS();
00098     this->setDispFlag(true, true, true);
00099     this->clear();
00100 
00101     // Function set = 0x38; 0b0011 1000
00102     // Display ON/OFF control = 0x0C; 0b 0000 1DCB
00103     // Clear Display = 0x01; 0b 0000 0001
00104 
00105     return;
00106 }
00107 
00108 void AQM1602::setContrast(char val, bool ctrlIS)
00109 {
00110 // C5,C4,C3,C2,C1,C0 can only be set when internal follower is used (OPF1=0,OPF2=0).
00111 // They can more precisely adjust the input reference voltage of V0 generator.
00112 // The details please refer to the supply voltage for LCD driver.
00113 
00114     if(val > 0x3f)// || val < 0x00)
00115         return;
00116 
00117     contrast= val;
00118     setPwContrast(ctrlIS);
00119 
00120     return;
00121 }
00122 
00123 void AQM1602::setPwContrast(bool ctrlIS)
00124 {
00125 // C5,C4,C3,C2,C1,C0 can only be set when internal follower is used (OPF1=0,OPF2=0).
00126 // They can more precisely adjust the input reference voltage of V0 generator.
00127 // The details please refer to the supply voltage for LCD driver.
00128 
00129     // Cmd of Contrast-setting must be setted IS.
00130     if(ctrlIS)
00131         this->setIS();
00132 
00133     // 0b 0101 0*++     // PW, C5, C4
00134     char val= 0x50;
00135     if(vdd3v3)      // set Booster circuit.
00136         val += 0x04;
00137     val += (contrast>>4);
00138     this->cmd(val);
00139 //    this->cmd(0x54+ (contrast>>4));
00140     wait_ms(wait4cmd);
00141 
00142     // 0b 0111 ++++     // C3, C2, C1, C0
00143     this->cmd(0x70+ (contrast&0x0f));
00144     wait_ms(wait4cmd);
00145 
00146     if(ctrlIS)
00147         this->clearIS();
00148 
00149     wait_ms(wait4cmd);
00150     return;
00151 }
00152 
00153 void AQM1602::clear()
00154 {
00155     this->cmd(0x01);
00156     locate(0, 0);
00157     wait_ms(wait4cmd);
00158     return;
00159 }
00160 
00161 
00162 void AQM1602::cr()
00163 {
00164     col = 0;
00165     locate(col, row);
00166     return;
00167 
00168 }
00169 
00170 void AQM1602::lf()
00171 {
00172     row++;
00173     row &= 1;
00174     int _col= col;
00175     for(int i= 0; i < 16; i++) {
00176         locate(i, row);
00177         buf[0]= 0x40;
00178         buf[1]= ' ';
00179         i2c.write(addr, buf, 2);
00180     }
00181     locate(_col, row);
00182     return;
00183 }
00184 
00185 void AQM1602::setDispFlag(bool disp, bool cursor, bool blink)
00186 {
00187     // set On/Off. b3=1, b2:Disp, b1:Cursor, b0:blink.
00188     char tmp=  0x08;
00189     if(disp)
00190         tmp += 0x04;
00191     if(cursor)
00192         tmp += 0x02;
00193     if(blink)
00194         tmp += 0x01;
00195     this->cmd(tmp);
00196     this->cmd(0x01);    //Clear Disp.
00197     wait_ms(wait4cmd);
00198     return;
00199 }
00200 
00201 // ******************** FUNCTION SET **********************
00202 // IS func-set -> 0b 001L NH0I
00203 //          L: Interface data Length. 1:8bit-bus, 0:4bit.
00204 //          N: Display line Number. 1: 2 lines, 0: 1 line.
00205 //          H: Double Hight font. if H=1 and N=1, 5x16 dots.
00206 //              0: Normal (5x8 dots).
00207 //          I: Normal / Extentsion Instruction. 1: Ext Instruction, 0: Norm.
00208 void AQM1602::setIS()
00209 {
00210     this->cmd(0x39);
00211     wait_ms(wait4cmd);
00212     return;
00213 }
00214 void AQM1602::clearIS()
00215 {
00216     this->cmd(0x38);
00217     wait_ms(wait4cmd);
00218     return;
00219 }
00220 
00221 // EOF