LCD Library 1.2.0
LCD Library - LCD control class hierarchy library. Drop in replacement for the LiquidCrystal Library.
/Users/fmalpartida/development/ardWorkspace/LiquidCrystal_I2C/LiquiCrystal_I2C/LCD.cpp
Go to the documentation of this file.
00001 // ---------------------------------------------------------------------------
00002 // Created by Francisco Malpartida on 20/08/11.
00003 // Copyright 2011 - Under creative commons license 3.0:
00004 //        Attribution-ShareAlike CC BY-SA
00005 //
00006 // This software is furnished "as is", without technical support, and with no 
00007 // warranty, express or implied, as to its usefulness for any purpose.
00008 //
00009 // Thread Safe: No
00010 // Extendable: Yes
00011 //
00012 // @file LCD.cpp
00013 // This file implements a basic liquid crystal library that comes as standard
00014 // in the Arduino SDK.
00015 // 
00016 // @brief 
00017 // This is a basic implementation of the HD44780 library of the
00018 // Arduino SDK. This library is a refactored version of the one supplied
00019 // in the Arduino SDK in such a way that it simplifies its extension
00020 // to support other mechanism to communicate to LCDs such as I2C, Serial, SR, ...
00021 // The original library has been reworked in such a way that this will be
00022 // the base class implementing all generic methods to command an LCD based
00023 // on the Hitachi HD44780 and compatible chipsets.
00024 //
00025 // This base class is a pure abstract class and needs to be extended. As reference,
00026 // it has been extended to drive 4 and 8 bit mode control, LCDs and I2C extension
00027 // backpacks such as the I2CLCDextraIO using the PCF8574* I2C IO Expander ASIC.
00028 //
00029 //
00030 // @version API 1.1.0
00031 //
00032 // @author F. Malpartida - fmalpartida@gmail.com
00033 // ---------------------------------------------------------------------------
00034 #include <stdio.h>
00035 #include <string.h>
00036 #include <inttypes.h>
00037 
00038 #if (ARDUINO <  100)
00039 #include <WProgram.h>
00040 #else
00041 #include <Arduino.h>
00042 #endif
00043 #include "LCD.h"
00044 
00045 // CLASS CONSTRUCTORS
00046 // ---------------------------------------------------------------------------
00047 // Constructor
00048 LCD::LCD () 
00049 {
00050    
00051 }
00052 
00053 // PUBLIC METHODS
00054 // ---------------------------------------------------------------------------
00055 // When the display powers up, it is configured as follows:
00056 //
00057 // 1. Display clear
00058 // 2. Function set: 
00059 //    DL = 1; 8-bit interface data 
00060 //    N = 0; 1-line display 
00061 //    F = 0; 5x8 dot character font 
00062 // 3. Display on/off control: 
00063 //    D = 0; Display off 
00064 //    C = 0; Cursor off 
00065 //    B = 0; Blinking off 
00066 // 4. Entry mode set: 
00067 //    I/D = 1; Increment by 1 
00068 //    S = 0; No shift 
00069 //
00070 // Note, however, that resetting the Arduino doesn't reset the LCD, so we
00071 // can't assume that its in that state when a sketch starts (and the
00072 // LiquidCrystal constructor is called).
00073 // A call to begin() will reinitialize the LCD.
00074 //
00075 void LCD::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) 
00076 {
00077    if (lines > 1) 
00078    {
00079       _displayfunction |= LCD_2LINE;
00080    }
00081    _numlines = lines;
00082    _cols = cols;
00083    
00084    // for some 1 line displays you can select a 10 pixel high font
00085    // ------------------------------------------------------------
00086    if ((dotsize != 0) && (lines == 1)) 
00087    {
00088       _displayfunction |= LCD_5x10DOTS;
00089    }
00090    
00091    // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
00092    // according to datasheet, we need at least 40ms after power rises above 2.7V
00093    // before sending commands. Arduino can turn on way before 4.5V so we'll wait 
00094    // 50
00095    // ---------------------------------------------------------------------------
00096    delayMicroseconds(100000); 
00097       
00098    //put the LCD into 4 bit or 8 bit mode
00099    // -------------------------------------
00100    if (! (_displayfunction & LCD_8BITMODE)) 
00101    {
00102       // this is according to the hitachi HD44780 datasheet
00103       // figure 24, pg 46
00104       
00105       // we start in 8bit mode, try to set 4 bit mode
00106       command ( 0x03 );
00107       delayMicroseconds(4500); // wait min 4.1ms
00108       
00109       // second try
00110       command ( 0x03 );
00111       delayMicroseconds(4500); // wait min 4.1ms
00112       
00113       // third go!
00114       command ( 0x03 ); 
00115       delayMicroseconds(150);
00116       
00117       // finally, set to 4-bit interface
00118       command ( 0x02 ); 
00119    } 
00120    else 
00121    {
00122       // this is according to the hitachi HD44780 datasheet
00123       // page 45 figure 23
00124       
00125       // Send function set command sequence
00126       command(LCD_FUNCTIONSET | _displayfunction);
00127       delayMicroseconds(4500);  // wait more than 4.1ms
00128       
00129       // second try
00130       command(LCD_FUNCTIONSET | _displayfunction);
00131       delayMicroseconds(150);
00132       
00133       // third go
00134       command(LCD_FUNCTIONSET | _displayfunction);
00135    }
00136    
00137    // finally, set # lines, font size, etc.
00138    command(LCD_FUNCTIONSET | _displayfunction);  
00139    
00140    // turn the display on with no cursor or blinking default
00141    _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;  
00142    display();
00143    
00144    // clear the LCD
00145    clear();
00146    
00147    // Initialize to default text direction (for romance languages)
00148    _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
00149    // set the entry mode
00150    command(LCD_ENTRYMODESET | _displaymode);
00151    
00152 }
00153 
00154 // Common LCD Commands
00155 // ---------------------------------------------------------------------------
00156 void LCD::clear()
00157 {
00158    command(LCD_CLEARDISPLAY);             // clear display, set cursor position to zero
00159    delayMicroseconds(HOME_CLEAR_EXEC);    // this command is time consuming
00160 }
00161 
00162 void LCD::home()
00163 {
00164    command(LCD_RETURNHOME);             // set cursor position to zero
00165    delayMicroseconds(HOME_CLEAR_EXEC);  // This command is time consuming
00166 }
00167 
00168 void LCD::setCursor(uint8_t col, uint8_t row)
00169 {
00170    const byte row_offsetsDef[]   = { 0x00, 0x40, 0x14, 0x54 }; // For regular LCDs
00171    const byte row_offsetsLarge[] = { 0x00, 0x40, 0x10, 0x50 }; // For 16x4 LCDs
00172    
00173    if ( row >= _numlines ) 
00174    {
00175       row = _numlines-1;    // rows start at 0
00176    }
00177    
00178    // 16x4 LCDs have special memory map layout
00179    // ----------------------------------------
00180    if ( _cols == 16 && _numlines == 4 )
00181    {
00182       command(LCD_SETDDRAMADDR | (col + row_offsetsLarge[row]));
00183    }
00184    else 
00185    {
00186       command(LCD_SETDDRAMADDR | (col + row_offsetsDef[row]));
00187    }
00188 
00189 }
00190 
00191 // Turn the display on/off
00192 void LCD::noDisplay() 
00193 {
00194    _displaycontrol &= ~LCD_DISPLAYON;
00195    command(LCD_DISPLAYCONTROL | _displaycontrol);
00196 }
00197 
00198 void LCD::display() 
00199 {
00200    _displaycontrol |= LCD_DISPLAYON;
00201    command(LCD_DISPLAYCONTROL | _displaycontrol);
00202 }
00203 
00204 // Turns the underline cursor on/off
00205 void LCD::noCursor() 
00206 {
00207    _displaycontrol &= ~LCD_CURSORON;
00208    command(LCD_DISPLAYCONTROL | _displaycontrol);
00209 }
00210 void LCD::cursor() 
00211 {
00212    _displaycontrol |= LCD_CURSORON;
00213    command(LCD_DISPLAYCONTROL | _displaycontrol);
00214 }
00215 
00216 // Turns on/off the blinking cursor
00217 void LCD::noBlink() 
00218 {
00219    _displaycontrol &= ~LCD_BLINKON;
00220    command(LCD_DISPLAYCONTROL | _displaycontrol);
00221 }
00222 
00223 void LCD::blink() 
00224 {
00225    _displaycontrol |= LCD_BLINKON;
00226    command(LCD_DISPLAYCONTROL | _displaycontrol);
00227 }
00228 
00229 // These commands scroll the display without changing the RAM
00230 void LCD::scrollDisplayLeft(void) 
00231 {
00232    command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
00233 }
00234 
00235 void LCD::scrollDisplayRight(void) 
00236 {
00237    command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
00238 }
00239 
00240 // This is for text that flows Left to Right
00241 void LCD::leftToRight(void) 
00242 {
00243    _displaymode |= LCD_ENTRYLEFT;
00244    command(LCD_ENTRYMODESET | _displaymode);
00245 }
00246 
00247 // This is for text that flows Right to Left
00248 void LCD::rightToLeft(void) 
00249 {
00250    _displaymode &= ~LCD_ENTRYLEFT;
00251    command(LCD_ENTRYMODESET | _displaymode);
00252 }
00253 
00254 // This method moves the cursor one space to the right
00255 void LCD::moveCursorRight(void)
00256 {
00257    command(LCD_CURSORSHIFT | LCD_CURSORMOVE | LCD_MOVERIGHT);
00258 }
00259 
00260 // This method moves the cursor one space to the left
00261 void LCD::moveCursorLeft(void)
00262 {
00263    command(LCD_CURSORSHIFT | LCD_CURSORMOVE | LCD_MOVELEFT);
00264 }
00265 
00266 
00267 // This will 'right justify' text from the cursor
00268 void LCD::autoscroll(void) 
00269 {
00270    _displaymode |= LCD_ENTRYSHIFTINCREMENT;
00271    command(LCD_ENTRYMODESET | _displaymode);
00272 }
00273 
00274 // This will 'left justify' text from the cursor
00275 void LCD::noAutoscroll(void) 
00276 {
00277    _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
00278    command(LCD_ENTRYMODESET | _displaymode);
00279 }
00280 
00281 // Write to CGRAM of new characters
00282 void LCD::createChar(uint8_t location, uint8_t charmap[]) 
00283 {
00284    location &= 0x7;            // we only have 8 locations 0-7
00285    
00286    command(LCD_SETCGRAMADDR | (location << 3));
00287    delayMicroseconds(30);
00288    
00289    for (int i=0; i<8; i++) 
00290    {
00291       write(charmap[i]);      // call the virtual write method
00292       delayMicroseconds(40);
00293    }
00294 }
00295 
00296 // General LCD commands - generic methods used by the rest of the commands
00297 // ---------------------------------------------------------------------------
00298 void LCD::command(uint8_t value) 
00299 {
00300    send(value, COMMAND);
00301 }
00302 
00303 #if (ARDUINO <  100)
00304 void LCD::write(uint8_t value)
00305 {
00306    send(value, DATA);
00307 }
00308 #else
00309 size_t LCD::write(uint8_t value) 
00310 {
00311    send(value, DATA);
00312    return 1;             // assume OK
00313 }
00314 #endif
 All Classes Files Functions Variables Typedefs Defines