#Microcontrollers LCD interfacing with 8051

// In Embedded C:

/* Connect LCD module to CN2
DATA -> Port0
RS -> P3.2
EN -> P3.3
*/

#include <reg51.h>

#define DATAPORT P0
#define EN 1<<3 // 0000 1000
#define RS 1<<2 // 0000 0100

void delay(void)
{
unsigned int i;
for(i=0;i<0xFF;i++);
}

void LCDcmd(unsigned char cmd)
{
DATAPORT = cmd;
P3 = 0;
delay();
P3 = EN; // P3 = 0000 1000
delay();
P3 = 0;
delay();
}

void LCDdata(unsigned char dat)
{
DATAPORT = dat;
P3 = RS;
delay();
P3 = RS | EN; //P3 = ( 0000 1000 OR 0000 0100) = 0000 1100
delay();
P3 = RS;
delay();
}


void LCD_string(int row, int pos, char *ptr)
{
unsigned char temp;
if(row ==1)
temp = (0x80 | pos);
else
temp = (0xC0 | pos);
LCDcmd(temp); // cursor is set to (row, pos)

while(*ptr)
{
LCDdata(*ptr);
ptr++;
}
}


int main()
{
LCDcmd(0x38); //8 bit
LCDcmd(0x06); //entry mode
LCDcmd(0x0C); // display on cursor off
LCDcmd(0x80); // cursor on 1st line 1st pos
LCDcmd(0x01); // clear display

LCD_string(1,1,"MicroEmbedded");
LCD_string(2,0,"Micro51 S Board");

while(1); //stop here
return 0;

}


// In Assembly

/*------------------LCD---------------------*/
/*
Interfacing Pins : 
P0.0 thru P0.7 interfaced to D0 thru D7.
P3.2 ----> LCD_RS (Register Select).
P3.3 ----> LCD_EN (Enable).

=============================================================================
*/
;Interface declaration
RS  EQU P3^2
EN  EQU P3^3

org 0h
ljmp start

DelaymS:
mov r4,#04 ;  (4*255) ~ 1000uS = 1mS
mov r5,#0
loop:
djnz r5,loop
djnz r4,loop
ret

;Subroutine to write DATA to LCD : 
;Input Value: passed in A accumulator

write_lcd_data:
mov P0,A ;Write the value on port P0
mov r6,#05h ;Call delay for 5mS
continue1:
lcall DelaymS
djnz r6, continue1
SETB RS
SETB EN
mov r6,#01h ;Call delay for 1mS
continue2:
lcall DelaymS
djnz r6, continue2
MOV P3,#00 ; clear the control signals
ret

;Subroutine to send command to LCD

write_lcd_cmd:
mov P0,A ;Write the value on port P0
mov r6,#0ah ;Call delay for 5mS
continue3:
lcall DelaymS
djnz r6, continue3
SETB EN
mov r6,#01h ;Call delay for 1mS
continue4:
lcall DelaymS
djnz r6, continue4
MOV P3,#00 ; clear the control signals
ret
;Subroutine to blink display
blink_display:
mov a,#08
lcall write_lcd_cmd
mov a, #0fh
lcall write_lcd_cmd
ret

start:
mov a,#0x38 ; //function set
lcall write_lcd_cmd
mov a,#0x38 ; //function set
lcall write_lcd_cmd
mov a,#0x08 ; //display off
lcall write_lcd_cmd
mov a,#0x01 ; //display clear
lcall write_lcd_cmd
mov a,#0x06 ; //entry mode set
lcall write_lcd_cmd
mov a,#0x0f ; //display on
lcall write_lcd_cmd
mov a,#0x80 ; //set address counter value
lcall write_lcd_cmd
;Start displaying the string
mov a,#'H'  
lcall write_lcd_data
mov a,#'E'  
lcall write_lcd_data
mov a,#'L'  
lcall write_lcd_data
mov a,#'L'  
lcall write_lcd_data
mov a,#'O'  
lcall write_lcd_data
mov a,#' '  
lcall write_lcd_data
mov a,#'W'  
lcall write_lcd_data
mov a,#'O'  
lcall write_lcd_data
mov a,#'R'  
lcall write_lcd_data
mov a,#'L'  
lcall write_lcd_data
mov a,#'D'  
lcall write_lcd_data

infinite_loop:
// lcall blink_display
sjmp infinite_loop
END


Comments