So, I've got my usart speaking modbus decently well thanks to the help of you all on the board.
But periodically the usart seems to hang. Here is my code...
Code:
void modbusSendOutputFrame ( cBuffer *buffer )
{
PORTB |= (1 << PB0); // Send RTS high
while (outBuffer.datalength > 0 )
{
//Wait for an empty transmit buffer
while ( !(UCSR0A & (1 << UDRE0 )) );
UDR0 = bufferGetFromFront( &outBuffer );
}
//Spin for a bit until the USART is done transmitting
while ( ! (UCSR0A & ( 1 << TXC0 ) ) );
UCSR0A |= ( 1 << TXC0 ); // clear TXCO
PORTB &= ~(1 << PB0); // Transmission complete. Send RTS low
}
For some reason the program gets stuck on this line...
Code:
//Spin for a bit until the USART is done transmitting
while ( ! (UCSR0A & ( 1 << TXC0 ) ) );
The way I know this is that I run the program in debug mode with the jtag. It runs fine for a while, but then begins to hang completely. Once it is hung, I pause the program execution with the debugger, and we always end up on that line.
It appears that TXCO is not going high.
After I pause the program, I add a breakpoint to the next line, and start execution again, but we seem to stay forever stuck waiting on TXC0.
What am I doing wrong?
Thanks,
Bill |