![]() |
LCD Library 1.2.0
LCD Library - LCD control class hierarchy library. Drop in replacement for the LiquidCrystal Library.
|
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.cpp 00013 // This file implements a basic liquid crystal library that comes as standard 00014 // in the Arduino SDK. 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 the parallel port of 00021 // the LCD (4 bit and 8 bit). 00022 // 00023 // The functionality provided by this class and its base class is identical 00024 // to the original functionality of the Arduino LiquidCrystal library. 00025 // 00026 // 00027 // @author F. Malpartida - fmalpartida@gmail.com 00028 // --------------------------------------------------------------------------- 00029 #include <stdio.h> 00030 #include <string.h> 00031 #include <inttypes.h> 00032 00033 #if (ARDUINO < 100) 00034 #include <WProgram.h> 00035 #else 00036 #include <Arduino.h> 00037 #endif 00038 #include <LiquidCrystal.h> 00039 00040 // CONSTANT definitions 00041 // --------------------------------------------------------------------------- 00042 #define LCD_NOBACKLIGHT 0xFF 00043 00044 // STATIC helper functions 00045 // --------------------------------------------------------------------------- 00046 00047 00048 // CONSTRUCTORS 00049 // --------------------------------------------------------------------------- 00050 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable, 00051 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, 00052 uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7) 00053 { 00054 init(LCD_8BIT, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7); 00055 } 00056 00057 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable, 00058 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, 00059 uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7) 00060 { 00061 init(LCD_8BIT, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7); 00062 } 00063 00064 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable, 00065 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3) 00066 { 00067 init(LCD_4BIT, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0); 00068 } 00069 00070 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable, 00071 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3) 00072 { 00073 init(LCD_4BIT, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0); 00074 } 00075 00076 00077 00078 // PUBLIC METHODS 00079 // --------------------------------------------------------------------------- 00080 00081 /************ low level data pushing commands **********/ 00082 00083 // send 00084 void LiquidCrystal::send(uint8_t value, uint8_t mode) 00085 { 00086 digitalWrite( _rs_pin, mode ); 00087 00088 // if there is a RW pin indicated, set it low to Write 00089 // --------------------------------------------------- 00090 if (_rw_pin != 255) 00091 { 00092 digitalWrite(_rw_pin, LOW); 00093 } 00094 00095 if (_displayfunction & LCD_8BITMODE) 00096 { 00097 writeNbits(value, 8); 00098 } 00099 else 00100 { 00101 writeNbits ( value >> 4, 4 ); 00102 writeNbits ( value, 4 ); 00103 } 00104 waitUsec ( EXEC_TIME ); // wait for the command to execute by the LCD 00105 } 00106 00107 // 00108 // setBackligh 00109 void LiquidCrystal::setBacklight ( uint8_t mode ) 00110 { 00111 if ( _backlightPin != LCD_NOBACKLIGHT ) 00112 { 00113 digitalWrite ( _backlightPin, mode ); 00114 } 00115 } 00116 00117 // 00118 // setBacklightPin 00119 void LiquidCrystal::setBacklightPin ( uint8_t pin ) 00120 { 00121 pinMode ( pin, OUTPUT ); // Difine the backlight pin as output 00122 _backlightPin = pin; 00123 } 00124 00125 // PRIVATE METHODS 00126 // --------------------------------------------------------------------------- 00127 00128 00129 // init 00130 void LiquidCrystal::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable, 00131 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, 00132 uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7) 00133 { 00134 uint8_t i; 00135 00136 // Initialize the IO pins 00137 // ----------------------- 00138 00139 _rs_pin = rs; 00140 _rw_pin = rw; 00141 _enable_pin = enable; 00142 00143 _data_pins[0] = d0; 00144 _data_pins[1] = d1; 00145 _data_pins[2] = d2; 00146 _data_pins[3] = d3; 00147 _data_pins[4] = d4; 00148 _data_pins[5] = d5; 00149 _data_pins[6] = d6; 00150 _data_pins[7] = d7; 00151 00152 // Initialize the IO port direction to OUTPUT 00153 // ------------------------------------------ 00154 00155 for ( i = 0; i < 4; i++ ) 00156 { 00157 pinMode ( _data_pins[i], OUTPUT ); 00158 } 00159 00160 // Initialize the rest of the ports if it is an 8bit controlled LCD 00161 // ---------------------------------------------------------------- 00162 00163 if ( !fourbitmode ) 00164 { 00165 for ( i = 4; i < 8; i++ ) 00166 { 00167 pinMode ( _data_pins[i], OUTPUT ); 00168 } 00169 } 00170 pinMode(_rs_pin, OUTPUT); 00171 00172 // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin# 00173 if (_rw_pin != 255) 00174 { 00175 pinMode(_rw_pin, OUTPUT); 00176 } 00177 00178 pinMode(_enable_pin, OUTPUT); 00179 00180 // Initialise displaymode functions to defaults: LCD_1LINE and LCD_5x8DOTS 00181 // ------------------------------------------------------------------------- 00182 if (fourbitmode) 00183 _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS; 00184 else 00185 _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS; 00186 00187 // Now we pull both RS and R/W low to begin commands 00188 digitalWrite(_rs_pin, LOW); 00189 digitalWrite(_enable_pin, LOW); 00190 00191 if (_rw_pin != 255) 00192 { 00193 digitalWrite(_rw_pin, LOW); 00194 } 00195 00196 // Initialise the backlight pin no nothing 00197 _backlightPin = LCD_NOBACKLIGHT; 00198 } 00199 00200 // 00201 // pulseEnable 00202 void LiquidCrystal::pulseEnable(void) 00203 { 00204 // There is no need for the delays, since the digitalWrite operation 00205 // takes longer. 00206 digitalWrite(_enable_pin, HIGH); 00207 waitUsec(1); // enable pulse must be > 450ns 00208 digitalWrite(_enable_pin, LOW); 00209 } 00210 00211 // 00212 // write4bits 00213 void LiquidCrystal::writeNbits(uint8_t value, uint8_t numBits) 00214 { 00215 for (uint8_t i = 0; i < numBits; i++) 00216 { 00217 digitalWrite(_data_pins[i], (value >> i) & 0x01); 00218 } 00219 pulseEnable(); 00220 } 00221 00222