2 LCD4Bit v0.1 16/Oct/2006 neillzero http://abstractplain.net
\r
5 An arduino library for comms with HD44780-compatible LCD, in 4-bit mode (saves pins)
\r
8 - The original "LiquidCrystal" 8-bit library and tutorial
\r
9 http://www.arduino.cc/en/uploads/Tutorial/LiquidCrystal.zip
\r
10 http://www.arduino.cc/en/Tutorial/LCDLibrary
\r
11 - DEM 16216 datasheet http://www.maplin.co.uk/Media/PDFs/N27AZ.pdf
\r
12 - Massimo's suggested 4-bit code (I took initialization from here) http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1144924220/8
\r
14 - glasspusher's code (probably more correct): http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1160586800/0#0
\r
16 Tested only with a DEM 16216 (maplin "N27AZ" - http://www.maplin.co.uk/Search.aspx?criteria=N27AZ)
\r
17 If you use this successfully, consider feeding back to the arduino wiki with a note of which LCD it worked on.
\r
20 see the examples folder of this library distribution.
\r
24 #include "LCD4Bit_mod.h"
\r
26 #include <stdio.h> //not needed yet
\r
27 #include <string.h> //needed for strlen()
\r
28 #include <inttypes.h>
\r
29 #include "WConstants.h" //all things wiring / arduino
\r
32 //command bytes for LCD
\r
33 #define CMD_CLR 0x01
\r
34 #define CMD_RIGHT 0x1C
\r
35 #define CMD_LEFT 0x18
\r
36 #define CMD_HOME 0x02
\r
38 // --------- PINS -------------------------------------
\r
39 //is the RW pin of the LCD under our control? If we're only ever going to write to the LCD, we can use one less microcontroller pin, and just tie the LCD pin to the necessary signal, high or low.
\r
40 //this stops us sending signals to the RW pin if it isn't being used.
\r
41 int USING_RW = false;
\r
43 //RS, RW and Enable can be set to whatever you like
\r
47 //DB should be an unseparated group of pins - because of lazy coding in pushNibble()
\r
48 int DB[] = {4, 5, 6, 7}; //wire these to DB4~7 on LCD.
\r
50 //--------------------------------------------------------
\r
52 //how many lines has the LCD? (don't change here - specify on calling constructor)
\r
53 int g_num_lines = 2;
\r
55 //pulse the Enable pin high (for a microsecond).
\r
56 //This clocks whatever command or data is in DB4~7 into the LCD controller.
\r
57 void LCD4Bit_mod::pulseEnablePin(){
\r
58 digitalWrite(Enable,LOW);
\r
59 delayMicroseconds(1);
\r
60 // send a pulse to enable
\r
61 digitalWrite(Enable,HIGH);
\r
62 delayMicroseconds(1);
\r
63 digitalWrite(Enable,LOW);
\r
64 delay(1); // pause 1 ms. TODO: what delay, if any, is necessary here?
\r
67 //push a nibble of data through the the LCD's DB4~7 pins, clocking with the Enable pin.
\r
68 //We don't care what RS and RW are, here.
\r
69 void LCD4Bit_mod::pushNibble(int value){
\r
70 int val_nibble= value & 0x0F; //clean the value. (unnecessary)
\r
72 for (int i=DB[0]; i <= DB[3]; i++) {
\r
73 digitalWrite(i,val_nibble & 01);
\r
79 //push a byte of data through the LCD's DB4~7 pins, in two steps, clocking each with the enable pin.
\r
80 void LCD4Bit_mod::pushByte(int value){
\r
81 int val_lower = value & 0x0F;
\r
82 int val_upper = value >> 4;
\r
83 pushNibble(val_upper);
\r
84 pushNibble(val_lower);
\r
88 //stuff the library user might call---------------------------------
\r
89 //constructor. num_lines must be 1 or 2, currently.
\r
90 LCD4Bit_mod::LCD4Bit_mod (int num_lines) {
\r
91 g_num_lines = num_lines;
\r
92 if (g_num_lines < 1 || g_num_lines > 2)
\r
98 void LCD4Bit_mod::commandWriteNibble(int nibble) {
\r
99 digitalWrite(RS, LOW);
\r
100 if (USING_RW) { digitalWrite(RW, LOW); }
\r
101 pushNibble(nibble);
\r
105 void LCD4Bit_mod::commandWrite(int value) {
\r
106 digitalWrite(RS, LOW);
\r
107 if (USING_RW) { digitalWrite(RW, LOW); }
\r
109 //TODO: perhaps better to add a delay after EVERY command, here. many need a delay, apparently.
\r
115 //print the given character at the current cursor position. overwrites, doesn't insert.
\r
116 void LCD4Bit_mod::print(int value) {
\r
117 //set the RS and RW pins to show we're writing data
\r
118 digitalWrite(RS, HIGH);
\r
119 if (USING_RW) { digitalWrite(RW, LOW); }
\r
121 //let pushByte worry about the intricacies of Enable, nibble order.
\r
126 //print the given string to the LCD at the current cursor position. overwrites, doesn't insert.
\r
127 //While I don't understand why this was named printIn (PRINT IN?) in the original LiquidCrystal library, I've preserved it here to maintain the interchangeability of the two libraries.
\r
128 void LCD4Bit_mod::printIn(char msg[]) {
\r
129 uint8_t i; //fancy int. avoids compiler warning when comparing i with strlen()'s uint8_t
\r
130 for (i=0;i < strlen(msg);i++){
\r
136 //send the clear screen command to the LCD
\r
137 void LCD4Bit_mod::clear(){
\r
138 commandWrite(CMD_CLR);
\r
143 // initiatize lcd after a short pause
\r
144 //while there are hard-coded details here of lines, cursor and blink settings, you can override these original settings after calling .init()
\r
145 void LCD4Bit_mod::init () {
\r
146 pinMode(Enable,OUTPUT);
\r
147 pinMode(RS,OUTPUT);
\r
148 if (USING_RW) { pinMode(RW,OUTPUT); }
\r
149 pinMode(DB[0],OUTPUT);
\r
150 pinMode(DB[1],OUTPUT);
\r
151 pinMode(DB[2],OUTPUT);
\r
152 pinMode(DB[3],OUTPUT);
\r
156 //The first 4 nibbles and timings are not in my DEM16217 SYH datasheet, but apparently are HD44780 standard...
\r
157 commandWriteNibble(0x03);
\r
159 commandWriteNibble(0x03);
\r
160 delayMicroseconds(100);
\r
161 commandWriteNibble(0x03);
\r
164 // needed by the LCDs controller
\r
165 //this being 2 sets up 4-bit mode.
\r
166 commandWriteNibble(0x02);
\r
167 commandWriteNibble(0x02);
\r
168 //todo: make configurable by the user of this library.
\r
170 //N = num lines (0=1 line or 1=2 lines).
\r
171 //F= format (number of dots (0=5x7 or 1=5x10)).
\r
174 int num_lines_ptn = g_num_lines - 1 << 3;
\r
175 int dot_format_ptn = 0x00; //5x7 dots. 0x04 is 5x10
\r
177 commandWriteNibble(num_lines_ptn | dot_format_ptn);
\r
178 delayMicroseconds(60);
\r
180 //The rest of the init is not specific to 4-bit mode.
\r
181 //NOTE: we're writing full bytes now, not nibbles.
\r
183 // display control:
\r
184 // turn display on, cursor off, no blinking
\r
185 commandWrite(0x0C);
\r
186 delayMicroseconds(60);
\r
189 commandWrite(0x01);
\r
192 // entry mode set: 06
\r
193 // increment automatically, display shift, entire shift off
\r
194 commandWrite(0x06);
\r
196 delay(1);//TODO: remove unnecessary delays
\r
200 //non-core stuff --------------------------------------
\r
201 //move the cursor to the given absolute position. line numbers start at 1.
\r
202 //if this is not a 2-line LCD4Bit_mod instance, will always position on first line.
\r
203 void LCD4Bit_mod::cursorTo(int line_num, int x){
\r
204 //first, put cursor home
\r
205 commandWrite(CMD_HOME);
\r
207 //if we are on a 1-line display, set line_num to 1st line, regardless of given
\r
208 if (g_num_lines==1){
\r
211 //offset 40 chars in if second line requested
\r
212 if (line_num == 2){
\r
215 //advance the cursor to the right according to position. (second line starts at position 40).
\r
216 for (int i=0; i<x; i++) {
\r
217 commandWrite(0x14);
\r
221 //scroll whole display to left
\r
222 void LCD4Bit_mod::leftScroll(int num_chars, int delay_time){
\r
223 for (int i=0; i<num_chars; i++) {
\r
224 commandWrite(CMD_LEFT);
\r
229 //Improvements ------------------------------------------------
\r
230 //Remove the unnecessary delays (e.g. from the end of pulseEnablePin()).
\r
231 //Allow the user to pass the pins to be used by the LCD in the constructor, and store them as member variables of the class instance.
\r
232 //-------------------------------------------------------------
\r