LCD4Bit Mod
LCD4Bit Mod
What is this?
An arduino library for comms with HD44780-compatible LCD, in 4-bit mode (saves
pins)
Sources:
- The original "LiquidCrystal" 8-bit library and tutorial
http://www.arduino.cc/en/uploads/Tutorial/LiquidCrystal.zip
http://www.arduino.cc/en/Tutorial/LCDLibrary
- DEM 16216 datasheet http://www.maplin.co.uk/Media/PDFs/N27AZ.pdf
- Massimo's suggested 4-bit code (I took initialization from here)
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1144924220/8
See also:
- glasspusher's code (probably more correct): http://www.arduino.cc/cgi-
bin/yabb2/YaBB.pl?num=1160586800/0#0
Usage:
see the examples folder of this library distribution.
*/
#include "LCD4Bit_mod.h"
#include "Arduino.h"
extern "C" {
#include <stdio.h> //not needed yet
#include <string.h> //needed for strlen()
#include <inttypes.h>
}
//--------------------------------------------------------
//how many lines has the LCD? (don't change here - specify on calling constructor)
int g_num_lines = 2;
//push a nibble of data through the the LCD's DB4~7 pins, clocking with the Enable
pin.
//We don't care what RS and RW are, here.
void LCD4Bit_mod::pushNibble(int value){
int val_nibble= value & 0x0F; //clean the value. (unnecessary)
//push a byte of data through the LCD's DB4~7 pins, in two steps, clocking each
with the enable pin.
void LCD4Bit_mod::pushByte(int value){
int val_lower = value & 0x0F;
int val_upper = value >> 4;
pushNibble(val_upper);
pushNibble(val_lower);
}
//print the given character at the current cursor position. overwrites, doesn't
insert.
void LCD4Bit_mod::print(int value) {
//set the RS and RW pins to show we're writing data
digitalWrite(RS, HIGH);
if (USING_RW) { digitalWrite(RW, LOW); }
//print the given string to the LCD at the current cursor position. overwrites,
doesn't insert.
//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.
void LCD4Bit_mod::printIn(char msg[]) {
uint8_t i; //fancy int. avoids compiler warning when comparing i with
strlen()'s uint8_t
for (i=0;i < strlen(msg);i++){
print(msg[i]);
}
}
delay(50);
//The first 4 nibbles and timings are not in my DEM16217 SYH datasheet, but
apparently are HD44780 standard...
commandWriteNibble(0x03);
delay(5);
commandWriteNibble(0x03);
delayMicroseconds(100);
commandWriteNibble(0x03);
delay(5);
commandWriteNibble(num_lines_ptn | dot_format_ptn);
delayMicroseconds(60);
// display control:
// turn display on, cursor off, no blinking
commandWrite(0x0C);
delayMicroseconds(60);
//clear display
commandWrite(0x01);
delay(3);
//if we are on a 1-line display, set line_num to 1st line, regardless of given
if (g_num_lines==1){
line_num = 1;
}
//offset 40 chars in if second line requested
if (line_num == 2){
x += 40;
}
//advance the cursor to the right according to position. (second line starts at
position 40).
for (int i=0; i<x; i++) {
commandWrite(0x14);
}
}
//Improvements ------------------------------------------------
//Remove the unnecessary delays (e.g. from the end of pulseEnablePin()).
//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.
//-------------------------------------------------------------