Introduction to Interfacing of 8051 Microcontroller with LCD
Interfacing of 8051 microcontroller with LCD is one of the very important step in learning the hardware and programming of the microcontroller 8051 based project. Many microcontroller based project required to communicate with the alphanumeric LCD. the most popular alphanumeric LCD is the 16×2 alphanumeric LCD. In the 16×2 alphanumeric LCD there are two rows each of 16 characters as shown below in the figure.
The most of the 16×2 LCD having 16 pins. the table below shows the naming and typical use of each of the 16 pins.Pin Description of 16×2 LCD
the table below shows the naming and typical use of each of the 16 pins.
| Pin Number | Pin Name | Description or Remark |
|---|---|---|
| 1 | VCC | It is VCC or +ive power pin and typically connected with +5 volt supply. |
| 2 | VEE | It is contrast control Pin and typically connected with voltage between +5 volt and ground as per the contrast requirement. |
Circuit Diagram of Interfacing of 8051 Microcontroller with LCD
The figure below shows the circuit diagram of Interfacing of 8051 Microcontroller with LCD.
Assembly Level Language Program of Interfacing of 8051 Microcontroller with LCD
The below shows the program of Interfacing of 8051 Microcontroller with LCD.
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
// used pins on port C control signals
// and D data
#define LCD_E 7 // PORTC.7
#define LCD_RS 6 //PORTC.6
#define LCD_RD 7 // PORTD.7
#define LCD_WR 6 // PORTD.6
//LCD commands
#define LCD_CLR 0x01 // clear LCD
#define LCD_8BIT 0x38 // 8-bit mode
#define LCD_INC 0x06 //Increment, display freeze
#define LCD_LINE1 0x80 // cursor Pos on line 1 (or with column)
#define LCD_LINE2 0xC0 // cursor Pos on line 2 (or with column)
void LCD_putchar(unsigned char data)
{
DDRA = 0xFF;
//RS high for data and Enable high
PORTC |= ((1<<LCD_RS)|(1<<LCD_E));
//put data on bus
PORTA = data;
/*the number of nops required varies with your clock frequency,
Can be altered */
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
// Enable low again
PORTC &= ~(1<<LCD_E);
//release port
DDRA = 0;
}
/*LCD_command works EXACTLY like LCD_putchar, but takes RS low for
accessing the command reg */
void LCD_command(unsigned char command)
{
DDRA = 0xFF; // data port as output
PORTC &= ~(1<<LCD_RS); //RS low for Command
PORTC |= (1<<LCD_E); //Enable pin high
PORTA = command; // put data on Port
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
PORTC &= ~(1<<LCD_E); //Enable Pin Low
DDRA = 0; // release bus
}
void LCD_init(void)
{
//setup the LCD control signals on PortC (RS and Enable as output)
DDRC |= ((1<<LCD_RS)|(1<<LCD_E));
PORTC &= ~((1<<LCD_RS)|(1<<LCD_E));// set both low
//setup the LCD control signals on PortD
DDRD |= ((1<<LCD_RD)|(1<<LCD_WR));// read and write pins (STK300 on PortG)
PORTD &= ~(1<<LCD_WR); // write pin low
// data Port A as output
DDRA = 0xFF;
/*if called right after power-up, we'll have to wait a bit (fine-tune
for faster execution) */
_delay_ms(50);
LCD_command(LCD_CLR); // clear display
LCD_command(LCD_8BIT); // set 8 data bits
LCD_command(LCD_INC); // cursor increments automatically
LCD_command(LCD_LINE1); // set cursor to row 1, position 0
}
void LCD_write(char* dstring)
{
//is the character pointed at by dstring a zero? If not, write character to LCD
while(*dstring)
{
// write the character from dstring to the LCD, then post-inc the string pointer.
LCD_putchar(*dstring++);
}
}
int main( void )
{
LCD_init();
LCD_write("Hello World");
LCD_command(LCD_LINE2 | 5); // move cursor to row 2, position 5
LCD_write("is here");

July 11th, 2016
admin 
Posted in
Tags:
