Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2CTextLCD.h Source File

I2CTextLCD.h

00001 /* mbed I2CTextLCD Library
00002  * Copyright (c) 2007-2009 sford
00003  * Copyright (c) 2010 Wim De Roeve  changed to work with I2C PCF8575
00004  * Released under the MIT License: http://mbed.org/license/mit
00005  */
00006 
00007 #ifndef MBED_I2CTextLCD_H
00008 #define MBED_I2CTextLCD_H
00009 
00010 #include "mbed.h"
00011 #include "Stream.h"
00012 
00013 #define RS_ON         0x10   //P4
00014 #define E1_ON         0x20   //P5
00015 #define E2_ON         0x40   //P6
00016 #define BACKLIGHT_ON  0x80   //P7
00017 
00018 /* Display ON/OFF Control defines */
00019 #define DON                0x0F  //0b00001111  Display on 
00020 #define DOFF               0x0B  //0b00001011  Display off 
00021 #define CURSOR_ON          0x0F  //0b00001111  Cursor on 
00022 #define CURSOR_OFF         OxOD  //0b00001101  Cursor off 
00023 #define BLINK_ON           0x0F  //0b00001111  Cursor Blink 
00024 #define BLINK_OFF          0x0E  //0b00001110  Cursor No Blink 
00025 
00026 /* Cursor or Display Shift defines */
00027 #define SHIFT_CUR_LEFT     Ox13  //0b00010011  Cursor shifts to the left 
00028 #define SHIFT_CUR_RIGHT    Ox17  //0b00010111  Cursor shifts to the right 
00029 #define SHIFT_DISP_LEFT    Ox1B  //0b00011011  Display shifts to the left 
00030 #define SHIFT_DISP_RIGHT   0x1F  //0b00011111  Display shifts to the right 
00031 
00032 /* Function Set defines */
00033 #define EIGHT_BITMODE      0x03  //0b00000011  8-bit Interface D4-D7
00034 #define FOUR_BITMODE       0x02  //0b00000010  4-bit Interface D4-D7
00035 #define LINE_5X7           0x30  //0b00110000
00036 #define LINE_5X10          0x34  //0b00110100
00037 #define LINES_5X7          0x38  //0b00111000
00038 
00039 // Addtional define to support display mode
00040 
00041 #define DISP_FLIP_NONE     0x00  //0b00111100  No flip
00042 #define CLEAR_LCD          0x01  //0b00000001 
00043 
00044 // Addtional define to support entry mode & shift mode
00045 //#define ENTRY_CURSOR_DEC 0b00000101 /* Entry cursor move left */
00046 //#define ENTRY_CURSOR_INC 0b00000111 /* Entry cursor move to right */
00047 //#define ENTRY_DISPLAY_SHIFT 0b00000111 /* Entry the display to shift */
00048 //#define ENTRY_DISPLAY_NO_SHIFT 0b00000110 /* Entry no shift */
00049 
00050 // Use generic address
00051 
00052 #define LINE0 0x80
00053 #define LINE1 0xC0 
00054 #define LINE2 0x94 
00055 #define LINE3 0xD4 
00056 namespace mbed {
00057 
00058 /* Class: I2CTextLCD
00059  * A 16x2 Text LCD controller
00060  *
00061  * Allows you to print to a Text LCD screen, and locate/cls. Could be
00062  * turned in to a more generic libray.
00063  *
00064  * If you are connecting multiple displays, you can connect them all in
00065  * parallel, the address of the PCF8575 or PCF8574 must be unique for each
00066  * display.
00067  *
00068  * Example:
00069  * > #include "mbed.h"
00070  * > #include "I2CTextLCD.h"
00071  * >
00072  * > I2CTextLCD lcd(p9, P10, 0x40); // sda scl, address
00073  * >
00074  * > int main() {
00075  * >     lcd.printf("Hello World!");
00076  * > }
00077  */
00078 class I2CTextLCD : public Stream {
00079 
00080 public:
00081     /* Constructor: I2CTextLCD
00082      * Create a I2CTextLCD object
00083      *
00084      * wiring
00085      *
00086      * PCF8575/PCF8574  to  LCD
00087      * ----------------------
00088      * P0              -  D4
00089      * P1              -  D5
00090      * P2              -  D6
00091      * P3              -  D7
00092      * P4              -  RS
00093      * P5              -  E1
00094      * P6              -  E2
00095      * P7              -  backlight (connected to a relay)
00096      * gnd             -  R/W
00097      *
00098      */
00099 
00100     I2CTextLCD(PinName sda, PinName scl, int address, int columns = 16, int rows = 2, bool backlight = true);
00101 
00102 #if 0 // Inhereted from Stream, for documentation only
00103     /* Function: putc
00104      *  Write a character
00105      *
00106      * Variables:
00107      *  c - The character to write to the serial port
00108      */
00109     int putc(int c);
00110 
00111     /* Function: printf
00112      *  Write a formated string
00113      *
00114      * Variables:
00115      *  format - A printf-style format string, followed by the
00116      *      variables to use in formating the string.
00117      */
00118     int printf(const char* format, ...);
00119 #endif
00120 
00121     /* Function: locate
00122      * Locate to a certian position
00123      *
00124      * Variables:
00125      *  column - the column to locate to, from 0..15
00126      *  row - the row to locate to, from 0..1
00127      */
00128     virtual void locate(int column, int row);
00129 
00130     /* Function: cls
00131      * Clear the screen, and locate to 0,0
00132      */
00133     virtual void cls();
00134 
00135     virtual void reset();
00136 
00137     /* Function: backlight
00138     * Sets the backlight on or off
00139     *
00140     * Variables:
00141     *  on (true or false)
00142     */
00143     virtual void backlight(bool on);
00144 
00145 protected:
00146 
00147     void clock();
00148     void writeData(int data);
00149     void writeCommand(int command);
00150     void writeByte(int value, bool rs);
00151     void writeNibble(int value, bool rs);
00152     void writeI2CByte(int data);
00153     int readI2C();
00154     virtual int _putc(int c);
00155     virtual int _getc();
00156     virtual void newline();
00157 
00158     int _row;
00159     int _column;
00160     int _columns;
00161     int _rows;
00162 
00163     I2C _i2c;
00164     int _i2cAddress;
00165     bool _backlight;
00166 
00167 
00168 private:
00169 
00170 
00171 
00172 };
00173 
00174 }
00175 
00176 #endif
00177 
00178