Need help with a g force meter project!!

Discussion in 'The Projects Forum' started by Ranayh, May 16, 2013.

  1. Ranayh

    Thread Starter New Member

    May 6, 2013
    8
    0
    Im working on a personal g-force meter using a pic18f452 and i have a few problems.

    1. i was wondering if u can revise the codes with me and make sure that the mikroC code matches.

    2. im trying to simulate on proteus before working with the hardware and theres no accelerometer in the library. any1 has any suggestions??

    3. i wana look at more than one accelerometer before picking one, i want a dual-axis accelerometer. share ur ideas with me.

    Thank!!!
    this is the mp-lab code

    Code ( (Unknown Language)):
    1. #include <p18f452.h>
    2. #include <delays.h>
    3. #include <adc.h>
    4. #include <math.h>
    5.  
    6. #define gravity_ss 512
    7. #define display_divider 20
    8.  
    9. //PORTD - 0bBGCDHAEF
    10. #define one     0b10100000
    11. #define two     0b11010110
    12. #define three   0b11110100
    13. #define four    0b11100001
    14. #define five    0b01110101
    15. #define six     0b01110111
    16. #define seven   0b10100100
    17. #define eight   0b11110111
    18. #define nine    0b11110101
    19. #define zero    0b10110111
    20. #define blank   0b00000000
    21.  
    22. void update_char_display(int *char_disp)
    23. {
    24.     int i=0;
    25.     int all_digits[11] = {zero, one, two, three, four, five, six, seven, eight, nine};
    26.     for(i=0;i<3;i++)
    27.     {
    28.  
    29.         switch(i)
    30.         {
    31.         case 0 :    PORTB = all_digits[char_disp[i]] + 0b1000;
    32.             break;
    33.         case 1 :    PORTD = all_digits[char_disp[i]];
    34.             break;
    35.         case 2 :    PORTC = all_digits[char_disp[i]];
    36.             break;
    37.         default :
    38.             break;
    39.         }
    40.     }
    41.  
    42. }
    43.  
    44. void main(void)
    45. {
    46.  
    47. int char_disp[3];
    48.  
    49. int current_result, past_result = 0;
    50. int g_val, temp = 0;
    51.  
    52. int i=0;
    53.  
    54. TRISA = 0xFF;
    55. TRISB = 0x00;
    56. TRISC = 0x00;
    57. TRISD = 0x00;
    58.                 //Turn All Displays Off Initially
    59. PORTB = blank;      //PORTB -> Display 1
    60. PORTC = blank;      //PORTC -> Display 2
    61. PORTD = blank;      //PORTD -> Display 3
    62.  
    63.                     //Initialize A/D Converter
    64. OpenADC( ADC_FOSC_32
    65.         & ADC_RIGHT_JUST
    66.         & ADC_8ANA_0REF,
    67.         ADC_CH0 & ADC_INT_OFF );
    68.  
    69. while(1){
    70.  
    71. update_char_display(char_disp);
    72. Delay1KTCYx(250);
    73.  
    74.  
    75. Delay10TCYx(5);
    76. ConvertADC();
    77. while( BusyADC() );
    78.     current_result = ReadADC();
    79.  
    80. temp = current_result - past_result;
    81.  
    82.     if( temp > 2 || temp < -2 )
    83.     {
    84.     past_result = current_result;
    85.     g_val = current_result - gravity_ss;
    86.     g_val = g_val << 5;
    87.     g_val = g_val / display_divider;
    88.  
    89.     if(g_val < 0)
    90.         g_val = g_val * -1;
    91.  
    92.     i = 2;
    93.     }
    94.     while(i!= 255)
    95.     {
    96.         char_disp[i]=g_val%10;
    97.         g_val = g_val/10;
    98.         i--;
    99.     }
    100.  
    101. }
    102.  
    103.  
    104.  
    105. }
    106. [/i][/i][/i][/i]


    #define gravity_ss 512
    #define display_divider 20

    //PORTD - 0bBGCDHAEF
    #define one 0b10100000
    #define two 0b11010110
    #define three 0b11110100
    #define four 0b11100001
    #define five 0b01110101
    #define six 0b01110111
    #define seven 0b10100100
    #define eight 0b11110111
    #define nine 0b11110101
    #define zero 0b10110111
    #define blank 0b00000000

    void update_char_display(int *char_disp)
    {
    int i=0;
    int all_digits[11] = {zero, one, two, three, four, five, six, seven, eight, nine};
    for(i=0;i<3;i++)
    {

    switch(i)
    {
    case 0 : PORTB = all_digits[char_disp] + 0b1000;
    break;
    case 1 : PORTD = all_digits[char_disp];
    break;
    case 2 : PORTC = all_digits[char_disp];
    break;
    default :
    break;
    }
    }

    }

    void main(void)
    {

    int char_disp[3];

    int current_result=0;
    int past_result = 0;
    int g_val, temp = 0;

    int i=0;

    TRISA = 0xFF;
    TRISB = 0x00;
    TRISC = 0x00;
    TRISD = 0x00;
    //Turn All Displays Off Initially
    PORTB = blank; //PORTB -> Display 1
    PORTC = blank; //PORTC -> Display 2
    PORTD = blank; //PORTD -> Display 3

    //Initialize A/D Converter
    ADC_Init();
    while(1){

    update_char_display(char_disp);
    Delay_ms(250);


    Delay_ms(5);

    while( 1 );
    current_result = ADC_read(1);
    temp = current_result - past_result;

    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=g_val%10;
    g_val = g_val/10;
    i--;
    }

    }



    }[/CODE]
     
    Last edited: May 16, 2013
Loading...