New LiquidCrystal Library 1.0.0
New LiquidCrystal Library - LCD control class hierarchy
/Users/fmalpartida/development/ardWorkspace/LiquidCrystal_I2C/LiquiCrystal_I2C/LiquidCrystal_4bit.cpp
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_4bit.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 // This library is only compatible with Arduino's SDK version 1.0
00028 //
00029 //
00030 // @author F. Malpartida - fmalpartida@gmail.com
00031 // ---------------------------------------------------------------------------
00032 #include <stdio.h>
00033 #include <string.h>
00034 #include <inttypes.h>
00035 #include <Arduino.h>
00036 #include <LiquidCrystal_4bit.h>
00037 
00038 // When the display powers up, it is configured as follows:
00039 //
00040 // 1. Display clear
00041 // 2. Function set: 
00042 //    DL = 1; 8-bit interface data 
00043 //    N = 0; 1-line display 
00044 //    F = 0; 5x8 dot character font 
00045 // 3. Display on/off control: 
00046 //    D = 0; Display off 
00047 //    C = 0; Cursor off 
00048 //    B = 0; Blinking off 
00049 // 4. Entry mode set: 
00050 //    I/D = 1; Increment by 1 
00051 //    S = 0; No shift 
00052 //
00053 // Note, however, that resetting the Arduino doesn't reset the LCD, so we
00054 // can't assume that its in that state when a sketch starts (and the
00055 // LiquidCrystal_4bit constructor is called).
00056 // A call to begin() will reinitialize the LCD.
00057 
00058 
00059 // CONSTRUCTORS
00060 // ---------------------------------------------------------------------------
00061 LiquidCrystal_4bit::LiquidCrystal_4bit(uint8_t rs, uint8_t rw, uint8_t enable,
00062                                        uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
00063                                        uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
00064 {
00065    init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
00066 }
00067 
00068 LiquidCrystal_4bit::LiquidCrystal_4bit(uint8_t rs, uint8_t enable,
00069                                        uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
00070                                        uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
00071 {
00072    init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7);
00073 }
00074 
00075 LiquidCrystal_4bit::LiquidCrystal_4bit(uint8_t rs, uint8_t rw, uint8_t enable,
00076                                        uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
00077 {
00078    init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0);
00079 }
00080 
00081 LiquidCrystal_4bit::LiquidCrystal_4bit(uint8_t rs,  uint8_t enable,
00082                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
00083 {
00084    init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
00085 }
00086 
00087 // PRIVATE METHODS
00088 // ---------------------------------------------------------------------------
00089 
00090 //
00091 // init
00092 void LiquidCrystal_4bit::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
00093                               uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
00094                               uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
00095 {
00096    uint8_t i;
00097    
00098    // Initialize the IO pins
00099    // -----------------------
00100    
00101    _rs_pin = rs;
00102    _rw_pin = rw;
00103    _enable_pin = enable;
00104    
00105    _data_pins[0] = d0;
00106    _data_pins[1] = d1;
00107    _data_pins[2] = d2;
00108    _data_pins[3] = d3; 
00109    _data_pins[4] = d4;
00110    _data_pins[5] = d5;
00111    _data_pins[6] = d6;
00112    _data_pins[7] = d7;
00113    
00114    // Initialize the IO port direction to OUTPUT
00115    // ------------------------------------------
00116    
00117    for ( uint8_t i = 0; i < 4; i++ )
00118    {
00119       pinMode ( _data_pins[i], OUTPUT );
00120    }
00121    
00122    // Initialize the rest of the ports if it is an 8bit controlled LCD
00123    // ----------------------------------------------------------------
00124    
00125    if ( !fourbitmode )
00126    {
00127       for ( uint8_t i = 4; i < 7; i++ )
00128       {
00129          pinMode ( _data_pins[i], OUTPUT );
00130       }
00131    }
00132    pinMode(_rs_pin, OUTPUT);
00133    
00134    // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
00135    if (_rw_pin != 255) 
00136    { 
00137       pinMode(_rw_pin, OUTPUT);
00138    }
00139    
00140    pinMode(_enable_pin, OUTPUT);
00141    
00142    // Initialise displaymode functions to defaults: LCD_1LINE and LCD_5x8DOTS
00143    // -------------------------------------------------------------------------
00144    if (fourbitmode)
00145       _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
00146    else 
00147       _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
00148 }
00149 
00150 // PUBLIC METHODS
00151 // ---------------------------------------------------------------------------
00152 
00153 //
00154 // begin
00155 void LiquidCrystal_4bit::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) 
00156 {
00157    if (lines > 1) 
00158    {
00159       _displayfunction |= LCD_2LINE;
00160    }
00161    _numlines = lines;
00162    
00163    // for some 1 line displays you can select a 10 pixel high font
00164    // ------------------------------------------------------------
00165    if ((dotsize != 0) && (lines == 1)) 
00166    {
00167       _displayfunction |= LCD_5x10DOTS;
00168    }
00169    
00170    // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
00171    // according to datasheet, we need at least 40ms after power rises above 2.7V
00172    // before sending commands. Arduino can turn on way before 4.5V so we'll wait 
00173    // 50
00174    // ---------------------------------------------------------------------------
00175    delayMicroseconds(50000); 
00176    
00177    // Now we pull both RS and R/W low to begin commands
00178    digitalWrite(_rs_pin, LOW);
00179    digitalWrite(_enable_pin, LOW);
00180    
00181    if (_rw_pin != 255) 
00182    { 
00183       digitalWrite(_rw_pin, LOW);
00184    }
00185    
00186    //put the LCD into 4 bit or 8 bit mode
00187    // -------------------------------------
00188    if (! (_displayfunction & LCD_8BITMODE)) 
00189    {
00190       // this is according to the hitachi HD44780 datasheet
00191       // figure 24, pg 46
00192       
00193       // we start in 8bit mode, try to set 4 bit mode
00194       write4bits(0x03);
00195       delayMicroseconds(4500); // wait min 4.1ms
00196       
00197       // second try
00198       write4bits(0x03);
00199       delayMicroseconds(4500); // wait min 4.1ms
00200       
00201       // third go!
00202       write4bits(0x03); 
00203       delayMicroseconds(150);
00204       
00205       // finally, set to 4-bit interface
00206       write4bits(0x02); 
00207    } 
00208    else 
00209    {
00210       // this is according to the hitachi HD44780 datasheet
00211       // page 45 figure 23
00212       
00213       // Send function set command sequence
00214       command(LCD_FUNCTIONSET | _displayfunction);
00215       delayMicroseconds(4500);  // wait more than 4.1ms
00216       
00217       // second try
00218       command(LCD_FUNCTIONSET | _displayfunction);
00219       delayMicroseconds(150);
00220       
00221       // third go
00222       command(LCD_FUNCTIONSET | _displayfunction);
00223    }
00224    
00225    // finally, set # lines, font size, etc.
00226    command(LCD_FUNCTIONSET | _displayfunction);  
00227    
00228    // turn the display on with no cursor or blinking default
00229    _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;  
00230    display();
00231    
00232    // clear the LCD
00233    clear();
00234    
00235    // Initialize to default text direction (for romance languages)
00236    _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
00237    // set the entry mode
00238    command(LCD_ENTRYMODESET | _displaymode);
00239    
00240 }
00241 
00242 /************ low level data pushing commands **********/
00243 
00244 // send
00245 void LiquidCrystal_4bit::send(uint8_t value, uint8_t mode) 
00246 {
00247    digitalWrite(_rs_pin, mode);
00248    
00249    // if there is a RW pin indicated, set it low to Write
00250    // ---------------------------------------------------
00251    if (_rw_pin != 255) 
00252    { 
00253       digitalWrite(_rw_pin, LOW);
00254    }
00255    
00256    if (_displayfunction & LCD_8BITMODE)
00257    {
00258       write8bits(value); 
00259    } 
00260    else 
00261    {
00262       write4bits(value>>4);
00263       write4bits(value);
00264    }
00265 }
00266 
00267 //
00268 // pulseEnable
00269 void LiquidCrystal_4bit::pulseEnable(void) 
00270 {
00271    digitalWrite(_enable_pin, LOW);
00272    delayMicroseconds(1);
00273    
00274    digitalWrite(_enable_pin, HIGH);
00275    delayMicroseconds(1);           // enable pulse must be > 450ns
00276    
00277    digitalWrite(_enable_pin, LOW);
00278    delayMicroseconds(100);         // commands need > 37us to settle
00279 }
00280 
00281 //
00282 // write4bits
00283 void LiquidCrystal_4bit::write4bits(uint8_t value) 
00284 {
00285    for (uint8_t i = 0; i < 4; i++) 
00286    {
00287       digitalWrite(_data_pins[i], (value >> i) & 0x01);
00288    }
00289    
00290    pulseEnable();
00291 }
00292 
00293 //
00294 // write8bits
00295 void LiquidCrystal_4bit::write8bits(uint8_t value) 
00296 {
00297    for (uint8_t i = 0; i < 8; i++) 
00298    {
00299       digitalWrite(_data_pins[i], (value >> i) & 0x01);
00300    }
00301    
00302    pulseEnable();
00303 }
 All Classes Functions