Hello everyone!
I'm trying to make a microcontroller communicate with a program on my desktop. I'm using serial port connections with Xbee radios on both ends.
The communication works fine when I send something from the microcontroller to the desktop and the program on the desktop then sends something back to the microcontroller.
However, when I require the information to be sent from the controller to the desktop program continuously until the desktop program sends a particular answer it doesn't work.
Here's the code for what I'm talking about:
unsigned char ans = 'N';
unsigned int count = 0;
void main(void)
{
while(1)
{
if(count == 0)
{
Configure();
count = 1;
}
//there is some more code here but is irrelevant to the serial communication
}
}
void Configure()
{
//Repeat this until the user accepts the sent string as correct
while(ans == 'N')
{
BuildString();
Send();
Receive();
}
}
void Send()
{
unsigned int i;
TMOD = 0x20;
TH1 = 0xFD;
SCON = 0x50;
TR1 = 1;
for(i=0; i<4; i++)
{
SBUF = toSend[i];
while(TI == 0);
TI = 0;
}
}
void Receive()
{
unsigned int j;
TMOD = 0x20;
TH1 = 0xFD;
SCON = 0x50;
TR1 = 1;
for(j=0; j<2; j++)
{
while(RI == 0);
Received[j] = SBUF;
RI = 0;
}
if(count == 0)
ans = Received[1];
else
{
RunType = Received[0];
Move = Received[1];
}
}
The BuildString() function simply constructs a string on the basis of some sensor inputs. The send and receive funtions work fine usually but when I need them to send and receive continuously, like in the Configure() funtion above, it doesn't work.
Any suggestions? I'd really appreciate them.