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_SR_LCD3.cpp
Go to the documentation of this file.
00001 // ---------------------------------------------------------------------------
00002 // Originally Created by Francisco Malpartida on 2011/08/20.
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 // 2012/01/21 - Marc MERLIN
00010 // This library, LiquidCrystal_SR_LCD3, was forked off LiquidCrystal_SR which
00011 // used a different wiring than the Pebble and Pebblev2 (just released by
00012 // freetronics in the arduino miniconf as part of linux.conf.au 2012) and
00013 // therefore this code organizes the output data differently.
00014 //
00015 // Upstream source for this module is
00016 // https://github.com/marcmerlin/NewLiquidCrystal
00017 //
00018 // Thread Safe: No
00019 // Extendable: Yes
00020 //
00021 // @file LiquidCrystal_SR_LCD3.h
00022 //  Connects an LCD using 3 pins from the Arduino, via an 8-bit 
00023 //  ShiftRegister (SR from now on).
00024 // 
00025 // @brief 
00026 // This is a port of the ShiftRegLCD library from raron and ported to the
00027 // LCD library.
00028 //
00029 // The functionality provided by this class and its base class is identical
00030 // to the original functionality of the Arduino LiquidCrystal library and can
00031 // be used as such.
00032 //
00033 // Pinout for this code is used by derivatives of the original LCD3Wire page:
00034 // http://www.arduino.cc/playground/Code/LCD3wires
00035 //
00036 // This includes the LCA (linux.conf.au) Arduino Miniconf Pebble: 
00037 // http://shieldlist.org/luke-weston/pebble
00038 // https://github.com/lukeweston/Pebble
00039 //
00040 // It also includes the Pebble v2:
00041 // http://www.arduinominiconf.org/index.php/Pebble_V2.0_Instructions
00042 // http://www.freetronics.com/pages/pebble-v2
00043 // https://github.com/lukeweston/pebble20/blob/master/README.md
00044 // https://github.com/lukeweston/pebble20/blob/master/pebble-sch.pdf
00045 //
00046 // Shiftregister connection description:
00047 // MC14094 input:  Arduino digital pin 2=Clock, pin 3=Data, pin 4=Strobe
00048 // MC14094 output: Q8=DB4, Q7=DB5, Q6=DB6, Q5=DB7, Q4=E, Q3=RW, Q2=RS, Q1=None
00049 // http://www.ee.mut.ac.th/datasheet/MC14094.pdf
00050 //
00051 //   +--------------------------------------------+
00052 //   |    Arduino (ATMega 168 or 328)             |
00053 //   |    D02           D03           D04         |
00054 //   +----+-------------+-------------+-----------+
00055 //        |4            |5            |6
00056 //        |1            |2            |3
00057 //   +----+-------------+-------------+-----------+
00058 //   |    Strobe        Data          Clock       |
00059 //   |    MC14094 8-bit shift/latch register      |
00060 //   |    Q8   Q7   Q6   Q5   Q4   Q3   Q2   Q1   |
00061 //   +----+----+----+----+----+----+----+----+----+
00062 //        |11  |12  |13  |14  |7   |6   |5   |4
00063 //        |11  |12  |13  |14  |6   |5   |4
00064 //   +----+----+----+----+----+----+----+---------+
00065 //   |    DB4  DB5  DB6  DB7  E    RW   RS        |
00066 //   |               LCD KS0066                   |
00067 //   +--------------------------------------------+
00068 //
00069 // 3 Pins required from the Arduino for Data, Clock, and Enable/Strobe.
00070 //
00071 // This code was inspired from LiquidCrystal_SR from
00072 // http://code.google.com/p/arduinoshiftreglcd/
00073 // but was written for implementing LiquidCrystal support for the Pebble
00074 // and Pebblev2 (see below).
00075 // The Pebbles's LCD and shift register wiring were inspired from this
00076 // original page:
00077 // http://www.arduino.cc/playground/Code/LCD3wires
00078 // 
00079 // Pebbles and the LCD3Wires design are compatible hardware-wise, but
00080 // the LCD3Wire code does not work with arduino 1.0 anymore and is generally
00081 // quite limited in functionality compared to this framework that provides the
00082 // entire LiquidDisplay functionality.
00083 // Why not just use the LiquidCrystal_SR pinout?
00084 // - LCD3Wire was first and therefore have hardware that was designed with 
00085 //   incompatible (IMO better if you don't mind 3 wires) pinout.
00086 // - The pinout used here is same saner (the 4 bits for the LCD are all in one 
00087 //   nibble of the shift register, not spread across 2 like in the
00088 //   LiquidCrystal_SR pinout)
00089 //  
00090 // This code makes sure to properly follow the specifications when talking
00091 // to the LCD while using minimal delays (it's faster than the LCD3wire and aiko
00092 // pebble code).
00093 //
00094 // @author Marc MERLIN - marc_soft<at>merlins.org.
00095 // ---------------------------------------------------------------------------
00096 #include <stdio.h>
00097 #include <string.h>
00098 #include <inttypes.h>
00099 
00100 #if (ARDUINO <  100)
00101 #include <WProgram.h>
00102 #else
00103 #include <Arduino.h>
00104 #endif
00105 #include <LiquidCrystal_SR_LCD3.h>
00106 
00107 // When the display powers up, it is configured as follows:
00108 //
00109 // 1. Display clear
00110 // 2. Function set: 
00111 //    DL = 1; 8-bit interface data 
00112 //    N = 0; 1-line display 
00113 //    F = 0; 5x8 dot character font 
00114 // 3. Display on/off control: 
00115 //    D = 0; Display off 
00116 //    C = 0; Cursor off 
00117 //    B = 0; Blinking off 
00118 // 4. Entry mode set: 
00119 //    I/D = 1; Increment by 1 
00120 //    S = 0; No shift 
00121 //
00122 // Note, however, that resetting the Arduino doesn't reset the LCD, so we
00123 // can't assume that its in that state when a sketch starts (and the
00124 // LiquidCrystal constructor is called).
00125 // A call to begin() will reinitialize the LCD.
00126 
00127 // STATIC helper functions
00128 // ---------------------------------------------------------------------------
00129 
00130 
00131 // CONSTRUCTORS
00132 // ---------------------------------------------------------------------------
00133 // Assuming 1 line 8 pixel high font
00134 LiquidCrystal_SR_LCD3::LiquidCrystal_SR_LCD3 ( uint8_t srdata, uint8_t srclock, 
00135                                                uint8_t strobe )
00136 {
00137         init ( srdata, srclock, strobe, 1, 0 );
00138 }
00139 
00140 
00141 // PRIVATE METHODS
00142 // ---------------------------------------------------------------------------
00143 
00144 //
00145 // init
00146 void LiquidCrystal_SR_LCD3::init( uint8_t srdata, uint8_t srclock, uint8_t strobe, 
00147                                   uint8_t lines, uint8_t font )
00148 {
00149         // Initialise private variables
00150         // translate all pins to bits and registers
00151         // pinMode to OUTPUT, Output LOW
00152 
00153    _srdata_bit = fio_pinToBit(srdata);
00154    _srdata_register = fio_pinToOutputRegister(srdata);
00155    _srclock_bit = fio_pinToBit(srclock);
00156    _srclock_register = fio_pinToOutputRegister(srclock);
00157    _strobe_bit = fio_pinToBit(strobe);
00158    _strobe_register = fio_pinToOutputRegister(strobe);
00159    
00160    // Little trick to force a pulse of the LCD enable bit and make sure it is
00161    // low before we start further writes since this is assumed.
00162 
00163    write4bits(0);
00164    
00165    _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x10DOTS;
00166 }
00167 
00168 // PUBLIC METHODS
00169 // ---------------------------------------------------------------------------
00170 
00171 //
00172 // begin
00173 void LiquidCrystal_SR_LCD3::begin( uint8_t cols, uint8_t lines, uint8_t dotsize ) 
00174 {
00175    if (lines > 1) 
00176    {
00177       _displayfunction |= LCD_2LINE;
00178    }
00179    
00180    _numlines = lines;
00181    _cols = cols;
00182    
00183    // for some 1 line displays you can select a 10 pixel high font
00184    // ------------------------------------------------------------
00185    if ((dotsize != 0) && (lines == 1)) 
00186    {
00187       _displayfunction |= LCD_5x10DOTS;
00188    }
00189    
00190    // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
00191    // according to datasheet, we need at least 40ms after power rises above 2.7V
00192    // before sending commands. Arduino can turn on way before 4.5V so we'll wait 
00193    // 50
00194    // ---------------------------------------------------------------------------
00195    delayMicroseconds(50000);
00196    
00197    // This init is copied verbatim from the spec sheet.
00198    // 8 bit codes are shifted to 4 bit
00199    write4bits((LCD_FUNCTIONSET | LCD_8BITMODE) >> 4);
00200    delayMicroseconds(4500);  // wait more than 4.1ms
00201    
00202    // Second try
00203    write4bits((LCD_FUNCTIONSET | LCD_8BITMODE) >> 4);
00204    delayMicroseconds(150);
00205    // Third go
00206    write4bits((LCD_FUNCTIONSET | LCD_8BITMODE) >> 4);
00207    
00208    // And finally, set to 4-bit interface
00209    write4bits((LCD_FUNCTIONSET | LCD_4BITMODE) >> 4);
00210    
00211    // Set # lines, font size, etc.
00212    command(LCD_FUNCTIONSET | _displayfunction);
00213    // Turn the display on with no cursor or blinking default
00214    _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
00215    display();
00216    // Clear it off
00217    clear();
00218    // Initialize to default text direction (for romance languages)
00219    _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
00220    // set the entry mode
00221    command(LCD_ENTRYMODESET | _displaymode);
00222    home();
00223    
00224 }
00225 
00226 /************ low level data pushing commands **********/
00227 
00228 // Code below was borrowed from LCD3Wire from
00229 // http://www.arduino.cc/playground/Code/LCD3wires
00230 
00231 // bitmasks for control bits on shift register
00232 #define SR_EN_BIT B00010000   // LCD Data enable bit.
00233 #define SR_RW_BIT B00100000   // RW can be pinned low since we only send
00234 #define SR_RS_BIT B01000000   // LOW: command. HIGH: character.
00235 
00236 void LiquidCrystal_SR_LCD3::send(uint8_t value, uint8_t mode) 
00237 {
00238    uint8_t nibble;
00239    
00240    mode = mode ? SR_RS_BIT : 0; // RS bit; LOW: command.  HIGH: character.
00241    
00242    nibble = value >> 4; // Get high nibble.
00243    write4bits(nibble | mode);
00244    
00245    //delay(1); // This was in the LCD3 code but does not seem needed -- merlin
00246    
00247    nibble = value & 15; // Get low nibble
00248    write4bits(nibble | mode);
00249 }
00250 
00251 void LiquidCrystal_SR_LCD3::write4bits(uint8_t nibble) 
00252 {
00253    nibble &= ~SR_RW_BIT; // set RW LOW (we do this always since we only write).
00254    
00255    // Send a High transition to display the data that was pushed
00256    nibble |=  SR_EN_BIT; // LCD Data Enable HIGH
00257    _pushOut(nibble);
00258    nibble &= ~SR_EN_BIT; // LCD Data Enable LOW
00259    _pushOut(nibble); 
00260 }
00261 
00262 // push byte to shift register and on to LCD
00263 void LiquidCrystal_SR_LCD3::_pushOut(uint8_t nibble) 
00264 {
00265    // Make data available for pushing to the LCD.
00266    fio_shiftOut(_srdata_register, _srdata_bit, _srclock_register, _srclock_bit, nibble, LSBFIRST);
00267    
00268    // Make new data active.
00269    fio_digitalWrite_HIGH(_strobe_register, _strobe_bit);
00270    waitUsec( 1 ); // strobe pulse must be >450ns (old code had 10ms)
00271    fio_digitalWrite_SWITCHTO(_strobe_register, _strobe_bit,LOW);
00272    waitUsec( 40 ); // commands need > 37us to settle
00273 }
 All Classes Files Functions Variables Typedefs Defines