snel,
Despite UART transmission is categorized as asynchronous, the sensing of each byte is synched with falling edge of first bit, an all further bits are read at middle of its level.
+++
Thanks andre for the valuable suggestion.
It got me to understand the concept better.
- - - Updated - - -
However,
I have one more serial programming issue, that seems quite challenging to me.
I have to write a program for 8051. The program has to pass a value serially.
This program will be used to send command to a Robot to control its movements.
( This value could be like MOV-LEFT60-CAPTAIN or MOV-RIGHT40-CAPTAIN or MOV-BACK20-CAPTAIN)
Now for this, my program could be divided into three section.
Section 1) to send MOV- values serially by using an array (since hese values would always be fixed/hardcoded).
Section 2) To send these dynamic values LEFT60/RIGHT40/BACK20 serially based on user requirement.
Section 3) to send -CAPTAIN values serially by using an array (since these values would always be fixed/hardcoded).
So now the issues comes in Section2. Since section 2 is completely dynamic and is based on users wish for controlling the movement of robot.
So there must be some way for for 8051 to check Rx pin for the last character.
Consider a case of LEFT6
0
So how the controller knows that there is nothing after
0and it should break the loop.
(I mean how to write a logic to wait for half a second or some miliseconds after transferring the last character.
If a character is not received at Rx for half a seconds the loop should itself break out and proceed to section 3.)
I have writte a code below in which section 1 is complete but i am not able to come out of section 2 based on the last character recieved at Rx.
Section 3 is not yet mentioned in my program.
#include <reg51.h>
void SerTx (unsigned char);
void SerRx (unsigned char *);
void main (void)
{
char byteBuf;
unsigned char z ;
unsigned char y ;
int flag=0; // making the initial value of flag to 0
code unsigned char atxb[]="MOV-";
TMOD = 0x20; //Timer 1, 8 bit auto reload
TH1 = 0XFD; // or: TH1 = -3, 9600 baud
SCON = 0x50;
TR1 = 1; //start timer
while(1)
{
while (flag==0) //checkin the condition based on flag
{
for (z=0;z<=3;z++)
{
y=atxb[z];
SerTx
;
}
flag=1; // changing the flag value to break the loop
}
while (flag==1)
{
SerRx(&byteBuf); // read byte from serial port
SerTx(byteBuf); // send byte back to serial port
}
}
}
void SerTx(unsigned char x)
{
SBUF = x; // put the char in SBUF register
while(TI==0); // wait until transmitted
TI = 0;
}
void SerRx(unsigned char *pX)
{
while(RI==0); // wait until received
RI = 0;
*pX = SBUF; // copy the data in SBUF to px
}
Please let me know the modification that i should make in the code to come out of the second while loop if the last character has been received at the Rx.