Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: STM32_ADC_InternalChannels tareabrayan2
Revision 22:35742ec80c24, committed 2014-04-02
- Comitter:
- wim
- Date:
- Wed Apr 02 17:49:55 2014 +0000
- Parent:
- 21:9eb628d9e164
- Child:
- 23:d47f226efb24
- Commit message:
- Refactored TextLCD bus version to fix issue with pins default defined as NC.; Note: I2C and SPI versions now need to be declared as TextLCD_I2C() or TextLCD_SPI() in user code.
Changed in this revision
| TextLCD.cpp | Show annotated file Show diff for this revision Revisions of this file |
| TextLCD.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/TextLCD.cpp Tue Apr 01 21:30:25 2014 +0000
+++ b/TextLCD.cpp Wed Apr 02 17:49:55 2014 +0000
@@ -7,7 +7,7 @@
* 2013, v05: WH, Added support for 8x2B, added some UDCs
* 2013, v06: WH, Added support for devices that use internal DC/DC converters
* 2013, v07: WH, Added support for backlight and include portdefinitions for LCD2004 Module from DFROBOT
- * 2014, v08: WH, Refactored in Base in Derived Classes to deal with mbed lib change regarding 'NC' defined pins
+ * 2014, v08: WH, Refactored in Base and Derived Classes to deal with mbed lib change regarding 'NC' defined pins
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -723,9 +723,11 @@
}
+///--------- End TextLCD_Base -----------
-///-------------------------------------------------------------------
+
+///--------- Start TextLCD Bus -----------
/* Create a TextLCD interface for using regular mbed pins
*
@@ -741,29 +743,56 @@
PinName d4, PinName d5, PinName d6, PinName d7,
LCDType type, PinName bl, PinName e2, LCDCtrl ctrl) :
TextLCD_Base(type, ctrl),
- _rs(rs), _e(e), _bl(bl), _e2(e2),
- _d(d4, d5, d6, d7) {
+ _rs(rs), _e(e), _d(d4, d5, d6, d7) {
+
+ // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
+ if (bl != NC) {
+ _bl = new DigitalOut(bl); //Construct new pin
+ _bl->write(0); //Deactivate
+ }
+ else {
+ // No Hardware Backlight pin
+ _bl = NULL; //Construct dummy pin
+ }
+
+ // The hardware Enable2 pin is only needed for LCD40x4. Test and make sure whether it exists or not to prevent illegal access.
+ if (e2 != NC) {
+ _e2 = new DigitalOut(e2); //Construct new pin
+ _e2->write(0); //Deactivate
+ }
+ else {
+ // No Hardware Enable pin
+ _e2 = NULL; //Construct dummy pin
+ }
_init();
}
-// Set E pin (or E2 pin)
-// Used for mbed pins, I2C bus expander or SPI shiftregister
+/** Set E pin (or E2 pin)
+ * Used for mbed pins, I2C bus expander or SPI shiftregister
+ * Default PinName value for E2 is NC, must be used as pointer to avoid issues with mbed lib and DigitalOut pins
+ * @param value true or false
+ * @return none
+ */
void TextLCD::_setEnable(bool value) {
- if(_ctrl_idx==_LCDCtrl_0) {
- if (value)
- _e = 1; // Set E bit
- else
- _e = 0; // Reset E bit
- }
- else {
- if (value)
- _e2 = 1; // Set E2 bit
- else
- _e2 = 0; // Reset E2 bit
- }
+ if(_ctrl_idx==_LCDCtrl_0) {
+ if (value) {
+ _e = 1; // Set E bit
+ }
+ else {
+ _e = 0; // Reset E bit
+ }
+ }
+ else {
+ if (value) {
+ if (_e2 != NULL) {_e2->write(1);} //Set E2 bit
+ }
+ else {
+ if (_e2 != NULL) {_e2->write(0);} //Reset E2 bit
+ }
+ }
}
@@ -771,50 +800,66 @@
// Used for mbed pins, I2C bus expander or SPI shiftregister
void TextLCD::_setRS(bool value) {
- if (value)
+ if (value) {
_rs = 1; // Set RS bit
- else
+ }
+ else {
_rs = 0; // Reset RS bit
+ }
}
-// Set BL pin
-// Used for mbed pins, I2C bus expander or SPI shiftregister
+/** Set BL pin
+ * Used for mbed pins, I2C bus expander or SPI shiftregister
+ * Default PinName value is NC, must be used as pointer to avoid issues with mbed lib and DigitalOut pins
+ * @param value true or false
+ * @return none
+ */
void TextLCD::_setBL(bool value) {
- if (value)
- _bl = 1; // Set BL bit
- else
- _bl = 0; // Reset BL bit
+ if (value) {
+ if (_bl != NULL) {_bl->write(1);} //Set BL bit
+ }
+ else {
+ if (_bl != NULL) {_bl->write(0);} //Reset BL bit
+ }
}
-
-
// Place the 4bit data on the databus
// Used for mbed pins, I2C bus expander or SPI shifregister
void TextLCD::_setData(int value) {
_d = value & 0x0F; // Write Databits
}
-
+/** Destruct a TextLCD interface for using regular mbed pins
+ *
+ * @param none
+ * @return none
+ */
+TextLCD::~TextLCD() {
+ if (_bl != NULL) {delete _bl;} // BL pin
+ if (_e2 != NULL) {delete _e2;} // E2 pin
+}
-
-///----------------------------------------------------------------------------------------
+
+///----------- End TextLCD ---------------
-/* Create a TextLCD interface using an I2C PC8574 portexpander
- *
- * @param i2c I2C Bus
- * @param deviceAddress I2C slave address (PCF8574)
- * @param type Sets the panel size/addressing mode (default = LCD16x2)
- * @param ctrl LCD controller (default = HD44780)
- */
+///--------- Start TextLCD_I2C -----------
+
+/** Create a TextLCD interface using an I2C PC8574 or PCF8574A portexpander
+ *
+ * @param i2c I2C Bus
+ * @param deviceAddress I2C slave address (PCF8574 or PCF8574A, default = 0x40)
+ * @param type Sets the panel size/addressing mode (default = LCD16x2)
+ * @param ctrl LCD controller (default = HD44780)
+ */
TextLCD_I2C::TextLCD_I2C(I2C *i2c, char deviceAddress, LCDType type, LCDCtrl ctrl) :
TextLCD_Base(type, ctrl),
_i2c(i2c){
- _slaveAddress = deviceAddress;
+ _slaveAddress = deviceAddress & 0xFE;
// Init the portexpander bus
_lcd_bus = D_LCD_BUS_DEF;
@@ -830,21 +875,21 @@
// Used for mbed pins, I2C bus expander or SPI shiftregister
void TextLCD_I2C::_setEnable(bool value) {
- if(_ctrl_idx==_LCDCtrl_0) {
- if (value)
- _lcd_bus |= D_LCD_E; // Set E bit
- else
- _lcd_bus &= ~D_LCD_E; // Reset E bit
- }
- else {
- if (value)
- _lcd_bus |= D_LCD_E2; // Set E2 bit
- else
- _lcd_bus &= ~D_LCD_E2; // Reset E2bit
- }
+ if(_ctrl_idx==_LCDCtrl_0) {
+ if (value)
+ _lcd_bus |= D_LCD_E; // Set E bit
+ else
+ _lcd_bus &= ~D_LCD_E; // Reset E bit
+ }
+ else {
+ if (value)
+ _lcd_bus |= D_LCD_E2; // Set E2 bit
+ else
+ _lcd_bus &= ~D_LCD_E2; // Reset E2bit
+ }
- // write the new data to the I2C portexpander
- _i2c->write(_slaveAddress, &_lcd_bus, 1);
+ // write the new data to the I2C portexpander
+ _i2c->write(_slaveAddress, &_lcd_bus, 1);
}
@@ -852,13 +897,13 @@
// Used for mbed pins, I2C bus expander or SPI shiftregister
void TextLCD_I2C::_setRS(bool value) {
- if (value)
- _lcd_bus |= D_LCD_RS; // Set RS bit
- else
- _lcd_bus &= ~D_LCD_RS; // Reset RS bit
+ if (value)
+ _lcd_bus |= D_LCD_RS; // Set RS bit
+ else
+ _lcd_bus &= ~D_LCD_RS; // Reset RS bit
- // write the new data to the I2C portexpander
- _i2c->write(_slaveAddress, &_lcd_bus, 1);
+ // write the new data to the I2C portexpander
+ _i2c->write(_slaveAddress, &_lcd_bus, 1);
}
@@ -882,55 +927,48 @@
// Used for mbed pins, I2C bus expander or SPI shifregister
void TextLCD_I2C::_setData(int value) {
int data;
+
+ // Set bit by bit to support any mapping of expander portpins to LCD pins
- data = value & 0x0F;
- if (data & 0x01)
- _lcd_bus |= D_LCD_D4; // Set Databit
- else
- _lcd_bus &= ~D_LCD_D4; // Reset Databit
+ data = value & 0x0F;
+ if (data & 0x01)
+ _lcd_bus |= D_LCD_D4; // Set Databit
+ else
+ _lcd_bus &= ~D_LCD_D4; // Reset Databit
- if (data & 0x02)
- _lcd_bus |= D_LCD_D5; // Set Databit
- else
- _lcd_bus &= ~D_LCD_D5; // Reset Databit
+ if (data & 0x02)
+ _lcd_bus |= D_LCD_D5; // Set Databit
+ else
+ _lcd_bus &= ~D_LCD_D5; // Reset Databit
- if (data & 0x04)
- _lcd_bus |= D_LCD_D6; // Set Databit
- else
- _lcd_bus &= ~D_LCD_D6; // Reset Databit
+ if (data & 0x04)
+ _lcd_bus |= D_LCD_D6; // Set Databit
+ else
+ _lcd_bus &= ~D_LCD_D6; // Reset Databit
- if (data & 0x08)
- _lcd_bus |= D_LCD_D7; // Set Databit
- else
- _lcd_bus &= ~D_LCD_D7; // Reset Databit
+ if (data & 0x08)
+ _lcd_bus |= D_LCD_D7; // Set Databit
+ else
+ _lcd_bus &= ~D_LCD_D7; // Reset Databit
- // write the new data to the I2C portexpander
- _i2c->write(_slaveAddress, &_lcd_bus, 1);
-
+ // write the new data to the I2C portexpander
+ _i2c->write(_slaveAddress, &_lcd_bus, 1);
+
+}
-}
+///---------- End TextLCD_I2C ------------
-
-
-///------------------------------------------------------------------------------------
-
-
-
-
-
+///--------- Start TextLCD_SPI -----------
-// TextLCD_SPI Implementation
-
-
- /* Create a TextLCD interface using an SPI 74595 portexpander
- *
- * @param spi SPI Bus
- * @param cs chip select pin (active low)
- * @param type Sets the panel size/addressing mode (default = LCD16x2)
- * @param ctrl LCD controller (default = HD44780)
- */
+ /** Create a TextLCD interface using an SPI 74595 portexpander
+ *
+ * @param spi SPI Bus
+ * @param cs chip select pin (active low)
+ * @param type Sets the panel size/addressing mode (default = LCD16x2)
+ * @param ctrl LCD controller (default = HD44780)
+ */
TextLCD_SPI::TextLCD_SPI(SPI *spi, PinName cs, LCDType type, LCDCtrl ctrl) :
TextLCD_Base(type, ctrl),
_spi(spi),
@@ -959,23 +997,23 @@
// Used for mbed pins, I2C bus expander or SPI shiftregister
void TextLCD_SPI::_setEnable(bool value) {
- if(_ctrl_idx==_LCDCtrl_0) {
- if (value)
- _lcd_bus |= D_LCD_E; // Set E bit
- else
- _lcd_bus &= ~D_LCD_E; // Reset E bit
- }
- else {
- if (value)
- _lcd_bus |= D_LCD_E2; // Set E2 bit
- else
- _lcd_bus &= ~D_LCD_E2; // Reset E2 bit
- }
+ if(_ctrl_idx==_LCDCtrl_0) {
+ if (value)
+ _lcd_bus |= D_LCD_E; // Set E bit
+ else
+ _lcd_bus &= ~D_LCD_E; // Reset E bit
+ }
+ else {
+ if (value)
+ _lcd_bus |= D_LCD_E2; // Set E2 bit
+ else
+ _lcd_bus &= ~D_LCD_E2; // Reset E2 bit
+ }
- // write the new data to the SPI portexpander
- _setCS(false);
- _spi->write(_lcd_bus);
- _setCS(true);
+ // write the new data to the SPI portexpander
+ _setCS(false);
+ _spi->write(_lcd_bus);
+ _setCS(true);
}
@@ -983,10 +1021,12 @@
// Used for mbed pins, I2C bus expander or SPI shiftregister
void TextLCD_SPI::_setRS(bool value) {
- if (value)
+ if (value) {
_lcd_bus |= D_LCD_RS; // Set RS bit
- else
+ }
+ else {
_lcd_bus &= ~D_LCD_RS; // Reset RS bit
+ }
// write the new data to the SPI portexpander
_setCS(false);
@@ -999,10 +1039,12 @@
// Used for mbed pins, I2C bus expander or SPI shiftregister
void TextLCD_SPI::_setBL(bool value) {
- if (value)
+ if (value) {
_lcd_bus |= D_LCD_BL; // Set BL bit
- else
+ }
+ else {
_lcd_bus &= ~D_LCD_BL; // Reset BL bit
+ }
// write the new data to the SPI portexpander
_setCS(false);
@@ -1017,32 +1059,34 @@
// Used for mbed pins, I2C bus expander or SPI shiftregister
void TextLCD_SPI::_setData(int value) {
int data;
-
- data = value & 0x0F;
- if (data & 0x01)
- _lcd_bus |= D_LCD_D4; // Set Databit
- else
- _lcd_bus &= ~D_LCD_D4; // Reset Databit
- if (data & 0x02)
- _lcd_bus |= D_LCD_D5; // Set Databit
- else
- _lcd_bus &= ~D_LCD_D5; // Reset Databit
+ // Set bit by bit to support any mapping of expander portpins to LCD pins
+
+ data = value & 0x0F;
+ if (data & 0x01)
+ _lcd_bus |= D_LCD_D4; // Set Databit
+ else
+ _lcd_bus &= ~D_LCD_D4; // Reset Databit
+
+ if (data & 0x02)
+ _lcd_bus |= D_LCD_D5; // Set Databit
+ else
+ _lcd_bus &= ~D_LCD_D5; // Reset Databit
- if (data & 0x04)
- _lcd_bus |= D_LCD_D6; // Set Databit
- else
- _lcd_bus &= ~D_LCD_D6; // Reset Databit
+ if (data & 0x04)
+ _lcd_bus |= D_LCD_D6; // Set Databit
+ else
+ _lcd_bus &= ~D_LCD_D6; // Reset Databit
- if (data & 0x08)
- _lcd_bus |= D_LCD_D7; // Set Databit
- else
- _lcd_bus &= ~D_LCD_D7; // Reset Databit
+ if (data & 0x08)
+ _lcd_bus |= D_LCD_D7; // Set Databit
+ else
+ _lcd_bus &= ~D_LCD_D7; // Reset Databit
- // write the new data to the SPI portexpander
- _setCS(false);
- _spi->write(_lcd_bus);
- _setCS(true);
+ // write the new data to the SPI portexpander
+ _setCS(false);
+ _spi->write(_lcd_bus);
+ _setCS(true);
}
@@ -1054,18 +1098,17 @@
if (value) {
_cs = 1; // Set CS pin
}
- else
+ else {
_cs = 0; // Reset CS pin
-
+ }
}
+///---------- End TextLCD_SPI ------------
+
+
-
-
-
-
--- a/TextLCD.h Tue Apr 01 21:30:25 2014 +0000
+++ b/TextLCD.h Wed Apr 02 17:49:55 2014 +0000
@@ -37,7 +37,7 @@
/** A TextLCD interface for driving 4-bit HD44780-based LCDs
*
* Currently supports 8x1, 8x2, 12x4, 16x1, 16x2, 16x4, 20x2, 20x4, 24x2, 24x4, 40x2 and 40x4 panels
- * Interface options include direct mbed pins, I2C portexpander (PCF8474) or SPI bus shiftregister (74595)
+ * Interface options include direct mbed pins, I2C portexpander (PCF8474, PCF8574A) or SPI bus shiftregister (74595)
* Supports some controllers that provide internal DC/DC converters for VLCD or VLED.
*
* @code
@@ -50,9 +50,9 @@
* // SPI Communication
* SPI spi_lcd(p5, NC, p7); // MOSI, MISO, SCLK
*
- * TextLCD lcd(p15, p16, p17, p18, p19, p20); // RS, E, D4-D7, LCDType=LCD16x2, BL=NC, E2=NC, LCDTCtrl=HD44780
- * //TextLCD_SPI lcd(&spi_lcd, p8, TextLCD::LCD40x4); // SPI bus, CS pin, LCD Type
- * //TextLCD_I2C lcd(&i2c_lcd, 0x42, TextLCD::LCD20x4); // I2C bus, PCF8574 Slaveaddress, LCD Type
+ * //TextLCD lcd(p15, p16, p17, p18, p19, p20); // RS, E, D4-D7, LCDType=LCD16x2, BL=NC, E2=NC, LCDTCtrl=HD44780
+ * //TextLCD_SPI lcd(&spi_lcd, p8, TextLCD::LCD40x4); // SPI bus, CS pin, LCD Type
+ * TextLCD_I2C lcd(&i2c_lcd, 0x42, TextLCD::LCD20x4); // I2C bus, PCF8574 Slaveaddress, LCD Type
* //TextLCD_I2C lcd(&i2c_lcd, 0x42, TextLCD::LCD16x2, TextLCD::WS0010); // I2C bus, PCF8574 Slaveaddress, LCD Type, Device Type
*
* int main() {
@@ -161,7 +161,8 @@
/** A TextLCD interface for driving 4-bit HD44780-based LCDs
*
- * Currently supports 8x1, 8x2, 12x2, 12x4, 16x1, 16x2, 16x4, 20x2, 20x4, 24x2, 24x4, 40x2 and 40x4 panels
+ * @brief Currently supports 8x1, 8x2, 12x2, 12x4, 16x1, 16x2, 16x4, 20x2, 20x4, 24x2, 24x4, 40x2 and 40x4 panels
+ * Interface options include direct mbed pins, I2C portexpander (PCF8474, PCF8574A) or SPI bus shiftregister (74595)
*
*/
class TextLCD_Base : public Stream {
@@ -194,8 +195,6 @@
};
-
-
/** LCD Cursor control */
enum LCDCursor {
CurOff_BlkOff = 0x00, /**< Cursor Off, Blinking Char Off */
@@ -258,7 +257,8 @@
void setAddress(int column, int row);
- /** Clear the screen and locate to 0,0 */
+ /** Clear the screen and locate to 0,0
+ */
void cls();
/** Return the number of rows
@@ -365,6 +365,11 @@
LCDCursor _currentCursor;
};
+///--------- End TextLCD_Base -----------
+
+
+
+///--------- Start TextLCD Bus -----------
/** Create a TextLCD interface for using regular mbed pins
*
@@ -383,6 +388,14 @@
*/
TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type = LCD16x2, PinName bl = NC, PinName e2 = NC, LCDCtrl ctrl = HD44780);
+
+ /** Destruct a TextLCD interface for using regular mbed pins
+ *
+ * @param none
+ * @return none
+ */
+ virtual ~TextLCD();
+
private:
//Low level writes to LCD Bus (serial or parallel)
virtual void _setEnable(bool value);
@@ -390,26 +403,37 @@
virtual void _setBL(bool value);
virtual void _setData(int value);
-// Regular mbed pins bus
- DigitalOut _rs, _e, _bl, _e2;
- BusOut _d;
+/** Regular mbed pins bus
+ */
+ DigitalOut _rs, _e;
+ BusOut _d;
+
+/** Optional Hardware pins for the Backlight and LCD40x4 device
+ * Default PinName value is NC, must be used as pointer to avoid issues with mbed lib and DigitalOut pins
+ */
+ DigitalOut *_bl, *_e2;
};
+
+///----------- End TextLCD ---------------
-/** Create a TextLCD interface using an I2C PC8574 portexpander
+///--------- Start TextLCD_I2C -----------
+
+
+/** Create a TextLCD interface using an I2C PC8574 or PCF8574A portexpander
*
*/
class TextLCD_I2C : public TextLCD_Base {
public:
- /** Create a TextLCD interface using an I2C PC8574 portexpander
+ /** Create a TextLCD interface using an I2C PC8574 or PCF8574A portexpander
*
* @param i2c I2C Bus
- * @param deviceAddress I2C slave address (PCF8574)
+ * @param deviceAddress I2C slave address (PCF8574 or PCF8574A, default = 0x40)
* @param type Sets the panel size/addressing mode (default = LCD16x2)
* @param ctrl LCD controller (default = HD44780)
*/
- TextLCD_I2C(I2C *i2c, char deviceAddress, LCDType type = LCD16x2, LCDCtrl ctrl = HD44780);
+ TextLCD_I2C(I2C *i2c, char deviceAddress = 0x40, LCDType type = LCD16x2, LCDCtrl ctrl = HD44780);
private:
//Low level writes to LCD Bus (serial or parallel)
@@ -428,6 +452,12 @@
};
+///---------- End TextLCD_I2C ------------
+
+
+
+///--------- Start TextLCD_SPI -----------
+
/** Create a TextLCD interface using an SPI 74595 portexpander
*
@@ -464,5 +494,6 @@
};
+///---------- End TextLCD_SPI ------------
#endif