I was talking in a separate spread with someone else and as a result of that conversation decided to convert LiquidCrystal to a 3-wire solution.
LiquidCrystal requires anywhere between 6 lines (enable, rs, and d4..7). Sometimes, that can be difficult for a board with limited pins. Thus, the 3-wire solution.
The 3-wire solution implemented here is wholly based on LiquidCrystal, including the user interface and calling convention / syntax. It utilizes a shift register (HC164 or HC595) to shift the data pins (D0..7) to the shift register (Q0..7).
Connection:
MOSI pin: from the mcu to shift register's data pin (pin1+2 on HC164 and pin 14 on HC595). LCD's RS pin tied to this line as well.
SCK pin: from the mcu to shift register's serial clock pin (pin 8 on HC164 and pin 11 on HC595)
Enable pin: from the mcu to the lcd.
Advantages over other 3-wire solutions:
- It allows 8-bit as well as 4-bit mode.
- It uses no parts other than the shift register.
- It allows HC164 as well as HC595.
You will need two files:
lcd3wire.h:
//header file to drive HD44780 using a shift register in a 3-wire connection
//based on LiquidCrystal
/*The 3-wire solution implemented here is wholly based on LiquidCrystal, including the user interface and calling convention / syntax. It utilizes a shift register (HC164 or HC595) to shift the data pins (D0..7) to the shift register (Q0..7).
Connection:
MOSI pin: from the mcu to shift register's data pin (pin1+2 on HC164 and pin 14 on HC595). LCD's RS pin tied to this line as well.
SCK pin: from the mcu to shift register's serial clock pin (pin 8 on HC164 and pin 11 on HC595)
Enable pin: from the mcu to the lcd.
Advantages over other 3-wire solutions:
1) It allows 8-bit as well as 4-bit mode.
2) It uses no parts other than the shift register.
3) It allows HC164 as well as HC595.
You will need two files: lcd3wire.h and lcd3wire.cpp
*/
#ifndef LiquidCrystal_h
#define LiquidCrystal_h
#include <inttypes.h>
#include "Print.h"
// commands
#define LCD_CLEARDISPLAY 0x01
#define LCD_RETURNHOME 0x02
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_CURSORSHIFT 0x10
#define LCD_FUNCTIONSET 0x20
#define LCD_SETCGRAMADDR 0x40
#define LCD_SETDDRAMADDR 0x80
// flags for display entry mode
#define LCD_ENTRYRIGHT 0x00
#define LCD_ENTRYLEFT 0x02
#define LCD_ENTRYSHIFTINCREMENT 0x01
#define LCD_ENTRYSHIFTDECREMENT 0x00
// flags for display on/off control
#define LCD_DISPLAYON 0x04
#define LCD_DISPLAYOFF 0x00
#define LCD_CURSORON 0x02
#define LCD_CURSOROFF 0x00
#define LCD_BLINKON 0x01
#define LCD_BLINKOFF 0x00
// flags for display/cursor shift
#define LCD_DISPLAYMOVE 0x08
#define LCD_CURSORMOVE 0x00
#define LCD_MOVERIGHT 0x04
#define LCD_MOVELEFT 0x00
// flags for function set
#define LCD_8BITMODE 0x10
#define LCD_4BITMODE 0x00
#define LCD_2LINE 0x08
#define LCD_1LINE 0x00
#define LCD_5x10DOTS 0x04
#define LCD_5x8DOTS 0x00
class LiquidCrystal : public Print {
public:
LiquidCrystal(uint8_t mosi, uint8_t sck, uint8_t enable);
void init(uint8_t fourbitmode, uint8_t mosi, uint8_t sck, uint8_t enable);
void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS);
void clear();
void home();
void noDisplay();
void display();
void noBlink();
void blink();
void noCursor();
void cursor();
void scrollDisplayLeft();
void scrollDisplayRight();
void leftToRight();
void rightToLeft();
void autoscroll();
void noAutoscroll();
void createChar(uint8_t, uint8_t[]);
void setCursor(uint8_t, uint8_t);
virtual size_t write(uint8_t);
void command(uint8_t);
using Print::write;
private:
void send(uint8_t, uint8_t);
void write4bits(uint8_t);
void write8bits(uint8_t);
void pulseEnable();
uint8_t _rs_pin; // LOW: command. HIGH: character.
//uint8_t _rw_pin; // LOW: write to LCD. HIGH: read from LCD.
uint8_t _enable_pin; // activated by a HIGH pulse.
//uint8_t _data_pins[8];
uint8_t _sck_pin; //SR's sck pin. data on rising edge
#define _mosi_pin _rs_pin //mosi pin share the same connection with rs pin
uint8_t _displayfunction;
uint8_t _displaycontrol;
uint8_t _displaymode;
uint8_t _initialized;
uint8_t _numlines,_currline;
};
#endif