<ask> H48C 3-axis accelerometer
songolikur
Posts: 1
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
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.
#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?