Rod Coleman / I2CTextLCD
Committer:
RodColeman
Date:
Tue Oct 15 10:44:20 2013 +0000
Revision:
9:16c12a5da0ac
Parent:
0:9d621b44ea24
Init changes as proved at Dawson

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RodColeman 0:9d621b44ea24 1 /* mbed I2CTextLCD Library
RodColeman 0:9d621b44ea24 2 * Copyright (c) 2007-2009 sford
RodColeman 0:9d621b44ea24 3 * Copyright (c) 2010 Wim De Roeve changed to work with I2C PCF8575
RodColeman 0:9d621b44ea24 4 * Released under the MIT License: http://mbed.org/license/mit
RodColeman 0:9d621b44ea24 5 */
RodColeman 0:9d621b44ea24 6
RodColeman 0:9d621b44ea24 7 #ifndef MBED_I2CTextLCD_H
RodColeman 0:9d621b44ea24 8 #define MBED_I2CTextLCD_H
RodColeman 0:9d621b44ea24 9
RodColeman 0:9d621b44ea24 10 #include "mbed.h"
RodColeman 0:9d621b44ea24 11 #include "Stream.h"
RodColeman 0:9d621b44ea24 12
RodColeman 0:9d621b44ea24 13 #define RS_ON 0x01 //PCF port P0
RodColeman 0:9d621b44ea24 14 #define E1_ON 0x02 //PCF port P1
RodColeman 0:9d621b44ea24 15 #define E2_ON 0x40 //P6
RodColeman 0:9d621b44ea24 16 #define BACKLIGHT_ON 0x80 //P7
RodColeman 0:9d621b44ea24 17
RodColeman 0:9d621b44ea24 18 /* Display ON/OFF Control defines */
RodColeman 0:9d621b44ea24 19 #define DON 0x0F //0b00001111 Display on
RodColeman 0:9d621b44ea24 20 #define DOFF 0x0B //0b00001011 Display off
RodColeman 0:9d621b44ea24 21 #define CURSOR_ON 0x0F //0b00001111 Cursor on
RodColeman 0:9d621b44ea24 22 #define CURSOR_OFF 0x0D //0b00001101 Cursor off
RodColeman 0:9d621b44ea24 23 #define BLINK_ON 0x0F //0b00001111 Cursor Blink
RodColeman 0:9d621b44ea24 24 #define BLINK_OFF 0x0E //0b00001110 Cursor No Blink
RodColeman 0:9d621b44ea24 25
RodColeman 0:9d621b44ea24 26 /* Cursor or Display Shift defines */
RodColeman 0:9d621b44ea24 27 #define SHIFT_CUR_LEFT 0x13 //0b00010011 Cursor shifts to the left
RodColeman 0:9d621b44ea24 28 #define SHIFT_CUR_RIGHT 0x17 //0b00010111 Cursor shifts to the right
RodColeman 0:9d621b44ea24 29 #define SHIFT_DISP_LEFT 0x1B //0b00011011 Display shifts to the left
RodColeman 0:9d621b44ea24 30 #define SHIFT_DISP_RIGHT 0x1F //0b00011111 Display shifts to the right
RodColeman 0:9d621b44ea24 31
RodColeman 0:9d621b44ea24 32 /* Function Set defines */
RodColeman 0:9d621b44ea24 33 #define EIGHT_BITMODE 0x03 //0b00000011 8-bit Interface D4-D7
RodColeman 0:9d621b44ea24 34 #define FOUR_BITMODE 0x02 //0b00000010 4-bit Interface D4-D7
RodColeman 0:9d621b44ea24 35 #define LINE_5X7 0x30 //0b00110000
RodColeman 0:9d621b44ea24 36 #define LINE_5X10 0x34 //0b00110100
RodColeman 0:9d621b44ea24 37 #define LINES_5X7 0x38 //0b00111000
RodColeman 0:9d621b44ea24 38
RodColeman 0:9d621b44ea24 39 // Addtional define to support display mode
RodColeman 0:9d621b44ea24 40
RodColeman 0:9d621b44ea24 41 #define DISP_FLIP_NONE 0x00 //0b00111100 No flip
RodColeman 0:9d621b44ea24 42 #define CLEAR_LCD 0x01 //0b00000001
RodColeman 0:9d621b44ea24 43
RodColeman 0:9d621b44ea24 44 // Addtional define to support entry mode & shift mode
RodColeman 0:9d621b44ea24 45 //#define ENTRY_CURSOR_DEC 0b00000101 /* Entry cursor move left */
RodColeman 0:9d621b44ea24 46 //#define ENTRY_CURSOR_INC 0b00000111 /* Entry cursor move to right */
RodColeman 0:9d621b44ea24 47 //#define ENTRY_DISPLAY_SHIFT 0b00000111 /* Entry the display to shift */
RodColeman 0:9d621b44ea24 48 //#define ENTRY_DISPLAY_NO_SHIFT 0b00000110 /* Entry no shift */
RodColeman 0:9d621b44ea24 49
RodColeman 0:9d621b44ea24 50 // Use generic address
RodColeman 0:9d621b44ea24 51
RodColeman 0:9d621b44ea24 52 #define LINE0 0x80
RodColeman 0:9d621b44ea24 53 #define LINE1 0xC0
RodColeman 0:9d621b44ea24 54 #define LINE2 0x94
RodColeman 0:9d621b44ea24 55 #define LINE3 0xD4
RodColeman 0:9d621b44ea24 56 namespace mbed {
RodColeman 0:9d621b44ea24 57
RodColeman 0:9d621b44ea24 58 /* Class: I2CTextLCD
RodColeman 0:9d621b44ea24 59 * A 16x2 Text LCD controller
RodColeman 0:9d621b44ea24 60 *
RodColeman 0:9d621b44ea24 61 * Allows you to print to a Text LCD screen, and locate/cls. Could be
RodColeman 0:9d621b44ea24 62 * turned in to a more generic libray.
RodColeman 0:9d621b44ea24 63 *
RodColeman 0:9d621b44ea24 64 * If you are connecting multiple displays, you can connect them all in
RodColeman 0:9d621b44ea24 65 * parallel, the address of the PCF8575 or PCF8574 must be unique for each
RodColeman 0:9d621b44ea24 66 * display.
RodColeman 0:9d621b44ea24 67 *
RodColeman 0:9d621b44ea24 68 * Example:
RodColeman 0:9d621b44ea24 69 * > #include "mbed.h"
RodColeman 0:9d621b44ea24 70 * > #include "I2CTextLCD.h"
RodColeman 0:9d621b44ea24 71 * >
RodColeman 0:9d621b44ea24 72 * > I2CTextLCD lcd(p9, P10, 0x40); // sda scl, address
RodColeman 0:9d621b44ea24 73 * >
RodColeman 0:9d621b44ea24 74 * > int main() {
RodColeman 0:9d621b44ea24 75 * > lcd.printf("Hello World!");
RodColeman 0:9d621b44ea24 76 * > }
RodColeman 0:9d621b44ea24 77 */
RodColeman 0:9d621b44ea24 78 class I2CTextLCD : public Stream {
RodColeman 0:9d621b44ea24 79
RodColeman 0:9d621b44ea24 80 public:
RodColeman 0:9d621b44ea24 81 /* Constructor: I2CTextLCD
RodColeman 0:9d621b44ea24 82 * Create a I2CTextLCD object
RodColeman 0:9d621b44ea24 83 *
RodColeman 0:9d621b44ea24 84 * wiring
RodColeman 0:9d621b44ea24 85 *
RodColeman 0:9d621b44ea24 86 * PCF8575/PCF8574 to LCD..........Rods Fassung:
RodColeman 0:9d621b44ea24 87 * ----------------------
RodColeman 0:9d621b44ea24 88 * P0 - D4.............RS
RodColeman 0:9d621b44ea24 89 * P1 - D5..............E
RodColeman 0:9d621b44ea24 90 * P2 - D6.............D4
RodColeman 0:9d621b44ea24 91 * P3 - D7.............D5
RodColeman 0:9d621b44ea24 92 * P4 - RS.............D6
RodColeman 0:9d621b44ea24 93 * P5 - E1.............D7
RodColeman 0:9d621b44ea24 94 * P6 - E2
RodColeman 0:9d621b44ea24 95 * P7 - backlight (connected to a relay)
RodColeman 0:9d621b44ea24 96 * gnd - R/W
RodColeman 0:9d621b44ea24 97 *
RodColeman 0:9d621b44ea24 98 */
RodColeman 0:9d621b44ea24 99 // I2C address for PCF8574AN [TI] is 0x38 for A0..2 = 000 0x39 for 001, NO it is 0x70.
RodColeman 0:9d621b44ea24 100 // I2C slave addres for NXP PCF8574 = 0x 40 for A0..2 = 000 0x42 for 001
RodColeman 0:9d621b44ea24 101 // I2C slave addres for NXP PCF8574A = 0x 70 for A0..2 = 000 0x72 for 001
RodColeman 0:9d621b44ea24 102
RodColeman 0:9d621b44ea24 103 I2CTextLCD(PinName sda, PinName scl, int address, int columns = 20, int rows = 4, bool backlight = false);
RodColeman 0:9d621b44ea24 104
RodColeman 0:9d621b44ea24 105 #if 0 // Inhereted from Stream, for documentation only
RodColeman 0:9d621b44ea24 106 /* Function: putc
RodColeman 0:9d621b44ea24 107 * Write a character
RodColeman 0:9d621b44ea24 108 *
RodColeman 0:9d621b44ea24 109 * Variables:
RodColeman 0:9d621b44ea24 110 * c - The character to write to the serial port
RodColeman 0:9d621b44ea24 111 */
RodColeman 0:9d621b44ea24 112 int putc(int c);
RodColeman 0:9d621b44ea24 113
RodColeman 0:9d621b44ea24 114 /* Function: printf
RodColeman 0:9d621b44ea24 115 * Write a formated string
RodColeman 0:9d621b44ea24 116 *
RodColeman 0:9d621b44ea24 117 * Variables:
RodColeman 0:9d621b44ea24 118 * format - A printf-style format string, followed by the
RodColeman 0:9d621b44ea24 119 * variables to use in formating the string.
RodColeman 0:9d621b44ea24 120 */
RodColeman 0:9d621b44ea24 121 int printf(const char* format, ...);
RodColeman 0:9d621b44ea24 122 #endif
RodColeman 0:9d621b44ea24 123
RodColeman 0:9d621b44ea24 124 /* Function: locate
RodColeman 0:9d621b44ea24 125 * Locate to a certian position
RodColeman 0:9d621b44ea24 126 *
RodColeman 0:9d621b44ea24 127 * Variables:
RodColeman 0:9d621b44ea24 128 * column - the column to locate to, from 0..15
RodColeman 0:9d621b44ea24 129 * row - the row to locate to, from 0..1
RodColeman 0:9d621b44ea24 130 */
RodColeman 0:9d621b44ea24 131 virtual void locate(int column, int row);
RodColeman 0:9d621b44ea24 132
RodColeman 0:9d621b44ea24 133 /* Function: cls
RodColeman 0:9d621b44ea24 134 * Clear the screen, and locate to 0,0
RodColeman 0:9d621b44ea24 135 */
RodColeman 0:9d621b44ea24 136 virtual void cls();
RodColeman 0:9d621b44ea24 137
RodColeman 0:9d621b44ea24 138 virtual void reset();
RodColeman 0:9d621b44ea24 139
RodColeman 0:9d621b44ea24 140 /* Function: backlight
RodColeman 0:9d621b44ea24 141 * Sets the backlight on or off
RodColeman 0:9d621b44ea24 142 *
RodColeman 0:9d621b44ea24 143 * Variables:
RodColeman 0:9d621b44ea24 144 * on (true or false)
RodColeman 0:9d621b44ea24 145 */
RodColeman 0:9d621b44ea24 146 //virtual void backlight(bool on);
RodColeman 0:9d621b44ea24 147
RodColeman 0:9d621b44ea24 148 protected:
RodColeman 0:9d621b44ea24 149
RodColeman 0:9d621b44ea24 150 void clock();
RodColeman 9:16c12a5da0ac 151 void init8574A(void);
RodColeman 0:9d621b44ea24 152 void writeData(int data);
RodColeman 0:9d621b44ea24 153 void writeCommand(int command);
RodColeman 0:9d621b44ea24 154 void writeByte(int value, bool rs);
RodColeman 0:9d621b44ea24 155 //void writeNibble(int value, bool rs);
RodColeman 0:9d621b44ea24 156 // void writeI2CByte(int data);
RodColeman 0:9d621b44ea24 157 int readI2C();
RodColeman 0:9d621b44ea24 158 virtual int _putc(int c);
RodColeman 0:9d621b44ea24 159 virtual int _getc();
RodColeman 0:9d621b44ea24 160 virtual void newline();
RodColeman 0:9d621b44ea24 161
RodColeman 0:9d621b44ea24 162 int _row;
RodColeman 0:9d621b44ea24 163 int _column;
RodColeman 0:9d621b44ea24 164 int _columns;
RodColeman 0:9d621b44ea24 165 int _rows;
RodColeman 0:9d621b44ea24 166
RodColeman 0:9d621b44ea24 167 I2C _i2c;
RodColeman 0:9d621b44ea24 168 int _i2cAddress;
RodColeman 0:9d621b44ea24 169 bool _backlight;
RodColeman 0:9d621b44ea24 170
RodColeman 0:9d621b44ea24 171
RodColeman 0:9d621b44ea24 172 private:
RodColeman 0:9d621b44ea24 173
RodColeman 0:9d621b44ea24 174
RodColeman 0:9d621b44ea24 175
RodColeman 0:9d621b44ea24 176 };
RodColeman 0:9d621b44ea24 177
RodColeman 0:9d621b44ea24 178 }
RodColeman 0:9d621b44ea24 179
RodColeman 0:9d621b44ea24 180 #endif
RodColeman 0:9d621b44ea24 181