root / arduino-1.0 / libraries / LiquidCrystal / LiquidCrystal.cpp @ 58d82c77
History | View | Annotate | Download (8.39 KB)
1 |
#include "LiquidCrystal.h" |
---|---|
2 |
|
3 |
#include <stdio.h> |
4 |
#include <string.h> |
5 |
#include <inttypes.h> |
6 |
#include "Arduino.h" |
7 |
|
8 |
// When the display powers up, it is configured as follows:
|
9 |
//
|
10 |
// 1. Display clear
|
11 |
// 2. Function set:
|
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:
|
16 |
// D = 0; Display off
|
17 |
// C = 0; Cursor off
|
18 |
// B = 0; Blinking off
|
19 |
// 4. Entry mode set:
|
20 |
// I/D = 1; Increment by 1
|
21 |
// S = 0; No shift
|
22 |
//
|
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).
|
26 |
|
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) |
30 |
{ |
31 |
init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
|
32 |
} |
33 |
|
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) |
37 |
{ |
38 |
init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7); |
39 |
} |
40 |
|
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) |
43 |
{ |
44 |
init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0); |
45 |
} |
46 |
|
47 |
LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable, |
48 |
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3) |
49 |
{ |
50 |
init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0); |
51 |
} |
52 |
|
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) |
56 |
{ |
57 |
_rs_pin = rs; |
58 |
_rw_pin = rw; |
59 |
_enable_pin = enable; |
60 |
|
61 |
_data_pins[0] = d0;
|
62 |
_data_pins[1] = d1;
|
63 |
_data_pins[2] = d2;
|
64 |
_data_pins[3] = d3;
|
65 |
_data_pins[4] = d4;
|
66 |
_data_pins[5] = d5;
|
67 |
_data_pins[6] = d6;
|
68 |
_data_pins[7] = d7;
|
69 |
|
70 |
pinMode(_rs_pin, OUTPUT); |
71 |
// we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
|
72 |
if (_rw_pin != 255) { |
73 |
pinMode(_rw_pin, OUTPUT); |
74 |
} |
75 |
pinMode(_enable_pin, OUTPUT); |
76 |
|
77 |
if (fourbitmode)
|
78 |
_displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS; |
79 |
else
|
80 |
_displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS; |
81 |
|
82 |
begin(16, 1); |
83 |
} |
84 |
|
85 |
void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
|
86 |
if (lines > 1) { |
87 |
_displayfunction |= LCD_2LINE; |
88 |
} |
89 |
_numlines = lines; |
90 |
_currline = 0;
|
91 |
|
92 |
// for some 1 line displays you can select a 10 pixel high font
|
93 |
if ((dotsize != 0) && (lines == 1)) { |
94 |
_displayfunction |= LCD_5x10DOTS; |
95 |
} |
96 |
|
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); |
106 |
} |
107 |
|
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
|
111 |
// figure 24, pg 46
|
112 |
|
113 |
// we start in 8bit mode, try to set 4 bit mode
|
114 |
write4bits(0x03);
|
115 |
delayMicroseconds(4500); // wait min 4.1ms |
116 |
|
117 |
// second try
|
118 |
write4bits(0x03);
|
119 |
delayMicroseconds(4500); // wait min 4.1ms |
120 |
|
121 |
// third go!
|
122 |
write4bits(0x03);
|
123 |
delayMicroseconds(150);
|
124 |
|
125 |
// finally, set to 4-bit interface
|
126 |
write4bits(0x02);
|
127 |
} else {
|
128 |
// this is according to the hitachi HD44780 datasheet
|
129 |
// page 45 figure 23
|
130 |
|
131 |
// Send function set command sequence
|
132 |
command(LCD_FUNCTIONSET | _displayfunction); |
133 |
delayMicroseconds(4500); // wait more than 4.1ms |
134 |
|
135 |
// second try
|
136 |
command(LCD_FUNCTIONSET | _displayfunction); |
137 |
delayMicroseconds(150);
|
138 |
|
139 |
// third go
|
140 |
command(LCD_FUNCTIONSET | _displayfunction); |
141 |
} |
142 |
|
143 |
// finally, set # lines, font size, etc.
|
144 |
command(LCD_FUNCTIONSET | _displayfunction); |
145 |
|
146 |
// turn the display on with no cursor or blinking default
|
147 |
_displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; |
148 |
display(); |
149 |
|
150 |
// clear it off
|
151 |
clear(); |
152 |
|
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); |
157 |
|
158 |
} |
159 |
|
160 |
/********** high level commands, for the user! */
|
161 |
void LiquidCrystal::clear()
|
162 |
{ |
163 |
command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
|
164 |
delayMicroseconds(2000); // this command takes a long time! |
165 |
} |
166 |
|
167 |
void LiquidCrystal::home()
|
168 |
{ |
169 |
command(LCD_RETURNHOME); // set cursor position to zero
|
170 |
delayMicroseconds(2000); // this command takes a long time! |
171 |
} |
172 |
|
173 |
void LiquidCrystal::setCursor(uint8_t col, uint8_t row)
|
174 |
{ |
175 |
int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 }; |
176 |
if ( row >= _numlines ) {
|
177 |
row = _numlines-1; // we count rows starting w/0 |
178 |
} |
179 |
|
180 |
command(LCD_SETDDRAMADDR | (col + row_offsets[row])); |
181 |
} |
182 |
|
183 |
// Turn the display on/off (quickly)
|
184 |
void LiquidCrystal::noDisplay() {
|
185 |
_displaycontrol &= ~LCD_DISPLAYON; |
186 |
command(LCD_DISPLAYCONTROL | _displaycontrol); |
187 |
} |
188 |
void LiquidCrystal::display() {
|
189 |
_displaycontrol |= LCD_DISPLAYON; |
190 |
command(LCD_DISPLAYCONTROL | _displaycontrol); |
191 |
} |
192 |
|
193 |
// Turns the underline cursor on/off
|
194 |
void LiquidCrystal::noCursor() {
|
195 |
_displaycontrol &= ~LCD_CURSORON; |
196 |
command(LCD_DISPLAYCONTROL | _displaycontrol); |
197 |
} |
198 |
void LiquidCrystal::cursor() {
|
199 |
_displaycontrol |= LCD_CURSORON; |
200 |
command(LCD_DISPLAYCONTROL | _displaycontrol); |
201 |
} |
202 |
|
203 |
// Turn on and off the blinking cursor
|
204 |
void LiquidCrystal::noBlink() {
|
205 |
_displaycontrol &= ~LCD_BLINKON; |
206 |
command(LCD_DISPLAYCONTROL | _displaycontrol); |
207 |
} |
208 |
void LiquidCrystal::blink() {
|
209 |
_displaycontrol |= LCD_BLINKON; |
210 |
command(LCD_DISPLAYCONTROL | _displaycontrol); |
211 |
} |
212 |
|
213 |
// These commands scroll the display without changing the RAM
|
214 |
void LiquidCrystal::scrollDisplayLeft(void) { |
215 |
command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT); |
216 |
} |
217 |
void LiquidCrystal::scrollDisplayRight(void) { |
218 |
command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT); |
219 |
} |
220 |
|
221 |
// This is for text that flows Left to Right
|
222 |
void LiquidCrystal::leftToRight(void) { |
223 |
_displaymode |= LCD_ENTRYLEFT; |
224 |
command(LCD_ENTRYMODESET | _displaymode); |
225 |
} |
226 |
|
227 |
// This is for text that flows Right to Left
|
228 |
void LiquidCrystal::rightToLeft(void) { |
229 |
_displaymode &= ~LCD_ENTRYLEFT; |
230 |
command(LCD_ENTRYMODESET | _displaymode); |
231 |
} |
232 |
|
233 |
// This will 'right justify' text from the cursor
|
234 |
void LiquidCrystal::autoscroll(void) { |
235 |
_displaymode |= LCD_ENTRYSHIFTINCREMENT; |
236 |
command(LCD_ENTRYMODESET | _displaymode); |
237 |
} |
238 |
|
239 |
// This will 'left justify' text from the cursor
|
240 |
void LiquidCrystal::noAutoscroll(void) { |
241 |
_displaymode &= ~LCD_ENTRYSHIFTINCREMENT; |
242 |
command(LCD_ENTRYMODESET | _displaymode); |
243 |
} |
244 |
|
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++) { |
251 |
write(charmap[i]); |
252 |
} |
253 |
} |
254 |
|
255 |
/*********** mid level commands, for sending data/cmds */
|
256 |
|
257 |
inline void LiquidCrystal::command(uint8_t value) { |
258 |
send(value, LOW); |
259 |
} |
260 |
|
261 |
inline size_t LiquidCrystal::write(uint8_t value) {
|
262 |
send(value, HIGH); |
263 |
return 1; // assume sucess |
264 |
} |
265 |
|
266 |
/************ low level data pushing commands **********/
|
267 |
|
268 |
// write either command or data, with automatic 4/8-bit selection
|
269 |
void LiquidCrystal::send(uint8_t value, uint8_t mode) {
|
270 |
digitalWrite(_rs_pin, mode); |
271 |
|
272 |
// if there is a RW pin indicated, set it low to Write
|
273 |
if (_rw_pin != 255) { |
274 |
digitalWrite(_rw_pin, LOW); |
275 |
} |
276 |
|
277 |
if (_displayfunction & LCD_8BITMODE) {
|
278 |
write8bits(value); |
279 |
} else {
|
280 |
write4bits(value>>4);
|
281 |
write4bits(value); |
282 |
} |
283 |
} |
284 |
|
285 |
void LiquidCrystal::pulseEnable(void) { |
286 |
digitalWrite(_enable_pin, LOW); |
287 |
delayMicroseconds(1);
|
288 |
digitalWrite(_enable_pin, HIGH); |
289 |
delayMicroseconds(1); // enable pulse must be >450ns |
290 |
digitalWrite(_enable_pin, LOW); |
291 |
delayMicroseconds(100); // commands need > 37us to settle |
292 |
} |
293 |
|
294 |
void LiquidCrystal::write4bits(uint8_t value) {
|
295 |
for (int i = 0; i < 4; i++) { |
296 |
pinMode(_data_pins[i], OUTPUT); |
297 |
digitalWrite(_data_pins[i], (value >> i) & 0x01);
|
298 |
} |
299 |
|
300 |
pulseEnable(); |
301 |
} |
302 |
|
303 |
void LiquidCrystal::write8bits(uint8_t value) {
|
304 |
for (int i = 0; i < 8; i++) { |
305 |
pinMode(_data_pins[i], OUTPUT); |
306 |
digitalWrite(_data_pins[i], (value >> i) & 0x01);
|
307 |
} |
308 |
|
309 |
pulseEnable(); |
310 |
} |