Committer:
Wimpie
Date:
Sun Dec 19 19:19:39 2010 +0000
Revision:
1:0eb3365ec819
Parent:
0:9218cf335f9b
Child:
2:1c5dea5d8783
backlight option

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 0:9218cf335f9b 13 /* The code assumes the following connections between the PCF8575
Wimpie 0:9218cf335f9b 14 * and the LCD
Wimpie 0:9218cf335f9b 15 *
Wimpie 0:9218cf335f9b 16 * nc - D0
Wimpie 0:9218cf335f9b 17 * nc - D1
Wimpie 0:9218cf335f9b 18 * nc - D2
Wimpie 0:9218cf335f9b 19 * nc - D3
Wimpie 0:9218cf335f9b 20 * P0 - D4
Wimpie 0:9218cf335f9b 21 * P1 - D5
Wimpie 0:9218cf335f9b 22 * P2 - D6
Wimpie 0:9218cf335f9b 23 * P3 - D7
Wimpie 0:9218cf335f9b 24 * P4 - E
Wimpie 0:9218cf335f9b 25 * P5 - nc
Wimpie 0:9218cf335f9b 26 * P6 - nc
Wimpie 0:9218cf335f9b 27 * P7 - RS
Wimpie 0:9218cf335f9b 28 * gnd - R/W
Wimpie 0:9218cf335f9b 29
Wimpie 0:9218cf335f9b 30 */
Wimpie 0:9218cf335f9b 31
Wimpie 0:9218cf335f9b 32 #define E_ON 0x10 //P4
Wimpie 1:0eb3365ec819 33 #define BACKLIGHT_ON 0x20 //P5
Wimpie 0:9218cf335f9b 34 #define RS_ON 0x80 //P7
Wimpie 0:9218cf335f9b 35
Wimpie 0:9218cf335f9b 36 namespace mbed {
Wimpie 0:9218cf335f9b 37
Wimpie 0:9218cf335f9b 38 /* Class: I2CTextLCD
Wimpie 0:9218cf335f9b 39 * A 16x2 Text LCD controller
Wimpie 0:9218cf335f9b 40 *
Wimpie 0:9218cf335f9b 41 * Allows you to print to a Text LCD screen, and locate/cls. Could be
Wimpie 0:9218cf335f9b 42 * turned in to a more generic libray.
Wimpie 0:9218cf335f9b 43 *
Wimpie 0:9218cf335f9b 44 * If you are connecting multiple displays, you can connect them all in
Wimpie 0:9218cf335f9b 45 * parallel except for the enable (e) pin, which must be unique for each
Wimpie 0:9218cf335f9b 46 * display.
Wimpie 0:9218cf335f9b 47 *
Wimpie 0:9218cf335f9b 48 * Example:
Wimpie 0:9218cf335f9b 49 * > #include "mbed.h"
Wimpie 0:9218cf335f9b 50 * > #include "I2CTextLCD.h"
Wimpie 0:9218cf335f9b 51 * >
Wimpie 0:9218cf335f9b 52 * > I2CTextLCD lcd(p9, P10, 0x40); // sda scl, address
Wimpie 0:9218cf335f9b 53 * >
Wimpie 0:9218cf335f9b 54 * > int main() {
Wimpie 0:9218cf335f9b 55 * > lcd.printf("Hello World!");
Wimpie 0:9218cf335f9b 56 * > }
Wimpie 0:9218cf335f9b 57 */
Wimpie 0:9218cf335f9b 58 class I2CTextLCD : public Stream {
Wimpie 0:9218cf335f9b 59
Wimpie 0:9218cf335f9b 60 public:
Wimpie 0:9218cf335f9b 61 /* Constructor: I2CTextLCD
Wimpie 0:9218cf335f9b 62 * Create a I2CTextLCD object, connected to the specified pins
Wimpie 0:9218cf335f9b 63 *
Wimpie 0:9218cf335f9b 64 * All signals must be connected to DigitalIn compatible pins.
Wimpie 0:9218cf335f9b 65 *
Wimpie 0:9218cf335f9b 66 * Variables:
Wimpie 0:9218cf335f9b 67 * rs - Used to specify data or command
Wimpie 0:9218cf335f9b 68 * rw - Used to determine read or write
Wimpie 0:9218cf335f9b 69 * e - enable
Wimpie 0:9218cf335f9b 70 * d0..d3 - The data lines
Wimpie 0:9218cf335f9b 71 */
Wimpie 0:9218cf335f9b 72
Wimpie 0:9218cf335f9b 73 I2CTextLCD(PinName sda, PinName scl, int address, int columns = 16, int rows = 2);
Wimpie 0:9218cf335f9b 74
Wimpie 0:9218cf335f9b 75 #if 0 // Inhereted from Stream, for documentation only
Wimpie 0:9218cf335f9b 76 /* Function: putc
Wimpie 0:9218cf335f9b 77 * Write a character
Wimpie 0:9218cf335f9b 78 *
Wimpie 0:9218cf335f9b 79 * Variables:
Wimpie 0:9218cf335f9b 80 * c - The character to write to the serial port
Wimpie 0:9218cf335f9b 81 */
Wimpie 0:9218cf335f9b 82 int putc(int c);
Wimpie 0:9218cf335f9b 83
Wimpie 0:9218cf335f9b 84 /* Function: printf
Wimpie 0:9218cf335f9b 85 * Write a formated string
Wimpie 0:9218cf335f9b 86 *
Wimpie 0:9218cf335f9b 87 * Variables:
Wimpie 0:9218cf335f9b 88 * format - A printf-style format string, followed by the
Wimpie 0:9218cf335f9b 89 * variables to use in formating the string.
Wimpie 0:9218cf335f9b 90 */
Wimpie 0:9218cf335f9b 91 int printf(const char* format, ...);
Wimpie 0:9218cf335f9b 92 #endif
Wimpie 0:9218cf335f9b 93
Wimpie 0:9218cf335f9b 94 /* Function: locate
Wimpie 0:9218cf335f9b 95 * Locate to a certian position
Wimpie 0:9218cf335f9b 96 *
Wimpie 0:9218cf335f9b 97 * Variables:
Wimpie 0:9218cf335f9b 98 * column - the column to locate to, from 0..15
Wimpie 0:9218cf335f9b 99 * row - the row to locate to, from 0..1
Wimpie 0:9218cf335f9b 100 */
Wimpie 0:9218cf335f9b 101 virtual void locate(int column, int row);
Wimpie 0:9218cf335f9b 102
Wimpie 0:9218cf335f9b 103 /* Function: cls
Wimpie 0:9218cf335f9b 104 * Clear the screen, and locate to 0,0
Wimpie 0:9218cf335f9b 105 */
Wimpie 0:9218cf335f9b 106 virtual void cls();
Wimpie 0:9218cf335f9b 107
Wimpie 0:9218cf335f9b 108 virtual void reset();
Wimpie 1:0eb3365ec819 109 virtual void backlight(bool on);
Wimpie 0:9218cf335f9b 110
Wimpie 0:9218cf335f9b 111 protected:
Wimpie 0:9218cf335f9b 112
Wimpie 0:9218cf335f9b 113 void clock();
Wimpie 0:9218cf335f9b 114 void writeData(int data);
Wimpie 0:9218cf335f9b 115 void writeCommand(int command);
Wimpie 0:9218cf335f9b 116 void writeByte(int value, bool rs);
Wimpie 0:9218cf335f9b 117 void writeNibble(int value, bool rs);
Wimpie 0:9218cf335f9b 118 void writeI2CByte(int data);
Wimpie 1:0eb3365ec819 119 int readI2C();
Wimpie 0:9218cf335f9b 120 virtual int _putc(int c);
Wimpie 0:9218cf335f9b 121 virtual int _getc();
Wimpie 0:9218cf335f9b 122 virtual void newline();
Wimpie 0:9218cf335f9b 123
Wimpie 0:9218cf335f9b 124 int _row;
Wimpie 0:9218cf335f9b 125 int _column;
Wimpie 0:9218cf335f9b 126 int _columns;
Wimpie 0:9218cf335f9b 127 int _rows;
Wimpie 0:9218cf335f9b 128
Wimpie 0:9218cf335f9b 129 I2C _i2c;
Wimpie 0:9218cf335f9b 130 int _i2cAddress;
Wimpie 1:0eb3365ec819 131 bool _backlight;
Wimpie 0:9218cf335f9b 132
Wimpie 0:9218cf335f9b 133 private:
Wimpie 0:9218cf335f9b 134
Wimpie 0:9218cf335f9b 135
Wimpie 0:9218cf335f9b 136
Wimpie 0:9218cf335f9b 137 };
Wimpie 0:9218cf335f9b 138
Wimpie 0:9218cf335f9b 139 }
Wimpie 0:9218cf335f9b 140
Wimpie 0:9218cf335f9b 141 #endif
Wimpie 0:9218cf335f9b 142
Wimpie 0:9218cf335f9b 143