Display character typed ( Input from user )
Code :
#include<reg51.h> // Include 8051 library
void ser_init(); // Initialize serial communications.
void tx(unsigned char w); // Serial transmission function definition.
unsigned char d; // Store the character received from user temporary.
unsigned char rx(); // Serial receive function definition.
void main() // Main function starts.
{
ser_init(); // Serial function calling.
d=rx(); // Receive data and store it in d.
tx(d); // Transmit data stored in d.
}
void tx(unsigned char w) // Transmit function.
{
SBUF = w; // SBUF temporary buffer location where character is stored.
while(TI==0); // Wait until transmission completed flag gets high.
TI=0; // Once high , make it zero for the next character.
}
{
SBUF = w; // SBUF temporary buffer location where character is stored.
while(TI==0); // Wait until transmission completed flag gets high.
TI=0; // Once high , make it zero for the next character.
}
unsigned char rx() // Receive function.
{
while(RI==0); // Wait while receive flag is not high.
RI=0; // Reset the receive flag.
return SBUF; // Return data received in SBUF.
}
void ser_init() // Serial transmission function
{
SCON = 0X50; // Mode 1 (8-bit UART )
TMOD = 0X20; // Timer 1 in Mode 2.
TH1 = 0XFD; // Baud rate of 9600.
TR1 = 1; // Timer run flag enabled.
}
{
SCON = 0X50; // Mode 1 (8-bit UART )
TMOD = 0X20; // Timer 1 in Mode 2.
TH1 = 0XFD; // Baud rate of 9600.
TR1 = 1; // Timer run flag enabled.
}
: Circuit Diagram
A terminal window is connected to microcontroller
for serial comm.
RxD of 8051 is connected to TxD of terminal.
TxD of 8051 is connected to RxD of terminal.
Character Output Results :
The character that you type gets immediately displayed on the screen.

.gif)
Comments
Post a Comment