LCD Library 1.1.7
LCD Library - LCD control class hierarchy library. Drop in replacement for the LiquidCrystal Library.
/Users/fmalpartida/development/ardWorkspace/LiquidCrystal_I2C/LiquiCrystal_I2C/LiquidCrystal_I2C.cpp
Go to the documentation of this file.
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_I2C.c
00013 // This file implements a basic liquid crystal library that comes as standard
00014 // in the Arduino SDK but using an I2C IO 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 I2C extension
00021 // backpacks such as the I2CLCDextraIO with the PCF8574* I2C IO Expander ASIC.
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 #if (ARDUINO <  100)
00033 #include <WProgram.h>
00034 #else
00035 #include <Arduino.h>
00036 #endif
00037 #include <inttypes.h>
00038 #include <I2CIO.h>
00039 #include <LiquidCrystal_I2C.h>
00040 
00041 
00042 
00043 // When the display powers up, it is configured as follows:
00044 //
00045 // 1. Display clear
00046 // 2. Function set: 
00047 //    DL = 1; 8-bit interface data 
00048 //    N = 0; 1-line display 
00049 //    F = 0; 5x8 dot character font 
00050 // 3. Display on/off control: 
00051 //    D = 0; Display off 
00052 //    C = 0; Cursor off 
00053 //    B = 0; Blinking off 
00054 // 4. Entry mode set: 
00055 //    I/D = 1; Increment by 1
00056 //    S = 0; No shift 
00057 //
00058 // Note, however, that resetting the Arduino doesn't reset the LCD, so we
00059 // can't assume that its in that state when a sketch starts (and the
00060 // LiquidCrystal_I2C constructor is called).
00061 // A call to begin() will reinitialize the LCD.
00062 
00063 // CONSTRUCTORS
00064 // ---------------------------------------------------------------------------
00065 LiquidCrystal_I2C::LiquidCrystal_I2C( uint8_t lcd_Addr )
00066 {
00067    _Addr = lcd_Addr;
00068    
00069    _backlightval = LCD_NOBACKLIGHT;
00070    _En = EN;
00071    _Rw = RW;
00072    _Rs = RS;
00073    
00074    // Initialise default values data[0] pin 0, data[1] pin 1, ...
00075    for ( uint8_t i = 0; i < 4; i++ )
00076    {
00077       _data_pins[i] = ( 1 << i );
00078    }
00079 }
00080 
00081 LiquidCrystal_I2C::LiquidCrystal_I2C( uint8_t lcd_Addr, uint8_t En, uint8_t Rw,
00082                                      uint8_t Rs)
00083 {
00084    _Addr = lcd_Addr;
00085    
00086    _backlightval = LCD_NOBACKLIGHT;
00087    _En = ( 1 << En );
00088    _Rw = ( 1 << Rw );
00089    _Rs = ( 1 << Rs );
00090    
00091    // Initialise default values data[0] pin 0, data[1] pin 1, ...
00092    for ( uint8_t i = 0; i < 4; i++ )
00093    {
00094       _data_pins[i] = ( 1 << i );
00095    }
00096 }
00097 
00098 LiquidCrystal_I2C::LiquidCrystal_I2C( uint8_t lcd_Addr, uint8_t En, uint8_t Rw,
00099                                      uint8_t Rs, uint8_t d0, uint8_t d1,
00100                                      uint8_t d2, uint8_t d3 )
00101 {
00102    _Addr = lcd_Addr;
00103    
00104    _backlightval = LCD_NOBACKLIGHT;
00105    _En = ( 1 << En );
00106    _Rw = ( 1 << Rw );
00107    _Rs = ( 1 << Rs );
00108    
00109    // Initialise pin mapping
00110    _data_pins[0] = ( 1 << d0 );
00111    _data_pins[1] = ( 1 << d1 );
00112    _data_pins[2] = ( 1 << d2 );
00113    _data_pins[3] = ( 1 << d3 );
00114 }
00115 
00116 // PRIVATE METHODS
00117 // ---------------------------------------------------------------------------
00118 
00119 //
00120 // init
00121 int LiquidCrystal_I2C::init()
00122 {
00123    int status = 0;
00124    
00125    // initialize the backpack IO expander
00126    // and display functions.
00127    // ------------------------------------------------------------------------
00128    if ( _i2cio.begin ( _Addr ) == 1 )
00129    {
00130       _i2cio.portMode ( OUTPUT );  // Set the entire IO extender to OUTPUT
00131       _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
00132       status = 1;
00133    }
00134    return ( status );
00135 }
00136 
00137 
00138 // PUBLIC METHODS
00139 // ---------------------------------------------------------------------------
00140 
00141 //
00142 // begin
00143 void LiquidCrystal_I2C::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) 
00144 {
00145    
00146    init();     // Initialise the I2C expander interface
00147    
00148    if (lines > 1) 
00149    {
00150       _displayfunction |= LCD_2LINE;
00151    }
00152    _numlines = lines;
00153    _cols = cols;
00154    
00155    // for some 1 line displays you can select a 10 pixel high font
00156    if ((dotsize != 0) && (lines == 1)) {
00157       _displayfunction |= LCD_5x10DOTS;
00158    }
00159    
00160    // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
00161    // according to datasheet, we need at least 40ms after power rises above 2.7V
00162    // before sending commands. Arduino can turn on way before 4.5V so we'll wait 50
00163    delayMicroseconds(50000); 
00164    
00165    // Now we pull both RS and R/W low to begin commands
00166    expanderWrite ( _backlightval );   // reset expander and turn backlight off (Bit 8 =1)
00167    delay(1000);
00168    
00169    //put the LCD into 4 bit mode
00170    // this is according to the hitachi HD44780 datasheet
00171    // figure 24, pg 46
00172    
00173    // we start in 8bit mode, try to set 4 bit mode
00174    write4bits ( 0x03, LOW );
00175    delayMicroseconds(4500); // wait min 4.1ms
00176    
00177    // second try
00178    write4bits ( 0x03, LOW );
00179    delayMicroseconds(4500); // wait min 4.1ms
00180    
00181    // third go!
00182    write4bits ( 0x03, LOW ); 
00183    delayMicroseconds(150);
00184    
00185    // finally, set to 4-bit interface
00186    write4bits ( 0x02, LOW ); 
00187    
00188    
00189    // set # lines, font size, etc.
00190    command(LCD_FUNCTIONSET | _displayfunction);  
00191    
00192    // turn the display on with no cursor or blinking default
00193    _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
00194    display();
00195    
00196    // clear it off
00197    clear();
00198    
00199    // Initialize to default text direction (for roman languages)
00200    _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
00201    
00202    // set the entry mode
00203    command(LCD_ENTRYMODESET | _displaymode);
00204    
00205    home();
00206    
00207 }
00208 
00209 
00210 // User commands - users can expand this section
00211 //----------------------------------------------------------------------------
00212 
00213 // Turn the (optional) backlight off/on
00214 void LiquidCrystal_I2C::noBacklight(void) 
00215 {
00216    _backlightval=LCD_NOBACKLIGHT;
00217    expanderWrite(0);
00218 }
00219 
00220 void LiquidCrystal_I2C::backlight(void) 
00221 {
00222    _backlightval=LCD_BACKLIGHT;
00223    expanderWrite(0);
00224 }
00225 
00226 // PRIVATE METHODS
00227 // ---------------------------------------------------------------------------
00228 
00229 // low level data pushing commands
00230 //----------------------------------------------------------------------------
00231 
00232 //
00233 // send - write either command or data
00234 void LiquidCrystal_I2C::send(uint8_t value, uint8_t mode) 
00235 {
00236    // No need to use the delay routines since the time taken to write takes
00237    // longer that what is needed both for toggling and enable pin an to execute
00238    // the command.
00239    write4bits( (value >> 4), mode );
00240    write4bits( (value & 0x0F), mode);
00241 }
00242 
00243 //
00244 // write4bits
00245 void LiquidCrystal_I2C::write4bits ( uint8_t value, uint8_t mode ) 
00246 {
00247    uint8_t pinMapValue = 0;
00248    
00249    // Map the value to LCD pin mapping
00250    // --------------------------------
00251    for ( uint8_t i = 0; i < 4; i++ )
00252    {
00253       if ( ( value & 0x1 ) == 1 )
00254       {
00255          pinMapValue |= _data_pins[i];
00256       }
00257       value = ( value >> 1 );
00258    }
00259    
00260    // Is it a command or data
00261    // -----------------------
00262    if ( mode == DATA )
00263    {
00264       mode = _Rs;
00265    }
00266    
00267    expanderWrite ( pinMapValue | mode );
00268    pulseEnable ( pinMapValue | mode );
00269 }
00270 
00271 //
00272 // write4bits
00273 void LiquidCrystal_I2C::pulseEnable (uint8_t _data)
00274 {
00275    expanderWrite (_data | _En);   // En HIGH
00276    expanderWrite(_data & ~_En);   // En LOW
00277 } 
00278 
00279 //
00280 // expanderWrite
00281 void LiquidCrystal_I2C::expanderWrite(uint8_t _data)
00282 {                                        
00283    _i2cio.write ( _data );
00284 }
00285 
00286 
00287 
00288 
 All Classes Files Functions Variables Defines