Microcontroller › 8051 › Serial communication in lcd
- This topic has 5 replies, 4 voices, and was last updated 12 years, 7 months ago by Dexter.
-
AuthorPosts
-
December 21, 2011 at 7:25 am #1457Syed ImranParticipant
Hi,
I need the code for how to communicate serially in lcd i.e watever i type in the hyperterminal,it should display on the lcd. Waiting for your response.
Thanking You
December 24, 2011 at 8:40 am #6941Syed ImranParticipantAny reply to my query.
December 28, 2011 at 4:14 am #6955DexterParticipantuse this coding
it may help u
re check according to ur circuit
#include <reg51.H>
#include <stdio.h>
#include <string.h>#define LCD_PORT P1 //lcd data port
/* PIN CONNECTIONS */
sbit EN=P3^2; //enable pin for LCD
sbit RW=P3^3; //write pin for LCD
sbit RS=P3^4; //register select pin of LCD/* Declarations */
// DELAYS
void del_1ms (void); //1 milli second delay
void del_30ms (void); //30 milli second delay
void del_1s (void); //delay of 1 second
void del_3ms (void); //delay of 3 milli seconds// LCD
void lcd_init (void); //routine to initialise LCD
void printLcd (unsigned char *); //prints a line on LCD
void printLcd2 (unsigned char *); //prints a line on LCD
void clear_display (void); //clears LCD screen
void display_data (unsigned char); //displays one char
void set_add (unsigned char ); //sets the display position
void write_in (unsigned char ); //writes data in to LCD as instruction
void write_data (unsigned char ); //writes one byte into LCD as data
void write (unsigned char ); //writes one byte into LCD
void lcd_cmdwrt(unsigned char );
void lcd_datawrt(unsigned char *);// SERIAL COMMUNICATION
void serial_init (void); //initialise serial communication
unsigned char recv_byte (void); //recieves th ASCII value
void send_byte (unsigned char); //sends ASCII value to PC or microcontroller
void sendString (unsigned char *str);
void sendString1 (unsigned char *str);
unsigned char volatile cha,i=0;void main()
{
unsigned char volatile no;
lcd_init();
serial_init(); //initialise lcd display
clear_display();
printLcd(“SYSTEM INSTALING”);
printLcd2(“..”);while(1)
{
run1=recv_byte();
if(isalpha(run1) != 0) display_data(run1);
else if(isdigit(run1) !=0) break;
}
display_data(run1);
del_1s();
}void lcd_cmdwrt(unsigned char cmd)
{P1=cmd;
RS=0;
RW=0;
EN=1;
_nop_();
_nop_();
_nop_();
EN=0;
for(i=0;i<200;i++);
for(i=0;i<200;i++);
}void lcd_datawrt(unsigned char *dat)
{
while(*dat)
{
P1=*dat;
RS=1;
RW=0;
EN=1;
_nop_();
_nop_();
_nop_();
EN=0;
for(i=0;i<200;i++);
for(i=0;i<200;i++);
dat++;
}
}
//LCD Routines
//clears the entire display area of LCDvoid clear_display(void)
{
write_in(0x01); //clears the lcd display
write_in(0x80); //write code to blink at first line first coloum
}
//routine to display the value in the accumulator in LCDvoid display_data(unsigned char ch)
{
unsigned char dummy;dummy = ch; //backup in variable
write_data(ch); //writes the value to LCD module
}
//routine to display a string on first line
//this routine will display a string stored at address pointed by dptrvoid printLcd(unsigned char *str)
{
unsigned char len,i;
len = strlen(str);
set_add(0x80); //sets display position in first line
for(i=0;i<len;i++)
{
display_data(*str); //displays the character on LCD
str++;
}
}void printLcd2(unsigned char *str)
{
unsigned char len,i;
len = strlen(str);
set_add(0xc0); //sets display position in second line
for(i=0;i<len;i++)
{
display_data(*str); //displays the character on LCD
str++;
}
}//routine to set the display position
void set_add(unsigned char ch)
{
unsigned char dummy;
dummy = ch; //backup in variable
write_in(ch); //writes it as instruction
}//routine to write an instruction to LCD
void write_in(unsigned char ch)
{
RS=0; //rs =0 for instr
RW=0;
write(ch); //routine to transfer 8 bits to LCD
}//routine to write a data to LCD
void write_data(unsigned char ch)
{
RS = 1;
RW=0; //rs = 1 for data
write(ch); //transfers 8 bits to lcd module
}//routine to transfer 8 bits to LCD module
void write(unsigned char ch)
{
LCD_PORT = ch; //preserves the old value of port
EN = 1; //enable pin high
del_1ms();
EN = 0; //enable = 0
del_1ms();
}//routine to initialise the lcd
void lcd_init(void)
{
del_30ms();
write_in(0x38); //to make lcd
del_3ms();
write_in(0x0f); //to make lcd to select 5×7 matrix display
del_3ms();
write_in(0x01); //to make lcd to clear the display
del_3ms();
write_in(0x80); //to make lcd cursor to bigining of first line
del_3ms();
}//SERIAL ROUTINES
void serial_init()
{
SCON=0x50; //mode 1 serial communication
TMOD=0x20; //timer 1 auto reload mode
TH1=0xfd; //9600 baudrate at 11.0592 MHz
TR1=1;
EA=1;
EX0=1;
IT0=1;
IP=0x10;
}unsigned char recv_byte()
{
unsigned char c;
while(!RI); //waits till a byte is recieved
RI = 0;
c = SBUF;
return(c);
}void send_byte(unsigned char ch)
{
TI = 0;
SBUF = ch;
while(!TI);
del_1ms();
}void sendString(unsigned char *str)
{
while(*str != ‘