Skip to content

Instantly share code, notes, and snippets.

@shilrobot
Last active December 10, 2015 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shilrobot/4479020 to your computer and use it in GitHub Desktop.
Save shilrobot/4479020 to your computer and use it in GitHub Desktop.
Arduino -> Adafruit 20x4 LCD (product ID 198): Test code, including optimized 4-bit or 8-bit LCD refresh routine.
#include <LiquidCrystal.h>
// Notes:
// - LCD has an on-board current limiting resistor for the LED backlight (510 ohm I think.)
// So it can be connected directly to +5V and GND.
// LCD backlight LED draws 34 mA.
// - The whole LCD only draws about 35-36 mA, so the backlight is the majority of the power.
// - The included contrast trim pot is 10K.
// - Line 1 wraps to line 3 and line 2 wraps to line 4, although LiquidCrystal::setCursor() works(!)
// So LCDWrite may require some adjustment...
// NOTE: R/W is hard wired to ground (write only)
LiquidCrystal lcd(
2, // RS / Port D pin 2
3, // E / Port D pin 3
8,9,10,11, // DB 0-3 / Port B pins 0-3
A0,A1,A2,A3 // DB 4-7 / Port C pins 0-3
);
// must match above settings
#define RS_PIN 2 // on port D
#define ENABLE_PIN 3 // also on port D
char buf[20*4+1];
void setup()
{
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
Serial.begin(9600);
lcd.begin(20,4);
for(int i=0; i<80; ++i)
buf[i] = ' ';
buf[80] = '\0';
lcd.setCursor(0,0);
lcd.print("Whee!");
lcd.setCursor(0,2);
lcd.print("The quick brown fox");
lcd.setCursor(0,3);
lcd.print("jumps over lazy dog");
}
void loop()
{
/*long start = millis();
int iterations = 80;
int last = 0;
int idx = 0;
for(int i=0; i<iterations; i++)
{
buf[last] = ' ';
buf[idx] = '#';
last = i;
idx = (idx+1)%80;
LCDWrite(buf);
delay(100);
}
buf[last] = ' ';
long end = millis();
Serial.print(iterations);
Serial.write(" iterations in ");
Serial.print(end-start);
Serial.write("millis\n");*/
lcd.setCursor(0,1);
lcd.print(millis());
}
// Fast (direct register access) LCD writing routine.
// This takes about 3.5 milliseconds to update the entire 20x4 screen
// when using full 8-bit parallel interfacing.
// Most of that is just spent waiting for the LCD to be ready again.
void LCDWrite(char* data)
{
PORTD |= (1<<RS_PIN);
while(*data != 0)
{
byte b = (byte)*data;
// TODO: If other pins on ports B or C get used, we will have to ensure we do not stomp on their values here.
PORTB = b & 0x0F;
PORTC = (b >> 4) & 0x0F;
delayMicroseconds(1);
PORTD |= (1<<ENABLE_PIN);
delayMicroseconds(1);
PORTD &= ~(1<<ENABLE_PIN);
delayMicroseconds(40);
/*
PORTC = (b) & 0x0F;
delayMicroseconds(1);
PORTD |= (1<<ENABLE_PIN);
delayMicroseconds(1);
PORTD &= ~(1<<ENABLE_PIN);
delayMicroseconds(40);
*/
++data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment