Shop OBEX P1 Docs P2 Docs Learn Events
<ask> H48C 3-axis accelerometer — Parallax Forums

<ask> H48C 3-axis accelerometer

songolikursongolikur Posts: 1
edited 2011-03-22 05:25 in Accessories
this sensor is very difficult for me...anyone can learn me how to programming this sensor in the C language??? and which formula that use to get the three angles from the three output axis??? HELP ME PLEASE......

Comments

  • FranklinFranklin Posts: 4,747
    edited 2009-08-08 03:06
    Here is the code Parallax uses (in basic)
    Get_H48C:
    LOW CS
    SHIFTOUT Dio, Clk, MSBFIRST, [noparse][[/noparse]%11\2, VRef\3] ' select vref register
    SHIFTIN Dio, Clk, MSBPOST, [noparse][[/noparse]rvCount\13] ' read ref voltage counts
    HIGH CS
    PAUSE 1
    LOW CS
    SHIFTOUT Dio, Clk, MSBFIRST, [noparse][[/noparse]%11\2, axis\3] ' select axis
    SHIFTIN Dio, Clk, MSBPOST, [noparse][[/noparse]axCount\13] ' read axis voltage counts
    HIGH CS
    
    RETURN
    
    
    

    All you need to do is convert to c. Depending on your version of c there should be a library routine or function for the equivalent of shiftout and shiftin.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • 01442240144224 Posts: 2
    edited 2010-11-13 10:21
    Franklin wrote: »
    Here is the code Parallax uses (in basic)
    Get_H48C:
    LOW CS
    SHIFTOUT Dio, Clk, MSBFIRST, [noparse][[/noparse]%11\2, VRef\3] ' select vref register
    SHIFTIN Dio, Clk, MSBPOST, [noparse][[/noparse]rvCount\13] ' read ref voltage counts
    HIGH CS
    PAUSE 1
    LOW CS
    SHIFTOUT Dio, Clk, MSBFIRST, [noparse][[/noparse]%11\2, axis\3] ' select axis
    SHIFTIN Dio, Clk, MSBPOST, [noparse][[/noparse]axCount\13] ' read axis voltage counts
    HIGH CS
    
    RETURN
    
    
    

    All you need to do is convert to c. Depending on your version of c there should be a library routine or function for the equivalent of shiftout and shiftin.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen

    I've been trying to convert this to c without anyluck. I'm using the pic16f887 to interface with the h48c.
  • FranklinFranklin Posts: 4,747
    edited 2010-11-13 18:02
    Show us what you've got so far.
  • mahbub90mahbub90 Posts: 1
    edited 2011-03-22 05:25
    I've got this far, but it doesnt work.. It keeps showing "2228" on the screen! I've connected an LCD screen to show the output.

    #include <avr/io.h>
    #include <util/delay.h>
    #include <avr/metio16.h>

    #define DIOOUT() DDRC |= 0x40 //PC6
    #define DIOIN() DDRC &= ~0x40
    #define CSHI() PORTC |= 0x10 //PC4
    #define CSLO() PORTC &= ~0x10
    #define CLKHI() PORTC |= 0x80 //PC7
    #define CLKLO() PORTC &= ~0x80
    #define DIOHI() PORTC |= 0x40 //PC6
    #define DIOLO() PORTC &= 0x40

    void clock(){
    CLKLO();
    _delay_us(1);
    CLKHI();
    _delay_us(1);
    }


    // A power function for x^n
    double power(double x, int n) {
    if(n == 0)
    return 1.0;
    else
    return x * power( x , n - 1 );
    }


    int Bin2Dec(int decimal)
    {
    int total = 0;
    int power = 1;

    while(decimal > 0)
    {
    total += decimal % 10 * power;
    decimal = decimal / 10;
    power = power * 2;
    }
    return total;
    }

    void shift_in(char axis[5]){
    DIOOUT(); // PC6 is set to output
    clock(); // Samplingtime
    clock(); // Nullbit
    for (int i=0; i<5; i++){
    clock();
    if (axis==1){
    DIOHI();
    }
    else {
    DIOLO();
    }

    }
    }

    int shift_out(void){
    DIOIN();
    int ad_bin[12];
    clock();
    clock();
    for (int i=0; i<12; i++){
    clock();
    ad_bin=((PINC>>6)&1);
    }

    return Bin2Dec(ad_bin[12]);
    }


    void init_screen(void){
    clear_disp();
    move_cursor(1,1);
    cursor_off();
    }


    int main(){
    init_disp();
    CSHI();

    while(1){
    int ad_value;

    CSLO(); // Activate the ADC

    // Read the axis. x=11000, y=11001, z=11010
    shift_in(0b11000);
    ad_value=(int)shift_out;
    // Print the result on the LCD
    init_screen();
    dprintf("%i", ad_value);

    CSHI();
    }

    }

    I suspect that my shiftin and shiftout are a bit messed up. Anyone has an answer to this?
Sign In or Register to comment.