FIFO buffer in C for AT89s8252

Status
Not open for further replies.

GrandAlf

Advanced Member level 2
Joined
Mar 9, 2002
Messages
520
Helped
47
Reputation
92
Reaction score
6
Trophy points
1,298
Location
UK
Activity points
4,730
Anyone help with this, not very good at C yet. What I have is an AT89s8252 programmed with k**l and tiny rtos. I have one task that that sits and uses _getkey and assigns it to an 8 bit int global variable. This variable is then read by another task that processes it if > 0. The problem I have is that if more than one number is received in a period less than the processing time, I find I am missing it. What I would like to do is for the _getkey task to write to a buffer of 10 say, and then read it in the other task, of course when a particular location is read it should then be destroyed and the next in the queue move up to be processed, until the buffer was empty. I have searched on the web, but cannot find anything suitable for MCUs. I had thought that this chip had a hardware buffer, but in this application it seems to have no effect. Would very much appreciate any assistance on this.

Mike
 

fifo in c

I usually do something like these code fragments:

Code:
#define BUFSIZE 16      /* power of two should compile efficiently */

volatile unsigned char fifo[BUFSIZE];
volatile unsigned rd=0, wr=0;       /* read and write positions */

/* write a byte into FIFO */
fifo[wr] = get_input_byte();
wr = (wr + 1) % BUFSIZE;

/* read bytes from FIFO */
while (rd != wr)
{
  do_something_with_byte(fifo[rd]);
  rd = (rd + 1) % BUFSIZE;
}
 

c fifo

Brilliant, just what I was looking for. It is similar to what I was trying to do, but I guess I screwed up in a few places, I can see where now. Many thanks.
 

c fifo buffer

There is another little trik with FIFO buffers.

If you use good FIFO sizes (16, 32, 64 or 128 bytes) you can use >> operator instead %.

But if you use 256 bytes FIFO size and one byte FIFO buffer pointer you don't need any divide operator, only pointer increment operation.

#define FIFO_BUFFER_SIZE 256

unsigned char FIFO[FIFO_BUFFER_SIZE];
unsigned char BUFFER[2048];

unisgned char *pw, *pr; // use appropriate compiler directive for one byte pointers
unisgned char *p; // two-byte pointer

void interrupt_service_routine()
{
*pw++=get_input_byte();
}

void main() {
pw=&FIFO[0]; // Init FIFO write pointer
pr=&FIFO[0]; // Init FIFO read pointer

p=&BUFFER[0];


// In some process routine check state of FIFO and store received bytes to
// linear buffer.

while (pr!= pw) {
*p++=*pr++; // store received byte
}

// In another process routine check, process and empty BUFFER

if (p!=BUFFER[0]) {
check_buffer();
if (condition_to_empty_buffer) {
p=BUFFER[0];
}
}
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…