Personal G-Force Meter: Software

Program Download:

Main C File
G_Force_Meter.c

The Software
The software for this project is limited to only a few parts. The A/D capture, conversion to a non-decimal numerical representation of the g-force and then that value is passed to a function that takes care of the output.

           Since most of the code is pretty straight forward in terms of output data, I'll just focus on the main algorithm that gets the data from the sensor and make sense of it. The main algorithm that I used was adopted from this project by the guys at Dimension Engineering. I updated it and changed it for the C18 compiler as well as changed a few parameters to better match the sensor.

----- Begin Code -----

while(1){
update_char_display(char_disp);  //Update 7-Segs
Delay1KTCYx(250);  //Delay A Little While
Delay10TCYx(5);  //Redundant Delay For A/D
ConvertADC();  //Do A/D Conversion
while( BusyADC() );
current_result = ReadADC();
temp = current_result - past_result;

      //If Only A Slight Change Don't Update
    if( temp > 2 || temp < -2 )
    {
      past_result = current_result;
      g_val = current_result - gravity_ss;
      g_val = g_val << 5;
      g_val = g_val / display_divider;
    if(g_val < 0)
      g_val = g_val * -1;
      i = 2;
    }
    while(i!= 255)
    {
      char_disp[i]=g_val%10;
      g_val = g_val/10;
      i--;
  }
}
----- End Code -----


           The algorithm above won't make much sense unless you're familiar with how the accelerometer work. First it looks at the change in A/D results, if that change is large enough to warrant the display to get updated it proceeds. The result is subtracted from the gravity steady state value which will be 512 @ 0 G's. What we do next is shift the values to the left and use a known display divider (in this case it is 20) to get the same value that we had before but in non-decimal form. So changing 1.23 => 123. This will make updating the display significantly easier.



;