Alphanumeric LCD Library for Arduino  1.0.4
LCD Control library for Arduino
 All Classes Files Functions Variables Macros Pages
/Volumes/John Doe/Firmware/Arduino LIbraries/AlphaLCD/AlphaLCD.cpp
Go to the documentation of this file.
1 
22 #include "AlphaLCD.h"
23 
24 #include <stdio.h>
25 #include <string.h>
26 #include <inttypes.h>
27 #include "Arduino.h"
28 
33 
34 }
35 
41 AlphaLCD::AlphaLCD(uint8_t dp, uint8_t cp, uint8_t lp)
42 {
43  init (dp, cp, lp);
44 }
45 
56 void AlphaLCD::init(uint8_t dp, uint8_t cp, uint8_t lp)
57 {
58  _data_pin = dp;
59  _clock_pin = cp;
60  _latch_pin = lp;
61 
63 
64  pinMode(_data_pin, OUTPUT);
65  pinMode(_clock_pin, OUTPUT);
66  pinMode(_latch_pin, OUTPUT);
67 
69 
70  // Display size (characters and lines). By default it is set to 16 characters x 2 lines
71  // but should be set with the right number of lines and columns,
72  // accordingly with the hardware specifications
73  begin(16, 2);
74 }
75 
83 void AlphaLCD::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
84  if (lines > 1) {
86  }
87 
88  _numlines = lines;
89  _currline = 0;
90 
91  // for some 1 line displays you can select a 10 pixel height font
92  if ((dotsize != 0) && (lines == 1)) {
94  }
95 
96  // We need at least 40ms after power rises before sending commands.
97  delayMicroseconds(50000);
98  // clear the shift register by sending 16 0's to it (twice)
99  shiftOut(_data_pin, _clock_pin, LSBFIRST, B00000000);
100  shiftOut(_data_pin, _clock_pin, LSBFIRST, B00000000);
101 
102  //put the LCD into 4 bit or 8 bit mode
103  if (! (_displayfunction & LCD_8BITMODE)) {
104 
105  // we start in 8bit mode, try to set 4 bit mode
106  write4bits(0x03, LOW);
107  delayMicroseconds(4500);
108 
109  // second try
110  write4bits(0x03, LOW);
111  delayMicroseconds(4500);
112 
113  // third go!
114  write4bits(0x03, LOW);
115  delayMicroseconds(150);
116 
117  // set to 8-bit interface
118  write4bits(0x02, LOW);
119  } else {
120  // Send function set command sequence
122  delayMicroseconds(4500);
123 
124  // second try
126  delayMicroseconds(150);
127 
128  // third go
130  }
131 
132  // finally, set # lines, font size, etc.
134 
135  // turn the display on with no cursor or blinking by default
137  display();
138 
139  // clear it off
140  clear();
141 
142  // Initialize to default text direction (for romance languages)
144  // set the entry mode
146 
147 }
148 
153 {
155  delayMicroseconds(2000);
156 }
157 
162 {
164  delayMicroseconds(2000);
165 }
166 
176 void AlphaLCD::setCursor(uint8_t col, uint8_t row)
177 {
178  int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
179  if ( row > _numlines ) {
180  row = _numlines-1;
181  }
182 
183  command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
184 }
185 
192 }
193 
200 }
201 
208 }
209 
216 }
217 
224 }
225 
232 }
233 
240 }
241 
248 }
249 
256 }
257 
264 }
265 
273 }
274 
281 }
282 
285 inline void AlphaLCD::isDisplay(bool set) {
286  set ? display() : noDisplay();
287 }
288 
291 inline void AlphaLCD::isCursor(bool set) {
292  set ? cursor() : noCursor();
293 }
294 
297 inline void AlphaLCD::isBlinking(bool set) {
298  set ? blink() : noBlink();
299 }
300 
303 inline void AlphaLCD::isRightToLeft(bool set) {
304  set ? rightToLeft() : leftToRight();
305 }
306 
309 inline void AlphaLCD::isAutoscroll(bool set) {
310  set ? autoscroll() : noAutoscroll();
311 }
312 
324 void AlphaLCD::createChar(uint8_t location, uint8_t charmap[]) {
325 
326  location &= 0x7; // No more than 7 memory locations
327 
328  command(LCD_SETCGRAMADDR | (location << 3));
329 
330  // Character definition loop
331  for (int i=0; i<8; i++) {
332  write(charmap[i]);
333  }
334 }
335 
339 inline void AlphaLCD::command(uint8_t value) {
340  send(value, false);
341 }
342 
346 inline size_t AlphaLCD::write(uint8_t value) {
347  send(value, true);
348 }
349 
353 void AlphaLCD::send(uint8_t value, uint8_t mode) {
354 
356  write8bits(value, mode);
357  } else {
358  write4bits(value>>4, mode);
359  write4bits(value, mode);
360  }
361 }
362 
369 void AlphaLCD::write4bits(uint8_t value, uint8_t mode) {
370  int EN_SWITCH = B00000010;
371  int RS_SWITCH = B00000001;
372  int cmd = 0;
373  int data = 0;
374  if (!mode) {
375  cmd = 0 | _backlight;
376  } else {
377  cmd = LCD_RS_PIN | _backlight;
378  }
379  data = value<<4 & B11110000;
380  cmd |= EN_SWITCH;
381  digitalWrite(_latch_pin, HIGH);
382  shiftOut(_data_pin, _clock_pin, LSBFIRST, data | cmd);
383  digitalWrite(_latch_pin, LOW);
384  delayMicroseconds(1);
385 
386  cmd &= ~EN_SWITCH;
387  digitalWrite(_latch_pin, HIGH);
388  shiftOut (_data_pin, _clock_pin, LSBFIRST, data | cmd);
389  digitalWrite(_latch_pin, LOW);
390  delayMicroseconds(1);
391 
392  cmd |= EN_SWITCH;
393  digitalWrite(_latch_pin, HIGH);
394  shiftOut(_data_pin, _clock_pin, LSBFIRST, data | cmd);
395  digitalWrite(_latch_pin, LOW);
396 
397  delayMicroseconds(100);
398 }
399 
406 void AlphaLCD::write8bits(uint8_t value, uint8_t mode) {
407  int EN_SWITCH = B00000010;
408  int RS_SWITCH = B00000001;
409  int cmd = 0;
410  if (!mode) {
411  cmd = 0;
412  } else {
413  cmd = RS_SWITCH;
414  }
415  //set enable low
416  cmd |= EN_SWITCH;
417  digitalWrite (_latch_pin, HIGH);
418  shiftOut(_data_pin, _clock_pin, LSBFIRST, cmd);
419  shiftOut(_data_pin, _clock_pin, LSBFIRST, value);
420  digitalWrite (_latch_pin, LOW);
421  //delay (500);
422  //set enable high;
423  cmd &= ~EN_SWITCH;
424  digitalWrite (_latch_pin, HIGH);
425  shiftOut(_data_pin, _clock_pin, LSBFIRST, cmd);
426  shiftOut(_data_pin, _clock_pin, LSBFIRST, value);
427  digitalWrite (_latch_pin, LOW);
428  delayMicroseconds (1);
429  //delay (500);
430  //set enable low
431  cmd |= EN_SWITCH;
432  digitalWrite (_latch_pin, HIGH);
433  shiftOut(_data_pin, _clock_pin, LSBFIRST, cmd);
434  shiftOut(_data_pin, _clock_pin, LSBFIRST, value);
435  digitalWrite (_latch_pin, LOW);
436  delayMicroseconds (100);
437 
438 }
439 
void noAutoscroll()
Disable the automatic horizontal scrolling of the text.
Definition: AlphaLCD.cpp:278
#define LCD_MOVELEFT
Definition: AlphaLCD.h:52
uint8_t _displaymode
Definition: AlphaLCD.h:111
#define LCD_CLEARDISPLAY
Definition: AlphaLCD.h:28
#define LCD_FUNCTIONSET
Definition: AlphaLCD.h:33
#define LCD_5x8DOTS
Definition: AlphaLCD.h:59
#define LCD_5x10DOTS
Definition: AlphaLCD.h:58
void noCursor()
Turn off the underline cursor.
Definition: AlphaLCD.cpp:205
#define LCD_4BITMODE
Definition: AlphaLCD.h:55
#define LCD_1LINE
Definition: AlphaLCD.h:57
uint8_t _clock_pin
Definition: AlphaLCD.h:105
void isDisplay(bool set)
Helper method to set on/off the display.
Definition: AlphaLCD.cpp:285
void createChar(uint8_t, uint8_t[])
Create one of the 8 CGRAM memory locations from 0x00 to 0x07 with a user defined characters.
Definition: AlphaLCD.cpp:324
void leftToRight()
Flow the text from left to right.
Definition: AlphaLCD.cpp:253
void noDisplay()
Turn off the display.
Definition: AlphaLCD.cpp:189
#define LCD_CURSORSHIFT
Definition: AlphaLCD.h:32
#define LCD_SETCGRAMADDR
Definition: AlphaLCD.h:34
uint8_t _data_pin
Definition: AlphaLCD.h:106
void cursor()
Turn on the underline cursor.
Definition: AlphaLCD.cpp:213
void command(uint8_t)
Helper method to send commands to the device.
Definition: AlphaLCD.cpp:339
uint8_t _numlines
Definition: AlphaLCD.h:114
void blink()
Turn off the blinking cursor.
Definition: AlphaLCD.cpp:229
void write4bits(uint8_t, uint8_t)
Write the character on the device, 4 bits mode.
Definition: AlphaLCD.cpp:369
void send(uint8_t, uint8_t)
Write either command or data, with automatic 4/8-bit selection.
Definition: AlphaLCD.cpp:353
void isAutoscroll(bool set)
Helper method to set on/off autoscroll.
Definition: AlphaLCD.cpp:309
#define LCD_DISPLAYMOVE
Definition: AlphaLCD.h:49
#define LCD_8BITMODE
Definition: AlphaLCD.h:54
#define LCD_BLINKON
Definition: AlphaLCD.h:46
#define LCD_ENTRYMODESET
Definition: AlphaLCD.h:30
void begin(uint8_t cols, uint8_t rows, uint8_t charsize=0x00)
Start the LCD modes and initializes the software configuration parameters.
Definition: AlphaLCD.cpp:83
void write8bits(uint8_t, uint8_t)
Write the character on the device, 8 bits mode.
Definition: AlphaLCD.cpp:406
#define LCD_CURSOROFF
Definition: AlphaLCD.h:45
uint8_t _backlight
Definition: AlphaLCD.h:112
#define LCD_RS_PIN
Definition: AlphaLCD.h:61
#define LCD_CURSORON
Definition: AlphaLCD.h:44
void isRightToLeft(bool set)
Helper method to set on/off the right-to-left writing direction.
Definition: AlphaLCD.cpp:303
uint8_t _displayfunction
Definition: AlphaLCD.h:109
void scrollDisplayRight()
Scroll the display to the right by one position.
Definition: AlphaLCD.cpp:246
void scrollDisplayLeft()
Scroll the display to the left by one position.
Definition: AlphaLCD.cpp:238
#define LCD_ENTRYLEFT
Definition: AlphaLCD.h:38
uint8_t _displaycontrol
Definition: AlphaLCD.h:110
void isCursor(bool set)
Helper method to set on/off the cursor visibility.
Definition: AlphaLCD.cpp:291
uint8_t _latch_pin
Definition: AlphaLCD.h:107
void autoscroll()
Enable the automatic horizontal scrolling of the text.
Definition: AlphaLCD.cpp:270
uint8_t _currline
Definition: AlphaLCD.h:114
void display()
Turn on the display.
Definition: AlphaLCD.cpp:197
void isBlinking(bool set)
Helper method to set on/off the blinking cursor.
Definition: AlphaLCD.cpp:297
void home()
Set the cursor to the position (0,0)
Definition: AlphaLCD.cpp:161
#define LCD_SETDDRAMADDR
Definition: AlphaLCD.h:35
#define LCD_MOVERIGHT
Definition: AlphaLCD.h:51
void noBlink()
Turn off the blinking cursor.
Definition: AlphaLCD.cpp:221
#define LCD_RETURNHOME
Definition: AlphaLCD.h:29
#define LCD_DISPLAYON
Definition: AlphaLCD.h:42
AlphaLCD, allows the LCD to be operated via a shift register.
#define LCD_ENTRYSHIFTDECREMENT
Definition: AlphaLCD.h:40
void init(uint8_t dp, uint8_t cp, uint8_t lp)
Hardware initialization.
Definition: AlphaLCD.cpp:56
#define LCD_ENTRYSHIFTINCREMENT
Definition: AlphaLCD.h:39
void setCursor(uint8_t, uint8_t)
Set the cursor to the requested position.
Definition: AlphaLCD.cpp:176
#define LCD_BL_PIN
Definition: AlphaLCD.h:63
virtual size_t write(uint8_t)
Helper method to send data to the device.
Definition: AlphaLCD.cpp:346
void rightToLeft()
Flow the text from right to left.
Definition: AlphaLCD.cpp:261
#define LCD_BLINKOFF
Definition: AlphaLCD.h:47
#define LCD_2LINE
Definition: AlphaLCD.h:56
#define LCD_DISPLAYCONTROL
Definition: AlphaLCD.h:31
AlphaLCD()
Constructor with no parameters, to create initial class instances.
Definition: AlphaLCD.cpp:32
void clear()
Clear the display content and set the cursor to the position (0,0)
Definition: AlphaLCD.cpp:152