aboutsummaryrefslogtreecommitdiffstats
path: root/PICCode.X/bufferlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'PICCode.X/bufferlib.c')
-rw-r--r--PICCode.X/bufferlib.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/PICCode.X/bufferlib.c b/PICCode.X/bufferlib.c
new file mode 100644
index 0000000..4768c95
--- /dev/null
+++ b/PICCode.X/bufferlib.c
@@ -0,0 +1,35 @@
+#include "xc.h"
+
+#define BUFFERSIZE 32
+
+volatile int write = 0, numE = 0;
+volatile unsigned int buffer[BUFFERSIZE];
+
+ void putVal(int newValue) { // add a new value to the buffer
+ buffer[write++] = newValue;
+ 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
+ int i;
+ for(i = 0; i < BUFFERSIZE; i++) {
+ buffer[i] = 0;
+ }
+ } \ No newline at end of file