Microcontroller › 8051 › Help with serial communication
- This topic has 2 replies, 2 voices, and was last updated 12 years, 10 months ago by Dexter.
-
AuthorPosts
-
October 29, 2011 at 6:56 pm #1362JulianParticipant
Hi,
I’m trying to do serial communication between 8051 and virtual terminal in Proteus. Whatever that is typed on the terminal, I wish to display it on the LCD. Problem : I can’t type anything on the terminal. Only the cursor is blinking. Below is my code, please correct me if there is any mistakes.
=================================================
#include <reg51.h>#define lcd_port P2int j;sbit rs = P0^5;sbit rw = P0^6;sbit en = P0^7;void delay(unsigned int msec){int i,j ;for(i=0;i<msec;i++)for(j=0;j<200;j++);}void lcdcmd(unsigned char value){rs = 0;rw = 0;lcd_port = value;en = 1;delay(1);en = 0;}void lcd_data(unsigned char display){rs = 1;rw = 0;lcd_port = display;en = 1;delay(1);en = 0;}void lcd_init(){lcdcmd(0x38);delay(150);lcdcmd(0x0E);delay(150);lcdcmd(0x01);delay(150);lcdcmd(0x06);delay(150);lcdcmd(0x80);delay(150);}void main(void){char word;lcd_init();TMOD = 0x20;TH1 = 0xFD;SCON = 0x50;TR1 = 1;while(1){while (RI == 0);word = SBUF;lcd_data(word);RI = 0;}}==============================================Not much experience in programming and first time doing serial comm, help will be much appreciated. Thanking in advance.
October 29, 2011 at 7:01 pm #6762JulianParticipantProblem solved!
November 25, 2011 at 12:46 am #6877DexterParticipantTRY THIS
MY WORKING CODE
#include………
…………..
…………
// 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 enter(void);void main()
{unsigned char A;
serial_init();
sendString(“WELCOME TO RS232 COM1”);
enter();
sendString(“DEXTER8051”);
enter();A=recv_byte()
…………………
………………
………………
}
/*SENDING THE ENTER CAMMAND*/
void enter()
{
send_byte(0x0d); // <CR>
send_byte(0x0a); // <LF>
}/*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;
}
/* RECIVING BIT FROM PC*/
unsigned char recv_byte()
{
unsigned char c;
while(!RI); //waits till a byte is recieved
RI = 0;
c = SBUF;
return(c);
}
/* SENDING BIT TO PC*/
void send_byte(unsigned char ch)
{
TI = 0;
SBUF = ch;
while(!TI);
del_1ms();
}
/* SENDING STRING BIT TO PC*/
void sendString(unsigned char *str)
{
while(*str != ‘