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