|
@@ -0,0 +1,389 @@
|
|
1
|
+#include "LiquidCrystalRus.h"
|
|
2
|
+
|
|
3
|
+#include <stdio.h>
|
|
4
|
+#include <string.h>
|
|
5
|
+#include <inttypes.h>
|
|
6
|
+#include <avr/pgmspace.h>
|
|
7
|
+
|
|
8
|
+#if defined(ARDUINO) && ARDUINO >= 100
|
|
9
|
+ #include "Arduino.h"
|
|
10
|
+#else
|
|
11
|
+ #include "WProgram.h"
|
|
12
|
+#endif
|
|
13
|
+
|
|
14
|
+// it is a russian alphabet translation
|
|
15
|
+// except 0401 --> 0xa2 = ╗, 0451 --> 0xb5
|
|
16
|
+PROGMEM prog_uchar utf_recode[] =
|
|
17
|
+ { 0x41,0xa0,0x42,0xa1,0xe0,0x45,0xa3,0xa4,0xa5,0xa6,0x4b,0xa7,0x4d,0x48,0x4f,
|
|
18
|
+ 0xa8,0x50,0x43,0x54,0xa9,0xaa,0x58,0xe1,0xab,0xac,0xe2,0xad,0xae,0x62,0xaf,0xb0,0xb1,
|
|
19
|
+ 0x61,0xb2,0xb3,0xb4,0xe3,0x65,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0x6f,
|
|
20
|
+ 0xbe,0x70,0x63,0xbf,0x79,0xe4,0x78,0xe5,0xc0,0xc1,0xe6,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7
|
|
21
|
+ };
|
|
22
|
+
|
|
23
|
+// When the display powers up, it is configured as follows:
|
|
24
|
+//
|
|
25
|
+// 1. Display clear
|
|
26
|
+// 2. Function set:
|
|
27
|
+// DL = 1; 8-bit interface data
|
|
28
|
+// N = 0; 1-line display
|
|
29
|
+// F = 0; 5x8 dot character font
|
|
30
|
+// 3. Display on/off control:
|
|
31
|
+// D = 0; Display off
|
|
32
|
+// C = 0; Cursor off
|
|
33
|
+// B = 0; Blinking off
|
|
34
|
+// 4. Entry mode set:
|
|
35
|
+// I/D = 1; Increment by 1
|
|
36
|
+// S = 0; No shift
|
|
37
|
+//
|
|
38
|
+// Note, however, that resetting the Arduino doesn't reset the LCD, so we
|
|
39
|
+// can't assume that its in that state when a sketch starts (and the
|
|
40
|
+// LiquidCrystal constructor is called).
|
|
41
|
+//
|
|
42
|
+// modified 27 Jul 2011
|
|
43
|
+// by Ilya V. Danilov http://mk90.ru/
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+LiquidCrystalRus::LiquidCrystalRus(uint8_t rs, uint8_t rw, uint8_t enable,
|
|
47
|
+ uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
|
|
48
|
+ uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
|
|
49
|
+{
|
|
50
|
+ init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
|
|
51
|
+}
|
|
52
|
+
|
|
53
|
+LiquidCrystalRus::LiquidCrystalRus(uint8_t rs, 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
|
+ init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7);
|
|
58
|
+}
|
|
59
|
+
|
|
60
|
+LiquidCrystalRus::LiquidCrystalRus(uint8_t rs, uint8_t rw, uint8_t enable,
|
|
61
|
+ uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
|
|
62
|
+{
|
|
63
|
+ init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0);
|
|
64
|
+}
|
|
65
|
+
|
|
66
|
+LiquidCrystalRus::LiquidCrystalRus(uint8_t rs, uint8_t enable,
|
|
67
|
+ uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
|
|
68
|
+{
|
|
69
|
+ init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
|
|
70
|
+}
|
|
71
|
+
|
|
72
|
+void LiquidCrystalRus::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
|
|
73
|
+ uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
|
|
74
|
+ uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
|
|
75
|
+{
|
|
76
|
+ _rs_pin = rs;
|
|
77
|
+ _rw_pin = rw;
|
|
78
|
+ _enable_pin = enable;
|
|
79
|
+
|
|
80
|
+ _data_pins[0] = d0;
|
|
81
|
+ _data_pins[1] = d1;
|
|
82
|
+ _data_pins[2] = d2;
|
|
83
|
+ _data_pins[3] = d3;
|
|
84
|
+ _data_pins[4] = d4;
|
|
85
|
+ _data_pins[5] = d5;
|
|
86
|
+ _data_pins[6] = d6;
|
|
87
|
+ _data_pins[7] = d7;
|
|
88
|
+
|
|
89
|
+ pinMode(_rs_pin, OUTPUT);
|
|
90
|
+ // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
|
|
91
|
+ if (_rw_pin != 255) {
|
|
92
|
+ pinMode(_rw_pin, OUTPUT);
|
|
93
|
+ }
|
|
94
|
+ pinMode(_enable_pin, OUTPUT);
|
|
95
|
+
|
|
96
|
+ if (fourbitmode)
|
|
97
|
+ _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
|
|
98
|
+ else
|
|
99
|
+ _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
|
|
100
|
+
|
|
101
|
+ begin(16, 1);
|
|
102
|
+}
|
|
103
|
+
|
|
104
|
+void LiquidCrystalRus::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
|
|
105
|
+ if (lines > 1) {
|
|
106
|
+ _displayfunction |= LCD_2LINE;
|
|
107
|
+ }
|
|
108
|
+ _numlines = lines;
|
|
109
|
+ _currline = 0;
|
|
110
|
+
|
|
111
|
+ // for some 1 line displays you can select a 10 pixel high font
|
|
112
|
+ if ((dotsize != 0) && (lines == 1)) {
|
|
113
|
+ _displayfunction |= LCD_5x10DOTS;
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
|
|
117
|
+ // according to datasheet, we need at least 40ms after power rises above 2.7V
|
|
118
|
+ // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
|
|
119
|
+ delayMicroseconds(50000);
|
|
120
|
+ // Now we pull both RS and R/W low to begin commands
|
|
121
|
+ digitalWrite(_rs_pin, LOW);
|
|
122
|
+ digitalWrite(_enable_pin, LOW);
|
|
123
|
+ if (_rw_pin != 255) {
|
|
124
|
+ digitalWrite(_rw_pin, LOW);
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ //put the LCD into 4 bit or 8 bit mode
|
|
128
|
+ if (! (_displayfunction & LCD_8BITMODE)) {
|
|
129
|
+ // this is according to the hitachi HD44780 datasheet
|
|
130
|
+ // figure 24, pg 46
|
|
131
|
+
|
|
132
|
+ // we start in 8bit mode, try to set 4 bit mode
|
|
133
|
+ writeNbits(0x03,4);
|
|
134
|
+ delayMicroseconds(4500); // wait min 4.1ms
|
|
135
|
+
|
|
136
|
+ // second try
|
|
137
|
+ writeNbits(0x03,4);
|
|
138
|
+ delayMicroseconds(4500); // wait min 4.1ms
|
|
139
|
+
|
|
140
|
+ // third go!
|
|
141
|
+ writeNbits(0x03,4);
|
|
142
|
+ delayMicroseconds(150);
|
|
143
|
+
|
|
144
|
+ // finally, set to 8-bit interface
|
|
145
|
+ writeNbits(0x02,4);
|
|
146
|
+ } else {
|
|
147
|
+ // this is according to the hitachi HD44780 datasheet
|
|
148
|
+ // page 45 figure 23
|
|
149
|
+
|
|
150
|
+ // Send function set command sequence
|
|
151
|
+ command(LCD_FUNCTIONSET | _displayfunction);
|
|
152
|
+ delayMicroseconds(4500); // wait more than 4.1ms
|
|
153
|
+
|
|
154
|
+ // second try
|
|
155
|
+ command(LCD_FUNCTIONSET | _displayfunction);
|
|
156
|
+ delayMicroseconds(150);
|
|
157
|
+
|
|
158
|
+ // third go
|
|
159
|
+ command(LCD_FUNCTIONSET | _displayfunction);
|
|
160
|
+ }
|
|
161
|
+
|
|
162
|
+ // finally, set # lines, font size, etc.
|
|
163
|
+ command(LCD_FUNCTIONSET | _displayfunction);
|
|
164
|
+
|
|
165
|
+ // turn the display on with no cursor or blinking default
|
|
166
|
+ _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
|
|
167
|
+ display();
|
|
168
|
+
|
|
169
|
+ // clear it off
|
|
170
|
+ clear();
|
|
171
|
+
|
|
172
|
+ // Initialize to default text direction (for romance languages)
|
|
173
|
+ _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
|
|
174
|
+ // set the entry mode
|
|
175
|
+ command(LCD_ENTRYMODESET | _displaymode);
|
|
176
|
+
|
|
177
|
+}
|
|
178
|
+
|
|
179
|
+void LiquidCrystalRus::setDRAMModel(uint8_t model) {
|
|
180
|
+ _dram_model = model;
|
|
181
|
+}
|
|
182
|
+
|
|
183
|
+/********** high level commands, for the user! */
|
|
184
|
+void LiquidCrystalRus::clear()
|
|
185
|
+{
|
|
186
|
+ command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
|
|
187
|
+ delayMicroseconds(2000); // this command takes a long time!
|
|
188
|
+}
|
|
189
|
+
|
|
190
|
+void LiquidCrystalRus::home()
|
|
191
|
+{
|
|
192
|
+ command(LCD_RETURNHOME); // set cursor position to zero
|
|
193
|
+ delayMicroseconds(2000); // this command takes a long time!
|
|
194
|
+}
|
|
195
|
+
|
|
196
|
+void LiquidCrystalRus::setCursor(uint8_t col, uint8_t row)
|
|
197
|
+{
|
|
198
|
+ int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
|
|
199
|
+ if ( row >= _numlines ) {
|
|
200
|
+ row = _numlines-1; // we count rows starting w/0
|
|
201
|
+ }
|
|
202
|
+
|
|
203
|
+ command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
|
|
204
|
+}
|
|
205
|
+
|
|
206
|
+// Turn the display on/off (quickly)
|
|
207
|
+void LiquidCrystalRus::noDisplay() {
|
|
208
|
+ _displaycontrol &= ~LCD_DISPLAYON;
|
|
209
|
+ command(LCD_DISPLAYCONTROL | _displaycontrol);
|
|
210
|
+}
|
|
211
|
+void LiquidCrystalRus::display() {
|
|
212
|
+ _displaycontrol |= LCD_DISPLAYON;
|
|
213
|
+ command(LCD_DISPLAYCONTROL | _displaycontrol);
|
|
214
|
+}
|
|
215
|
+
|
|
216
|
+// Turns the underline cursor on/off
|
|
217
|
+void LiquidCrystalRus::noCursor() {
|
|
218
|
+ _displaycontrol &= ~LCD_CURSORON;
|
|
219
|
+ command(LCD_DISPLAYCONTROL | _displaycontrol);
|
|
220
|
+}
|
|
221
|
+void LiquidCrystalRus::cursor() {
|
|
222
|
+ _displaycontrol |= LCD_CURSORON;
|
|
223
|
+ command(LCD_DISPLAYCONTROL | _displaycontrol);
|
|
224
|
+}
|
|
225
|
+
|
|
226
|
+// Turn on and off the blinking cursor
|
|
227
|
+void LiquidCrystalRus::noBlink() {
|
|
228
|
+ _displaycontrol &= ~LCD_BLINKON;
|
|
229
|
+ command(LCD_DISPLAYCONTROL | _displaycontrol);
|
|
230
|
+}
|
|
231
|
+void LiquidCrystalRus::blink() {
|
|
232
|
+ _displaycontrol |= LCD_BLINKON;
|
|
233
|
+ command(LCD_DISPLAYCONTROL | _displaycontrol);
|
|
234
|
+}
|
|
235
|
+
|
|
236
|
+// These commands scroll the display without changing the RAM
|
|
237
|
+void LiquidCrystalRus::scrollDisplayLeft(void) {
|
|
238
|
+ command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
|
|
239
|
+}
|
|
240
|
+void LiquidCrystalRus::scrollDisplayRight(void) {
|
|
241
|
+ command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
|
|
242
|
+}
|
|
243
|
+
|
|
244
|
+// This is for text that flows Left to Right
|
|
245
|
+void LiquidCrystalRus::leftToRight(void) {
|
|
246
|
+ _displaymode |= LCD_ENTRYLEFT;
|
|
247
|
+ command(LCD_ENTRYMODESET | _displaymode);
|
|
248
|
+}
|
|
249
|
+
|
|
250
|
+// This is for text that flows Right to Left
|
|
251
|
+void LiquidCrystalRus::rightToLeft(void) {
|
|
252
|
+ _displaymode &= ~LCD_ENTRYLEFT;
|
|
253
|
+ command(LCD_ENTRYMODESET | _displaymode);
|
|
254
|
+}
|
|
255
|
+
|
|
256
|
+// This will 'right justify' text from the cursor
|
|
257
|
+void LiquidCrystalRus::autoscroll(void) {
|
|
258
|
+ _displaymode |= LCD_ENTRYSHIFTINCREMENT;
|
|
259
|
+ command(LCD_ENTRYMODESET | _displaymode);
|
|
260
|
+}
|
|
261
|
+
|
|
262
|
+// This will 'left justify' text from the cursor
|
|
263
|
+void LiquidCrystalRus::noAutoscroll(void) {
|
|
264
|
+ _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
|
|
265
|
+ command(LCD_ENTRYMODESET | _displaymode);
|
|
266
|
+}
|
|
267
|
+
|
|
268
|
+// Allows us to fill the first 8 CGRAM locations
|
|
269
|
+// with custom characters
|
|
270
|
+void LiquidCrystalRus::createChar(uint8_t location, uint8_t charmap[]) {
|
|
271
|
+ location &= 0x7; // we only have 8 locations 0-7
|
|
272
|
+ command(LCD_SETCGRAMADDR | (location << 3));
|
|
273
|
+ for (int i=0; i<8; i++) {
|
|
274
|
+ write(charmap[i]);
|
|
275
|
+ }
|
|
276
|
+}
|
|
277
|
+
|
|
278
|
+/*********** mid level commands, for sending data/cmds */
|
|
279
|
+
|
|
280
|
+inline void LiquidCrystalRus::command(uint8_t value) {
|
|
281
|
+ send(value, LOW);
|
|
282
|
+}
|
|
283
|
+
|
|
284
|
+#if defined(ARDUINO) && ARDUINO >= 100
|
|
285
|
+ size_t LiquidCrystalRus::write(uint8_t value)
|
|
286
|
+#else
|
|
287
|
+ void LiquidCrystalRus::write(uint8_t value)
|
|
288
|
+#endif
|
|
289
|
+{
|
|
290
|
+ uint8_t out_char=value;
|
|
291
|
+
|
|
292
|
+ if (_dram_model == LCD_DRAM_WH1601) {
|
|
293
|
+ uint8_t ac=recv(LOW) & 0x7f;
|
|
294
|
+ if (ac>7 && ac<0x14) command(LCD_SETDDRAMADDR | (0x40+ac-8));
|
|
295
|
+ }
|
|
296
|
+
|
|
297
|
+ if (value>=0x80) { // UTF-8 handling
|
|
298
|
+ if (value >= 0xc0) {
|
|
299
|
+ utf_hi_char = value - 0xd0;
|
|
300
|
+ } else {
|
|
301
|
+ value &= 0x3f;
|
|
302
|
+ if (!utf_hi_char && (value == 1))
|
|
303
|
+ send(0xa2,HIGH); // ╗
|
|
304
|
+ else if ((utf_hi_char == 1) && (value == 0x11))
|
|
305
|
+ send(0xb5,HIGH); // ╦
|
|
306
|
+ else
|
|
307
|
+ send(pgm_read_byte_near(utf_recode + value + (utf_hi_char<<6) - 0x10), HIGH);
|
|
308
|
+ }
|
|
309
|
+ } else send(out_char, HIGH);
|
|
310
|
+#if defined(ARDUINO) && ARDUINO >= 100
|
|
311
|
+ return 1; // assume sucess
|
|
312
|
+#endif
|
|
313
|
+}
|
|
314
|
+
|
|
315
|
+/************ low level data pushing commands **********/
|
|
316
|
+
|
|
317
|
+// write either command or data, with automatic 4/8-bit selection
|
|
318
|
+void LiquidCrystalRus::send(uint8_t value, uint8_t mode) {
|
|
319
|
+ digitalWrite(_rs_pin, mode);
|
|
320
|
+
|
|
321
|
+ // if there is a RW pin indicated, set it low to Write
|
|
322
|
+ if (_rw_pin != 255) {
|
|
323
|
+ digitalWrite(_rw_pin, LOW);
|
|
324
|
+ }
|
|
325
|
+
|
|
326
|
+ if (_displayfunction & LCD_8BITMODE) {
|
|
327
|
+ writeNbits(value,8);
|
|
328
|
+ } else {
|
|
329
|
+ writeNbits(value>>4,4);
|
|
330
|
+ writeNbits(value,4);
|
|
331
|
+ }
|
|
332
|
+}
|
|
333
|
+
|
|
334
|
+// read data, with automatic 4/8-bit selection
|
|
335
|
+uint8_t LiquidCrystalRus::recv(uint8_t mode) {
|
|
336
|
+ uint8_t retval;
|
|
337
|
+ digitalWrite(_rs_pin, mode);
|
|
338
|
+
|
|
339
|
+ // if there is a RW pin indicated, set it low to Write
|
|
340
|
+ if (_rw_pin != 255) {
|
|
341
|
+ digitalWrite(_rw_pin, HIGH);
|
|
342
|
+ }
|
|
343
|
+
|
|
344
|
+ if (_displayfunction & LCD_8BITMODE) {
|
|
345
|
+ retval = readNbits(8);
|
|
346
|
+ } else {
|
|
347
|
+ retval = readNbits(4) << 4;
|
|
348
|
+ retval |= readNbits(4);
|
|
349
|
+ }
|
|
350
|
+ return retval;
|
|
351
|
+}
|
|
352
|
+void LiquidCrystalRus::pulseEnable() {
|
|
353
|
+ digitalWrite(_enable_pin, LOW);
|
|
354
|
+ delayMicroseconds(1);
|
|
355
|
+ digitalWrite(_enable_pin, HIGH);
|
|
356
|
+ delayMicroseconds(1); // enable pulse must be >450ns
|
|
357
|
+ digitalWrite(_enable_pin, LOW);
|
|
358
|
+ delayMicroseconds(100); // commands need > 37us to settle
|
|
359
|
+}
|
|
360
|
+
|
|
361
|
+void LiquidCrystalRus::writeNbits(uint8_t value, uint8_t n) {
|
|
362
|
+ for (int i = 0; i < n; i++) {
|
|
363
|
+ pinMode(_data_pins[i], OUTPUT);
|
|
364
|
+ digitalWrite(_data_pins[i], (value >> i) & 0x01);
|
|
365
|
+ }
|
|
366
|
+
|
|
367
|
+ pulseEnable();
|
|
368
|
+}
|
|
369
|
+
|
|
370
|
+uint8_t LiquidCrystalRus::readNbits(uint8_t n) {
|
|
371
|
+ uint8_t retval=0;
|
|
372
|
+ for (int i = 0; i < n; i++) {
|
|
373
|
+ pinMode(_data_pins[i], INPUT);
|
|
374
|
+ }
|
|
375
|
+
|
|
376
|
+ digitalWrite(_enable_pin, LOW);
|
|
377
|
+ delayMicroseconds(1);
|
|
378
|
+ digitalWrite(_enable_pin, HIGH);
|
|
379
|
+ delayMicroseconds(1); // enable pulse must be >450ns
|
|
380
|
+
|
|
381
|
+ for (int i = 0; i < n; i++) {
|
|
382
|
+ retval |= (digitalRead(_data_pins[i]) == HIGH)?(1 << i):0;
|
|
383
|
+ }
|
|
384
|
+
|
|
385
|
+ digitalWrite(_enable_pin, LOW);
|
|
386
|
+
|
|
387
|
+ return retval;
|
|
388
|
+}
|
|
389
|
+
|