LiquidCrystal_SR.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_SR.h
    00013 //  Connects an LCD using 2 or 3 pins from the Arduino, via an 8-bit
    00014 // ShiftRegister (SR from now on).
    00015 //
    00016 // @brief
    00017 // This is a port of the ShiftRegLCD library from raron and ported to the
    00018 // LCD library.
    00019 //
    00020 // The functionality provided by this class and its base class is identical
    00021 // to the original functionality of the Arduino LiquidCrystal library and can
    00022 // be used as such.
    00023 //
    00024 // Modified to work serially with the shiftOut() function, an 8-bit
    00025 // unlatched, no-tristate, unidirectional SIPO (Serial-In-Parallel-Out)
    00026 // shift register (IE a very simple SR), and an LCD in 4-bit mode.
    00027 // Any such shift register should do (pref. 74LS family IC's for 2-wire).
    00028 // I used 74LS164, for the reason that's what I had at hand.
    00029 //
    00030 // Connection description:
    00031 //
    00032 // SR output:
    00033 // Bit  #0   - N/C - not connected, used to hold a zero
    00034 // Bit  #1   - N/C
    00035 // Bit  #2   - connects to RS (Register Select) on the LCD
    00036 // Bits #3-6 - connects to LCD data inputs D4 - D7.
    00037 // Bit  #7   - enables the LCD enable-puls (via the diode-resistor AND "gate")
    00038 //
    00039 // 2 or 3 Pins required from the Arduino for Data, Clock and (optional) Enable
    00040 // If not using Enable, the Data pin is used for the enable signal by defining
    00041 // the same pin for Enable as for Data. Data and Clock outputs/pins goes to
    00042 // the shiftregister.
    00043 // LCD RW-pin hardwired to LOW (only writing to LCD).
    00044 // Busy Flag (BF, data bit D7) is not read.
    00045 //
    00046 //  Original project homepage: http://code.google.com/p/arduinoshiftreglcd/
    00047 //
    00048 //
    00049 // History
    00050 // 2012.03.29  bperrybap - Added delays for faster fio shiftout (it got too fast)
    00051 //             AVR needed delay. cmd/write delays are based on CPU speed so it works on pic32.
    00052 //             Added code to support indicating two wire mode by using enable=data pin
    00053 //             (documentation indicated this as working)
    00054 //             Fixed incorrect use of 5x10 for default font - now matches original LQ library.
    00055 //             can now eliminate enable pin in constructor for two wire mode.
    00056 // 2012.01.16  Florian Fida - faster digitalWrite/shiftOut
    00057 // 2011.10.29  fmalpartida - adaption of the library to the LCD class hierarchy.
    00058 // 2011.07.02  Fixed a minor flaw in setCursor function. No functional change,
    00059 //             just a bit more memory efficient.
    00060 //             Thanks to CapnBry (from google code and github) who noticed it.
    00061 //             URL to his version of shiftregLCD:
    00062 //             https://github.com/CapnBry/HeaterMeter/commit/c6beba1b46b092ab0b33bcbd0a30a201fd1f28c1
    00063 // 2009.07.30  raron - minor corrections to the comments.
    00064 //             Fixed timing to datasheet safe. Fixed keyword highlights.
    00065 // 2009.07.28  Mircho / raron - a new modification to the schematics, and a
    00066 //             more streamlined interface
    00067 // 2009.07.27  Thanks to an excellent suggestion from mircho at the Arduino
    00068 //             playgrond forum, the number of wires now required is only two!
    00069 // 2009.07.25  raron - Fixed comments. I really messed up the comments before
    00070 //             posting this, so I had to fix it.
    00071 //             Renamed a function, but no improvements or functional changes.
    00072 // 2009.07.23  Incorporated some proper initialization routines
    00073 //             inspired (lets say copy-paste-tweaked) from LiquidCrystal
    00074 //             library improvements from LadyAda.
    00075 // 2009.05.23  raron - first version, but based mostly (as in almost verbatim)
    00076 //             on the "official" LiquidCrystal library.
    00077 //
    00078 //
    00079 // @author F. Malpartida - fmalpartida@gmail.com
    00080 // ---------------------------------------------------------------------------
    00081 #include <stdio.h>
    00082 #include <string.h>
    00083 #include <inttypes.h>
    00084
    00085 #if (ARDUINO <  100)
    00086 #include <WProgram.h>
    00087 #else
    00088 #include <Arduino.h>
    00089 #endif
    00090 #include "LiquidCrystal_SR.h"
    00091
    00092 #include "FastIO.h"
    00093
    00094
    00095 // CONSTRUCTORS
    00096 // ---------------------------------------------------------------------------
    00097 // Assuming 1 line 8 pixel high font
    00098 LiquidCrystal_SR::LiquidCrystal_SR (uint8_t srdata, uint8_t srclock,
    00099                                     uint8_t enable )
    00100 {
    00101         init ( srdata, srclock, enable, 1, 0 );
    00102 }
    00103
    00104
    00105 // PRIVATE METHODS
    00106 // ---------------------------------------------------------------------------
    00107
    00108 //
    00109 // init
    00110 void LiquidCrystal_SR::init(uint8_t srdata, uint8_t srclock, uint8_t enable,
    00111                             uint8_t lines, uint8_t font)
    00112 {
    00113    // Initialise private variables
    00114    _two_wire = 0;
    00115
    00116    _srDataRegister = fio_pinToOutputRegister(srdata);
    00117    _srDataBit = fio_pinToBit(srdata);
    00118    _srClockRegister = fio_pinToOutputRegister(srclock);
    00119    _srClockBit = fio_pinToBit(srclock);
    00120
    00121    if ((enable == TWO_WIRE) || (enable == srdata))
    00122    {
    00123       _two_wire = 1;
    00124       _srEnableRegister = _srDataRegister;
    00125       _srEnableBit = _srDataBit;
    00126    }
    00127    else
    00128    {
    00129       _srEnableRegister = fio_pinToOutputRegister(enable);
    00130       _srEnableBit = fio_pinToBit(enable);
    00131    }
    00132
    00133    // Configure control pins as outputs
    00134    // ------------------------------------------------------------------------
    00135
    00136    _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
    00137 }
    00138
    00139 //
    00140 // shiftIt
    00141 void LiquidCrystal_SR::shiftIt(uint8_t val)
    00142 {
    00143    if (_two_wire)
    00144    {
    00145       // Clear to get Enable LOW
    00146       fio_shiftOut(_srDataRegister, _srDataBit, _srClockRegister, _srClockBit);
    00147    }
    00148    fio_shiftOut(_srDataRegister, _srDataBit, _srClockRegister, _srClockBit, val, MSBFIRST);
    00149
    00150    // LCD ENABLE PULSE
    00151    //
    00152    // While this library is written with a shift register without an output
    00153    // latch in mind, it can work in 3-wire mode with a shiftregister with a
    00154    // latch. The shiftregister latch pin (STR, RCL or similar) is then
    00155    // connected to the LCD enable pin. The LCD is (very likely) slower
    00156    // to read the Enable pulse, and then reads the new contents of the SR.
    00157    ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
    00158    {
    00159       fio_digitalWrite_HIGH(_srEnableRegister, _srEnableBit);
    00160       delayMicroseconds (1);         // enable pulse must be >450ns
    00161       fio_digitalWrite_SWITCHTO(_srEnableRegister, _srEnableBit, LOW);
    00162    } // end critical section
    00163 }
    00164
    00165 // PUBLIC METHODS
    00166 // ---------------------------------------------------------------------------
    00167
    00168
    00169 /************ low level data pushing commands **********/
    00170 //
    00171 // send
    00172 void LiquidCrystal_SR::send(uint8_t value, uint8_t mode)
    00173 {
    00174    // Divide byte in two nibbles include the RS signal
    00175    // and format it for shiftregister output wiring to the LCD
    00176    // We are only interested in my COMMAND or DATA for myMode
    00177    uint8_t myMode = ( mode == DATA ) ? SR_RS_BIT : 0; // RS bit; LOW: command.  HIGH: character.
    00178
    00179    if ( mode != FOUR_BITS )
    00180    {
    00181       shiftIt(myMode | SR_EN_BIT | ((value >> 1) & 0x78)); // upper nibble
    00182    }
    00183
    00184    shiftIt(myMode | SR_EN_BIT | ((value << 3) & 0x78)); // lower nibble
    00185    /*
    00186     * Add some delay since this code is so fast it needs some added delay
    00187     * even on AVRs because the shiftout is shorter than the LCD command execution time.
    00188     */
    00189 #if (F_CPU <= 16000000)
    00190    if(_two_wire)
    00191         delayMicroseconds ( 10 );
    00192    else
    00193         delayMicroseconds ( 17 ); // 3 wire mode is faster so it must delay longer
    00194 #else
    00195    delayMicroseconds ( 37 );      // commands & data writes need > 37us to complete
    00196 #endif
    00197
    00198 }
    00199
    00200 //
    00201 // setBacklightPin
    00202 void LiquidCrystal_SR::setBacklightPin ( uint8_t pin, t_backlighPol pol )
    00203 { }
    00204
    00205 //
    00206 // setBacklight
    00207 void LiquidCrystal_SR::setBacklight ( uint8_t mode )
    00208 { }
    00209

All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines