D:/DRISSI/arduino-0022/arduino-0022/libraries/LiquidCrystal/LiquidCrystal.cpp
00001 #include "LiquidCrystal.h"
00002 
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <inttypes.h>
00006 #include "WProgram.h"
00007 
00008 // When the display powers up, it is configured as follows:
00009 //
00010 // 1. Display clear
00011 // 2. Function set: 
00012 //    DL = 1; 8-bit interface data 
00013 //    N = 0; 1-line display 
00014 //    F = 0; 5x8 dot character font 
00015 // 3. Display on/off control: 
00016 //    D = 0; Display off 
00017 //    C = 0; Cursor off 
00018 //    B = 0; Blinking off 
00019 // 4. Entry mode set: 
00020 //    I/D = 1; Increment by 1 
00021 //    S = 0; No shift 
00022 //
00023 // Note, however, that resetting the Arduino doesn't reset the LCD, so we
00024 // can't assume that its in that state when a sketch starts (and the
00025 // LiquidCrystal constructor is called).
00026 
00027 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
00028                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
00029                              uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
00030 {
00031   init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
00032 }
00033 
00034 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable,
00035                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
00036                              uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
00037 {
00038   init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7);
00039 }
00040 
00041 LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
00042                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
00043 {
00044   init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0);
00045 }
00046 
00047 LiquidCrystal::LiquidCrystal(uint8_t rs,  uint8_t enable,
00048                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
00049 {
00050   init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
00051 }
00052 
00053 void LiquidCrystal::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
00054                          uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
00055                          uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
00056 {
00057   _rs_pin = rs;
00058   _rw_pin = rw;
00059   _enable_pin = enable;
00060   
00061   _data_pins[0] = d0;
00062   _data_pins[1] = d1;
00063   _data_pins[2] = d2;
00064   _data_pins[3] = d3; 
00065   _data_pins[4] = d4;
00066   _data_pins[5] = d5;
00067   _data_pins[6] = d6;
00068   _data_pins[7] = d7; 
00069 
00070   pinMode(_rs_pin, OUTPUT);
00071   // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
00072   if (_rw_pin != 255) { 
00073     pinMode(_rw_pin, OUTPUT);
00074   }
00075   pinMode(_enable_pin, OUTPUT);
00076   
00077   if (fourbitmode)
00078     _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
00079   else 
00080     _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
00081   
00082   begin(16, 1);  
00083 }
00084 
00085 void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
00086   if (lines > 1) {
00087     _displayfunction |= LCD_2LINE;
00088   }
00089   _numlines = lines;
00090   _currline = 0;
00091 
00092   // for some 1 line displays you can select a 10 pixel high font
00093   if ((dotsize != 0) && (lines == 1)) {
00094     _displayfunction |= LCD_5x10DOTS;
00095   }
00096 
00097   // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
00098   // according to datasheet, we need at least 40ms after power rises above 2.7V
00099   // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
00100   delayMicroseconds(50000); 
00101   // Now we pull both RS and R/W low to begin commands
00102   digitalWrite(_rs_pin, LOW);
00103   digitalWrite(_enable_pin, LOW);
00104   if (_rw_pin != 255) { 
00105     digitalWrite(_rw_pin, LOW);
00106   }
00107   
00108   //put the LCD into 4 bit or 8 bit mode
00109   if (! (_displayfunction & LCD_8BITMODE)) {
00110     // this is according to the hitachi HD44780 datasheet
00111     // figure 24, pg 46
00112 
00113     // we start in 8bit mode, try to set 4 bit mode
00114     write4bits(0x03);
00115     delayMicroseconds(4500); // wait min 4.1ms
00116 
00117     // second try
00118     write4bits(0x03);
00119     delayMicroseconds(4500); // wait min 4.1ms
00120     
00121     // third go!
00122     write4bits(0x03); 
00123     delayMicroseconds(150);
00124 
00125     // finally, set to 4-bit interface
00126     write4bits(0x02); 
00127   } else {
00128     // this is according to the hitachi HD44780 datasheet
00129     // page 45 figure 23
00130 
00131     // Send function set command sequence
00132     command(LCD_FUNCTIONSET | _displayfunction);
00133     delayMicroseconds(4500);  // wait more than 4.1ms
00134 
00135     // second try
00136     command(LCD_FUNCTIONSET | _displayfunction);
00137     delayMicroseconds(150);
00138 
00139     // third go
00140     command(LCD_FUNCTIONSET | _displayfunction);
00141   }
00142 
00143   // finally, set # lines, font size, etc.
00144   command(LCD_FUNCTIONSET | _displayfunction);  
00145 
00146   // turn the display on with no cursor or blinking default
00147   _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;  
00148   display();
00149 
00150   // clear it off
00151   clear();
00152 
00153   // Initialize to default text direction (for romance languages)
00154   _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
00155   // set the entry mode
00156   command(LCD_ENTRYMODESET | _displaymode);
00157 
00158 }
00159 
00160 /********** high level commands, for the user! */
00161 void LiquidCrystal::clear()
00162 {
00163   command(LCD_CLEARDISPLAY);  // clear display, set cursor position to zero
00164   delayMicroseconds(2000);  // this command takes a long time!
00165 }
00166 
00167 void LiquidCrystal::home()
00168 {
00169   command(LCD_RETURNHOME);  // set cursor position to zero
00170   delayMicroseconds(2000);  // this command takes a long time!
00171 }
00172 
00173 void LiquidCrystal::setCursor(uint8_t col, uint8_t row)
00174 {
00175   int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
00176   if ( row > _numlines ) {
00177     row = _numlines-1;    // we count rows starting w/0
00178   }
00179   
00180   command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
00181 }
00182 
00183 // Turn the display on/off (quickly)
00184 void LiquidCrystal::noDisplay() {
00185   _displaycontrol &= ~LCD_DISPLAYON;
00186   command(LCD_DISPLAYCONTROL | _displaycontrol);
00187 }
00188 void LiquidCrystal::display() {
00189   _displaycontrol |= LCD_DISPLAYON;
00190   command(LCD_DISPLAYCONTROL | _displaycontrol);
00191 }
00192 
00193 // Turns the underline cursor on/off
00194 void LiquidCrystal::noCursor() {
00195   _displaycontrol &= ~LCD_CURSORON;
00196   command(LCD_DISPLAYCONTROL | _displaycontrol);
00197 }
00198 void LiquidCrystal::cursor() {
00199   _displaycontrol |= LCD_CURSORON;
00200   command(LCD_DISPLAYCONTROL | _displaycontrol);
00201 }
00202 
00203 // Turn on and off the blinking cursor
00204 void LiquidCrystal::noBlink() {
00205   _displaycontrol &= ~LCD_BLINKON;
00206   command(LCD_DISPLAYCONTROL | _displaycontrol);
00207 }
00208 void LiquidCrystal::blink() {
00209   _displaycontrol |= LCD_BLINKON;
00210   command(LCD_DISPLAYCONTROL | _displaycontrol);
00211 }
00212 
00213 // These commands scroll the display without changing the RAM
00214 void LiquidCrystal::scrollDisplayLeft(void) {
00215   command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
00216 }
00217 void LiquidCrystal::scrollDisplayRight(void) {
00218   command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
00219 }
00220 
00221 // This is for text that flows Left to Right
00222 void LiquidCrystal::leftToRight(void) {
00223   _displaymode |= LCD_ENTRYLEFT;
00224   command(LCD_ENTRYMODESET | _displaymode);
00225 }
00226 
00227 // This is for text that flows Right to Left
00228 void LiquidCrystal::rightToLeft(void) {
00229   _displaymode &= ~LCD_ENTRYLEFT;
00230   command(LCD_ENTRYMODESET | _displaymode);
00231 }
00232 
00233 // This will 'right justify' text from the cursor
00234 void LiquidCrystal::autoscroll(void) {
00235   _displaymode |= LCD_ENTRYSHIFTINCREMENT;
00236   command(LCD_ENTRYMODESET | _displaymode);
00237 }
00238 
00239 // This will 'left justify' text from the cursor
00240 void LiquidCrystal::noAutoscroll(void) {
00241   _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
00242   command(LCD_ENTRYMODESET | _displaymode);
00243 }
00244 
00245 // Allows us to fill the first 8 CGRAM locations
00246 // with custom characters
00247 void LiquidCrystal::createChar(uint8_t location, uint8_t charmap[]) {
00248   location &= 0x7; // we only have 8 locations 0-7
00249   command(LCD_SETCGRAMADDR | (location << 3));
00250   for (int i=0; i<8; i++) {
00251     write(charmap[i]);
00252   }
00253 }
00254 
00255 /*********** mid level commands, for sending data/cmds */
00256 
00257 inline void LiquidCrystal::command(uint8_t value) {
00258   send(value, LOW);
00259 }
00260 
00261 inline void LiquidCrystal::write(uint8_t value) {
00262   send(value, HIGH);
00263 }
00264 
00265 /************ low level data pushing commands **********/
00266 
00267 // write either command or data, with automatic 4/8-bit selection
00268 void LiquidCrystal::send(uint8_t value, uint8_t mode) {
00269   digitalWrite(_rs_pin, mode);
00270 
00271   // if there is a RW pin indicated, set it low to Write
00272   if (_rw_pin != 255) { 
00273     digitalWrite(_rw_pin, LOW);
00274   }
00275   
00276   if (_displayfunction & LCD_8BITMODE) {
00277     write8bits(value); 
00278   } else {
00279     write4bits(value>>4);
00280     write4bits(value);
00281   }
00282 }
00283 
00284 void LiquidCrystal::pulseEnable(void) {
00285   digitalWrite(_enable_pin, LOW);
00286   delayMicroseconds(1);    
00287   digitalWrite(_enable_pin, HIGH);
00288   delayMicroseconds(1);    // enable pulse must be >450ns
00289   digitalWrite(_enable_pin, LOW);
00290   delayMicroseconds(100);   // commands need > 37us to settle
00291 }
00292 
00293 void LiquidCrystal::write4bits(uint8_t value) {
00294   for (int i = 0; i < 4; i++) {
00295     pinMode(_data_pins[i], OUTPUT);
00296     digitalWrite(_data_pins[i], (value >> i) & 0x01);
00297   }
00298 
00299   pulseEnable();
00300 }
00301 
00302 void LiquidCrystal::write8bits(uint8_t value) {
00303   for (int i = 0; i < 8; i++) {
00304     pinMode(_data_pins[i], OUTPUT);
00305     digitalWrite(_data_pins[i], (value >> i) & 0x01);
00306   }
00307   
00308   pulseEnable();
00309 }