R.J.Hamlett Guest
|
Re: XON/XOFF flow control |
Posted: Wed Jul 30, 2003 3:14 am |
|
|
:=Does anyone perhaps have advise or example code to do XON/XOFF serial comms flow control between a PIC16F877 and a modem?
:=
:=Thank you
:=
:=Levett
Use a ring buffer on receive (do a search here for details of this). Have a counter reflecting how many characters are in this. When adding characters to the buffer check the counter. If it rises beyond an arbitrary size (depends on the size of the buffer, and the likely size of the FIFO at the other end - I have used a count of '44', with a 64 character buffer), then add a 'XOFF' character to the output. I have the 'output' routine modified, so it too has a buffer, but also has a 'handshake' byte stored seperately. To send XOFF, this is put into the 'handshake' byte. The interrupt driven transmit code, tests this byte for being non-zero, _before_ sending the next character from the output buffer. In the event there is a handshake character waiting, this is sent, the byte is cleared, and the routine returns. If there is no handshake character, the next character from the output buffer is sent instead. This way the handshake character 'pre-empts' the data waiting in the output buffer.
Now the receive code also checks for the buffer dropping to a 'low' value (I used 16 characters), and then sends 'XON' the same way.
The receive code also checks if the character received, is either XON, or XOFF. In the latter case, I disable the TBE interrupt (which stops data from being sent), while the XON character turns this back on.
Now I was talking to a PC, which had large(ish) FIFO buffers, so when I sent 'XOFF', I'd still typically receive perhaps another 14 characters, before the communications stopped, hence my use of a large buffer, and sending the stop at 44 characters. In this case very large strings were being sent in both directions, with quite a lot of processing involved. Generally, provided your own comm routine, is interrupt driven, and fast, there may be no need for you to ever send 'XOFF', so you only have to handle the handshake in the transmit direction, which is easy to do by just disabling/enabling the transmit interrupt. :-)
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516492 |
|