1
#include "LiquidCrystal.h"
8
// When the display powers up, it is configured as follows:
12
// DL = 1; 8-bit interface data
13
// N = 0; 1-line display
14
// F = 0; 5x8 dot character font
15
// 3. Display on/off control:
18
// B = 0; Blinking off
20
// I/D = 1; Increment by 1
23
// Note, however, that resetting the Arduino doesn't reset the LCD, so we
24
// can't assume that its in that state when a sketch starts (and the
25
// LiquidCrystal constructor is called).
27
LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
28
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
29
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
31
init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
34
LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable,
35
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
36
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
38
init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7);
41
LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
42
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
44
init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0);
47
LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable,
48
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
50
init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
53
void LiquidCrystal::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
54
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
55
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
70
pinMode(_rs_pin, OUTPUT);
71
// we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
73
pinMode(_rw_pin, OUTPUT);
75
pinMode(_enable_pin, OUTPUT);
78
_displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
80
_displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
85
void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
87
_displayfunction |= LCD_2LINE;
92
// for some 1 line displays you can select a 10 pixel high font
93
if ((dotsize != 0) && (lines == 1)) {
94
_displayfunction |= LCD_5x10DOTS;
97
// SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
98
// according to datasheet, we need at least 40ms after power rises above 2.7V
99
// before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
100
delayMicroseconds(50000);
101
// Now we pull both RS and R/W low to begin commands
102
digitalWrite(_rs_pin, LOW);
103
digitalWrite(_enable_pin, LOW);
104
if (_rw_pin != 255) {
105
digitalWrite(_rw_pin, LOW);
108
//put the LCD into 4 bit or 8 bit mode
109
if (! (_displayfunction & LCD_8BITMODE)) {
110
// this is according to the hitachi HD44780 datasheet
113
// we start in 8bit mode, try to set 4 bit mode
115
delayMicroseconds(4500); // wait min 4.1ms
119
delayMicroseconds(4500); // wait min 4.1ms
123
delayMicroseconds(150);
125
// finally, set to 8-bit interface
128
// this is according to the hitachi HD44780 datasheet
131
// Send function set command sequence
132
command(LCD_FUNCTIONSET | _displayfunction);
133
delayMicroseconds(4500); // wait more than 4.1ms
136
command(LCD_FUNCTIONSET | _displayfunction);
137
delayMicroseconds(150);
140
command(LCD_FUNCTIONSET | _displayfunction);
143
// finally, set # lines, font size, etc.
144
command(LCD_FUNCTIONSET | _displayfunction);
146
// turn the display on with no cursor or blinking default
147
_displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
153
// Initialize to default text direction (for romance languages)
154
_displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
155
// set the entry mode
156
command(LCD_ENTRYMODESET | _displaymode);
160
/********** high level commands, for the user! */
161
void LiquidCrystal::clear()
163
command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
164
delayMicroseconds(2000); // this command takes a long time!
167
void LiquidCrystal::home()
169
command(LCD_RETURNHOME); // set cursor position to zero
170
delayMicroseconds(2000); // this command takes a long time!
173
void LiquidCrystal::setCursor(uint8_t col, uint8_t row)
175
int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
176
if ( row > _numlines ) {
177
row = _numlines-1; // we count rows starting w/0
180
command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
183
// Turn the display on/off (quickly)
184
void LiquidCrystal::noDisplay() {
185
_displaycontrol &= ~LCD_DISPLAYON;
186
command(LCD_DISPLAYCONTROL | _displaycontrol);
188
void LiquidCrystal::display() {
189
_displaycontrol |= LCD_DISPLAYON;
190
command(LCD_DISPLAYCONTROL | _displaycontrol);
193
// Turns the underline cursor on/off
194
void LiquidCrystal::noCursor() {
195
_displaycontrol &= ~LCD_CURSORON;
196
command(LCD_DISPLAYCONTROL | _displaycontrol);
198
void LiquidCrystal::cursor() {
199
_displaycontrol |= LCD_CURSORON;
200
command(LCD_DISPLAYCONTROL | _displaycontrol);
203
// Turn on and off the blinking cursor
204
void LiquidCrystal::noBlink() {
205
_displaycontrol &= ~LCD_BLINKON;
206
command(LCD_DISPLAYCONTROL | _displaycontrol);
208
void LiquidCrystal::blink() {
209
_displaycontrol |= LCD_BLINKON;
210
command(LCD_DISPLAYCONTROL | _displaycontrol);
213
// These commands scroll the display without changing the RAM
214
void LiquidCrystal::scrollDisplayLeft(void) {
215
command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
217
void LiquidCrystal::scrollDisplayRight(void) {
218
command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
221
// This is for text that flows Left to Right
222
void LiquidCrystal::leftToRight(void) {
223
_displaymode |= LCD_ENTRYLEFT;
224
command(LCD_ENTRYMODESET | _displaymode);
227
// This is for text that flows Right to Left
228
void LiquidCrystal::rightToLeft(void) {
229
_displaymode &= ~LCD_ENTRYLEFT;
230
command(LCD_ENTRYMODESET | _displaymode);
233
// This will 'right justify' text from the cursor
234
void LiquidCrystal::autoscroll(void) {
235
_displaymode |= LCD_ENTRYSHIFTINCREMENT;
236
command(LCD_ENTRYMODESET | _displaymode);
239
// This will 'left justify' text from the cursor
240
void LiquidCrystal::noAutoscroll(void) {
241
_displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
242
command(LCD_ENTRYMODESET | _displaymode);
245
// Allows us to fill the first 8 CGRAM locations
246
// with custom characters
247
void LiquidCrystal::createChar(uint8_t location, uint8_t charmap[]) {
248
location &= 0x7; // we only have 8 locations 0-7
249
command(LCD_SETCGRAMADDR | (location << 3));
250
for (int i=0; i<8; i++) {
255
/*********** mid level commands, for sending data/cmds */
257
inline void LiquidCrystal::command(uint8_t value) {
261
inline void LiquidCrystal::write(uint8_t value) {
265
/************ low level data pushing commands **********/
267
// write either command or data, with automatic 4/8-bit selection
268
void LiquidCrystal::send(uint8_t value, uint8_t mode) {
269
digitalWrite(_rs_pin, mode);
271
// if there is a RW pin indicated, set it low to Write
272
if (_rw_pin != 255) {
273
digitalWrite(_rw_pin, LOW);
276
if (_displayfunction & LCD_8BITMODE) {
279
write4bits(value>>4);
284
void LiquidCrystal::pulseEnable(void) {
285
digitalWrite(_enable_pin, LOW);
286
delayMicroseconds(1);
287
digitalWrite(_enable_pin, HIGH);
288
delayMicroseconds(1); // enable pulse must be >450ns
289
digitalWrite(_enable_pin, LOW);
290
delayMicroseconds(100); // commands need > 37us to settle
293
void LiquidCrystal::write4bits(uint8_t value) {
294
for (int i = 0; i < 4; i++) {
295
pinMode(_data_pins[i], OUTPUT);
296
digitalWrite(_data_pins[i], (value >> i) & 0x01);
302
void LiquidCrystal::write8bits(uint8_t value) {
303
for (int i = 0; i < 8; i++) {
304
pinMode(_data_pins[i], OUTPUT);
305
digitalWrite(_data_pins[i], (value >> i) & 0x01);