Erik Kerger / NewTextLCD

Dependents:   tuner Base_Hybrid_V3 Base_Hybrid_V2 base_rekam_darurat_v1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NewTextLCD.cpp Source File

NewTextLCD.cpp

00001 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
00002  * Copyright (c) 2007-2010, sford, http://mbed.org
00003  * Changes by Erik Kerger
00004  *
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a copy
00007  * of this software and associated documentation files (the "Software"), to deal
00008  * in the Software without restriction, including without limitation the rights
00009  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010  * copies of the Software, and to permit persons to whom the Software is
00011  * furnished to do so, subject to the following conditions:
00012  *
00013  * The above copyright notice and this permission notice shall be included in
00014  * all copies or substantial portions of the Software.
00015  *
00016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022  * THE SOFTWARE.
00023  */
00024 
00025 #include "NewTextLCD.h"
00026 #include "mbed.h"
00027 
00028 TextLCD::TextLCD(PinName rs,PinName e, PinName d4, PinName d5,
00029                  PinName d6, PinName d7, LCDType type) : _rs(rs), _e(e), 
00030                     _d(d4, d5, d6, d7), _type(type) {
00031     _rs = 0;            // command mode
00032     _e  = 0;            // 0 --> inactive (most important change to original TextLCD)
00033     
00034     if (_type != LCDuser)
00035         setLCDparam(_type); // otherwise rows, colums, comdelay, adresses must be set before
00036 
00037     wait(0.050f);       // Wait 50ms to ensure powered up
00038 
00039     // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
00040     // this sets controler into 8 bit mode, so we have a defined state
00041     for (int i=0; i<3; i++) {
00042         _e = 1;
00043         __nop();
00044         __nop();
00045         _d = 0x3;
00046         __nop();        // data setup time at least 60 ns
00047         _e = 0;
00048         wait(0.005f);   // 5ms
00049        }
00050     _e = 1;
00051     __nop();
00052     __nop();
00053     _d = 0x2;           // 4 Bit mode (after this command, we have to send 2 nibbles)
00054     __nop(); 
00055     _e = 0;
00056     wait_us(LCDparam.delay);
00057     
00058     writeCommand(0x28); // Function set 4 Bit, 2Line, 5*7
00059     writeCommand(0x08); // Display off
00060     cls();              // clear display, reset _column and _row
00061     writeCommand(0x04); // cursor right, Display is not shifted
00062     writeCommand(0x0C); // Display on , Cursor off 
00063 }
00064 
00065 void TextLCD::character(int column, int row, int c) {
00066     int a = 0x80 | (LCDparam.adresses[row & 3] + column);
00067     writeCommand(a);    // set cursor address
00068     writeData(c);       // write char
00069 }
00070 
00071 void TextLCD::cls() {
00072     writeCommand(0x01); // cls, and set cursor to 0
00073     locate(0, 0);       // set internal position
00074     wait_us(45 * LCDparam.delay);   // CLS need much time
00075 }
00076 
00077 void TextLCD::locate(int column, int row) {
00078     _column = column;   // set position for next char
00079     _row = row;         // note: cursor is not set yet
00080 }
00081 
00082 
00083 
00084 int TextLCD::_putc(int value) {
00085     if (value == '\n') {
00086         _column = 0;
00087         _row++;
00088         if (_row >= LCDparam.rows) {
00089             _row = 0;
00090         }
00091     } else {
00092         character(_column, _row, value);
00093         _column++;
00094         if (_column >= LCDparam.columns) {
00095             _column = 0;
00096             _row++;
00097             if (_row >= LCDparam.rows) {
00098                 _row = 0;
00099             }
00100         }
00101     }
00102     return value;
00103 }
00104 
00105 // Dummy function - read not supported
00106 int TextLCD::_getc() {
00107     return -1;
00108 }
00109 
00110 void TextLCD::writeByte(int value) {
00111     _e = 1;
00112     __nop();    
00113     _d = value >> 4;        // higher nibble first
00114     __nop();
00115     _e = 0;
00116     __nop();
00117     __nop();
00118     _e = 1;
00119     __nop();
00120     _d = value >> 0;        // then lower
00121     __nop();
00122     _e = 0;
00123 }
00124 
00125 void TextLCD::writeCommand(int command) {
00126     _rs = 0;
00127     writeByte(command);
00128     wait_us(LCDparam.delay);
00129 }
00130 
00131 
00132 void TextLCD::writeData(int data) {
00133     _rs = 1;
00134     writeByte(data);
00135     wait_us(LCDparam.delay);
00136 }
00137 
00138 
00139 // set user defined char 
00140 void  TextLCD::writeCGRAM(int address, int pattern[8]){
00141     int i;
00142     address = address & 7;  //max 8 char
00143     for(i=0;i<8;i++){
00144         writeCommand(0x40 | (address * 8) + i);
00145         writeData(pattern[i]);
00146         }
00147 }   
00148 
00149 void TextLCD::setLCDparam(LCDType _type){
00150     switch (_type) {
00151     
00152         case LCD16x2:
00153         case LCD16x2B:
00154             LCDparam.columns = 16;
00155             break;
00156         case LCD20x2:
00157         case LCD20x4:
00158             LCDparam.columns = 20;
00159             break;
00160         case LCD24x2:
00161             LCDparam.columns = 24;
00162             break;
00163     }
00164     if (_type == LCD20x4) 
00165         LCDparam.rows = 4;
00166     else 
00167         LCDparam.rows = 2;
00168         
00169     LCDparam.adresses[0] = 0;
00170     
00171     if (_type == LCD16x2B)
00172         LCDparam.adresses[1] = 40;
00173     else
00174         LCDparam.adresses[1] = 0x40;
00175         
00176     if (_type == LCD20x4) {
00177         LCDparam.adresses[2] = 0x14;
00178         LCDparam.adresses[3] = 0x54;}
00179     else {
00180         LCDparam.adresses[2] = 0;
00181         LCDparam.adresses[3] = 0;}
00182         
00183     LCDparam.delay = 50;            // 50 us delays as default              
00184 }
00185