4 errors

Dependencies:   KS0108_PCF8574 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers menbedDisplayHD44780.cpp Source File

menbedDisplayHD44780.cpp

00001 /* This code based on mbed TextLCD Library, for a 4-bit LCD based on HD44780,
00002  * Copyright (c) 2007-2010, sford, http://mbed.org
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 "include/menbedDisplayHD44780.h"
00025 #include "menbedDisplay.h"
00026 
00027 MenbedDisplayHD44780::MenbedDisplayHD44780 (PinName rs, PinName e, 
00028     PinName d4, PinName d5, PinName d6, PinName d7, LCDSize size) : 
00029     rs(rs), e(e), d(d4, d5, d6, d7), size(size) 
00030 {
00031     this->e  = 1;
00032     this->rs = 0;
00033     
00034     upArrowVisible = false;
00035     downArrowVisible = false;
00036     topLineSelected = false;
00037     bottomLineSelected = false;
00038     cursorCol = -1;
00039     cursorRow = -1;
00040 
00041     wait(0.015);        // Wait 15ms to ensure powered up
00042 
00043     // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
00044     for (int i=0; i<3; i++) {
00045         writeByte(0x3);
00046         wait(0.00164);  // this command takes 1.64ms, so wait for it
00047     }
00048     
00049     writeByte(0x2);     // 4-bit mode
00050     wait(0.000040f);    // most instructions take 40us
00051 
00052     writeCommand(0x28); // Function set 001 BW N F - -
00053     writeCommand(0x0C); // Display on but keep cursor and blinking off
00054     writeCommand(0x6);  // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
00055     
00056     loadCustomChars();
00057     
00058     clear();
00059 }
00060 
00061 
00062 bool MenbedDisplayHD44780::writeLine (const char *line, uint8_t row)
00063 {
00064     int i = 0;
00065     int cursorPos = -1;
00066 
00067     if (row >= rows())
00068         return false;
00069 
00070     // Skip writing to the left-most column to leave space for the selector
00071     // and up/down arrows that will be filled in later.
00072     gotoPosition (row, 1);
00073 
00074     while ((line[i] != '\0') && (line[i] != '\n') && (i+1 < columns()))
00075     {
00076         // Place the cursor at the end of the active parameter, when it exists.
00077         // A parameter is active when it is highlighted as indicated by the 
00078         // MSB of the character code being set.
00079         if ((i > 0) && (line[i-1] & 0x80) && !(line[i] & 0x80))
00080             cursorPos = i-1;
00081 
00082         // Print each character to the display after clearing its MSB so that
00083         // it prints as a standard ASCII character.
00084         writeData (line[i] & ~0x80);
00085         i++;
00086     }
00087     
00088     // If the first character of the line is not highlighted but the last is,
00089     // a parameter must be selected for editing, and we place the cursor at
00090     // the end of the parameter which, in this special case, corresponds to
00091     // the end of the line.
00092     if (!(line[0] & 0x80) && (line[i-1] & 0x80))
00093         cursorPos = i-1;
00094 
00095     // Fill the remainder of the row with spaces to overwrite any old data
00096     while (i++ < columns() - 1)
00097         writeData(' ');
00098 
00099     // Now go back and fill in the selector and up/down arrows
00100     gotoPosition(row, 0);
00101     if ((line[0] & 0x80) && (cursorPos == -1))
00102     {
00103         if (row == 0)
00104             topLineSelected = true;
00105         else if (row == rows() - 1)
00106             bottomLineSelected = true;
00107     
00108         if ((row == 0) && upArrowVisible)
00109             writeData (CharUP_SELECT);
00110         else if ((row == rows() - 1) && downArrowVisible)
00111             writeData (CharDOWN_SELECT);
00112         else
00113             writeData (CharSELECT);
00114     }
00115     else
00116     {
00117         if (row == 0)
00118             topLineSelected = false;
00119         else if (row == rows() - 1)
00120             bottomLineSelected = false;
00121     
00122         if ((row == 0) && upArrowVisible)
00123             writeData (CharUP);
00124         else if ((row == rows() - 1) && downArrowVisible)
00125             writeData (CharDOWN);
00126         else
00127             writeData (' ');
00128     }
00129 
00130     // If a parameter is being edited, we turn on the blinking cursor.
00131     if (cursorPos != -1)
00132     {
00133         cursorRow = row;
00134         cursorCol = cursorPos + 1;
00135         gotoPosition (cursorRow, cursorCol); // Add 1 to account for selector col.
00136         cursorOn();
00137     }
00138     // If this line used to contain the cursor but should not any more reset
00139     // the cursor row and column position and turn it off.
00140     else if (row == cursorRow)
00141     {
00142         cursorRow = -1;
00143         cursorCol = -1;
00144         cursorOff();
00145     }
00146 
00147     return true;
00148 }
00149 
00150 
00151 void MenbedDisplayHD44780::showUpArrow (bool show)
00152 {
00153     upArrowVisible = show;
00154     
00155     gotoPosition (0, 0);
00156     
00157     if (show && topLineSelected)
00158         writeData (CharUP_SELECT);
00159     else if (!show && topLineSelected)
00160         writeData (CharSELECT);
00161     else if (show && !topLineSelected)
00162         writeData (CharUP);
00163     else
00164         writeData(' ');
00165         
00166     // Return cursor to its original location
00167     if ((cursorRow >= 0) && (cursorCol >= 0))
00168         gotoPosition (cursorRow, cursorCol);
00169 }
00170 
00171 
00172 void MenbedDisplayHD44780::showDownArrow (bool show)
00173 {
00174     downArrowVisible = show;
00175     
00176     gotoPosition (rows() - 1, 0);
00177     
00178     if (show && bottomLineSelected)
00179         writeData (CharDOWN_SELECT);
00180     else if (!show && bottomLineSelected)
00181         writeData (CharSELECT);
00182     else if (show && !bottomLineSelected)
00183         writeData (CharDOWN);
00184     else
00185         writeData(' ');
00186         
00187     // Return cursor to its original location
00188     if ((cursorRow >= 0) && (cursorCol >= 0))
00189         gotoPosition (cursorRow, cursorCol);
00190 }
00191 
00192 
00193 uint8_t MenbedDisplayHD44780::getLines (void)
00194 {
00195     return rows();
00196 }
00197 
00198 
00199 uint8_t MenbedDisplayHD44780::getLineLength (void)
00200 {
00201     return columns();
00202 }
00203 
00204 
00205 void MenbedDisplayHD44780::clear()
00206 {
00207     writeCommand (0x01); // Clear, and set cursor to 0
00208     wait (0.050f);
00209     gotoPosition (0, 0);
00210 }
00211 
00212 
00213 bool MenbedDisplayHD44780::gotoPosition(int row, int column)
00214 {
00215     if ((column < 0) || (column >= columns()) || (row < 0) || (row >= rows()))
00216         return false;
00217 
00218     writeCommand (address (row, column));
00219     return true;
00220 }
00221 
00222 
00223 void MenbedDisplayHD44780::cursorOn()
00224 {
00225     writeCommand(0x0D); // Display and blinking on, cursor off
00226 }
00227 
00228 
00229 void MenbedDisplayHD44780::cursorOff()
00230 {
00231     writeCommand(0x0C); // Display on, cursor and blinking off
00232 }
00233 
00234 
00235 void MenbedDisplayHD44780::writeCommand(int command) {
00236     rs = 0;
00237     writeByte(command);
00238 }
00239 
00240 
00241 void MenbedDisplayHD44780::writeData(int data) {
00242     rs = 1;
00243     writeByte(data);
00244 }
00245 
00246 
00247 void MenbedDisplayHD44780::writeByte(int value) {
00248     d = value >> 4;
00249     wait(0.000040f); // most instructions take 40us
00250     e = 0;
00251     wait(0.000040f);
00252     e = 1;
00253     d = value >> 0;
00254     wait(0.000040f);
00255     e = 0;
00256     wait(0.000040f);  // most instructions take 40us
00257     e = 1;
00258 }
00259 
00260 
00261 int MenbedDisplayHD44780::address(int row, int column) {
00262     switch (size) {
00263         case LCD20x4:
00264             switch (row) {
00265                 case 0:
00266                     return 0x80 + column;
00267                 case 1:
00268                     return 0xc0 + column;
00269                 case 2:
00270                     return 0x94 + column;
00271                 case 3:
00272                     return 0xd4 + column;
00273             }
00274         case LCD16x2B:
00275             return 0x80 + (row * 40) + column;
00276         case LCD16x2:
00277         case LCD20x2:
00278         default:
00279             return 0x80 + (row * 0x40) + column;
00280     }
00281 }
00282 
00283 
00284 int MenbedDisplayHD44780::columns() 
00285 {
00286     switch (size) {
00287         case LCD20x4:
00288         case LCD20x2:
00289             return 20;
00290         case LCD16x2:
00291         case LCD16x2B:
00292         default:
00293             return 16;
00294     }
00295 }
00296 
00297 
00298 int MenbedDisplayHD44780::rows() 
00299 {
00300     switch (size) {
00301         case LCD20x4:
00302             return 4;
00303         case LCD16x2:
00304         case LCD16x2B:
00305         case LCD20x2:
00306         default:
00307             return 2;
00308     }
00309 }
00310 
00311 
00312 void MenbedDisplayHD44780::loadCustomChars (void)
00313 {
00314 
00315     // Up arrow
00316     writeCommand(0x40 + (8 * CharUP));
00317     writeData(0x04);
00318     writeData(0x0E);
00319     writeData(0x1F);
00320     writeData(0x00);
00321     writeData(0x00);
00322     writeData(0x00);
00323     writeData(0x00);
00324     writeData(0x00);
00325     
00326     // Up arrow with selector
00327     writeCommand(0x40 + (8 * CharUP_SELECT));
00328     writeData(0x04);
00329     writeData(0x0E);
00330     writeData(0x1F);
00331     writeData(0x00);
00332     writeData(0x1F);
00333     writeData(0x1F);
00334     writeData(0x00);
00335     writeData(0x00);
00336     
00337     // Selector alone
00338     writeCommand(0x40 + (8 * CharSELECT));
00339     writeData(0x00);
00340     writeData(0x00);
00341     writeData(0x00);
00342     writeData(0x1F);
00343     writeData(0x1F);
00344     writeData(0x00);
00345     writeData(0x00);
00346     writeData(0x00);    
00347     
00348     // Down arrow with selector
00349     writeCommand(0x40 + (8 * CharDOWN_SELECT));
00350     writeData(0x00);
00351     writeData(0x00);
00352     writeData(0x1F);
00353     writeData(0x1F);
00354     writeData(0x00);
00355     writeData(0x1F);
00356     writeData(0x0E);
00357     writeData(0x04);
00358     
00359     // Down arrow
00360     writeCommand(0x40 + (8 * CharDOWN));
00361     writeData(0x00);
00362     writeData(0x00);
00363     writeData(0x00);
00364     writeData(0x00);
00365     writeData(0x00);
00366     writeData(0x1F);
00367     writeData(0x0E);
00368     writeData(0x04);        
00369 }