1// --------------------------------------------------------------------------- 2// Created/Adapted by Bill Perry 2012-03-16 3// Copyright 2012 - Under creative commons license 3.0: 4// Attribution-ShareAlike CC BY-SA 5// 6// This software is furnished "as is", without technical support, and with no 7// warranty, express or implied, as to its usefulness for any purpose. 8// 9// @file LiquidCrystal_SR2W.h 10// Connects a hd44780 LCD using 2 pins from the Arduino, via an 8-bit 11// ShiftRegister (SR2W from now on). 12// 13// @brief 14// This is the 2 wire shift register interface class for the LCD library 15// 16// The functionality provided by this class and its base class is a superset of 17// the original functionality of the Arduino LiquidCrystal library and can 18// be used as such. 19// See the LCD class for a full description of the API functions available. 20// 21// It works with a 8-bit unlatched, no-tristate, unidirectional SIPO (Serial-In-Parallel-Out) 22// shift register (IE a very simple SR), and an hd44780 LCD in 4-bit mode. 23// Any such shift register should do (pref. 74LS family IC's for 2-wire). 24// 74LS164 and 74HC595 have been exstensively tested. 25// 26// 27// 2 Pins required from the Arduino: 28// - Data/Enable 29// - Clock 30// The Data pin is also used to control the enable signal 31// LCD RW-pin hardwired to LOW (only writing to LCD). 32// Busy Flag (BF, data bit D7) is not read. 33// 34// Original project homepage: http://code.google.com/p/arduinoshiftreglcd/ 35// 36// Shift register bits 37// Bit #0 - (cannot be used on 74HC595) 38// Bit #1 - optional backlight control 39// Bit #2 - connects to RS (Register Select) on the LCD 40// Bit #3 - connects to LCD data inputs D4 41// Bit #4 - connects to LCD data inputs D5 42// Bit #5 - connects to LCD data inputs D6 43// Bit #6 - connects to LCD data inputs D7 44// Bit #7 - enables the LCD enable-puls (via the diode-resistor AND "gate") 45// 46// Wiring for a 74LS164 47// --------------------- 48// 1k/4.7k 49// +--------[ Resistor ]--------+---(LCD Enable) 50// | | 51// | 74LS164 (VCC) | 52// | +----u----+ | _V_ diode 53// (data pin)---+---+--1-|A VCC|-14-+ | 54// | | | | 55// +--2-|B Q7|-13------+ 56// 3-|Q0 Q6|-12--(LCD D7) 57// (BL Circuit)--------4-|Q1 Q5|-11--(LCD D6) 58// (LCD RS)------------5-|Q2 Q4|-10--(LCD D5) 59// (LCD D4)------------6-|Q3 /MR|--9--(VCC) 60// +-7-|GND CP|--8--(clock pin) 61// | +---------+ 62// | 0.1uf 63// (gnd)-----||----(vcc) 64// 65// Wiring for a 74HC595 66// -------------------- 67// NOTE: the 74HC595 is a latching shift register. In order to get it to operate 68// in a "non latching" mode, RCLK and SCLK are tied together. The side effect of this 69// is that the latched output is one clock behind behind the internal shift register bits. 70// To compensate for this the wiring is offset by one bit position lower. 71// For example, while the backlight is hooked to Q0 it is still using bit 1 of 72// of the shift register because the outputs are 1 clock behind the real internal shift 73// register. 74// 75// 74HC595 (VCC) 76// +----u----+ | +-----------------------(BL circuit) 77// (LCD RS)------------1-|Q1 VCC|-16-+ | +--------------------(data pin) 78// (LCD D4)------------2-|Q2 Q0|-15----+ | 1k/4.7k 79// (LCD D5)------------3-|Q3 SER|-14-------+---[ Resistor ]--+--(LCD Enable) 80// (LCD D6)------------4-|Q4 /OE|-13--(gnd) | 81// (LCD D7)------------5-|Q5 RCLK|-12-------+ | 82// | | | | 83// +------6-|Q6 SCLK|-11-------+--(clock pin) | 84// | 7-|Q7 /MR|-10--(VCC) | 85// | +-8-|GND Q6'|--9 | 86// | | +---------+ diode _V_ 87// | | 0.1uf | 88// | (gnd)-----||----(vcc) | 89// +-----------------------------------------------+ 90// 91// 92// Backlight Control circuit 93// ------------------------- 94// Because the shift resiter is not latching the outputs, the backlight circuitry 95// will "see" the output bits as they are shifted into the shift register which 96// can cause the backlight to flicker rather than remain constantly on/off. 97// The circuit below slows down the transitions to the transistor to remove 98// the visible flicker. When the BL input is HIGH the LCD backlight will turn on. 99// 100// (value depends on LCD, 100ohm is usually safe) 101// (LCD BL anode)---[ resistor ]---(vcc) 102// 103// (LCD BL cathode)-------------------------------+ 104// | 105// D 106// | 107// (BL input)----[ 4.7k Resistor ]----+-------G-|-< (2N7000 FET) 108// | | 109// (0.1uf) = S 110// | | 111// (gnd) (gnd) 112// 113// 114// 115// 116// History 117// 2012.03.16 bperrybap - creation/adaption from SR header to create SR2W header. 118// Fixed typo in SR2W mask define names 119// @author B. Perry - bperrybap@opensource.billsworld.billandterrie.com 120// -------------------------------------------------------------------------------- 121#ifndef _LIQUIDCRYSTAL_SR2W_ 122#define _LIQUIDCRYSTAL_SR2W_ 123 124#include <inttypes.h> 125#include"Visuino_LCD.h" 126#include"Visuino_FastIO.h" 127 128 129// two-wire SR output bit constants 130// --------------------------------------------------------------------------- 131 132#define SR2W_BL_MASK 0x02 133#define SR2W_RS_MASK 0x04 134#define SR2W_DATA_MASK 0x78// data bits are hard coded to be SR bits 6,5,4,3 135#define SR2W_EN_MASK 0x80// cannot ever be changed 136 137class LiquidCrystal_SR2W :public LCD 138{ 139public: 140/*! 141 @method 142 @abstract LCD 2 wire SHIFT REGISTER constructor. 143 @discussion Defines the pin assignments that connect to the shift register. 144 The constructor does not initialize the LCD. Assuming 1 line 8 pixel high 145 font. 146 147 @param srdata[in] Arduino pin for shift register data line. 148 @param srclock[in] Arduino pin for shift register clock line. 149 @param blpol[in] optional backlight polarity (default = POSITIVE) 150 */ 151LiquidCrystal_SR2W(uint8_t srdata,uint8_t srclock, t_backlighPol blpol = POSITIVE); 152 153/*! 154 @function 155 @abstract Send a particular value to the LCD. 156 @discussion Sends a particular value to the LCD for writing to the LCD or 157 as an LCD command using the shift register. 158 159 Users should never call this method. 160 161 @param value[in] Value to send to the LCD. 162 @param mode[in] DATA=8bit data, COMMAND=8bit cmd, FOUR_BITS=4bit cmd 163 the LCD. 164 */ 165virtualvoidsend(uint8_t value,uint8_t mode); 166 167 168/*! 169 @function 170 @abstract Switch-on/off the LCD backlight. 171 @discussion Switch-on/off the LCD backlight. 172 The setBacklightPin has to be called before setting the backlight for 173 this method to work. @see setBacklightPin. 174 175 @param mode[in] backlight mode (0 off, non-zero on) 176 */ 177voidsetBacklight(uint8_t mode ); 178 179private: 180 181/*! 182 @method 183 @abstract Initializes the LCD pin allocation 184 @discussion Initializes the LCD pin allocation and configuration. 185 */ 186voidinit(uint8_t srdata,uint8_t srclock, t_backlighPol blpol,uint8_t lines,uint8_t font ); 187 188/*! 189 * @method 190 * @abstract takes care of shifting and the enable pulse 191 */ 192voidloadSR(uint8_t val); 193 194 fio_register _srDataRegister;// Serial Data pin 195 fio_bit _srDataMask; 196 fio_register _srClockRegister;// Clock Pin 197 fio_bit _srClockMask; 198 199uint8_t _blPolarity; 200uint8_t _blMask; 201}; 202#endif