| LCD Library 1.3.0 LCD Library - LCD control class hierarchy library. Drop in replacement for the LiquidCrystal Library.

- Main Page
- Classes
- Files

- File List
- File Members

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 // 2012.03.29 bperrybap - changed comparision to use LCD_5x8DOTS rather than 0
    00033 // @author F. Malpartida - fmalpartida@gmail.com
    00034 // ---------------------------------------------------------------------------
    00035 #include <stdio.h>
    00036 #include <string.h>
    00037 #include <inttypes.h>
    00038
    00039 #if (ARDUINO <  100)
    00040 #include <WProgram.h>
    00041 #else
    00042 #include <Arduino.h>
    00043 #endif
    00044
    00045 #include "LCD.h"
    00046
    00047 // CLASS CONSTRUCTORS
    00048 // ---------------------------------------------------------------------------
    00049 // Constructor
    00050 LCD::LCD ()
    00051 {
    00052
    00053 }
    00054
    00055 // PUBLIC METHODS
    00056 // ---------------------------------------------------------------------------
    00057 // When the display powers up, it is configured as follows:
    00058 // 0. LCD starts in 8 bit mode
    00059 // 1. Display clear
    00060 // 2. Function set:
    00061 //    DL = 1; 8-bit interface data
    00062 //    N = 0; 1-line display
    00063 //    F = 0; 5x8 dot character font
    00064 // 3. Display on/off control:
    00065 //    D = 0; Display off
    00066 //    C = 0; Cursor off
    00067 //    B = 0; Blinking off
    00068 // 4. Entry mode set:
    00069 //    I/D = 1; Increment by 1
    00070 //    S = 0; No shift
    00071 //
    00072 // Note, however, that resetting the Arduino doesn't reset the LCD, so we
    00073 // can't assume that its in that state when a application starts (and the
    00074 // LiquidCrystal constructor is called).
    00075 // A call to begin() will reinitialize the LCD.
    00076 //
    00077 void LCD::begin(uint8_t cols, uint8_t lines, uint8_t dotsize)
    00078 {
    00079    if (lines > 1)
    00080    {
    00081       _displayfunction |= LCD_2LINE;
    00082    }
    00083    _numlines = lines;
    00084    _cols = cols;
    00085
    00086    // for some 1 line displays you can select a 10 pixel high font
    00087    // ------------------------------------------------------------
    00088    if ((dotsize != LCD_5x8DOTS) && (lines == 1))
    00089    {
    00090       _displayfunction |= LCD_5x10DOTS;
    00091    }
    00092
    00093    // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
    00094    // according to datasheet, we need at least 40ms after power rises above 2.7V
    00095    // before sending commands. Arduino can turn on way before 4.5V so we'll wait
    00096    // 50
    00097    // ---------------------------------------------------------------------------
    00098    delay (100); // 100ms delay
    00099
    00100    //put the LCD into 4 bit or 8 bit mode
    00101    // -------------------------------------
    00102    if (! (_displayfunction & LCD_8BITMODE))
    00103    {
    00104       // this is according to the hitachi HD44780 datasheet
    00105       // figure 24, pg 46
    00106
    00107       // we start in 8bit mode, try to set 4 bit mode
    00108       // Special case of "Function Set"
    00109       send(0x03, FOUR_BITS);
    00110       delayMicroseconds(4500); // wait min 4.1ms
    00111
    00112       // second try
    00113       send ( 0x03, FOUR_BITS );
    00114       delayMicroseconds(150); // wait min 100us
    00115
    00116       // third go!
    00117       send( 0x03, FOUR_BITS );
    00118       delayMicroseconds(150); // wait min of 100us
    00119
    00120       // finally, set to 4-bit interface
    00121       send ( 0x02, FOUR_BITS );
    00122       delayMicroseconds(150); // wait min of 100us
    00123
    00124    }
    00125    else
    00126    {
    00127       // this is according to the hitachi HD44780 datasheet
    00128       // page 45 figure 23
    00129
    00130       // Send function set command sequence
    00131       command(LCD_FUNCTIONSET | _displayfunction);
    00132       delayMicroseconds(4500);  // wait more than 4.1ms
    00133
    00134       // second try
    00135       command(LCD_FUNCTIONSET | _displayfunction);
    00136       delayMicroseconds(150);
    00137
    00138       // third go
    00139       command(LCD_FUNCTIONSET | _displayfunction);
    00140       delayMicroseconds(150);
    00141
    00142    }
    00143
    00144    // finally, set # lines, font size, etc.
    00145    command(LCD_FUNCTIONSET | _displayfunction);
    00146    delayMicroseconds ( 60 );  // wait more
    00147
    00148    // turn the display on with no cursor or blinking default
    00149    _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
    00150    display();
    00151
    00152    // clear the LCD
    00153    clear();
    00154
    00155    // Initialize to default text direction (for romance languages)
    00156    _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
    00157    // set the entry mode
    00158    command(LCD_ENTRYMODESET | _displaymode);
    00159
    00160    backlight();
    00161
    00162 }
    00163
    00164 // Common LCD Commands
    00165 // ---------------------------------------------------------------------------
    00166 void LCD::clear()
    00167 {
    00168    command(LCD_CLEARDISPLAY);             // clear display, set cursor position to zero
    00169    delayMicroseconds(HOME_CLEAR_EXEC);    // this command is time consuming
    00170 }
    00171
    00172 void LCD::home()
    00173 {
    00174    command(LCD_RETURNHOME);             // set cursor position to zero
    00175    delayMicroseconds(HOME_CLEAR_EXEC);  // This command is time consuming
    00176 }
    00177
    00178 void LCD::setCursor(uint8_t col, uint8_t row)
    00179 {
    00180    const byte row_offsetsDef[]   = { 0x00, 0x40, 0x14, 0x54 }; // For regular LCDs
    00181    const byte row_offsetsLarge[] = { 0x00, 0x40, 0x10, 0x50 }; // For 16x4 LCDs
    00182
    00183    if ( row >= _numlines )
    00184    {
    00185       row = _numlines-1;    // rows start at 0
    00186    }
    00187
    00188    // 16x4 LCDs have special memory map layout
    00189    // ----------------------------------------
    00190    if ( _cols == 16 && _numlines == 4 )
    00191    {
    00192       command(LCD_SETDDRAMADDR | (col + row_offsetsLarge[row]));
    00193    }
    00194    else
    00195    {
    00196       command(LCD_SETDDRAMADDR | (col + row_offsetsDef[row]));
    00197    }
    00198
    00199 }
    00200
    00201 // Turn the display on/off
    00202 void LCD::noDisplay()
    00203 {
    00204    _displaycontrol &= ~LCD_DISPLAYON;
    00205    command(LCD_DISPLAYCONTROL | _displaycontrol);
    00206 }
    00207
    00208 void LCD::display()
    00209 {
    00210    _displaycontrol |= LCD_DISPLAYON;
    00211    command(LCD_DISPLAYCONTROL | _displaycontrol);
    00212 }
    00213
    00214 // Turns the underline cursor on/off
    00215 void LCD::noCursor()
    00216 {
    00217    _displaycontrol &= ~LCD_CURSORON;
    00218    command(LCD_DISPLAYCONTROL | _displaycontrol);
    00219 }
    00220 void LCD::cursor()
    00221 {
    00222    _displaycontrol |= LCD_CURSORON;
    00223    command(LCD_DISPLAYCONTROL | _displaycontrol);
    00224 }
    00225
    00226 // Turns on/off the blinking cursor
    00227 void LCD::noBlink()
    00228 {
    00229    _displaycontrol &= ~LCD_BLINKON;
    00230    command(LCD_DISPLAYCONTROL | _displaycontrol);
    00231 }
    00232
    00233 void LCD::blink()
    00234 {
    00235    _displaycontrol |= LCD_BLINKON;
    00236    command(LCD_DISPLAYCONTROL | _displaycontrol);
    00237 }
    00238
    00239 // These commands scroll the display without changing the RAM
    00240 void LCD::scrollDisplayLeft(void)
    00241 {
    00242    command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
    00243 }
    00244
    00245 void LCD::scrollDisplayRight(void)
    00246 {
    00247    command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
    00248 }
    00249
    00250 // This is for text that flows Left to Right
    00251 void LCD::leftToRight(void)
    00252 {
    00253    _displaymode |= LCD_ENTRYLEFT;
    00254    command(LCD_ENTRYMODESET | _displaymode);
    00255 }
    00256
    00257 // This is for text that flows Right to Left
    00258 void LCD::rightToLeft(void)
    00259 {
    00260    _displaymode &= ~LCD_ENTRYLEFT;
    00261    command(LCD_ENTRYMODESET | _displaymode);
    00262 }
    00263
    00264 // This method moves the cursor one space to the right
    00265 void LCD::moveCursorRight(void)
    00266 {
    00267    command(LCD_CURSORSHIFT | LCD_CURSORMOVE | LCD_MOVERIGHT);
    00268 }
    00269
    00270 // This method moves the cursor one space to the left
    00271 void LCD::moveCursorLeft(void)
    00272 {
    00273    command(LCD_CURSORSHIFT | LCD_CURSORMOVE | LCD_MOVELEFT);
    00274 }
    00275
    00276
    00277 // This will 'right justify' text from the cursor
    00278 void LCD::autoscroll(void)
    00279 {
    00280    _displaymode |= LCD_ENTRYSHIFTINCREMENT;
    00281    command(LCD_ENTRYMODESET | _displaymode);
    00282 }
    00283
    00284 // This will 'left justify' text from the cursor
    00285 void LCD::noAutoscroll(void)
    00286 {
    00287    _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
    00288    command(LCD_ENTRYMODESET | _displaymode);
    00289 }
    00290
    00291 // Write to CGRAM of new characters
    00292 void LCD::createChar(uint8_t location, uint8_t charmap[])
    00293 {
    00294    location &= 0x7;            // we only have 8 locations 0-7
    00295
    00296    command(LCD_SETCGRAMADDR | (location << 3));
    00297    delayMicroseconds(30);
    00298
    00299    for (uint8_t i = 0; i < 8; i++)
    00300    {
    00301       write(charmap[i]);      // call the virtual write method
    00302       delayMicroseconds(40);
    00303    }
    00304 }
    00305
    00306 #ifdef __AVR__
    00307 void LCD::createChar(uint8_t location, const prog_uchar charmap[])
    00308 {
    00309    location &= 0x7;   // we only have 8 memory locations 0-7
    00310
    00311    command(LCD_SETCGRAMADDR | (location << 3));
    00312    delayMicroseconds(30);
    00313
    00314    for (uint8_t i = 0; i < 8; i++)
    00315    {
    00316       write(pgm_read_byte_near(charmap++));
    00317       delayMicroseconds(40);
    00318    }
    00319 }
    00320 #endif // __AVR__
    00321
    00322 //
    00323 // Switch on the backlight
    00324 void LCD::backlight ( void )
    00325 {
    00326    setBacklight(255);
    00327 }
    00328
    00329 //
    00330 // Switch off the backlight
    00331 void LCD::noBacklight ( void )
    00332 {
    00333    setBacklight(0);
    00334 }
    00335
    00336 //
    00337 // Switch fully on the LCD (backlight and LCD)
    00338 void LCD::on ( void )
    00339 {
    00340    display();
    00341    backlight();
    00342 }
    00343
    00344 //
    00345 // Switch fully off the LCD (backlight and LCD)
    00346 void LCD::off ( void )
    00347 {
    00348    noBacklight();
    00349    noDisplay();
    00350 }
    00351
    00352 // General LCD commands - generic methods used by the rest of the commands
    00353 // ---------------------------------------------------------------------------
    00354 void LCD::command(uint8_t value)
    00355 {
    00356    send(value, COMMAND);
    00357 }
    00358
    00359 #if (ARDUINO <  100)
    00360 void LCD::write(uint8_t value)
    00361 {
    00362    send(value, DATA);
    00363 }
    00364 #else
    00365 size_t LCD::write(uint8_t value)
    00366 {
    00367    send(value, DATA);
    00368    return 1;             // assume OK
    00369 }
    00370 #endif

All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines