Committer:
Wimpie
Date:
Thu Dec 23 12:42:37 2010 +0000
Revision:
2:1c5dea5d8783
Parent:
1:0eb3365ec819
Child:
3:704f87be7993

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wimpie 0:9218cf335f9b 1 /* mbed I2CTextLCD Library
Wimpie 0:9218cf335f9b 2 * Copyright (c) 2007-2009 sford
Wimpie 0:9218cf335f9b 3 * Copyright (c) 2010 Wim De Roeve changed to work with I2C PCF8575
Wimpie 0:9218cf335f9b 4 * Released under the MIT License: http://mbed.org/license/mit
Wimpie 0:9218cf335f9b 5 */
Wimpie 0:9218cf335f9b 6
Wimpie 0:9218cf335f9b 7 #ifndef MBED_I2CTextLCD_H
Wimpie 0:9218cf335f9b 8 #define MBED_I2CTextLCD_H
Wimpie 0:9218cf335f9b 9
Wimpie 0:9218cf335f9b 10 #include "mbed.h"
Wimpie 0:9218cf335f9b 11 #include "Stream.h"
Wimpie 0:9218cf335f9b 12
Wimpie 2:1c5dea5d8783 13 #define E_ON 0x10 //P4
Wimpie 1:0eb3365ec819 14 #define BACKLIGHT_ON 0x20 //P5
Wimpie 2:1c5dea5d8783 15 #define RS_ON 0x80 //P7
Wimpie 0:9218cf335f9b 16
Wimpie 0:9218cf335f9b 17 namespace mbed {
Wimpie 0:9218cf335f9b 18
Wimpie 0:9218cf335f9b 19 /* Class: I2CTextLCD
Wimpie 0:9218cf335f9b 20 * A 16x2 Text LCD controller
Wimpie 0:9218cf335f9b 21 *
Wimpie 0:9218cf335f9b 22 * Allows you to print to a Text LCD screen, and locate/cls. Could be
Wimpie 0:9218cf335f9b 23 * turned in to a more generic libray.
Wimpie 0:9218cf335f9b 24 *
Wimpie 0:9218cf335f9b 25 * If you are connecting multiple displays, you can connect them all in
Wimpie 2:1c5dea5d8783 26 * parallel, the address of the PCF8575 or PCF8574 must be unique for each
Wimpie 0:9218cf335f9b 27 * display.
Wimpie 0:9218cf335f9b 28 *
Wimpie 0:9218cf335f9b 29 * Example:
Wimpie 0:9218cf335f9b 30 * > #include "mbed.h"
Wimpie 0:9218cf335f9b 31 * > #include "I2CTextLCD.h"
Wimpie 0:9218cf335f9b 32 * >
Wimpie 0:9218cf335f9b 33 * > I2CTextLCD lcd(p9, P10, 0x40); // sda scl, address
Wimpie 0:9218cf335f9b 34 * >
Wimpie 0:9218cf335f9b 35 * > int main() {
Wimpie 0:9218cf335f9b 36 * > lcd.printf("Hello World!");
Wimpie 0:9218cf335f9b 37 * > }
Wimpie 0:9218cf335f9b 38 */
Wimpie 0:9218cf335f9b 39 class I2CTextLCD : public Stream {
Wimpie 0:9218cf335f9b 40
Wimpie 0:9218cf335f9b 41 public:
Wimpie 0:9218cf335f9b 42 /* Constructor: I2CTextLCD
Wimpie 2:1c5dea5d8783 43 * Create a I2CTextLCD object
Wimpie 0:9218cf335f9b 44 *
Wimpie 2:1c5dea5d8783 45 * wiring
Wimpie 0:9218cf335f9b 46 *
Wimpie 2:1c5dea5d8783 47 * PCF8575/PCF8574 to LCD
Wimpie 2:1c5dea5d8783 48 * ----------------------
Wimpie 2:1c5dea5d8783 49 * P0 - D4
Wimpie 2:1c5dea5d8783 50 * P1 - D5
Wimpie 2:1c5dea5d8783 51 * P2 - D6
Wimpie 2:1c5dea5d8783 52 * P3 - D7
Wimpie 2:1c5dea5d8783 53 * P4 - E
Wimpie 2:1c5dea5d8783 54 * P5 - backlight (connected to a relay)
Wimpie 2:1c5dea5d8783 55 * P6 - nc
Wimpie 2:1c5dea5d8783 56 * P7 - RS
Wimpie 2:1c5dea5d8783 57 * gnd - R/W
Wimpie 2:1c5dea5d8783 58 *
Wimpie 0:9218cf335f9b 59 */
Wimpie 0:9218cf335f9b 60
Wimpie 2:1c5dea5d8783 61 I2CTextLCD(PinName sda, PinName scl, int address, int columns = 16, int rows = 2, bool backlight = true);
Wimpie 0:9218cf335f9b 62
Wimpie 0:9218cf335f9b 63 #if 0 // Inhereted from Stream, for documentation only
Wimpie 0:9218cf335f9b 64 /* Function: putc
Wimpie 0:9218cf335f9b 65 * Write a character
Wimpie 0:9218cf335f9b 66 *
Wimpie 0:9218cf335f9b 67 * Variables:
Wimpie 0:9218cf335f9b 68 * c - The character to write to the serial port
Wimpie 0:9218cf335f9b 69 */
Wimpie 0:9218cf335f9b 70 int putc(int c);
Wimpie 0:9218cf335f9b 71
Wimpie 0:9218cf335f9b 72 /* Function: printf
Wimpie 0:9218cf335f9b 73 * Write a formated string
Wimpie 0:9218cf335f9b 74 *
Wimpie 0:9218cf335f9b 75 * Variables:
Wimpie 0:9218cf335f9b 76 * format - A printf-style format string, followed by the
Wimpie 0:9218cf335f9b 77 * variables to use in formating the string.
Wimpie 0:9218cf335f9b 78 */
Wimpie 0:9218cf335f9b 79 int printf(const char* format, ...);
Wimpie 0:9218cf335f9b 80 #endif
Wimpie 0:9218cf335f9b 81
Wimpie 0:9218cf335f9b 82 /* Function: locate
Wimpie 0:9218cf335f9b 83 * Locate to a certian position
Wimpie 0:9218cf335f9b 84 *
Wimpie 0:9218cf335f9b 85 * Variables:
Wimpie 0:9218cf335f9b 86 * column - the column to locate to, from 0..15
Wimpie 0:9218cf335f9b 87 * row - the row to locate to, from 0..1
Wimpie 0:9218cf335f9b 88 */
Wimpie 0:9218cf335f9b 89 virtual void locate(int column, int row);
Wimpie 0:9218cf335f9b 90
Wimpie 0:9218cf335f9b 91 /* Function: cls
Wimpie 0:9218cf335f9b 92 * Clear the screen, and locate to 0,0
Wimpie 0:9218cf335f9b 93 */
Wimpie 0:9218cf335f9b 94 virtual void cls();
Wimpie 0:9218cf335f9b 95
Wimpie 0:9218cf335f9b 96 virtual void reset();
Wimpie 2:1c5dea5d8783 97
Wimpie 2:1c5dea5d8783 98 /* Function: backlight
Wimpie 2:1c5dea5d8783 99 * Sets the backlight on or off
Wimpie 2:1c5dea5d8783 100 *
Wimpie 2:1c5dea5d8783 101 * Variables:
Wimpie 2:1c5dea5d8783 102 * on (true or false)
Wimpie 2:1c5dea5d8783 103 */
Wimpie 1:0eb3365ec819 104 virtual void backlight(bool on);
Wimpie 0:9218cf335f9b 105
Wimpie 0:9218cf335f9b 106 protected:
Wimpie 0:9218cf335f9b 107
Wimpie 0:9218cf335f9b 108 void clock();
Wimpie 0:9218cf335f9b 109 void writeData(int data);
Wimpie 0:9218cf335f9b 110 void writeCommand(int command);
Wimpie 0:9218cf335f9b 111 void writeByte(int value, bool rs);
Wimpie 0:9218cf335f9b 112 void writeNibble(int value, bool rs);
Wimpie 0:9218cf335f9b 113 void writeI2CByte(int data);
Wimpie 1:0eb3365ec819 114 int readI2C();
Wimpie 0:9218cf335f9b 115 virtual int _putc(int c);
Wimpie 0:9218cf335f9b 116 virtual int _getc();
Wimpie 0:9218cf335f9b 117 virtual void newline();
Wimpie 0:9218cf335f9b 118
Wimpie 0:9218cf335f9b 119 int _row;
Wimpie 0:9218cf335f9b 120 int _column;
Wimpie 0:9218cf335f9b 121 int _columns;
Wimpie 0:9218cf335f9b 122 int _rows;
Wimpie 0:9218cf335f9b 123
Wimpie 0:9218cf335f9b 124 I2C _i2c;
Wimpie 0:9218cf335f9b 125 int _i2cAddress;
Wimpie 1:0eb3365ec819 126 bool _backlight;
Wimpie 2:1c5dea5d8783 127
Wimpie 2:1c5dea5d8783 128
Wimpie 0:9218cf335f9b 129 private:
Wimpie 0:9218cf335f9b 130
Wimpie 2:1c5dea5d8783 131
Wimpie 0:9218cf335f9b 132
Wimpie 0:9218cf335f9b 133 };
Wimpie 0:9218cf335f9b 134
Wimpie 0:9218cf335f9b 135 }
Wimpie 0:9218cf335f9b 136
Wimpie 0:9218cf335f9b 137 #endif
Wimpie 0:9218cf335f9b 138
Wimpie 0:9218cf335f9b 139