Reputation: 167
I'm trying to get the EUSART setup over RS485 on a PIC16F. For the RS485 i have an external circuit MAX3430 to make the conversion to RS485. I am able to both send and receive data. The problem I have is that as I do the switch back to RO after a TX transaction I notice the last two bytes don't get sent. It seems that if i do as below (I use pin RB6 as a switch between RO/TX) it's to fast and the last two bytes never get transmitted. What's the best practice here? I'm guessing adding some delay after last sent TX but i don't want to halt my entire program for this, unless that is the general recommendation? I am also exploring a timer as a possible solution, would just need a way to reset the timer so i always get the same delay in that case, currently working on this alternative. Any thoughts are appreciated.
IO_RB6_SetHigh(); // Enable TX mode
for(uint8_t i = 0; i < sizeof(msg); i++)
{
EUSART1_Write(msg[i]);
}
IO_RB6_SetLow(); // Enable Read mode
Thanks, Marcus
Upvotes: 0
Views: 342
Reputation: 497
You're switching to RX before transmission completes. You need to either wait two symbol times before calling IO_RB6_SetLow()
or (better) check the status of USART Tx buffer to see if it has completed the transmission. There is a bit called "Transmit shift register status bit", in PIC16F877A it's called TXSTA.TRMT
.
EUSART1_Write
would do this check before sending the next character, you may want to take a look at the source to see how it's done.
Upvotes: 3