May 20, 2015
11:05 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
May 20, 2015
11:05 AM
I'm trying to calculate the average of all values in an array. The array values get loaded from a Measure() function (not shown) and array values are constantly updated to create a running average.
There is a for loop that sums the array values. Then I'm dividing the sum by the number of array values and printing result on LCD. My problem is when i place the division directly after the summing "for" loop, the division never takes place and I end up with FlightTimeAvg = FlightTimeSum. When i moved the division line after a couple of LCD instructions I get the proper answer.
Does anyone have any ideas what could be going on?
Thanks
Aaron
while(1)
{
// Clear LCD screen
LCD_Position(0,0);
LCD_PrCString (" ");
LCD_Position(1,0);
LCD_PrCString (" ");
// Print "Calculating" on row 1
LCD_Position(0,0);
LCD_PrCString ("Calculating...");
// Reset Tick counter
Tick = 0;
// Delay
while(Tick < 40);
// Call Measure function
FlightTime = Measure();
// Calculate sum of FlightTime array
FlightTimeSum = 0;
for (j = 0; j < Samples; j++)
{
FlightTimeSum += FlightTime;
}
//Calculate average of array, when placed immediatelyhere
//after summing routine, the division does not
//take place and I end up with FlightTimeAvg = FlightTimeSum
//FlightTimeAvg = FlightTimeSum / Samples;
// Clear LCD row 1
LCD_Position(0,0);
LCD_PrCString (" ");
// Print "Done" on row 1
LCD_Position(0,0);
LCD_PrCString ("Done");
//Calculate average of array, when placed here division takes
//place and get expected result
FlightTimeAvg = FlightTimeSum / Samples;
// Print FlightTime on row 2
LCD_Position(1,0);
csprintf(LCDBuffer, "%u ", FlightTimeAvg);
LCD_PrString(LCDBuffer);
// Reset Tick counter
Tick = 0;
// Delay
while(Tick < 40);
i++;
if (i == Samples)
{
i = 0;
}
}
}
Labels
- Labels:
-
PSoC 1
10 Replies