blob: e9816a8af3a59d8a52fee362154f8cafeaf6fe2e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
#include "xc.h"
#define BUFFERSIZE 32
volatile int write = 0, numE = 0;
volatile unsigned int buffer[BUFFERSIZE]; // Basic circular buffer. Prime for buffer overflows, probably.
void putVal(int Vo, int ref) { // add a new value to the buffer
buffer[write++] = ref - Vo; // This is backwards because reasons
write %= BUFFERSIZE;
if (numE < BUFFERSIZE) {
++numE;
}
}
int getAvg() { // average all buffer values
unsigned long int sum = 0;
unsigned int avg;
int i;
for (i = 0; i < BUFFERSIZE; i++) {
sum += buffer[i];
}
avg = sum/numE;
return(avg);
}
void initBuffer() { // set all buffer values to zero, likely also prime for a buffer overflow
int i;
for(i = 0; i < BUFFERSIZE; i++) {
buffer[i] = 0;
}
}
|