![]() |
LCD Library 1.1.7
LCD Library - LCD control class hierarchy library. Drop in replacement for the LiquidCrystal Library.
|
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 _srdata_pin = srdata; 00151 _srclock_pin = srclock; 00152 _strobe_pin = strobe; 00153 00154 // Configure control pins as outputs 00155 // ------------------------------------------------------------------------ 00156 pinMode(_srclock_pin, OUTPUT); 00157 pinMode(_srdata_pin, OUTPUT); 00158 pinMode(_strobe_pin, OUTPUT); 00159 00160 // Initialize _strobe_pin at low. 00161 digitalWrite( _strobe_pin, LOW ); 00162 // Little trick to force a pulse of the LCD enable bit and make sure it is 00163 // low before we start further writes since this is assumed. 00164 write4bits(0); 00165 00166 _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x10DOTS; 00167 } 00168 00169 // PUBLIC METHODS 00170 // --------------------------------------------------------------------------- 00171 00172 // 00173 // begin 00174 void LiquidCrystal_SR_LCD3::begin( uint8_t cols, uint8_t lines, uint8_t dotsize ) 00175 { 00176 if (lines > 1) 00177 { 00178 _displayfunction |= LCD_2LINE; 00179 } 00180 00181 _numlines = lines; 00182 _cols = cols; 00183 00184 // for some 1 line displays you can select a 10 pixel high font 00185 // ------------------------------------------------------------ 00186 if ((dotsize != 0) && (lines == 1)) 00187 { 00188 _displayfunction |= LCD_5x10DOTS; 00189 } 00190 00191 // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION! 00192 // according to datasheet, we need at least 40ms after power rises above 2.7V 00193 // before sending commands. Arduino can turn on way before 4.5V so we'll wait 00194 // 50 00195 // --------------------------------------------------------------------------- 00196 delayMicroseconds(50000); 00197 00198 // This init is copied verbatim from the spec sheet. 00199 // 8 bit codes are shifted to 4 bit 00200 write4bits((LCD_FUNCTIONSET | LCD_8BITMODE) >> 4); 00201 delayMicroseconds(4500); // wait more than 4.1ms 00202 00203 // Second try 00204 write4bits((LCD_FUNCTIONSET | LCD_8BITMODE) >> 4); 00205 delayMicroseconds(150); 00206 // Third go 00207 write4bits((LCD_FUNCTIONSET | LCD_8BITMODE) >> 4); 00208 00209 // And finally, set to 4-bit interface 00210 write4bits((LCD_FUNCTIONSET | LCD_4BITMODE) >> 4); 00211 00212 // Set # lines, font size, etc. 00213 command(LCD_FUNCTIONSET | _displayfunction); 00214 // Turn the display on with no cursor or blinking default 00215 _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; 00216 display(); 00217 // Clear it off 00218 clear(); 00219 // Initialize to default text direction (for romance languages) 00220 _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT; 00221 // set the entry mode 00222 command(LCD_ENTRYMODESET | _displaymode); 00223 home(); 00224 00225 } 00226 00227 /************ low level data pushing commands **********/ 00228 00229 // Code below was borrowed from LCD3Wire from 00230 // http://www.arduino.cc/playground/Code/LCD3wires 00231 00232 // bitmasks for control bits on shift register 00233 #define SR_EN_BIT B00010000 // LCD Data enable bit. 00234 #define SR_RW_BIT B00100000 // RW can be pinned low since we only send 00235 #define SR_RS_BIT B01000000 // LOW: command. HIGH: character. 00236 00237 void LiquidCrystal_SR_LCD3::send(uint8_t value, uint8_t mode) 00238 { 00239 uint8_t nibble; 00240 00241 mode = mode ? SR_RS_BIT : 0; // RS bit; LOW: command. HIGH: character. 00242 00243 nibble = value >> 4; // Get high nibble. 00244 write4bits(nibble | mode); 00245 00246 //delay(1); // This was in the LCD3 code but does not seem needed -- merlin 00247 00248 nibble = value & 15; // Get low nibble 00249 write4bits(nibble | mode); 00250 } 00251 00252 void LiquidCrystal_SR_LCD3::write4bits(uint8_t nibble) 00253 { 00254 nibble &= ~SR_RW_BIT; // set RW LOW (we do this always since we only write). 00255 00256 // Send a High transition to display the data that was pushed 00257 nibble |= SR_EN_BIT; // LCD Data Enable HIGH 00258 _pushOut(nibble); 00259 nibble &= ~SR_EN_BIT; // LCD Data Enable LOW 00260 _pushOut(nibble); 00261 } 00262 00263 // push byte to shift register and on to LCD 00264 void LiquidCrystal_SR_LCD3::_pushOut(uint8_t nibble) 00265 { 00266 // Make data available for pushing to the LCD. 00267 shiftOut(_srdata_pin, _srclock_pin, LSBFIRST, nibble); 00268 00269 // Make new data active. 00270 digitalWrite(_strobe_pin, HIGH); 00271 waitUsec( 1 ); // strobe pulse must be >450ns (old code had 10ms) 00272 digitalWrite(_strobe_pin, LOW); 00273 waitUsec( 40 ); // commands need > 37us to settle 00274 }