| ??? 02/07/04 16:45 Read: times |
#64166 - RE: Circular buffers Responding to: ???'s previous message |
When working with circular buffers it is very handy to size them in powers of two. e.g. I have a circular buffer 32 bytes long and the offset is after each increment just ANDed with 0x1f.
I see many things above about the pointers being less or more than whatever. With circular buffers, that does not hold, the only valid comparison is EQUAL. Also why do you bother with counters? For a circular buffer of, say 64 bytes, I'll show transmit for example. The code is written with no regard for accuracy, but should show the principle.
//to request a byte to be transmitted:
CirBuf[xstoffs] = byte;
xstoffs++;
xstoffs &= 0x3f;
if (!xmitBusy)
{
sbuf = byte;
xftoffs++
xftoffs &= 0x3f;
xmit Busy = TRUE;
}
//TI interrupt
if ( xstoffs == xftoffs) xmitBusy = FALSE;
else
SBUF = CirBuf[xftoffs] ;
xftoffs++
xftoffs &= 0x3f;
Erik |



