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/LiquidCrystal_SR3W.cpp
Go to the documentation of this file.
00001 // ---------------------------------------------------------------------------
00002 // Created by Francisco Malpartida on 7.3.2012.
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_SRG.h
00013 // This file implements a basic liquid crystal library that comes as standard
00014 // in the Arduino SDK but using a generic SHIFT REGISTER extension board.
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 a 3 wire latching
00021 // shift register. While it has been tested with a 74HC595N shift register
00022 // it should also work with other latching shift registers such as the MC14094
00023 // and the HEF4094
00024 //
00025 // This particular driver has been created as generic as possible to enable
00026 // users to configure and connect their LCDs using just 3 digital IOs from the
00027 // AVR or Arduino, and connect the LCD to the outputs of the shiftregister
00028 // in any configuration. The library is configured by passing the IO pins
00029 // that control the strobe, data and clock of the shift register and a map
00030 // of how the shiftregister is connected to the LCD.
00031 // 
00032 //
00033 //   +--------------------------------------------+
00034 //   |                 MCU                        |
00035 //   |   IO1           IO2           IO3          |
00036 //   +----+-------------+-------------+-----------+
00037 //        |             |             |
00038 //        |             |             |
00039 //   +----+-------------+-------------+-----------+
00040 //   |    Strobe        Data          Clock       |
00041 //   |          8-bit shift/latch register        |
00042 //   |    Qa   Qb   Qc   Qd   Qe   Qf   Qg   Qh   |
00043 //   +----+----+----+----+----+----+----+----+----+
00044 //        |    |    |    |    |    |    |    
00045 //        |11  |12  |13  |14  |6   |5   |4    (LCD pins)
00046 //   +----+----+----+----+----+----+----+----+----+
00047 //   |    DB4  DB5  DB6  DB7  E    Rw   RS        |
00048 //   |                 LCD Module                 |
00049 //
00050 // The functionality provided by this class and its base class is identical
00051 // to the original functionality of the Arduino LiquidCrystal library.
00052 //
00053 //
00054 // @author F. Malpartida - fmalpartida@gmail.com
00055 // ---------------------------------------------------------------------------
00056 // flags for backlight control
00057 #include <stdio.h>
00058 #include <string.h>
00059 #include <inttypes.h>
00060 
00061 #if (ARDUINO <  100)
00062 #include <WProgram.h>
00063 #else
00064 #include <Arduino.h>
00065 #endif
00066 #include "LiquidCrystal_SR3W.h"
00067 
00068 #include "FastIO.h"
00069 
00075 #define LCD_NOBACKLIGHT 0x00
00076 
00082 #define LCD_BACKLIGHT   0xFF
00083 
00084 
00085 // Default library configuration parameters used by class constructor with
00086 // only the I2C address field.
00087 // ---------------------------------------------------------------------------
00093 #define EN 4  // Enable bit
00094 
00100 #define RW 5  // Read/Write bit
00101 
00107 #define RS 6  // Register select bit
00108 
00115 #define D4 0
00116 #define D5 1
00117 #define D6 2
00118 #define D7 3
00119 
00120 
00121 
00122 LiquidCrystal_SR3W::LiquidCrystal_SR3W(uint8_t data, uint8_t clk, uint8_t strobe)
00123 {
00124    init( data, clk, strobe, RS, RW, EN, D4, D5, D6, D7 );
00125 }
00126 
00127 LiquidCrystal_SR3W::LiquidCrystal_SR3W(uint8_t data, uint8_t clk, uint8_t strobe,
00128                                        uint8_t backlighPin, t_backlighPol pol)
00129 {
00130    init( data, clk, strobe, RS, RW, EN, D4, D5, D6, D7 );
00131    setBacklightPin(backlighPin, pol);
00132 }
00133 
00134 LiquidCrystal_SR3W::LiquidCrystal_SR3W(uint8_t data, uint8_t clk, uint8_t strobe,
00135                                        uint8_t En, uint8_t Rw, uint8_t Rs, 
00136                                        uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7 )
00137 {
00138    init( data, clk, strobe, En, Rw, En, d4, d5, d6, d7 );
00139 }
00140 
00141 LiquidCrystal_SR3W::LiquidCrystal_SR3W(uint8_t data, uint8_t clk, uint8_t strobe, 
00142                                        uint8_t En, uint8_t Rw, uint8_t Rs, 
00143                                        uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7,
00144                                        uint8_t backlighPin, t_backlighPol pol)
00145 {
00146    init( data, clk, strobe, En, Rw, En, d4, d5, d6, d7 );
00147    setBacklightPin(backlighPin, pol);
00148 }
00149 
00150 
00151 void LiquidCrystal_SR3W::send(uint8_t value, uint8_t mode)
00152 {
00153    // No need to use the delay routines since the time taken to write takes
00154    // longer that what is needed both for toggling and enable pin an to execute
00155    // the command.
00156    
00157    if ( mode == FOUR_BITS )
00158    {
00159       write4bits( (value & 0x0F), COMMAND );
00160    }
00161    else 
00162    {
00163       write4bits( (value >> 4), mode );
00164       write4bits( (value & 0x0F), mode);
00165    }   
00166 }
00167 
00168 
00169 void LiquidCrystal_SR3W::setBacklightPin ( uint8_t value, t_backlighPol pol = POSITIVE )
00170 {
00171    _backlightPinMask = ( 1 << value );
00172    _backlightStsMask = LCD_NOBACKLIGHT;
00173    _polarity = pol;
00174    setBacklight (BACKLIGHT_OFF);     // Set backlight to off as initial setup
00175 }
00176 
00177 void LiquidCrystal_SR3W::setBacklight ( uint8_t value )
00178 {
00179    // Check if backlight is available
00180    // ----------------------------------------------------
00181    if ( _backlightPinMask != 0x0 )
00182    {
00183       // Check for polarity to configure mask accordingly
00184       // ----------------------------------------------------------
00185       if  (((_polarity == POSITIVE) && (value > 0)) || 
00186            ((_polarity == NEGATIVE ) && ( value == 0 )))
00187       {
00188          _backlightStsMask = _backlightPinMask & LCD_BACKLIGHT;
00189       }
00190       else 
00191       {
00192          _backlightStsMask = _backlightPinMask & LCD_NOBACKLIGHT;
00193       }
00194       loadSR( _backlightStsMask );
00195    }
00196 }
00197 
00198 
00199 // PRIVATE METHODS
00200 // -----------------------------------------------------------------------------
00201 
00202 int LiquidCrystal_SR3W::init(uint8_t data, uint8_t clk, uint8_t strobe, 
00203                              uint8_t Rs, uint8_t Rw, uint8_t En,
00204                              uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
00205 {
00206    _data       = fio_pinToBit(data);
00207    _clk        = fio_pinToBit(clk);
00208    _strobe     = fio_pinToBit(strobe);
00209    _data_reg   = fio_pinToOutputRegister(data);
00210    _clk_reg    = fio_pinToOutputRegister(clk);
00211    _strobe_reg = fio_pinToOutputRegister(strobe);
00212    
00213    // LCD pin mapping
00214    _backlightPinMask = 0;
00215    _backlightStsMask = LCD_NOBACKLIGHT;
00216    _polarity = POSITIVE;
00217    
00218    _En = ( 1 << En );
00219    _Rw = ( 1 << Rw );
00220    _Rs = ( 1 << Rs );
00221    
00222    // Initialise pin mapping
00223    _data_pins[0] = ( 1 << d4 );
00224    _data_pins[1] = ( 1 << d5 );
00225    _data_pins[2] = ( 1 << d6 );
00226    _data_pins[3] = ( 1 << d7 );
00227    
00228    _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x10DOTS;
00229    
00230    return (1);
00231 }
00232 
00233 void LiquidCrystal_SR3W::write4bits(uint8_t value, uint8_t mode)
00234 {
00235    uint8_t pinMapValue = 0;
00236    
00237    // Map the value to LCD pin mapping
00238    // --------------------------------
00239    for ( uint8_t i = 0; i < 4; i++ )
00240    {
00241       if ( ( value & 0x1 ) == 1 )
00242       {
00243          pinMapValue |= _data_pins[i];
00244       }
00245       value = ( value >> 1 );
00246    }
00247    
00248    // Is it a command or data
00249    // -----------------------
00250    if ( mode == DATA )
00251    {
00252       mode = _Rs;
00253    }
00254    
00255    pinMapValue |= mode | _backlightStsMask;
00256    loadSR ( pinMapValue | _En );  // Send with enable high
00257    loadSR ( pinMapValue & ~_En ); // Send with enable low
00258 }
00259 
00260 
00261 void LiquidCrystal_SR3W::loadSR(uint8_t value) 
00262 {
00263    // Load the shift register with information
00264    fio_shiftOut(_data_reg, _data, _clk_reg, _clk, value, MSBFIRST);
00265    
00266    // Strobe the data into the latch
00267    ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
00268    {
00269       fio_digitalWrite_HIGH(_strobe_reg, _strobe);
00270       fio_digitalWrite_SWITCHTO(_strobe_reg, _strobe, LOW);
00271    }
00272    waitUsec( 40 ); // commands need > 37us to settle
00273 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines