| 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

LiquidCrystal.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 LiquidCrystal.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 LiquidCrystal library of the
    00018 // Arduino SDK. The original library has been reworked in such a way that
    00019 // this class implements the all methods to command an LCD based
    00020 // on the Hitachi HD44780 and compatible chipsets using the parallel port of
    00021 // the LCD (4 bit and 8 bit).
    00022 //
    00023 // The functionality provided by this class and its base class is identical
    00024 // to the original functionality of the Arduino LiquidCrystal library.
    00025 //
    00026 //
    00027 // @author F. Malpartida - fmalpartida@gmail.com
    00028 // ---------------------------------------------------------------------------
    00029 #include <stdio.h>
    00030 #include <string.h>
    00031 #include <inttypes.h>
    00032
    00033 #if (ARDUINO <  100)
    00034 #include <WProgram.h>
    00035 #else
    00036 #include <Arduino.h>
    00037 #endif
    00038 #include "LiquidCrystal.h"
    00039
    00040 // CONSTANT  definitions
    00041 // ---------------------------------------------------------------------------
    00042 #define LCD_NOBACKLIGHT 0xFF
    00043
    00044 // LCD driver configuration (4bit or 8bit driver control)
    00045 #define LCD_4BIT                1
    00046 #define LCD_8BIT                0
    00047
    00048 // STATIC helper functions
    00049 // ---------------------------------------------------------------------------
    00050
    00051
    00052 // CONSTRUCTORS
    00053 // ---------------------------------------------------------------------------
    00054
    00055 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable,
    00056                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
    00057                              uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
    00058 {
    00059    init(LCD_8BIT, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7);
    00060 }
    00061
    00062 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
    00063                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
    00064                              uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
    00065 {
    00066    init(LCD_8BIT, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
    00067 }
    00068
    00069 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
    00070                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
    00071 {
    00072    init(LCD_4BIT, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0);
    00073 }
    00074
    00075 LiquidCrystal::LiquidCrystal(uint8_t rs,  uint8_t enable,
    00076                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
    00077 {
    00078    init(LCD_4BIT, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
    00079 }
    00080
    00081 // Contructors with backlight control
    00082 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable,
    00083                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
    00084                              uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7,
    00085                              uint8_t backlightPin, t_backlighPol pol)
    00086 {
    00087    init(LCD_8BIT, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7);
    00088    setBacklightPin ( backlightPin, pol );
    00089 }
    00090
    00091 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
    00092                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
    00093                              uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7,
    00094                              uint8_t backlightPin, t_backlighPol pol)
    00095 {
    00096    init(LCD_8BIT, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
    00097    setBacklightPin ( backlightPin, pol );
    00098 }
    00099
    00100 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
    00101                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
    00102                              uint8_t backlightPin, t_backlighPol pol)
    00103 {
    00104    init(LCD_4BIT, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0);
    00105    setBacklightPin ( backlightPin, pol );
    00106 }
    00107
    00108 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable,
    00109                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
    00110                              uint8_t backlightPin, t_backlighPol pol)
    00111 {
    00112    init(LCD_4BIT, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
    00113    setBacklightPin ( backlightPin, pol );
    00114 }
    00115
    00116 // PUBLIC METHODS
    00117 // ---------------------------------------------------------------------------
    00118
    00119 /************ low level data pushing commands **********/
    00120 //
    00121 // send
    00122 void LiquidCrystal::send(uint8_t value, uint8_t mode)
    00123 {
    00124    // Only interested in COMMAND or DATA
    00125    digitalWrite( _rs_pin, ( mode == DATA ) );
    00126
    00127    // if there is a RW pin indicated, set it low to Write
    00128    // ---------------------------------------------------
    00129    if (_rw_pin != 255)
    00130    {
    00131       digitalWrite(_rw_pin, LOW);
    00132    }
    00133
    00134    if ( mode != FOUR_BITS )
    00135    {
    00136       if ( (_displayfunction & LCD_8BITMODE ) )
    00137       {
    00138          writeNbits(value, 8);
    00139       }
    00140       else
    00141       {
    00142          writeNbits ( value >> 4, 4 );
    00143          writeNbits ( value, 4 );
    00144       }
    00145    }
    00146    else
    00147    {
    00148       writeNbits ( value, 4 );
    00149    }
    00150    waitUsec ( EXEC_TIME ); // wait for the command to execute by the LCD
    00151 }
    00152
    00153 //
    00154 // setBacklightPin
    00155 void LiquidCrystal::setBacklightPin ( uint8_t pin, t_backlighPol pol )
    00156 {
    00157    pinMode ( pin, OUTPUT );       // Difine the backlight pin as output
    00158    _backlightPin = pin;
    00159    _polarity = pol;
    00160    setBacklight(BACKLIGHT_OFF);   // Set the backlight low by default
    00161 }
    00162
    00163 //
    00164 // setBackligh
    00165 void LiquidCrystal::setBacklight ( uint8_t value )
    00166 {
    00167    // Check if there is a pin assigned to the backlight
    00168    // ---------------------------------------------------
    00169    if ( _backlightPin != LCD_NOBACKLIGHT )
    00170    {
    00171       // Check if the pin is associated to a timer, i.e. PWM
    00172       // ---------------------------------------------------
    00173       if(digitalPinToTimer(_backlightPin) != NOT_ON_TIMER)
    00174       {
    00175          // Check for control polarity inversion
    00176          // ---------------------------------------------------
    00177          if ( _polarity == POSITIVE )
    00178          {
    00179             analogWrite ( _backlightPin, value );
    00180          }
    00181          else
    00182          {
    00183             analogWrite ( _backlightPin, 255 - value );
    00184          }
    00185       }
    00186       // Not a PWM pin, set the backlight pin for POSI or NEG
    00187       // polarity
    00188       // --------------------------------------------------------
    00189       else if (((value > 0) && (_polarity == POSITIVE)) ||
    00190                ((value == 0) && (_polarity == NEGATIVE)))
    00191       {
    00192          digitalWrite( _backlightPin, HIGH);
    00193       }
    00194       else
    00195       {
    00196          digitalWrite( _backlightPin, LOW);
    00197       }
    00198    }
    00199 }
    00200
    00201 // PRIVATE METHODS
    00202 // ---------------------------------------------------------------------------
    00203
    00204
    00205 // init
    00206 void LiquidCrystal::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
    00207                          uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
    00208                          uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
    00209 {
    00210    uint8_t i;
    00211
    00212    // Initialize the IO pins
    00213    // -----------------------
    00214
    00215    _rs_pin = rs;
    00216    _rw_pin = rw;
    00217    _enable_pin = enable;
    00218
    00219    _data_pins[0] = d0;
    00220    _data_pins[1] = d1;
    00221    _data_pins[2] = d2;
    00222    _data_pins[3] = d3;
    00223    _data_pins[4] = d4;
    00224    _data_pins[5] = d5;
    00225    _data_pins[6] = d6;
    00226    _data_pins[7] = d7;
    00227
    00228    // Initialize the IO port direction to OUTPUT
    00229    // ------------------------------------------
    00230
    00231    for ( i = 0; i < 4; i++ )
    00232    {
    00233       pinMode ( _data_pins[i], OUTPUT );
    00234    }
    00235
    00236    // Initialize the rest of the ports if it is an 8bit controlled LCD
    00237    // ----------------------------------------------------------------
    00238
    00239    if ( !fourbitmode )
    00240    {
    00241       for ( i = 4; i < 8; i++ )
    00242       {
    00243          pinMode ( _data_pins[i], OUTPUT );
    00244       }
    00245    }
    00246    pinMode(_rs_pin, OUTPUT);
    00247
    00248    // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
    00249    if (_rw_pin != 255)
    00250    {
    00251       pinMode(_rw_pin, OUTPUT);
    00252    }
    00253
    00254    pinMode(_enable_pin, OUTPUT);
    00255
    00256    // Initialise displaymode functions to defaults: LCD_1LINE and LCD_5x8DOTS
    00257    // -------------------------------------------------------------------------
    00258    if (fourbitmode)
    00259       _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
    00260    else
    00261       _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
    00262
    00263    // Now we pull both RS and R/W low to begin commands
    00264    digitalWrite(_rs_pin, LOW);
    00265    digitalWrite(_enable_pin, LOW);
    00266
    00267    if (_rw_pin != 255)
    00268    {
    00269       digitalWrite(_rw_pin, LOW);
    00270    }
    00271
    00272    // Initialise the backlight pin no nothing
    00273    _backlightPin = LCD_NOBACKLIGHT;
    00274    _polarity = POSITIVE;
    00275 }
    00276
    00277 //
    00278 // pulseEnable
    00279 void LiquidCrystal::pulseEnable(void)
    00280 {
    00281    // There is no need for the delays, since the digitalWrite operation
    00282    // takes longer.
    00283    digitalWrite(_enable_pin, HIGH);
    00284    waitUsec(1);          // enable pulse must be > 450ns
    00285    digitalWrite(_enable_pin, LOW);
    00286 }
    00287
    00288 //
    00289 // write4bits
    00290 void LiquidCrystal::writeNbits(uint8_t value, uint8_t numBits)
    00291 {
    00292    for (uint8_t i = 0; i < numBits; i++)
    00293    {
    00294       digitalWrite(_data_pins[i], (value >> i) & 0x01);
    00295    }
    00296    pulseEnable();
    00297 }
    00298
    00299

All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines