![]() |
LCD Library 1.3.0
LCD Library - LCD control class hierarchy library. Drop in replacement for the LiquidCrystal Library.
|
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 | 74HC595N 00042 // | Qa0 Qb1 Qc2 Qd3 Qe4 Qf5 Qg6 Qh7 | 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 // NOTE: Rw is not used by the driver so it can be connected to GND. 00051 // 00052 // The functionality provided by this class and its base class is identical 00053 // to the original functionality of the Arduino LiquidCrystal library. 00054 // 00055 // 00056 // History 00057 // 2012.03.29 bperrybap - fixed constructors not properly using Rs 00058 // Fixed incorrect use of 5x10 for default font 00059 // - now matches original LQ library. 00060 // moved delay to send() so it is per cmd/write vs shiftout() 00061 // NOTE: delay is on hairy edge of working when FAST_MODE is on. 00062 // because of waitUsec(). 00063 // There is margin at 16Mhz AVR but might fail on 20Mhz AVRs. 00064 // 00065 // @author F. Malpartida - fmalpartida@gmail.com 00066 // --------------------------------------------------------------------------- 00067 // flags for backlight control 00068 #include <stdio.h> 00069 #include <string.h> 00070 #include <inttypes.h> 00071 00072 #if (ARDUINO < 100) 00073 #include <WProgram.h> 00074 #else 00075 #include <Arduino.h> 00076 #endif 00077 #include "LiquidCrystal_SR3W.h" 00078 00079 #include "FastIO.h" 00080 00086 #define LCD_NOBACKLIGHT 0x00 00087 00093 #define LCD_BACKLIGHT 0xFF 00094 00095 00096 // Default library configuration parameters used by class constructor with 00097 // only the I2C address field. 00098 // --------------------------------------------------------------------------- 00104 #define EN 4 // Enable bit 00105 00111 #define RW 5 // Read/Write bit 00112 00118 #define RS 6 // Register select bit 00119 00126 #define D4 0 00127 #define D5 1 00128 #define D6 2 00129 #define D7 3 00130 00131 00132 00133 LiquidCrystal_SR3W::LiquidCrystal_SR3W(uint8_t data, uint8_t clk, uint8_t strobe) 00134 { 00135 init( data, clk, strobe, RS, RW, EN, D4, D5, D6, D7 ); 00136 } 00137 00138 LiquidCrystal_SR3W::LiquidCrystal_SR3W(uint8_t data, uint8_t clk, uint8_t strobe, 00139 uint8_t backlighPin, t_backlighPol pol) 00140 { 00141 init( data, clk, strobe, RS, RW, EN, D4, D5, D6, D7 ); 00142 setBacklightPin(backlighPin, pol); 00143 } 00144 00145 LiquidCrystal_SR3W::LiquidCrystal_SR3W(uint8_t data, uint8_t clk, uint8_t strobe, 00146 uint8_t En, uint8_t Rw, uint8_t Rs, 00147 uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7 ) 00148 { 00149 init( data, clk, strobe, Rs, Rw, En, d4, d5, d6, d7 ); 00150 } 00151 00152 LiquidCrystal_SR3W::LiquidCrystal_SR3W(uint8_t data, uint8_t clk, uint8_t strobe, 00153 uint8_t En, uint8_t Rw, uint8_t Rs, 00154 uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7, 00155 uint8_t backlighPin, t_backlighPol pol) 00156 { 00157 init( data, clk, strobe, Rs, Rw, En, d4, d5, d6, d7 ); 00158 setBacklightPin(backlighPin, pol); 00159 } 00160 00161 00162 void LiquidCrystal_SR3W::send(uint8_t value, uint8_t mode) 00163 { 00164 00165 if ( mode != FOUR_BITS ) 00166 { 00167 write4bits( (value >> 4), mode ); // upper nibble 00168 } 00169 write4bits( (value & 0x0F), mode); // lower nibble 00170 00171 00172 #if (F_CPU <= 16000000) 00173 // No need to use the delay routines on AVR since the time taken to write 00174 // on AVR with SR pin mapping even with fio is longer than LCD command execution. 00175 waitUsec(37); //goes away on AVRs 00176 #else 00177 delayMicroseconds ( 37 ); // commands & data writes need > 37us to complete 00178 #endif 00179 00180 } 00181 00182 00183 void LiquidCrystal_SR3W::setBacklightPin ( uint8_t value, t_backlighPol pol = POSITIVE ) 00184 { 00185 _backlightPinMask = ( 1 << value ); 00186 _backlightStsMask = LCD_NOBACKLIGHT; 00187 _polarity = pol; 00188 setBacklight (BACKLIGHT_OFF); // Set backlight to off as initial setup 00189 } 00190 00191 void LiquidCrystal_SR3W::setBacklight ( uint8_t value ) 00192 { 00193 // Check if backlight is available 00194 // ---------------------------------------------------- 00195 if ( _backlightPinMask != 0x0 ) 00196 { 00197 // Check for polarity to configure mask accordingly 00198 // ---------------------------------------------------------- 00199 if (((_polarity == POSITIVE) && (value > 0)) || 00200 ((_polarity == NEGATIVE ) && ( value == 0 ))) 00201 { 00202 _backlightStsMask = _backlightPinMask & LCD_BACKLIGHT; 00203 } 00204 else 00205 { 00206 _backlightStsMask = _backlightPinMask & LCD_NOBACKLIGHT; 00207 } 00208 loadSR( _backlightStsMask ); 00209 } 00210 } 00211 00212 00213 // PRIVATE METHODS 00214 // ----------------------------------------------------------------------------- 00215 00216 int LiquidCrystal_SR3W::init(uint8_t data, uint8_t clk, uint8_t strobe, 00217 uint8_t Rs, uint8_t Rw, uint8_t En, 00218 uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7) 00219 { 00220 _data = fio_pinToBit(data); 00221 _clk = fio_pinToBit(clk); 00222 _strobe = fio_pinToBit(strobe); 00223 _data_reg = fio_pinToOutputRegister(data); 00224 _clk_reg = fio_pinToOutputRegister(clk); 00225 _strobe_reg = fio_pinToOutputRegister(strobe); 00226 00227 // LCD pin mapping 00228 _backlightPinMask = 0; 00229 _backlightStsMask = LCD_NOBACKLIGHT; 00230 _polarity = POSITIVE; 00231 00232 _En = ( 1 << En ); 00233 _Rw = ( 1 << Rw ); 00234 _Rs = ( 1 << Rs ); 00235 00236 // Initialise pin mapping 00237 _data_pins[0] = ( 1 << d4 ); 00238 _data_pins[1] = ( 1 << d5 ); 00239 _data_pins[2] = ( 1 << d6 ); 00240 _data_pins[3] = ( 1 << d7 ); 00241 00242 _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS; 00243 00244 return (1); 00245 } 00246 00247 void LiquidCrystal_SR3W::write4bits(uint8_t value, uint8_t mode) 00248 { 00249 uint8_t pinMapValue = 0; 00250 00251 // Map the value to LCD pin mapping 00252 // -------------------------------- 00253 for ( uint8_t i = 0; i < 4; i++ ) 00254 { 00255 if ( ( value & 0x1 ) == 1 ) 00256 { 00257 pinMapValue |= _data_pins[i]; 00258 } 00259 value = ( value >> 1 ); 00260 } 00261 00262 // Is it a command or data 00263 // ----------------------- 00264 mode = ( mode == DATA ) ? _Rs : 0; 00265 00266 pinMapValue |= mode | _backlightStsMask; 00267 loadSR ( pinMapValue | _En ); // Send with enable high 00268 loadSR ( pinMapValue); // Send with enable low 00269 } 00270 00271 00272 void LiquidCrystal_SR3W::loadSR(uint8_t value) 00273 { 00274 // Load the shift register with information 00275 fio_shiftOut(_data_reg, _data, _clk_reg, _clk, value, MSBFIRST); 00276 00277 // Strobe the data into the latch 00278 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) 00279 { 00280 fio_digitalWrite_HIGH(_strobe_reg, _strobe); 00281 fio_digitalWrite_SWITCHTO(_strobe_reg, _strobe, LOW); 00282 } 00283 }