PololuHD44780 library
PololuHD44780.h
Go to the documentation of this file.
1 // Copyright Pololu Corporation. For more information, see http://www.pololu.com/
2 
12 #pragma once
13 #include <Arduino.h>
14 #include <util/delay.h>
15 
56 class PololuHD44780Base : public Print
57 {
58 public:
59 
61 
64  virtual void initPins() = 0;
65 
67  void init()
68  {
69  if (!initialized)
70  {
71  initialized = true;
72  init2();
73  }
74  }
75 
80  void reinitialize()
81  {
82  initialized = true;
83  init2();
84  }
85 
102  virtual void send(uint8_t data, bool rsValue, bool only4bits) = 0;
103 
104 private:
105 
106  void sendAndDelay(uint8_t data, bool rsValue, bool only4bit);
107 
109  void sendCommand(uint8_t cmd)
110  {
111  sendAndDelay(cmd, false, false);
112  }
113 
115  void sendCommand4Bit(uint8_t cmd)
116  {
117  sendAndDelay(cmd, false, true);
118  }
119 
121  void sendData(uint8_t data)
122  {
123  sendAndDelay(data, true, false);
124  }
125 
126 public:
127 
130  void clear();
131 
135  void loadCustomCharacter(const uint8_t * picture, uint8_t number);
136 
140  void loadCustomCharacterFromRam(const uint8_t * picture, uint8_t number);
141 
145  void loadCustomCharacter(const char * picture, uint8_t number)
146  {
147  loadCustomCharacter((const uint8_t *)picture, number);
148  }
149 
152  void createChar(uint8_t number, uint8_t picture[])
153  {
154  loadCustomCharacterFromRam(picture, number);
155  }
156 
166  void gotoXY(uint8_t x, uint8_t y);
167 
170  void setCursor(uint8_t col, uint8_t row)
171  {
172  gotoXY(col, row);
173  }
174 
178  void noDisplay();
179 
182  void display();
183 
193  void noCursor();
194 
205  void cursor();
206 
216  void noBlink();
217 
228  void blink();
229 
237  void cursorSolid();
238 
246  void cursorBlinking();
247 
251  void hideCursor();
252 
256  void scrollDisplayLeft();
257 
261  void scrollDisplayRight();
262 
269  void home();
270 
273  void leftToRight();
274 
277  void rightToLeft();
278 
284  void autoscroll();
285 
287  void noAutoscroll();
288 
289  //void initPrintf();
290  //void initPrintf(uint8_t lcdWidth, uint8_t lcdHeight);
291 
294  void command(uint8_t cmd)
295  {
296  sendCommand(cmd);
297  }
298 
300  virtual size_t write(uint8_t c);
301 
308  virtual size_t write(const uint8_t * buffer, size_t size);
309 
310  // This allows us to easily call overrides of write that are
311  // defined in Print.
312  using Print::write;
313 
314 private:
315  bool initialized;
316 
317  /* The lower three bits of this store the arguments to the
318  * last "Display on/off control" HD44780 command that we sent.
319  * bit 2: D: Whether the display is on.
320  * bit 1: C: Whether the cursor is shown.
321  * bit 0: B: Whether the cursor is blinking. */
322  uint8_t displayControl;
323 
324  /* The lower two bits of this variable store the arguments to the
325  * last "Entry mode set" HD44780 command that we sent.
326  * bit 1: I/D: 0 for moving the cursor to the left after data is written,
327  * 1 for moving the cursor to the right.
328  * bit 0: 1 for autoscrolling. */
329  uint8_t entryMode;
330 
331  void setEntryMode(uint8_t entryMode);
332  void setDisplayControl(uint8_t displayControl);
333 
334  void init2();
335 };
336 
361 {
362 public:
378  PololuHD44780(uint8_t rs, uint8_t e, uint8_t db4, uint8_t db5,
379  uint8_t db6, uint8_t db7)
380  {
381  this->rs = rs;
382  this->e = e;
383  this->db4 = db4;
384  this->db5 = db5;
385  this->db6 = db6;
386  this->db7 = db7;
387  }
388 
389  virtual void initPins()
390  {
391  digitalWrite(e, LOW);
392  pinMode(e, OUTPUT);
393  }
394 
395  virtual void send(uint8_t data, bool rsValue, bool only4bits)
396  {
397  digitalWrite(rs, rsValue);
398 
399  pinMode(rs, OUTPUT);
400  pinMode(db4, OUTPUT);
401  pinMode(db5, OUTPUT);
402  pinMode(db6, OUTPUT);
403  pinMode(db7, OUTPUT);
404 
405  if (!only4bits) { sendNibble(data >> 4); }
406  sendNibble(data & 0x0F);
407  }
408 
409 private:
410 
411  void sendNibble(uint8_t data)
412  {
413  digitalWrite(db4, data >> 0 & 1);
414  digitalWrite(db5, data >> 1 & 1);
415  digitalWrite(db6, data >> 2 & 1);
416  digitalWrite(db7, data >> 3 & 1);
417 
418  digitalWrite(e, HIGH);
419  _delay_us(1); // Must be at least 450 ns.
420  digitalWrite(e, LOW);
421  _delay_us(1); // Must be at least 550 ns.
422  }
423 
424  uint8_t rs, e, db4, db5, db6, db7;
425 };
PololuHD44780Base::cursor
void cursor()
Definition: PololuHD44780.cpp:167
PololuHD44780Base::command
void command(uint8_t cmd)
Definition: PololuHD44780.h:294
PololuHD44780
Main class for interfacing with the HD44780 LCDs.
Definition: PololuHD44780.h:361
PololuHD44780Base::loadCustomCharacter
void loadCustomCharacter(const uint8_t *picture, uint8_t number)
Definition: PololuHD44780.cpp:103
PololuHD44780Base::cursorBlinking
void cursorBlinking()
Definition: PololuHD44780.cpp:142
PololuHD44780Base::initPins
virtual void initPins()=0
PololuHD44780Base::leftToRight
void leftToRight()
Definition: PololuHD44780.cpp:204
PololuHD44780Base::reinitialize
void reinitialize()
Definition: PololuHD44780.h:80
PololuHD44780::initPins
virtual void initPins()
Definition: PololuHD44780.h:389
PololuHD44780Base
General class for handling the HD44780 protocol.
Definition: PololuHD44780.h:57
PololuHD44780Base::autoscroll
void autoscroll()
Definition: PololuHD44780.cpp:214
PololuHD44780Base::send
virtual void send(uint8_t data, bool rsValue, bool only4bits)=0
PololuHD44780Base::blink
void blink()
Definition: PololuHD44780.cpp:177
PololuHD44780Base::createChar
void createChar(uint8_t number, uint8_t picture[])
Definition: PololuHD44780.h:152
PololuHD44780Base::gotoXY
void gotoXY(uint8_t x, uint8_t y)
Definition: PololuHD44780.cpp:88
PololuHD44780Base::noDisplay
void noDisplay()
Definition: PololuHD44780.cpp:152
PololuHD44780Base::display
void display()
Definition: PololuHD44780.cpp:157
PololuHD44780Base::rightToLeft
void rightToLeft()
Definition: PololuHD44780.cpp:209
PololuHD44780Base::clear
void clear()
Definition: PololuHD44780.cpp:78
PololuHD44780::send
virtual void send(uint8_t data, bool rsValue, bool only4bits)
Definition: PololuHD44780.h:395
PololuHD44780Base::write
virtual size_t write(uint8_t c)
Definition: PololuHD44780.cpp:62
PololuHD44780Base::noBlink
void noBlink()
Definition: PololuHD44780.cpp:172
PololuHD44780Base::noCursor
void noCursor()
Definition: PololuHD44780.cpp:162
PololuHD44780Base::scrollDisplayRight
void scrollDisplayRight()
Definition: PololuHD44780.cpp:187
PololuHD44780Base::init
void init()
Definition: PololuHD44780.h:67
PololuHD44780Base::scrollDisplayLeft
void scrollDisplayLeft()
Definition: PololuHD44780.cpp:182
PololuHD44780Base::hideCursor
void hideCursor()
Definition: PololuHD44780.cpp:147
PololuHD44780Base::cursorSolid
void cursorSolid()
Definition: PololuHD44780.cpp:137
PololuHD44780Base::noAutoscroll
void noAutoscroll()
Definition: PololuHD44780.cpp:219
PololuHD44780Base::loadCustomCharacterFromRam
void loadCustomCharacterFromRam(const uint8_t *picture, uint8_t number)
Definition: PololuHD44780.cpp:117
PololuHD44780Base::home
void home()
Definition: PololuHD44780.cpp:192
PololuHD44780Base::loadCustomCharacter
void loadCustomCharacter(const char *picture, uint8_t number)
Definition: PololuHD44780.h:145
PololuHD44780Base::setCursor
void setCursor(uint8_t col, uint8_t row)
Definition: PololuHD44780.h:170
PololuHD44780::PololuHD44780
PololuHD44780(uint8_t rs, uint8_t e, uint8_t db4, uint8_t db5, uint8_t db6, uint8_t db7)
Definition: PololuHD44780.h:378