Arduino-LCD
LCD library for Arduino
HD44780.h
Go to the documentation of this file.
1 
19 #ifndef HD44780_H
20 #define HD44780_H
21 
22 #include "LCD.h"
23 
34 class HD44780 : public LCD::Device {
35 public:
40  class Adapter {
41  public:
48  virtual bool setup()
49  {
50  return (false);
51  }
52 
58  virtual void write4b(uint8_t data) = 0;
59 
65  virtual void write8b(uint8_t data)
66  {
67  write4b(data >> 4);
68  write4b(data);
69  }
70 
77  virtual void write8n(const void* buf, size_t size)
78  {
79  const uint8_t* bp = (const uint8_t*) buf;
80  while (size--) write8b(*bp++);
81  }
82 
89  virtual void set_mode(uint8_t flag) = 0;
90 
96  virtual void set_backlight(uint8_t flag) = 0;
97  };
98 
100  static const uint8_t BITMAP_MAX = 8;
101 
103  const uint8_t WIDTH;
104 
106  const uint8_t HEIGHT;
107 
115  HD44780(Adapter& io, uint8_t width = 16, uint8_t height = 2) :
116  LCD::Device(),
117  WIDTH(width),
118  HEIGHT(height),
119  m_io(io),
123  m_offset(NULL)
124  {}
125 
132  virtual bool begin()
133  {
134  // Initiate display; See fig. 24, 4-bit interface, pp. 46.
135  // http://web.alfredstate.edu/weimandn/lcd/lcd_initialization/-
136  // LCD%204-bit%20Initialization%20v06.pdf
137  static const uint8_t offset0[] PROGMEM = { 0x00, 0x40, 0x14, 0x54 };
138  static const uint8_t offset1[] PROGMEM = { 0x00, 0x40, 0x10, 0x50 };
139  m_offset = ((HEIGHT == 4) && (WIDTH == 16) ? offset1 : offset0);
140  const uint8_t FS0 = (FUNCTION_SET | DATA_LENGTH_8BITS);
141  const uint8_t FS1 = (FUNCTION_SET | DATA_LENGTH_4BITS);
142  bool mode = m_io.setup();
143  delay(POWER_ON_TIME);
144  // 4-bit initialization mode
145  if (!mode) {
146  m_io.write4b(FS0 >> 4);
147  delayMicroseconds(INIT0_TIME);
148  m_io.write4b(FS0 >> 4);
149  delayMicroseconds(INIT1_TIME);
150  m_io.write4b(FS0 >> 4);
151  delayMicroseconds(INIT1_TIME);
152  m_io.write4b(FS1 >> 4);
153  delayMicroseconds(INIT1_TIME);
154  }
155  // 8-bit initialization mode
156  else {
158  }
159 
160  // Initialization with the function, control and mode setting
163 
164  // Initialization completed. Turn on the display and backlight
166  display_on();
167  backlight_on();
168  display_clear();
169  return (true);
170  }
171 
176  virtual void backlight_on()
177  {
178  m_io.set_backlight(true);
179  }
180 
185  virtual void backlight_off()
186  {
187  m_io.set_backlight(false);
188  }
189 
194  virtual void display_on()
195  {
197  }
198 
203  virtual void display_off()
204  {
206  }
207 
212  virtual void display_clear()
213  {
215  m_x = 0;
216  m_y = 0;
217  m_mode |= INCREMENT;
218  delayMicroseconds(LONG_EXEC_TIME);
219  }
220 
225  virtual void cursor_blink_on()
226  {
228  }
229 
234  virtual void cursor_blink_off()
235  {
237  }
238 
245  virtual void cursor_set(uint8_t x, uint8_t y)
246  {
247  if (x >= WIDTH) x = 0;
248  if (y >= HEIGHT) y = 0;
249  uint8_t offset = (uint8_t) pgm_read_byte(&m_offset[y]);
250  m_io.write8b(SET_DDRAM_ADDR | ((x + offset) & SET_DDRAM_MASK));
251  m_x = x;
252  m_y = y;
253  }
254 
259  virtual void cursor_home()
260  {
262  m_x = 0;
263  m_y = 0;
264  delayMicroseconds(LONG_EXEC_TIME);
265  }
266 
271  __attribute__((always_inline))
272  {
274  }
275 
280  __attribute__((always_inline))
281  {
283  }
284 
289  __attribute__((always_inline))
290  {
292  }
293 
298  __attribute__((always_inline))
299  {
301  }
302 
303 
308  __attribute__((always_inline))
309  {
311  }
312 
317  __attribute__((always_inline))
318  {
320  }
321 
326  __attribute__((always_inline))
327  {
329  }
330 
336  void set_custom_char(uint8_t id, const uint8_t* bitmap)
337  {
338  m_io.write8b(SET_CGRAM_ADDR | ((id << 3) & SET_CGRAM_MASK));
339  m_io.set_mode(true);
340  for (uint8_t i = 0; i < BITMAP_MAX; i++)
341  m_io.write8b(*bitmap++);
342  m_io.set_mode(false);
343  }
344 
351  void set_custom_char_P(uint8_t id, const uint8_t* bitmap)
352  {
353  m_io.write8b(SET_CGRAM_ADDR | ((id << 3) & SET_CGRAM_MASK));
354  m_io.set_mode(true);
355  for (uint8_t i = 0; i < BITMAP_MAX; i++, bitmap++)
356  m_io.write8b(pgm_read_byte(bitmap));
357  m_io.set_mode(false);
358  }
359 
368  virtual size_t write(uint8_t c)
369  {
370  // Check for special characters
371  if (c < ' ') {
372  switch (c) {
373  case '\a': // Alert: invert text mode
374  m_mode = ~m_mode;
375  return (1);
376  case '\b': // Back-space: move cursor back one step (if possible)
377  cursor_set(m_x - 1, m_y);
378  write(' ');
379  cursor_set(m_x - 1, m_y);
380  return (1);
381  case '\f': // Form-feed: clear the display
382  display_clear();
383  return (1);
384  case '\n': // New-line: clear line
385  {
386  cursor_set(0, m_y + 1);
387  m_io.set_mode(true);
388  for (uint8_t i = 0; i < WIDTH; i++) write(' ');
389  m_io.set_mode(false);
390  cursor_set(m_x, m_y);
391  }
392  return (1);
393  case '\r': // Carriage-return: move to start of line
394  cursor_set(0, m_y);
395  return (1);
396  case '\t': // Horizontal tab
397  {
398  uint8_t x = m_x + m_tab - (m_x % m_tab);
399  uint8_t y = m_y + (x >= WIDTH);
400  cursor_set(x, y);
401  }
402  return (1);
403  default:
404  return (0);
405  }
406  }
407 
408  // Write character
409  if (m_x == WIDTH) write('\n');
410  m_x += 1;
411  m_io.set_mode(true);
412  m_io.write8b(c);
413  m_io.set_mode(false);
414  return (c & 0xff);
415  }
416 
417 protected:
421  static const uint16_t LONG_EXEC_TIME = 1600;
422  static const uint16_t POWER_ON_TIME = 48;
423  static const uint16_t INIT0_TIME = 4500;
424  static const uint16_t INIT1_TIME = 150;
425 
429  enum {
430  CLEAR_DISPLAY = 0x01,
431  RETURN_HOME = 0x02,
432  ENTRY_MODE_SET = 0x04,
433  CONTROL_SET = 0x08,
434  SHIFT_SET = 0x10,
435  FUNCTION_SET = 0x20,
436  SET_CGRAM_ADDR = 0x40,
437  SET_CGRAM_MASK = 0x3f,
438  SET_DDRAM_ADDR = 0x80,
439  SET_DDRAM_MASK = 0x7f,
442  COM_SEG_SET = 0x40,
443  COM_SET_MASK = 0x0f,
446  } __attribute__((packed));
447 
451  enum {
452  DISPLAY_SHIFT = 0x01,
453  INCREMENT = 0x02,
454  DECREMENT = 0x00
455  } __attribute__((packed));
456 
460  enum {
461  BLINK_ON = 0x01,
462  CURSOR_ON = 0x02,
463  DISPLAY_ON = 0x04,
464  } __attribute__((packed));
465 
469  enum {
470  MOVE_LEFT = 0x00,
471  MOVE_RIGHT = 0x04,
472  CURSOR_MODE = 0x00,
474  } __attribute__((packed));
475 
479  enum {
482  NR_LINES_1 = 0x00,
483  NR_LINES_2 = 0x08,
484  FONT_5X8DOTS = 0x00,
485  FONT_5X10DOTS = 0x04,
486  BASIC_SET = 0x00,
487  EXTENDED_SET = 0x04
488  } __attribute__((packed));
489 
492  uint8_t m_mode;
493  uint8_t m_cntl;
494  uint8_t m_func;
495  const uint8_t* m_offset;
496 };
497 #endif
uint8_t m_y
Cursor position y.
Definition: LCD.h:205
Sets the number of display lines, 1 or.
Definition: HD44780.h:482
Decrement (left) on write.
Definition: HD44780.h:454
virtual bool begin()
Definition: HD44780.h:132
COM SEG direction select.
Definition: HD44780.h:442
void text_flow_right_to_left()
Definition: HD44780.h:307
uint8_t m_x
Cursor position x.
Definition: LCD.h:204
void text_scroll_right_adjust()
Definition: HD44780.h:325
virtual void set_mode(uint8_t flag)=0
void text_normal_mode()
Definition: LCD.h:188
void set_custom_char(uint8_t id, const uint8_t *bitmap)
Definition: HD44780.h:336
static const uint16_t LONG_EXEC_TIME
Definition: HD44780.h:421
virtual void write8n(const void *buf, size_t size)
Definition: HD44780.h:77
Adapter & m_io
IO port adapter.
Definition: HD44780.h:491
virtual size_t write(uint8_t c)
Definition: HD44780.h:368
The display is on.
Definition: HD44780.h:463
Sets CGRAM address.
Definition: HD44780.h:436
virtual void display_on()
Definition: HD44780.h:194
Definition: Debug.h:28
virtual void set_backlight(uint8_t flag)=0
without changing DDRAM contents.
Definition: HD44780.h:471
void cursor_underline_on()
Definition: HD44780.h:288
static const uint16_t INIT1_TIME
Definition: HD44780.h:424
void display_scroll_right()
Definition: HD44780.h:279
uint8_t m_tab
Tab step.
Definition: LCD.h:206
Set cursor and shifts display.
Definition: HD44780.h:434
virtual void cursor_blink_off()
Definition: HD44780.h:234
virtual bool setup()
Definition: HD44780.h:48
uint8_t m_cntl
Control.
Definition: HD44780.h:493
uint8_t m_mode
Entry mode.
Definition: HD44780.h:492
static const uint16_t POWER_ON_TIME
Definition: HD44780.h:422
The cursor is displayed.
Definition: HD44780.h:462
virtual void display_clear()
Definition: HD44780.h:212
Sets the character font, 5X8 dots or.
Definition: HD44780.h:484
Set display, cursor and blinking controls.
Definition: HD44780.h:433
virtual void cursor_set(uint8_t x, uint8_t y)
Definition: HD44780.h:245
Sets cursor move direction and display shift.
Definition: HD44780.h:432
Bias resistor select.
Definition: HD44780.h:440
const uint8_t * m_offset
Row offset table.
Definition: HD44780.h:495
const uint8_t HEIGHT
Definition: HD44780.h:106
void text_scroll_left_adjust()
Definition: HD44780.h:316
Sets basic instruction set.
Definition: HD44780.h:486
virtual void write8b(uint8_t data)
Definition: HD44780.h:65
Moves cursor and shifts display.
Definition: HD44780.h:470
Clears entrire display and return home.
Definition: HD44780.h:430
virtual void cursor_blink_on()
Definition: HD44780.h:225
static const uint16_t INIT0_TIME
Definition: HD44780.h:423
Sets DDRAM address.
Definition: HD44780.h:438
Set display data length.
Definition: HD44780.h:444
uint8_t m_func
Function set.
Definition: HD44780.h:494
virtual void backlight_off()
Definition: HD44780.h:185
Sets DDRAM 0 in address counter.
Definition: HD44780.h:431
HD44780(Adapter &io, uint8_t width=16, uint8_t height=2)
Definition: HD44780.h:115
static const uint8_t BITMAP_MAX
Definition: HD44780.h:100
LCD::GY_IICLCD io(twi)
void cursor_underline_off()
Definition: HD44780.h:297
Sets interface data length, line and font.
Definition: HD44780.h:435
The character indicated by cursor blinks.
Definition: HD44780.h:461
Sets the interface data length, 4-bit or.
Definition: HD44780.h:480
Device()
Definition: LCD.h:31
Increment (right) on write.
Definition: HD44780.h:453
Shift the entire display not cursor.
Definition: HD44780.h:452
virtual void display_off()
Definition: HD44780.h:203
virtual void write4b(uint8_t data)=0
void display_scroll_left()
Definition: HD44780.h:270
virtual void backlight_on()
Definition: HD44780.h:176
void set_custom_char_P(uint8_t id, const uint8_t *bitmap)
Definition: HD44780.h:351
const uint8_t WIDTH
Definition: HD44780.h:103
virtual void cursor_home()
Definition: HD44780.h:259