Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
* Description: Computes the "moving average"
* for a stream of data points it is receiving.
*******************************************/

#include <msp430.h>
#include "L22_header.h"
#define N_AVG_SAMPLES 4
#define SIZE 10 //Amount of numbers in number array
#include "moving_average.h"

#define N_AVG_SAMPLES 4
#define SIZE 10 //Amount of numbers in number array

int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
Expand All @@ -18,8 +19,10 @@ int main(void) {
int range;
int numbers[] = {174, 162, 149, 85, 130, 149, 153, 164, 169, 173};
int sampleArray[N_AVG_SAMPLES]; //Array that numbers are moved into and out of
Max = -256; // Lowest number possible for 1 byte, easily overwritten
Min = 255; // Highest number possible for 1 byte, easily overwritten

Max = -256; // Lowest number possible for 1 byte, easily overwritten
Min = 255; // Highest number possible for 1 byte, easily overwritten

int i = 0;
int x = 0;
int avg;
Expand All @@ -34,7 +37,7 @@ int main(void) {
}

//Determine size of array
// int SizeOfArray = sizeof(numbers)/sizeof(int); //Number of bytes in numbers divided by number of bytes in int
// int SizeOfArray = sizeof(numbers)/sizeof(int); //Number of bytes in numbers divided by number of bytes in int
int SizeOfArray = SIZE; //Can get by with only

//Finds the average
Expand All @@ -51,6 +54,5 @@ int main(void) {

while(1){} //Trap CPU


return 0;
}
12 changes: 10 additions & 2 deletions L22_imp.c → moving_average.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: Implementation for moving average computation
*/

#include "L22_Header.h"
#include "moving_average.h"

/*
*Sums up all the numbers in the input array and
Expand All @@ -15,9 +15,11 @@
int CalcAverage(int array[], int amountSamples){
int sum = 0;
int i;

for(i = 0; i < amountSamples; i++){
sum += array[i];
}

int avg = sum / amountSamples;

return avg;
Expand All @@ -29,9 +31,11 @@ int CalcAverage(int array[], int amountSamples){
*/
void shiftarray(int number, int array[], int amountSamples){
int i;

for(i=0; i < amountSamples-1; i++){
array[i] = array[i+1]; // Move array to the right one place
}

array[amountSamples-1] = number; //Move the next number into the array
}

Expand All @@ -44,12 +48,14 @@ void shiftarray(int number, int array[], int amountSamples){
*/
int maximum(int* Numbers, int maximum, int size){
int i;

for(i = 0; i < size; i++){
if(*Numbers > maximum){
maximum = *Numbers;
}
Numbers++;
Numbers++;
}

return maximum;
}

Expand All @@ -62,12 +68,14 @@ int maximum(int* Numbers, int maximum, int size){
*/
int minimum(int* Numbers, int minimum, int size){
int i;

for(i = 0; i < size; i++){
if(*Numbers < minimum){
minimum = *Numbers;
}
Numbers++;
}

return minimum;
}

Expand Down
2 changes: 0 additions & 2 deletions L22_header.h → moving_average.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* Description: Header file for moving average computation
*/



int CalcAverage(int array[], int numberOfSamples);

void shiftarray(int sample, int array[], int numberOfSamples);
Expand Down