Committer:
Wimpie
Date:
Sat Dec 18 20:06:14 2010 +0000
Revision:
0:9218cf335f9b
Child:
1:0eb3365ec819

        

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