Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
It is quite a sophisticated device but gives very good and stable output.
This code shows how to set up the accelerometer and then uses the chip's Data-Ready interrupt to trigger the conversion code.
The code outputs the raw acceleration data (in G) in the x, y and z dimensions and uses the chips orientation capability to output it orientation in text. Finally it uses some math to output the pitch and roll angles allowing for use as a digital level etc.
twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1629
Posted: 10:11am 15 Nov 2015
Copy link to clipboard
Print this post
Hi Peter,
it is always a pleasure to read your code - clean and
straight -, thanks!
I wanted already since long time to play with my acceleration module.
Maybe your code (or parts of it) will fit. Even more interesting seems a
combinated compass/accelerometer module (like GY-511).
Anyhow, thanks for contributing!
Do you like to share your C-code source also, please?
Regards
MichaelEdited by twofingers 2015-11-16causality ≠ correlation ≠ coincidence
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 10406
Posted: 11:57am 15 Nov 2015
Copy link to clipboard
Print this post
unsigned long long intconv(unsigned char innumber[],unsigned int *signednum){//Cfunction
int k,j;
union utype{
unsigned long long b;
unsigned char a[8];
}u;
u.b=0;
j=innumber[0];//get the number of bytes
for(k=1;k<=innumber[0];k++)u.a[j-k] =innumber[k];
if (*signednum){k=u.a[j-1] & 0x80;//get the top bit
if(k){
for( ;j<=7;j++)u.a[j]=0xFF;//sign extend
}
}
return u.b;
}
WhiteWizzard Guru Joined: 05/04/2013 Location: United KingdomPosts: 2948
Posted: 08:56pm 15 Nov 2015
Copy link to clipboard
Print this post
Your timing is amazing. Today I am writing code for a MMA8452 so I immediately wonder if these two chips are 'compatible' in terms of code!
All I have done so far is connected it up and got a 'I'm here' response. Your code has hopefully saved me some time once again
I haven't even Googled yet - but do you know if the MMA8451 and MMA8452 are like-for-like in terms of code?
WW
WhiteWizzard Guru Joined: 05/04/2013 Location: United KingdomPosts: 2948
Posted: 10:37pm 15 Nov 2015
Copy link to clipboard
Print this post
Just tried your code on the MMa8452.
I had to change the value of the response of WHO_AM_I from &h1A to &h2A, but now getting some data.
Need to convert output string (due to orientation of chip in enclosure), but otherwise looks good!
Thanks for posting this code - saved a few hours by the look of things!
WW
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 10406
Posted: 11:18pm 15 Nov 2015
Copy link to clipboard
Print this post
Glad it works.
Do you get an acceleration of approximately 1 in the z direction when the chip is flat?
The 8452 is 12-bit, the 8451 is 14-bit. If the z-value is either 4 or 0.25 then change the divide ratio as applicable in these statements:
xx%= intconv(mid$(i2cin$,1,2),1) \ 4 'convert signed two byte number
yy%= intconv(mid$(i2cin$,3,2),1) \ 4
zz%= intconv(mid$(i2cin$,5,2),1) \ 4
WhiteWizzard Guru Joined: 05/04/2013 Location: United KingdomPosts: 2948
Posted: 11:45pm 15 Nov 2015
Copy link to clipboard
Print this post
Yes, I get near perfect values for X, Y, and Z (i.e 1 or -1) depending on orientation with no need to modify the division. Will examine your Pitch & Roll next.
The one thing I am wanting to 'configure' is INT2 as a movement trigger. Am working my way through the data sheet (for both the 100th time) to see exactly how to set up all the necessary thresholds and configurations.
Your code has giving me a great head-start; thanks once again for posting this in such a timely manner
WW
twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1629