Page 1 of 1

Program Rolling Table in C

Posted: Sun Mar 16, 2014 2:55 pm
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
}

Re: Program Rolling Table in C

Posted: Sun Mar 16, 2014 5:18 pm
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];
}

Re: Program Rolling Table in C

Posted: Mon Mar 17, 2014 12:08 am
by danlee58
Look good, Thanks I'll try it.

Re: Program Rolling Table in C

Posted: Sat Mar 22, 2014 8:01 pm
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;