Program Rolling Table in C

basically miscellaneous, if it doesn't fit another sub-forum enter it here, we might even promote it to its own sub-forum
Post Reply
danlee58
Posts: 210
Joined: Thu Jan 17, 2013 2:29 am

Program Rolling Table in C

Post by danlee58 »

I have an Interrupt that fills a 64 Integer table with 1 Integer for each Interrupt. I need to roll the table, so it always has the last 63 values when a new value is obtained. I need to do that without overwriting the next value in the table, because the next value has to be used for the following operation.

for (k=0; k < 64; k++){

TABLE[62-k] = TABLE[63-k]; //Move Table down 1
}



demoman
Posts: 4
Joined: Thu Mar 28, 2013 6:02 pm

Re: Program Rolling Table in C

Post by demoman »

Copying data always takes time, as your buffer is a multiple of 2 why not just wrap around within the buffer.

Code: Select all

tablePtr = 0;           // initial value
 ...
Table[tablePtr++] = sample;
tablePtr &= 63;       // wrap around buffer
...
// as an example for reading the last 64 values -- add them up
sum = 0;
for (i=0; i<64; i++) {
  sum += Table [(tablePtr + i) & 0x3f];
}

danlee58
Posts: 210
Joined: Thu Jan 17, 2013 2:29 am

Re: Program Rolling Table in C

Post by danlee58 »

Look good, Thanks I'll try it.

danlee58
Posts: 210
Joined: Thu Jan 17, 2013 2:29 am

Re: Program Rolling Table in C

Post by danlee58 »

That works OK, but I need to insert zeros, if there is no interrupt in a certain time, otherwise the old data never changes.

I suppose that I can read the Timer Value in the Main program, then

if T1_CR0 > xxxxx, Table[tablePtr++] = 0;

tablePtr would have to be extern, and I need to reset T1_CR0;

Post Reply