Display a string at once using serial comm.
Code :
#include<reg51.h> // Include 8051 library.void ser_init(); // Initialize serial communications.
unsigned char p[100]; // Store the string received from user.
void rx_word(unsigned int len); // receive string from user.
void rx_word(unsigned char a[]); // transmit string of user.
void tx(unsigned char w); // transmit character of string.
unsigned char rx(); // receive character of string.
void main() // Main function starts
{
ser_init(); // Serial function calling
rx_word(8); // receive string from user of length 8
tx_word(p); // transmit function for displaying data.
}
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.
}
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.}
void tx_word(unsigned char a[]) // transmit function for character stored in array
{
int i; // integer i
for(i=0;a[i]!='\0';i++) // for loop until null is reached
{
tx(a[i]); // transmit function
}
}
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 rx_word(unsigned int len) // receive function to store character in array
{
int i; // integer i
for(i=0;i<len;i++) // for loop until string user defined length is reached.
{
p[i]=rx(); // Receive function.
}
}
: 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.
String Output Results :
Here the word you type will be displayed at once, once the string length is reached .
Comments
Post a Comment