![]() |
LCD Library 1.2.0
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 // 00023 // Connects an LCD using 3 pins from the Arduino, via an 8-bit 00024 // ShiftRegister (SR from now on). 00025 // The original port source for this module is https://github.com/marcmerlin/NewLiquidCrystal 00026 // The 'FastIO' merge has madethis code 4 times faster. 00027 // 00028 // @brief 00029 // This is a port of the ShiftRegLCD library from raron and ported to the 00030 // LCD library. 00031 // 00032 // The functionality provided by this class and its base class is identical 00033 // to the original functionality of the Arduino LiquidCrystal library and can 00034 // be used as such. 00035 // 00036 // Pinout for this code is used by derivatives of the original LCD3Wire page: 00037 // http://www.arduino.cc/playground/Code/LCD3wires 00038 // 00039 // This includes the LCA (linux.conf.au) Arduino Miniconf Pebble: 00040 // http://shieldlist.org/luke-weston/pebble 00041 // https://github.com/lukeweston/Pebble 00042 // 00043 // It also includes the Pebble v2: 00044 // http://www.arduinominiconf.org/index.php/Pebble_V2.0_Instructions 00045 // http://www.freetronics.com/pages/pebble-v2 00046 // https://github.com/lukeweston/pebble20/blob/master/README.md 00047 // https://github.com/lukeweston/pebble20/blob/master/pebble-sch.pdf 00048 // 00049 // Shiftregister connection description: 00050 // MC14094 input: Arduino digital pin 2=Clock, pin 3=Data, pin 4=Strobe 00051 // MC14094 output: Q8=DB4, Q7=DB5, Q6=DB6, Q5=DB7, Q4=E, Q3=RW, Q2=RS, Q1=None 00052 // 00053 // +--------------------------------------------+ 00054 // | Arduino (ATMega 168 or 328) | 00055 // | D02 D03 D04 | 00056 // +----+-------------+-------------+-----------+ 00057 // |4 |5 |6 00058 // |1 |2 |3 00059 // +----+-------------+-------------+-----------+ 00060 // | Strobe Data Clock | 00061 // | MC14094 8-bit shift/latch register | 00062 // | Q8 Q7 Q6 Q5 Q4 Q3 Q2 Q1 | 00063 // +----+----+----+----+----+----+----+----+----+ 00064 // |11 |12 |13 |14 |7 |6 |5 |4 00065 // |11 |12 |13 |14 |6 |5 |4 00066 // +----+----+----+----+----+----+----+---------+ 00067 // | DB4 DB5 DB6 DB7 E RW RS | 00068 // | LCD KS0066 | 00069 // +--------------------------------------------+ 00070 // 00071 // 3 Pins required from the Arduino for Data, Clock, and Enable/Strobe. 00072 // 00073 // This code was inspired from LiquidCrystal_SR from 00074 // http://code.google.com/p/arduinoshiftreglcd/ 00075 // but was written for implementing LiquidCrystal support for the Pebble 00076 // and Pebblev2 (see below). 00077 // The Pebbles's LCD and shift register wiring were inspired from this 00078 // original page: 00079 // http://www.arduino.cc/playground/Code/LCD3wires 00080 // 00081 // Pebbles and the LCD3Wires design are compatible hardware-wise, but 00082 // the LCD3Wire code does not work with arduino 1.0 anymore and is generally 00083 // quite limited in functionality compared to this framework that provides the 00084 // entire LiquidDisplay functionality. 00085 // Why not just use the LiquidCrystal_SR pinout? 00086 // - LCD3Wire was first and therefore have hardware that was designed with 00087 // incompatible (IMO better if you don't mind 3 wires) pinout. 00088 // - The pinout used here is same saner (the 4 bits for the LCD are all in one 00089 // nibble of the shift register, not spread across 2 like in the 00090 // LiquidCrystal_SR pinout) 00091 // 00092 // Note however that LiquidCrystal_SR while a bit more complex wiring and code 00093 // wise, supports non latching shift registers and it a few percent faster than 00094 // this code since it can address the LCD enable pin without having to send 00095 // a pulse through the shift register like the LCD3Wires setup requires. 00096 // 00097 // This code makes sure to properly follow the specifications when talking 00098 // to the LCD while using minimal delays (it's faster than the LCD3wire and aiko 00099 // pebble code). 00100 // 00101 // @author Marc MERLIN - marc_soft<at>merlins.org. 00102 // --------------------------------------------------------------------------- 00103 #include <stdio.h> 00104 #include <string.h> 00105 #include <inttypes.h> 00106 00107 #if (ARDUINO < 100) 00108 #include <WProgram.h> 00109 #else 00110 #include <Arduino.h> 00111 #endif 00112 #include "LiquidCrystal_SR_LCD3.h" 00113 00114 00115 // STATIC helper functions 00116 // --------------------------------------------------------------------------- 00117 00118 00119 // CONSTRUCTORS 00120 // --------------------------------------------------------------------------- 00121 // Assuming 1 line 8 pixel high font 00122 LiquidCrystal_SR_LCD3::LiquidCrystal_SR_LCD3 ( uint8_t srdata, uint8_t srclock, 00123 uint8_t strobe ) 00124 { 00125 init ( srdata, srclock, strobe, 1, 0 ); 00126 } 00127 00128 00129 // PRIVATE METHODS 00130 // --------------------------------------------------------------------------- 00131 00132 // 00133 // init 00134 void LiquidCrystal_SR_LCD3::init( uint8_t srdata, uint8_t srclock, uint8_t strobe, 00135 uint8_t lines, uint8_t font ) 00136 { 00137 // Initialise private variables 00138 // translate all pins to bits and registers 00139 // pinMode to OUTPUT, Output LOW 00140 00141 _srdata_bit = fio_pinToBit(srdata); 00142 _srdata_register = fio_pinToOutputRegister(srdata); 00143 _srclock_bit = fio_pinToBit(srclock); 00144 _srclock_register = fio_pinToOutputRegister(srclock); 00145 _strobe_bit = fio_pinToBit(strobe); 00146 _strobe_register = fio_pinToOutputRegister(strobe); 00147 00148 // Little trick to force a pulse of the LCD enable bit and make sure it is 00149 // low before we start further writes since this is assumed. 00150 00151 write4bits(0); 00152 00153 _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x10DOTS; 00154 } 00155 00156 // PUBLIC METHODS 00157 // --------------------------------------------------------------------------- 00158 00159 00160 /************ low level data pushing commands **********/ 00161 00162 // Code below was borrowed from LCD3Wire from 00163 // http://www.arduino.cc/playground/Code/LCD3wires 00164 00165 // bitmasks for control bits on shift register 00166 #define SR_EN_BIT B00010000 // LCD Data enable bit. 00167 #define SR_RW_BIT B00100000 // RW can be pinned low since we only send 00168 #define SR_RS_BIT B01000000 // LOW: command. HIGH: character. 00169 00170 void LiquidCrystal_SR_LCD3::send(uint8_t value, uint8_t mode) 00171 { 00172 uint8_t nibble; 00173 00174 mode = mode ? SR_RS_BIT : 0; // RS bit; LOW: command. HIGH: character. 00175 00176 nibble = value >> 4; // Get high nibble. 00177 write4bits(nibble | mode); 00178 00179 //delay(1); // This was in the LCD3 code but does not seem needed -- merlin 00180 00181 nibble = value & 15; // Get low nibble 00182 write4bits(nibble | mode); 00183 } 00184 00185 void LiquidCrystal_SR_LCD3::write4bits(uint8_t nibble) 00186 { 00187 nibble &= ~SR_RW_BIT; // set RW LOW (we do this always since we only write). 00188 00189 // Send a High transition to display the data that was pushed 00190 nibble |= SR_EN_BIT; // LCD Data Enable HIGH 00191 _pushOut(nibble); 00192 nibble &= ~SR_EN_BIT; // LCD Data Enable LOW 00193 _pushOut(nibble); 00194 } 00195 00196 // push byte to shift register and on to LCD 00197 void LiquidCrystal_SR_LCD3::_pushOut(uint8_t nibble) 00198 { 00199 // Make data available for pushing to the LCD. 00200 fio_shiftOut(_srdata_register, _srdata_bit, _srclock_register, _srclock_bit, nibble, LSBFIRST); 00201 00202 // Make new data active. 00203 fio_digitalWrite_HIGH(_strobe_register, _strobe_bit); 00204 waitUsec( 1 ); // strobe pulse must be >450ns (old code had 10ms) 00205 fio_digitalWrite_SWITCHTO(_strobe_register, _strobe_bit,LOW); 00206 waitUsec( 40 ); // commands need > 37us to settle 00207 }