diff --git a/LCD.cpp b/LCD.cpp index 406ba12..06201b2 100644 --- a/LCD.cpp +++ b/LCD.cpp @@ -54,6 +54,104 @@ LCD::LCD () // PUBLIC METHODS // --------------------------------------------------------------------------- +// When the display powers up, it is configured as follows: +// +// 1. Display clear +// 2. Function set: +// DL = 1; 8-bit interface data +// N = 0; 1-line display +// F = 0; 5x8 dot character font +// 3. Display on/off control: +// D = 0; Display off +// C = 0; Cursor off +// B = 0; Blinking off +// 4. Entry mode set: +// I/D = 1; Increment by 1 +// S = 0; No shift +// +// Note, however, that resetting the Arduino doesn't reset the LCD, so we +// can't assume that its in that state when a sketch starts (and the +// LiquidCrystal constructor is called). +// A call to begin() will reinitialize the LCD. +// +void LCD::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) +{ + if (lines > 1) + { + _displayfunction |= LCD_2LINE; + } + _numlines = lines; + _cols = cols; + + // for some 1 line displays you can select a 10 pixel high font + // ------------------------------------------------------------ + if ((dotsize != 0) && (lines == 1)) + { + _displayfunction |= LCD_5x10DOTS; + } + + // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION! + // according to datasheet, we need at least 40ms after power rises above 2.7V + // before sending commands. Arduino can turn on way before 4.5V so we'll wait + // 50 + // --------------------------------------------------------------------------- + delayMicroseconds(50000); + + //put the LCD into 4 bit or 8 bit mode + // ------------------------------------- + if (! (_displayfunction & LCD_8BITMODE)) + { + // this is according to the hitachi HD44780 datasheet + // figure 24, pg 46 + + // we start in 8bit mode, try to set 4 bit mode + command ( 0x03 ); + delayMicroseconds(4500); // wait min 4.1ms + + // second try + command ( 0x03 ); + delayMicroseconds(4500); // wait min 4.1ms + + // third go! + command ( 0x03 ); + delayMicroseconds(150); + + // finally, set to 4-bit interface + command ( 0x02 ); + } + else + { + // this is according to the hitachi HD44780 datasheet + // page 45 figure 23 + + // Send function set command sequence + command(LCD_FUNCTIONSET | _displayfunction); + delayMicroseconds(4500); // wait more than 4.1ms + + // second try + command(LCD_FUNCTIONSET | _displayfunction); + delayMicroseconds(150); + + // third go + command(LCD_FUNCTIONSET | _displayfunction); + } + + // finally, set # lines, font size, etc. + command(LCD_FUNCTIONSET | _displayfunction); + + // turn the display on with no cursor or blinking default + _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; + display(); + + // clear the LCD + clear(); + + // Initialize to default text direction (for romance languages) + _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT; + // set the entry mode + command(LCD_ENTRYMODESET | _displaymode); + +} // Common LCD Commands // --------------------------------------------------------------------------- diff --git a/LCD.h b/LCD.h index 687412e..96e5fd3 100644 --- a/LCD.h +++ b/LCD.h @@ -166,12 +166,8 @@ public: @param cols[in] the number of columns that the display has @param rows[in] the number of rows that the display has */ -#if (ARDUINO < 100) - virtual void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS) { }; -#else - virtual void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS) = 0; -#endif - + virtual void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS); + /*! @function @abstract Clears the LCD. diff --git a/LiquidCrystal.cpp b/LiquidCrystal.cpp index 0dd7a6c..5e77473 100644 --- a/LiquidCrystal.cpp +++ b/LiquidCrystal.cpp @@ -75,26 +75,7 @@ LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable, // PRIVATE METHODS // --------------------------------------------------------------------------- -// When the display powers up, it is configured as follows: -// -// 1. Display clear -// 2. Function set: -// DL = 1; 8-bit interface data -// N = 0; 1-line display -// F = 0; 5x8 dot character font -// 3. Display on/off control: -// D = 0; Display off -// C = 0; Cursor off -// B = 0; Blinking off -// 4. Entry mode set: -// I/D = 1; Increment by 1 -// S = 0; No shift -// -// Note, however, that resetting the Arduino doesn't reset the LCD, so we -// can't assume that its in that state when a sketch starts (and the -// LiquidCrystal constructor is called). -// A call to begin() will reinitialize the LCD. -// + // init void LiquidCrystal::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, @@ -152,35 +133,6 @@ void LiquidCrystal::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t en _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS; else _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS; -} - -// PUBLIC METHODS -// --------------------------------------------------------------------------- - -// -// begin -void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) -{ - if (lines > 1) - { - _displayfunction |= LCD_2LINE; - } - _numlines = lines; - _cols = cols; - - // for some 1 line displays you can select a 10 pixel high font - // ------------------------------------------------------------ - if ((dotsize != 0) && (lines == 1)) - { - _displayfunction |= LCD_5x10DOTS; - } - - // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION! - // according to datasheet, we need at least 40ms after power rises above 2.7V - // before sending commands. Arduino can turn on way before 4.5V so we'll wait - // 50 - // --------------------------------------------------------------------------- - delayMicroseconds(50000); // Now we pull both RS and R/W low to begin commands digitalWrite(_rs_pin, LOW); @@ -190,63 +142,11 @@ void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) { digitalWrite(_rw_pin, LOW); } - - //put the LCD into 4 bit or 8 bit mode - // ------------------------------------- - if (! (_displayfunction & LCD_8BITMODE)) - { - // this is according to the hitachi HD44780 datasheet - // figure 24, pg 46 - - // we start in 8bit mode, try to set 4 bit mode - writeNbits(0x03, 4); - delayMicroseconds(4500); // wait min 4.1ms - - // second try - writeNbits(0x03, 4); - delayMicroseconds(4500); // wait min 4.1ms - - // third go! - writeNbits(0x03, 4); - delayMicroseconds(150); - - // finally, set to 4-bit interface - writeNbits(0x02, 4); - } - else - { - // this is according to the hitachi HD44780 datasheet - // page 45 figure 23 - - // Send function set command sequence - command(LCD_FUNCTIONSET | _displayfunction); - delayMicroseconds(4500); // wait more than 4.1ms - - // second try - command(LCD_FUNCTIONSET | _displayfunction); - delayMicroseconds(150); - - // third go - command(LCD_FUNCTIONSET | _displayfunction); - } - - // finally, set # lines, font size, etc. - command(LCD_FUNCTIONSET | _displayfunction); - - // turn the display on with no cursor or blinking default - _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; - display(); - - // clear the LCD - clear(); - - // Initialize to default text direction (for romance languages) - _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT; - // set the entry mode - command(LCD_ENTRYMODESET | _displaymode); - } +// PUBLIC METHODS +// --------------------------------------------------------------------------- + /************ low level data pushing commands **********/ // send diff --git a/LiquidCrystal.h b/LiquidCrystal.h index 0bd66b3..cd8af1b 100644 --- a/LiquidCrystal.h +++ b/LiquidCrystal.h @@ -72,20 +72,6 @@ public: LiquidCrystal(uint8_t rs, uint8_t enable, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3); - /*! - @function - @abstract LCD initialization. - @discussion Initializes the LCD to a given size (col, row). This methods - initializes the LCD, therefore, it MUST be called prior to using any other - method from this class or parent class. - - @param cols[in] the number of columns that the display has - @param rows[in] the number of rows that the display has - @param charsize: size of the characters of the LCD: LCD_5x8DOTS or - LCD_5x10DOTS. - */ - virtual void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS); - /*! @function @abstract Send a particular value to the LCD. @@ -105,7 +91,7 @@ private: /*! @method - @abstract Initializes the LCD pin allocation + @abstract Initializes the LCD pin allocation and associated HW @discussion Initializes the LCD pin allocation and configuration. */ void init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable, diff --git a/LiquidCrystal_I2C.cpp b/LiquidCrystal_I2C.cpp index 688b9cc..354f023 100755 --- a/LiquidCrystal_I2C.cpp +++ b/LiquidCrystal_I2C.cpp @@ -144,66 +144,7 @@ void LiquidCrystal_I2C::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) { init(); // Initialise the I2C expander interface - - if (lines > 1) - { - _displayfunction |= LCD_2LINE; - } - _numlines = lines; - _cols = cols; - - // for some 1 line displays you can select a 10 pixel high font - if ((dotsize != 0) && (lines == 1)) { - _displayfunction |= LCD_5x10DOTS; - } - - // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION! - // according to datasheet, we need at least 40ms after power rises above 2.7V - // before sending commands. Arduino can turn on way before 4.5V so we'll wait 50 - delayMicroseconds(50000); - - // Now we pull both RS and R/W low to begin commands - expanderWrite ( _backlightval ); // reset expander and turn backlight off (Bit 8 =1) - delay(1000); - - //put the LCD into 4 bit mode - // this is according to the hitachi HD44780 datasheet - // figure 24, pg 46 - - // we start in 8bit mode, try to set 4 bit mode - write4bits ( 0x03, LOW ); - delayMicroseconds(4500); // wait min 4.1ms - - // second try - write4bits ( 0x03, LOW ); - delayMicroseconds(4500); // wait min 4.1ms - - // third go! - write4bits ( 0x03, LOW ); - delayMicroseconds(150); - - // finally, set to 4-bit interface - write4bits ( 0x02, LOW ); - - - // set # lines, font size, etc. - command(LCD_FUNCTIONSET | _displayfunction); - - // turn the display on with no cursor or blinking default - _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; - display(); - - // clear it off - clear(); - - // Initialize to default text direction (for roman languages) - _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT; - - // set the entry mode - command(LCD_ENTRYMODESET | _displaymode); - - home(); - + LCD::begin ( cols, lines, dotsize ); } diff --git a/LiquidCrystal_I2C.h b/LiquidCrystal_I2C.h index 66f8047..834fd37 100755 --- a/LiquidCrystal_I2C.h +++ b/LiquidCrystal_I2C.h @@ -118,11 +118,15 @@ public: /*! @function - @abstract LCD initialization. + @abstract LCD initialization and associated HW. @discussion Initializes the LCD to a given size (col, row). This methods initializes the LCD, therefore, it MUST be called prior to using any other method from this class or parent class. + The begin method can be overloaded if necessary to initialize any HW that + is implemented by a library and can't be done during construction, here + we use the Wire class. + @param cols[in] the number of columns that the display has @param rows[in] the number of rows that the display has @param charsize[in] size of the characters of the LCD: LCD_5x8DOTS or diff --git a/LiquidCrystal_SR.cpp b/LiquidCrystal_SR.cpp index 7dde09a..65c7c09 100644 --- a/LiquidCrystal_SR.cpp +++ b/LiquidCrystal_SR.cpp @@ -88,29 +88,6 @@ #include -// When the display powers up, it is configured as follows: -// -// 1. Display clear -// 2. Function set: -// DL = 1; 8-bit interface data -// N = 0; 1-line display -// F = 0; 5x8 dot character font -// 3. Display on/off control: -// D = 0; Display off -// C = 0; Cursor off -// B = 0; Blinking off -// 4. Entry mode set: -// I/D = 1; Increment by 1 -// S = 0; No shift -// -// Note, however, that resetting the Arduino doesn't reset the LCD, so we -// can't assume that its in that state when a sketch starts (and the -// LiquidCrystal constructor is called). -// A call to begin() will reinitialize the LCD. - -// STATIC helper functions -// --------------------------------------------------------------------------- - // CONSTRUCTORS // --------------------------------------------------------------------------- @@ -122,7 +99,6 @@ LiquidCrystal_SR::LiquidCrystal_SR ( uint8_t srdata, uint8_t srclock, } - // PRIVATE METHODS // --------------------------------------------------------------------------- @@ -139,11 +115,14 @@ void LiquidCrystal_SR::init( uint8_t srdata, uint8_t srclock, uint8_t enable, _srClockRegister = fio_pinToOutputRegister(srclock); _srClockBit = fio_pinToBit(srclock); - if (enable == TWO_WIRE){ + if (enable == TWO_WIRE) + { _two_wire = 1; _srEnableRegister = _srDataRegister; _srEnableBit = _srDataBit; - }else{ + } + else + { _srEnableRegister = fio_pinToOutputRegister(enable); _srEnableBit = fio_pinToBit(enable); } @@ -157,58 +136,6 @@ void LiquidCrystal_SR::init( uint8_t srdata, uint8_t srclock, uint8_t enable, // PUBLIC METHODS // --------------------------------------------------------------------------- -// -// begin -void LiquidCrystal_SR::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) -{ - - if (lines > 1) - { - _displayfunction |= LCD_2LINE; - } - - _numlines = lines; - _cols = cols; - - // for some 1 line displays you can select a 10 pixel high font - // ------------------------------------------------------------ - if ((dotsize != 0) && (lines == 1)) - { - _displayfunction |= LCD_5x10DOTS; - } - - // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION! - // according to datasheet, we need at least 40ms after power rises above 2.7V - // before sending commands. Arduino can turn on way before 4.5V so we'll wait - // 50 - // --------------------------------------------------------------------------- - delayMicroseconds(50000); - write4bits(LCD_FUNCTIONSET | LCD_8BITMODE); - delayMicroseconds(4500); // wait more than 4.1ms - - // Second try - write4bits(LCD_FUNCTIONSET | LCD_8BITMODE); - delayMicroseconds(150); - // Third go - write4bits(LCD_FUNCTIONSET | LCD_8BITMODE); - - // And finally, set to 4-bit interface - write4bits(LCD_FUNCTIONSET | LCD_4BITMODE); - - // Set # lines, font size, etc. - command(LCD_FUNCTIONSET | _displayfunction); - // Turn the display on with no cursor or blinking default - _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; - display(); - // Clear it off - clear(); - // Initialize to default text direction (for romance languages) - _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT; - // set the entry mode - command(LCD_ENTRYMODESET | _displaymode); - home(); - -} /************ low level data pushing commands **********/ diff --git a/LiquidCrystal_SR.h b/LiquidCrystal_SR.h index 4d79c1e..5d37dc9 100644 --- a/LiquidCrystal_SR.h +++ b/LiquidCrystal_SR.h @@ -104,20 +104,6 @@ public: */ LiquidCrystal_SR ( uint8_t srdata, uint8_t srclock, uint8_t enable ); - /*! - @function - @abstract LCD initialization. - @discussion Initializes the LCD to a given size (col, row). This methods - initializes the LCD, therefore, it MUST be called prior to using any other - method from this class or parent class. - - @param cols[in] the number of columns that the display has - @param rows[in] the number of rows that the display has - @param charsize[in] size of the characters of the LCD: LCD_5x8DOTS or - LCD_5x10DOTS. - */ - virtual void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS); - /*! @function @abstract Send a particular value to the LCD. diff --git a/LiquidCrystal_SR_LCD3.cpp b/LiquidCrystal_SR_LCD3.cpp index 961c938..63c3dc7 100755 --- a/LiquidCrystal_SR_LCD3.cpp +++ b/LiquidCrystal_SR_LCD3.cpp @@ -168,60 +168,6 @@ void LiquidCrystal_SR_LCD3::init( uint8_t srdata, uint8_t srclock, uint8_t strob // PUBLIC METHODS // --------------------------------------------------------------------------- -// -// begin -void LiquidCrystal_SR_LCD3::begin( uint8_t cols, uint8_t lines, uint8_t dotsize ) -{ - if (lines > 1) - { - _displayfunction |= LCD_2LINE; - } - - _numlines = lines; - _cols = cols; - - // for some 1 line displays you can select a 10 pixel high font - // ------------------------------------------------------------ - if ((dotsize != 0) && (lines == 1)) - { - _displayfunction |= LCD_5x10DOTS; - } - - // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION! - // according to datasheet, we need at least 40ms after power rises above 2.7V - // before sending commands. Arduino can turn on way before 4.5V so we'll wait - // 50 - // --------------------------------------------------------------------------- - delayMicroseconds(50000); - - // This init is copied verbatim from the spec sheet. - // 8 bit codes are shifted to 4 bit - write4bits((LCD_FUNCTIONSET | LCD_8BITMODE) >> 4); - delayMicroseconds(4500); // wait more than 4.1ms - - // Second try - write4bits((LCD_FUNCTIONSET | LCD_8BITMODE) >> 4); - delayMicroseconds(150); - // Third go - write4bits((LCD_FUNCTIONSET | LCD_8BITMODE) >> 4); - - // And finally, set to 4-bit interface - write4bits((LCD_FUNCTIONSET | LCD_4BITMODE) >> 4); - - // Set # lines, font size, etc. - command(LCD_FUNCTIONSET | _displayfunction); - // Turn the display on with no cursor or blinking default - _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; - display(); - // Clear it off - clear(); - // Initialize to default text direction (for romance languages) - _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT; - // set the entry mode - command(LCD_ENTRYMODESET | _displaymode); - home(); - -} /************ low level data pushing commands **********/ diff --git a/LiquidCrystal_SR_LCD3.h b/LiquidCrystal_SR_LCD3.h index 8559639..6198325 100755 --- a/LiquidCrystal_SR_LCD3.h +++ b/LiquidCrystal_SR_LCD3.h @@ -30,21 +30,6 @@ public: */ LiquidCrystal_SR_LCD3 ( uint8_t srdata, uint8_t srclock, uint8_t enable ); - - /*! - @function - @abstract LCD initialization. - @discussion Initializes the LCD to a given size (col, row). This methods - initializes the LCD, therefore, it MUST be called prior to using any other - method from this class or parent class. - - @param cols[in] the number of columns that the display has - @param rows[in] the number of rows that the display has - @param charsize[in] size of the characters of the LCD: LCD_5x8DOTS or - LCD_5x10DOTS. - */ - virtual void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS); - /*! @function @abstract Send a particular value to the LCD.