Programa que muestra el mensaje Hello World en una LCD utilizando la libreria TextLCD en una tarjeta Nucleo F302

Dependencies:   mbed

Fork of TextLCD by Wim Huiskamp

Files at this revision

API Documentation at this revision

Comitter:
wim
Date:
Wed Feb 20 19:37:53 2013 +0000
Parent:
15:b70ebfffb258
Child:
17:652ab113bc2e
Commit message:
Cleaned up and Commented

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 Feb 19 22:09:09 2013 +0000
+++ b/TextLCD.cpp	Wed Feb 20 19:37:53 2013 +0000
@@ -220,8 +220,7 @@
 }
 
 
- 
-#if(LCD40x4Test)
+// Clear the screen, Cursor home. 
 void TextLCD::cls() {
 
   // Select and configure second LCD controller when needed
@@ -256,17 +255,7 @@
   _column=0;
 }
 
-#else
-//standard
-void TextLCD::cls() {
-    _writeCommand(0x01); // cls, and set cursor to 0
-
-    wait_ms(10);     // The CLS command takes 1.64 ms.
-                     // Since we are not using the Busy flag, Lets be safe and take 10 ms
-    locate(0, 0);
-}
-#endif
-
+// Move cursor to selected row and column
 void TextLCD::locate(int column, int row) {
     
    // setAddress() does all the heavy lifting:
@@ -279,17 +268,7 @@
 }
     
 
-//Not needed in new version, is now part of _putc()
-void TextLCD::_character(int column, int row, int c) {
-  int addr = getAddress(column, row);
-    
-  _writeCommand(0x80 | addr);
-  _writeData(c);
-}
-
-
-#if(LCD40x4Test)
-
+// Write a single character (Stream implementation)
 int TextLCD::_putc(int value) {
   int addr;
     
@@ -324,46 +303,19 @@
             
     return value;
 }
-#else
-//Standard
-int TextLCD::_putc(int value) {
-    if (value == '\n') {
-        _column = 0;
-        _row++;
-        if (_row >= rows()) {
-            _row = 0;
-        }
-    } else {
-        _character(_column, _row, value);
-        _column++;
-        if (_column >= columns()) {
-            _column = 0;
-            _row++;
-            if (_row >= rows()) {
-                _row = 0;
-            }
-        }
-    }
-       
-    return value;
-}
-
-#endif
 
 
-
-
-
+// get a single character (Stream implementation)
 int TextLCD::_getc() {
     return -1;
 }
 
-
+// Set E pin (or E2 pin)
+// Used for mbed pins, I2C bus expander or SPI shifregister
 void TextLCD::_setEnable(bool value) {
 
   switch(_busType) {
     case _PinBus : 
-#if(LCD40x4Test)
                     if(_ctrl==TextLCD::_LCDCtrl_0) {
                       if (value)
                         _e  = 1;    // Set E bit 
@@ -377,17 +329,10 @@
                         _e2 = 0;    // Reset E2 bit  
                     }    
 
-#else
-                    if (value)
-                      _e  = 1;    // Set E bit 
-                    else  
-                      _e  = 0;    // Reset E bit  
-#endif
                     break;  
     
     case _I2CBus : 
     
-#if(LCD40x4Test)
                    if(_ctrl==TextLCD::_LCDCtrl_0) {
                      if (value)
                        _lcd_bus |= D_LCD_E;     // Set E bit 
@@ -401,20 +346,12 @@
                        _lcd_bus &= ~D_LCD_E2;   // Reset E2bit                     
                    }    
 
-#else
-                   if (value)
-                     _lcd_bus |= D_LCD_E;     // Set E bit 
-                   else                     
-                     _lcd_bus &= ~D_LCD_E;    // Reset E bit                     
-
-#endif                   
                    // write the new data to the I2C portexpander
                    _i2c->write(_slaveAddress, &_lcd_bus, 1);    
 
                    break;  
     
     case _SPIBus :
-#if(LCD40x4Test)
                    if(_ctrl==TextLCD::_LCDCtrl_0) {
                      if (value)
                        _lcd_bus |= D_LCD_E;     // Set E bit 
@@ -428,13 +365,6 @@
                        _lcd_bus &= ~D_LCD_E2;   // Reset E2 bit                     
                    }
                   
-#else    
-                   if (value)
-                     _lcd_bus |= D_LCD_E;     // Set E bit 
-                   else                     
-                     _lcd_bus &= ~D_LCD_E;    // Reset E bit                     
-#endif         
-
                    // write the new data to the SPI portexpander
                    _setCS(false);  
                    _spi->write(_lcd_bus);   
@@ -444,6 +374,8 @@
   }
 }    
 
+// Set RS pin
+// Used for mbed pins, I2C bus expander or SPI shifregister
 void TextLCD::_setRS(bool value) {
 
   switch(_busType) {
@@ -482,6 +414,8 @@
 
 }    
 
+// Place the 4bit data on the databus
+// Used for mbed pins, I2C bus expander or SPI shifregister
 void TextLCD::_setData(int value) {
   int data;
   
@@ -552,7 +486,8 @@
 }    
 
 
-// Set CS line. Only used for SPI bus
+// Set CS line.
+// Only used for SPI bus
 void TextLCD::_setCS(bool value) {
 
   if (value) {   
@@ -564,18 +499,19 @@
 }
 
 
-
+// Write a byte using the 4-bit interface
+// Used for mbed pins, I2C bus expander or SPI shifregister
 void TextLCD::_writeByte(int value) {
 
 // Enable is Low
     _setEnable(true);          
-    _setData(value >> 4);   
+    _setData(value >> 4);   // High nibble
     wait_us(1); // Data setup time    
     _setEnable(false);   
     wait_us(1); // Data hold time
     
     _setEnable(true);        
-    _setData(value >> 0);    
+    _setData(value >> 0);   // Low nibble
     wait_us(1); // Data setup time        
     _setEnable(false);    
     wait_us(1); // Datahold time
@@ -587,7 +523,7 @@
 void TextLCD::_writeCommand(int command) {
 
     _setRS(false);        
-    wait_us(1);  // Data setup time            
+    wait_us(1);  // Data setup time for RS       
     
     _writeByte(command);   
     wait_us(40); // most instructions take 40us            
@@ -596,7 +532,7 @@
 void TextLCD::_writeData(int data) {
 
     _setRS(true);            
-    wait_us(1);  // Data setup time        
+    wait_us(1);  // Data setup time for RS 
         
     _writeByte(data);
     wait_us(40); // data writes take 40us                
@@ -604,7 +540,7 @@
 
 
 #if (0)
-// This is the original method.
+// This is the original _address() method.
 // It is confusing since it returns the memoryaddress or-ed with the set memorycommand 0x80.
 // Left it in here for compatibility with older code. New applications should use getAddress() instead.
 // 
@@ -632,7 +568,7 @@
 #endif
 
 
-// This replaces the original method.
+// This replaces the original _address() method.
 // Left it in here for compatibility with older code. New applications should use getAddress() instead.
 int TextLCD::_address(int column, int row) {
   return 0x80 | getAddress(column, row);
@@ -714,7 +650,6 @@
         case LCD40x2:                
             return 0x00 + (row * 0x40) + column;
 
-#if(LCD40x4Test)
         case LCD40x4:                
           // LCD40x4 is a special case since it has 2 controllers
           // Each controller is configured as 40x2
@@ -749,8 +684,6 @@
                                    
             return 0x00 + ((row-2) * 0x40) + column;          
           } 
-          
-#endif
             
 // Should never get here.
         default:            
@@ -813,10 +746,7 @@
             return 24;        
 
         case LCD40x2:
-
-#if(LCD40x4Test)
         case LCD40x4:
-#endif        
             return 40;        
         
 // Should never get here.
@@ -844,9 +774,7 @@
         case LCD16x4:
         case LCD20x4:
         case LCD24x4:        
-#if(LCD40x4Test)
         case LCD40x4:
-#endif        
             return 4;
 
 // Should never get here.      
@@ -856,9 +784,6 @@
 }
 
 
-
-#if(LCD40x4Test)
-
 void TextLCD::setCursor(TextLCD::LCDCursor show) { 
 
   // Save new cursor mode, needed when 2 controllers are in use
@@ -888,42 +813,11 @@
 // Should never get here.
       default : 
                            break;
-                      
+                     
     }
-
 }
 
-#else
-//standard
-void TextLCD::setCursor(TextLCD::LCDCursor show) { 
-    
-    switch (show) {
-      case CurOff_BlkOff : _writeCommand(0x0C); // Cursor off and Blink Off
-                           break;
 
-      case CurOn_BlkOff  : _writeCommand(0x0E); // Cursor on and Blink Off
-                           break;
-
-      case CurOff_BlkOn :  _writeCommand(0x0D); // Cursor off and Blink On
-                           break;
-
-      case CurOn_BlkOn   : _writeCommand(0x0F); // Cursor on and Blink char
-                           break;
-
-// Should never get here.
-      default : 
-                           break;
-                      
-    }
-
-}
-
-#endif
-
-
-
-
-#if(LCD40x4Test)
 void TextLCD::setUDC(unsigned char c, char *udc_data) {
   
   // Select and configure second LCD controller when needed
@@ -967,22 +861,3 @@
   _writeCommand(0x80 | addr);
   
 }
-
-#else
-//standard
-void TextLCD::setUDC(unsigned char c, char *udc_data) {
-  // Select CG RAM for current LCD controller
-  _writeCommand(0x40 + ((c & 0x07) << 3)); //Set CG-RAM address
-                                           //8 sequential locations needed per UDC
-  // Store UDC pattern 
-  for (int i=0; i<8; i++) {
-    _writeData(*udc_data++);
-  }
-  
-  //Select DD RAM again for current LCD controller
-  addr = getAddress(_column, _row);
-  _writeCommand(0x80 | addr);
-  
-}
-
-#endif
--- a/TextLCD.h	Tue Feb 19 22:09:09 2013 +0000
+++ b/TextLCD.h	Wed Feb 20 19:37:53 2013 +0000
@@ -29,28 +29,33 @@
 #include "mbed.h"
 
 
-//Test Only
-//#define LCD40x4Test 0
-#define LCD40x4Test 1
-
 /** A TextLCD interface for driving 4-bit HD44780-based LCDs
  *
- * Currently supports 8x1, 8x2, 12x4, 16x1, 16x2, 16x4, 20x2, 20x4, 24x2, 24x4 and 40x2 panels
+ * 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)
  *
  * @code
  * #include "mbed.h"
  * #include "TextLCD.h"
  * 
- * TextLCD lcd(p15, p16, p17, p18, p19, p20); // RS, E, D4-D7, LCDType=LCD16x2
+ * // I2C Communication
+ * I2C i2c_lcd(p28,p27); // SDA, SCL
+ *
+ * // 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
+ * //TextLCD lcd(&spi_lcd, p8, TextLCD::LCD40x4);   // SPI bus, CS pin, LCD Type  
+ * //TextLCD lcd(&i2c_lcd, 0x42, TextLCD::LCD20x4); // I2C bus, PCF8574 Slaveaddress, LCD Type
  * 
  * int main() {
- *     lcd.printf("Hello World!\n");
+ *   lcd.printf("Hello World!\n");
  * }
  * @endcode
  */
 
 
-//Pin Defines for I2C PCF8574 and SPI 74595 Bus
+//Pin Defines for I2C PCF8574 and SPI 74595 Bus interfaces
 //LCD and serial portexpanders should be wired accordingly 
 //Note: LCD RW pin must be connected to GND
 //      E2 is used for LCD40x4 (second controller)
@@ -119,7 +124,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
+ * Currently supports 8x1, 8x2, 12x2, 12x4, 16x1, 16x2, 16x4, 20x2, 20x4, 24x2, 24x4, 40x2 and 40x4 panels
  *
  */
 class TextLCD : public Stream {
@@ -271,7 +276,6 @@
     void _init();    
     void _initCtrl();    
     int  _address(int column, int row);
-    void _character(int column, int row, int c);
     void _setCursor(TextLCD::LCDCursor show);
     void _setUDC(unsigned char c, char *udc_data);