Components / Freetronics_16x2_LCD

Dependents:   Thermo_Voltmeter Freetronics_16x2_LCD DR14_DHT11_LCD Freetronics_16x2_LCD3 ... more

Fork of freetronicsLCDShield by Koen Kempeneers

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers freetronicsLCDShield.cpp Source File

freetronicsLCDShield.cpp

00001 /* mbed freetronicsLCDShield Library, written by Koen J.F. Kempeneers
00002  * koen.kempeneers@damiaaninstituut.be
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 #include "mbed.h"
00024 #include "freetronicsLCDShield.h"
00025 
00026 #define PULSE_E     wait(0.000001f);    \
00027                     _e = 0;             \
00028                     wait(0.000001f);    \
00029                     _e = 1;
00030 
00031 
00032 freetronicsLCDShield::freetronicsLCDShield (PinName rs, PinName e, PinName d0, PinName d1, PinName d2, PinName d3, PinName bl, PinName a0) 
00033             : _rs(rs), _e(e), _d(d0, d1, d2, d3), _bl(bl), _a0(a0) {
00034     // Class constructor
00035     // Init the display, wait 15ms to insure the power is up
00036     _e = true;
00037     _rs = false;
00038     wait(0.015f);
00039     
00040     for (int i = 0; i < 3; i++){
00041         writeByte(0x3);
00042         wait(0.00164f);  // this command takes 1.64ms, so wait for it
00043     }
00044 
00045     writeByte(0x2);     // 4-bit mode
00046     writeCommand(0x28); // Function set 001 BW N F - -
00047     writeCommand(0x0C); // Display on/off controll 0000 1 D C B (D(isplay) On/Off C(ursor) On/Off B(link) On/Off 
00048     writeCommand(0x6);  // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
00049     cls();
00050     
00051     // Set the PWM period to 20ms
00052     _bl.period_ms(1);
00053     _bl.write(0.0);
00054 }
00055 
00056 void freetronicsLCDShield::setCursorPosition (int line, int col) {
00057     // Set the new cursorposition
00058     writeCommand(0x80 + (line * 0x40) + col);
00059 }
00060 
00061 void freetronicsLCDShield::setBackLight (bool blStatus) {
00062     // Switch the backlight on (true) or off (false)
00063     _bl = (blStatus) ? 1.0 : 0.0;
00064 }
00065 
00066 void freetronicsLCDShield::setBackLight (float blIntensity) {
00067     // Switch the backlight on (true) or off (false)
00068     _bl = blIntensity;
00069 }
00070 
00071 void freetronicsLCDShield::setCursor (bool cStatus, bool blink) {
00072     int tmp = 0;
00073     
00074     if (blink) tmp = 0x01;
00075     if (cStatus) tmp |= 0x02; 
00076     writeCommand(0x0C + tmp);
00077 }
00078 
00079 void freetronicsLCDShield::shift(bool direction) {
00080     if(direction == LEFT) shiftLeft();
00081     else shiftRight();
00082 }
00083 
00084 void freetronicsLCDShield::shiftLeft(void) {
00085     writeCommand(0x18 + 0x04);
00086 }
00087 
00088 void freetronicsLCDShield::shiftRight(void) {
00089     writeCommand(0x18);
00090 }
00091 
00092 void freetronicsLCDShield::cls(void) {
00093     // Clear the display and place the cursor at 0, 0
00094     writeCommand(0x01);
00095     wait(0.00164f);
00096 }
00097 
00098 void freetronicsLCDShield::home(void) {
00099     // Undo shift operations and place cursor at 0,0
00100     writeCommand(0x02);
00101     wait(0.00164f);
00102 }
00103 
00104 void freetronicsLCDShield::writeCGRAM (char address, const char *ptr, char nbytes) {
00105     // Write the address only once, it is autoincremented 
00106     writeCommand(0x40 | address);
00107 
00108     // Write the data
00109     for(int i = 0; i < nbytes; i++) {
00110         writeData(*ptr++);
00111     }
00112 }
00113 
00114 // Low level output functions
00115 void freetronicsLCDShield::writeByte (int byte) {
00116     // Split the byte in high and low nibble, write high nibble first
00117     _d = byte >> 4;
00118     PULSE_E;
00119     _d = byte >> 0;
00120     PULSE_E;
00121     // Most instructions take 40us
00122     wait(0.000040f);  
00123 }
00124 
00125 void freetronicsLCDShield::writeData (int data) {
00126     _rs = true;
00127     writeByte(data);
00128 } 
00129 
00130 void freetronicsLCDShield::writeCommand (int command) {
00131     _rs = false;
00132     writeByte(command);
00133 } 
00134 
00135 float freetronicsLCDShield::readButton(void) {
00136     return(_a0.read());
00137 }
00138 
00139 // Virtual functions for stream
00140 int freetronicsLCDShield::_putc(int value) {   
00141     writeData(value);
00142     return value;
00143 }
00144 
00145 int freetronicsLCDShield::_getc() {
00146     return -1;
00147 }