Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
07/09/01 17:52
Read: times


 
#13097 - RE: Circural buffer
Andrej,

The code isn't too successful because you are mixing a pre-test of the circular index with a post increment. This makes sure the index doesn't go beyond 63, but 63 is never reached.

Sakri's solution is better, however, it will ONLY work on buffers that are of a power of 2 size (.., 16, 32, 64,...).

On a PC where multipy is cheap, I use something more like...

to store data:
<code>serin_buff[sihead] = SBUF;
sihead = (sihead + 1) % SLEN;</code>

to get data:
<code>tmp = serin_buff[sitail];
sitail = (sitail + 1) % SLEN;</code>

However, this can be more cheaply done on a 8051 by doing something like...

to store data:
<code>serin_buff[sihead] = SBUF;
if (++sihead >= SLEN) sihead = 0;</code>

to get data:
<code>tmp = serin_buff[sitail];
sitail = (sitail + 1);
if (++sitail >= SLEN) sitail = 0;</code>

Another possible problem with your original source is that you don't disable interrupts before using com_rbuflen() within the com_getchar() routine. com_rbuflen() must have interrupts dissabled when testing and enable interrupts before leaving to work in your sample code.

To handle overflow, you can do something like...

to store data:
<code>serin_buff[sihead] = SBUF;
if (++sihead >= SLEN) sihead = 0;
if (sihead == sitail) {
  /* OVERFLOW CONDITION */
  if (--sihead < 0) sihead = SBUF-1;
  sound_the_alarms();
}
</code>

<>< Lance.


List of 7 messages in thread
TopicAuthorDate
Circural buffer            01/01/70 00:00      
RE: Circural buffer            01/01/70 00:00      
RE: Circural buffer            01/01/70 00:00      
RE: Circural buffer            01/01/70 00:00      
RE: Circular buffer            01/01/70 00:00      
RE: Circural buffer            01/01/70 00:00      
RE: Circural buffer            01/01/70 00:00      

Back to Subject List