LCD Example

From Pinguino
Jump to: navigation, search


How to use Lcdlib Library.

You have to connect a LCD using the following pins (8 bit mode):

LCD pin pin Name Pinguino Other
1 Vss (Ground) Ground
2 Vdd +5v
3 Vo Connect to the cursor of a ~ 10k potentiometer between Ground and +5v to adjust contrast
4 RS (Register Select) Pin 8 (see #define)
5 R/W (Read/Write) Ground (Low=Write High=Read)
6 E (Enable) Pin 9 (see #define)
7 D0 Pin 0
8 D1 Pin 1
9 D2 Pin 2
10 D3 Pin 3
11 D4 Pin 4
12 D5 Pin 5
13 D6 Pin 6
14 D7 Pin 7
15 A (BackLight Anode)
16 K (BackLight Cathode)

R/W is connected to Ground as we do not read from LCD.

The source is now working.

Init sequence must be adapted according to display (2 lines x 16 characters in this example), if you understand French, you can read http://www.aurel32.net/elec/lcd.php . For English, look for HD44780 on Google or use Google Translate.

#define LCD_RS	8
#define LCD_E	9

/* Write a data character on LCD */
void lcdWrite(uchar c) {
	digitalWrite(LCD_RS, HIGH);	// Data RS=1
	PORTB = c;			// Output Data
	digitalWrite(LCD_E, HIGH);	// Positive pulse on E
	delay(1);
	digitalWrite(LCD_E, LOW);
	delay(1);
}

/* Write a control command on LCD */
void lcdWriteControl(uchar c) {
	digitalWrite(LCD_RS, LOW);	// Command RS=0
	PORTB = c;			// Output Data
	digitalWrite(LCD_E, HIGH);	// Positive pulse on E
	delay(1);
	digitalWrite(LCD_E, LOW);
	delay(1);
}

int i;
uchar hello[] = "Hello world";

void setup()
{
	/* Init output pins */
	digitalWrite(LCD_E, LOW);	// Set E low and E and RS as output
	pinMode(LCD_E, OUTPUT);		// E and RS as output
	pinMode(LCD_RS, OUTPUT);
	for(i=0; i<8; i++) {		// Port A as output		
		pinMode(i,OUTPUT);
	}

	/* Init LCD */
	delay(200);
	lcdWriteControl(0x38);		// 0x38 for 5x7 font or 0x3C for 5x10
	delay(5);
	lcdWriteControl(0x38);		// same
	delay(5);
	lcdWriteControl(0x06);		// Not shifted, RtoL
	lcdWriteControl(0x0C);		// Display on, don't display cursor
	lcdWriteControl(0x01);		// Clear display
	lcdWriteControl(0x02);		// Cursor home
	delay(2);
	
	/* Debug */
	pinMode(12, OUTPUT);		// LED on pin 12
	
}

void loop()
{
	// Output string
	for( i=0; hello[i]; i++) {	// While not null char
		lcdWrite(hello[i]);	// Output char
	}
	
	// Debug
	for(;;) {
		delay(500);		// Blink LED on port 12
		digitalWrite(12, LOW);
		delay(500);
		digitalWrite(12, HIGH);
	}
}
Here is the result:
Picture of a Test-Board running LCD