-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiquidCrystal_PCF8574.cpp
336 lines (275 loc) · 9.48 KB
/
LiquidCrystal_PCF8574.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
///
/// \file LiquidCrystal_PCF8574.cpp
/// \brief LiquidCrystal (LCD) library with PCF8574 I2C adapter.
///
/// \author Matthias Hertel, http://www.mathertel.de
/// \copyright Copyright (c) 2014 by Matthias Hertel.\n
/// This work is licensed under a BSD style license.\n
/// See http://www.mathertel.de/License.aspx
///
/// \details
/// This is a library for driving LiquidCrystal displays (LCD) by using the I2C bus and an PCF8574 I2C adapter.
/// This library is derived from the original Arduino LiquidCrystal library and uses the original Wire library for communication.
///
/// More documentation and source code is available at http://www.mathertel.de/Arduino
///
/// ChangeLog see: LiquidCrystal_PCF8574.h
#include "LiquidCrystal_PCF8574.h"
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "Arduino.h"
#include <Wire.h>
/// Definitions on how the PCF8574 is connected to the LCD
/// These are Bit-Masks for the special signals and background light
#define PCF_RS 0x01
#define PCF_RW 0x02
#define PCF_EN 0x04
#define PCF_BACKLIGHT 0x08
// Definitions on how the PCF8574 is connected to the LCD
// These are Bit-Masks for the special signals and Background
#define RSMODE_CMD 0
#define RSMODE_DATA 1
// When the display powers up, it is configured as follows:
//
// 1. Display clear
// 2. Function set:
// DL = 1; 8-bit interface data
// N = 0; 1-line display
// F = 0; 5x8 dot character font
// 3. Display on/off control:
// D = 0; Display off
// C = 0; Cursor off
// B = 0; Blinking off
// 4. Entry mode set:
// I/D = 1; Increment by 1
// S = 0; No shift
//
// Note, however, that resetting the Arduino doesn't reset the LCD, so we
// can't assume that its in that state when a sketch starts (and the
// LiquidCrystal constructor is called).
// modification:
// don't use ports from Arduino, but use ports from Wire
// a nibble is a half Byte
// NEW: http://playground.arduino.cc//Code/LCDAPI
// NEW: setBacklight
LiquidCrystal_PCF8574::LiquidCrystal_PCF8574()
{
//_backlight = 255;
} // LiquidCrystal_PCF8574
LiquidCrystal_PCF8574::LiquidCrystal_PCF8574(uint8_t addr)
{
_Addr = addr;
_backlight = 0;
} // LiquidCrystal_PCF8574
void LiquidCrystal_PCF8574::begin(uint8_t cols, uint8_t lines, uint8_t addr, uint8_t backlight)
{
_backlight = backlight;
_Addr = addr;
begin(cols, lines);
}
void LiquidCrystal_PCF8574::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
// cols ignored !
_numlines = lines;
_displayfunction = 0;
if (lines > 1) {
_displayfunction |= LCD_2LINE;
}
// for some 1 line displays you can select a 10 pixel high font
if ((dotsize != 0) && (lines == 1)) {
_displayfunction |= LCD_5x10DOTS;
}
// SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
// according to datasheet, we need at least 40ms after power rises above 2.7V
// before sending commands. Arduino can turn on way befor 4.5V so we'll wait 50
Wire.begin();
// initializing th display
_write2Wire(0x00, LOW, false);
delayMicroseconds(50000);
// put the LCD into 4 bit mode according to the hitachi HD44780 datasheet figure 26, pg 47
_sendNibble(0x03, RSMODE_CMD);
delayMicroseconds(4500);
_sendNibble(0x03, RSMODE_CMD);
delayMicroseconds(4500);
_sendNibble(0x03, RSMODE_CMD);
delayMicroseconds(150);
// finally, set to 4-bit interface
_sendNibble(0x02, RSMODE_CMD);
// finally, set # lines, font size, etc.
_command(LCD_FUNCTIONSET | _displayfunction);
// turn the display on with no cursor or blinking default
_displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
display();
// clear it off
clear();
// Initialize to default text direction (for romance languages)
_displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
// set the entry mode
_command(LCD_ENTRYMODESET | _displaymode);
setBacklight(_backlight);
checkI2CConnection();
}
/********** high level commands, for the user! */
void LiquidCrystal_PCF8574::clear()
{
_command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
delayMicroseconds(2000); // this command takes a long time!
}
void LiquidCrystal_PCF8574::home()
{
_command(LCD_RETURNHOME); // set cursor position to zero
delayMicroseconds(2000); // this command takes a long time!
}
/// Set the cursor to a new position.
void LiquidCrystal_PCF8574::setCursor(uint8_t col, uint8_t row)
{
int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
if ( row >= _numlines ) {
row = _numlines-1; // we count rows starting w/0
}
_command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
}
/** select begining of line
* line - 1 for first row, 2 for second row
*
*/
void LiquidCrystal_PCF8574::selectLine(uint8_t line)
{
setCursor(0, line - 1);
}
// Turn the display on/off (quickly)
void LiquidCrystal_PCF8574::noDisplay() {
_displaycontrol &= ~LCD_DISPLAYON;
_command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void LiquidCrystal_PCF8574::display() {
_displaycontrol |= LCD_DISPLAYON;
_command(LCD_DISPLAYCONTROL | _displaycontrol);
}
// Turns the underline cursor on/off
void LiquidCrystal_PCF8574::noCursor() {
_displaycontrol &= ~LCD_CURSORON;
_command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void LiquidCrystal_PCF8574::cursor() {
_displaycontrol |= LCD_CURSORON;
_command(LCD_DISPLAYCONTROL | _displaycontrol);
}
// Turn on and off the blinking cursor
void LiquidCrystal_PCF8574::noBlink() {
_displaycontrol &= ~LCD_BLINKON;
_command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void LiquidCrystal_PCF8574::blink() {
_displaycontrol |= LCD_BLINKON;
_command(LCD_DISPLAYCONTROL | _displaycontrol);
}
/** These commands scroll the display without changing the RAM
* charsToScroll - how many chars to scroll left
* scrollSpeed - define scrolling speed, im milliseconds.
*/
void LiquidCrystal_PCF8574::scrollDisplayLeft(int charsToScroll, int scrollSpeed) {
for (int i=0 ; i < charsToScroll ; i++)
{
_command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
delay(scrollSpeed);
}
}
/** These commands scroll the display without changing the RAM
* charsToScroll - how many chars to scroll right
* scrollSpeed - define scrolling speed, im milliseconds.
*/
void LiquidCrystal_PCF8574::scrollDisplayRight(int charsToScroll, int scrollSpeed) {
for (int i=0 ; i < charsToScroll ; i++)
{
_command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
delay(scrollSpeed);
}
}
// This is for text that flows Left to Right
void LiquidCrystal_PCF8574::leftToRight(void) {
_displaymode |= LCD_ENTRYLEFT;
_command(LCD_ENTRYMODESET | _displaymode);
}
// This is for text that flows Right to Left
void LiquidCrystal_PCF8574::rightToLeft(void) {
_displaymode &= ~LCD_ENTRYLEFT;
_command(LCD_ENTRYMODESET | _displaymode);
}
// This will 'right justify' text from the cursor
void LiquidCrystal_PCF8574::autoscroll(void) {
_displaymode |= LCD_ENTRYSHIFTINCREMENT;
_command(LCD_ENTRYMODESET | _displaymode);
}
// This will 'left justify' text from the cursor
void LiquidCrystal_PCF8574::noAutoscroll(void) {
_displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
_command(LCD_ENTRYMODESET | _displaymode);
}
/// Setting the brightness of the background display light.
/// The backlight can be switched on and off.
/// The current brightness is stored in the private _backlight variable to have it available for further data transfers.
void LiquidCrystal_PCF8574::setBacklight(uint8_t brightness) {
_backlight = brightness;
// send no data but set the background-pin right;
_write2Wire(0x00, RSMODE_DATA, false);
} // setBacklight
// Allows us to fill the first 8 CGRAM locations
// with custom characters
void LiquidCrystal_PCF8574::createChar(uint8_t location, uint8_t charmap[]) {
location &= 0x7; // we only have 8 locations 0-7
_command(LCD_SETCGRAMADDR | (location << 3));
for (int i=0; i<8; i++) {
write(charmap[i]);
}
}
/* The write function is needed for derivation from the Print class. */
inline size_t LiquidCrystal_PCF8574::write(uint8_t value) {
_send(value, RSMODE_DATA);
return 1; // assume sucess
}
/* ----- low level functions ----- */
inline void LiquidCrystal_PCF8574::_command(uint8_t value) {
_send(value, RSMODE_CMD);
} // _command()
// write either command or data
void LiquidCrystal_PCF8574::_send(uint8_t value, uint8_t mode) {
// separate the 4 value-nibbles
uint8_t valueLo = value & 0x0F;
uint8_t valueHi = value>>4 & 0x0F;
_sendNibble(valueHi, mode);
_sendNibble(valueLo, mode);
} // _send()
// write a nibble / halfByte with handshake
void LiquidCrystal_PCF8574::_sendNibble(uint8_t halfByte, uint8_t mode) {
_write2Wire(halfByte, mode, true);
delayMicroseconds(1); // enable pulse must be >450ns
_write2Wire(halfByte, mode, false);
delayMicroseconds(37); // commands need > 37us to settle
} // _sendNibble
// private function to change the PCF8674 pins to the given value
void LiquidCrystal_PCF8574::_write2Wire(uint8_t halfByte, uint8_t mode, uint8_t enable) {
// map the given values to the hardware of the I2C schema
uint8_t i2cData = halfByte << 4;
if (mode > 0) i2cData |= PCF_RS;
// PCF_RW is never used.
if (enable > 0) i2cData |= PCF_EN;
if (_backlight > 0) i2cData |= PCF_BACKLIGHT;
Wire.beginTransmission(_Addr);
Wire.write(i2cData);
Wire.endTransmission();
} // write2Wire
bool LiquidCrystal_PCF8574::checkI2CConnection()
{
int error;
// See http://playground.arduino.cc/Main/I2cScanner
Wire.begin();
Wire.beginTransmission(_Addr);
error = Wire.endTransmission();
if (error)
{
Serial.println("LCD not found.");
}
return error;
}
// The End.