Serial Input to EEPROM
Receives Serial Data, Serial Parallel Conversion and Write To EEPROM
LiquidCrystal.cpp
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  if (fourbitmode)
71  _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
72  else
73  _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
74 
75  begin(16, 1);
76 }
77 
78 void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
79  if (lines > 1) {
80  _displayfunction |= LCD_2LINE;
81  }
82  _numlines = lines;
83 
84  setRowOffsets(0x00, 0x40, 0x00 + cols, 0x40 + cols);
85 
86  // for some 1 line displays you can select a 10 pixel high font
87  if ((dotsize != LCD_5x8DOTS) && (lines == 1)) {
88  _displayfunction |= LCD_5x10DOTS;
89  }
90 
91  pinMode(_rs_pin, OUTPUT);
92  // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
93  if (_rw_pin != 255) {
94  pinMode(_rw_pin, OUTPUT);
95  }
96  pinMode(_enable_pin, OUTPUT);
97 
98  // Do these once, instead of every time a character is drawn for speed reasons.
99  for (int i=0; i<((_displayfunction & LCD_8BITMODE) ? 8 : 4); ++i)
100  {
101  pinMode(_data_pins[i], OUTPUT);
102  }
103 
104  // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
105  // according to datasheet, we need at least 40ms after power rises above 2.7V
106  // before sending commands. Arduino can turn on way before 4.5V so we'll wait 50
107  delayMicroseconds(50000);
108  // Now we pull both RS and R/W low to begin commands
109  digitalWrite(_rs_pin, LOW);
110  digitalWrite(_enable_pin, LOW);
111  if (_rw_pin != 255) {
112  digitalWrite(_rw_pin, LOW);
113  }
114 
115  //put the LCD into 4 bit or 8 bit mode
116  if (! (_displayfunction & LCD_8BITMODE)) {
117  // this is according to the hitachi HD44780 datasheet
118  // figure 24, pg 46
119 
120  // we start in 8bit mode, try to set 4 bit mode
121  write4bits(0x03);
122  delayMicroseconds(4500); // wait min 4.1ms
123 
124  // second try
125  write4bits(0x03);
126  delayMicroseconds(4500); // wait min 4.1ms
127 
128  // third go!
129  write4bits(0x03);
130  delayMicroseconds(150);
131 
132  // finally, set to 4-bit interface
133  write4bits(0x02);
134  } else {
135  // this is according to the hitachi HD44780 datasheet
136  // page 45 figure 23
137 
138  // Send function set command sequence
139  command(LCD_FUNCTIONSET | _displayfunction);
140  delayMicroseconds(4500); // wait more than 4.1ms
141 
142  // second try
143  command(LCD_FUNCTIONSET | _displayfunction);
144  delayMicroseconds(150);
145 
146  // third go
147  command(LCD_FUNCTIONSET | _displayfunction);
148  }
149 
150  // finally, set # lines, font size, etc.
151  command(LCD_FUNCTIONSET | _displayfunction);
152 
153  // turn the display on with no cursor or blinking default
154  _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
155  display();
156 
157  // clear it off
158  clear();
159 
160  // Initialize to default text direction (for romance languages)
161  _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
162  // set the entry mode
163  command(LCD_ENTRYMODESET | _displaymode);
164 
165 }
166 
167 void LiquidCrystal::setRowOffsets(int row0, int row1, int row2, int row3)
168 {
169  _row_offsets[0] = row0;
170  _row_offsets[1] = row1;
171  _row_offsets[2] = row2;
172  _row_offsets[3] = row3;
173 }
174 
175 /********** high level commands, for the user! */
176 void LiquidCrystal::clear()
177 {
178  command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
179  delayMicroseconds(2000); // this command takes a long time!
180 }
181 
182 void LiquidCrystal::home()
183 {
184  command(LCD_RETURNHOME); // set cursor position to zero
185  delayMicroseconds(2000); // this command takes a long time!
186 }
187 
188 void LiquidCrystal::setCursor(uint8_t col, uint8_t row)
189 {
190  const size_t max_lines = sizeof(_row_offsets) / sizeof(*_row_offsets);
191  if ( row >= max_lines ) {
192  row = max_lines - 1; // we count rows starting w/0
193  }
194  if ( row >= _numlines ) {
195  row = _numlines - 1; // we count rows starting w/0
196  }
197 
198  command(LCD_SETDDRAMADDR | (col + _row_offsets[row]));
199 }
200 
201 // Turn the display on/off (quickly)
202 void LiquidCrystal::noDisplay() {
203  _displaycontrol &= ~LCD_DISPLAYON;
204  command(LCD_DISPLAYCONTROL | _displaycontrol);
205 }
206 void LiquidCrystal::display() {
207  _displaycontrol |= LCD_DISPLAYON;
208  command(LCD_DISPLAYCONTROL | _displaycontrol);
209 }
210 
211 // Turns the underline cursor on/off
212 void LiquidCrystal::noCursor() {
213  _displaycontrol &= ~LCD_CURSORON;
214  command(LCD_DISPLAYCONTROL | _displaycontrol);
215 }
216 void LiquidCrystal::cursor() {
217  _displaycontrol |= LCD_CURSORON;
218  command(LCD_DISPLAYCONTROL | _displaycontrol);
219 }
220 
221 // Turn on and off the blinking cursor
222 void LiquidCrystal::noBlink() {
223  _displaycontrol &= ~LCD_BLINKON;
224  command(LCD_DISPLAYCONTROL | _displaycontrol);
225 }
226 void LiquidCrystal::blink() {
227  _displaycontrol |= LCD_BLINKON;
228  command(LCD_DISPLAYCONTROL | _displaycontrol);
229 }
230 
231 // These commands scroll the display without changing the RAM
232 void LiquidCrystal::scrollDisplayLeft(void) {
233  command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
234 }
235 void LiquidCrystal::scrollDisplayRight(void) {
236  command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
237 }
238 
239 // This is for text that flows Left to Right
240 void LiquidCrystal::leftToRight(void) {
241  _displaymode |= LCD_ENTRYLEFT;
242  command(LCD_ENTRYMODESET | _displaymode);
243 }
244 
245 // This is for text that flows Right to Left
246 void LiquidCrystal::rightToLeft(void) {
247  _displaymode &= ~LCD_ENTRYLEFT;
248  command(LCD_ENTRYMODESET | _displaymode);
249 }
250 
251 // This will 'right justify' text from the cursor
252 void LiquidCrystal::autoscroll(void) {
253  _displaymode |= LCD_ENTRYSHIFTINCREMENT;
254  command(LCD_ENTRYMODESET | _displaymode);
255 }
256 
257 // This will 'left justify' text from the cursor
258 void LiquidCrystal::noAutoscroll(void) {
259  _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
260  command(LCD_ENTRYMODESET | _displaymode);
261 }
262 
263 // Allows us to fill the first 8 CGRAM locations
264 // with custom characters
265 void LiquidCrystal::createChar(uint8_t location, uint8_t charmap[]) {
266  location &= 0x7; // we only have 8 locations 0-7
267  command(LCD_SETCGRAMADDR | (location << 3));
268  for (int i=0; i<8; i++) {
269  write(charmap[i]);
270  }
271 }
272 
273 /*********** mid level commands, for sending data/cmds */
274 
275 inline void LiquidCrystal::command(uint8_t value) {
276  send(value, LOW);
277 }
278 
279 inline size_t LiquidCrystal::write(uint8_t value) {
280  send(value, HIGH);
281  return 1; // assume sucess
282 }
283 
284 /************ low level data pushing commands **********/
285 
286 // write either command or data, with automatic 4/8-bit selection
287 void LiquidCrystal::send(uint8_t value, uint8_t mode) {
288  digitalWrite(_rs_pin, mode);
289 
290  // if there is a RW pin indicated, set it low to Write
291  if (_rw_pin != 255) {
292  digitalWrite(_rw_pin, LOW);
293  }
294 
295  if (_displayfunction & LCD_8BITMODE) {
296  write8bits(value);
297  } else {
298  write4bits(value>>4);
299  write4bits(value);
300  }
301 }
302 
303 void LiquidCrystal::pulseEnable(void) {
304  digitalWrite(_enable_pin, LOW);
305  delayMicroseconds(1);
306  digitalWrite(_enable_pin, HIGH);
307  delayMicroseconds(1); // enable pulse must be >450ns
308  digitalWrite(_enable_pin, LOW);
309  delayMicroseconds(100); // commands need > 37us to settle
310 }
311 
312 void LiquidCrystal::write4bits(uint8_t value) {
313  for (int i = 0; i < 4; i++) {
314  digitalWrite(_data_pins[i], (value >> i) & 0x01);
315  }
316 
317  pulseEnable();
318 }
319 
320 void LiquidCrystal::write8bits(uint8_t value) {
321  for (int i = 0; i < 8; i++) {
322  digitalWrite(_data_pins[i], (value >> i) & 0x01);
323  }
324 
325  pulseEnable();
326 }