After installing the maa7455 library and also getting familiar with the functions available, let us move on to writing our first demo that will show raw 8 bit measurement values read from the X,Y and Z axis.
In this demo the sensitivity is set to 2G. So if an acceleration of 2G is sensed along any axis, the readings for that axis would be close to 128. But since acceleration due to gravity is only 1G the maximum value shown along any axis would not exceed ± 64.
Bring each axis perpendicular to the earths surface such that it is parallel to the earths gravitational field. The value should come close to 64 along that axis.
The Source Code
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "lib/lcd/lcd_hd44780_avr.h"
#include "lib/mma7455/acc_mma7455_avr.h"
int main(void)
{
//Initialize LCD
LCDInit(LS_NONE);
//Show intro screen
LCDWriteFStringXY(0,0,PSTR("MMA7455 Demo !"));
LCDWriteFStringXY(0,1,PSTR("By Avinash Gupta"));
_delay_ms(1500);
LCDClear();
LCDWriteFStringXY(0,0,PSTR("DEMO #1"));
LCDWriteFStringXY(0,1,PSTR("Raw 8 bit data"));
_delay_ms(1500);
LCDClear();
//Initialize the MMA7455 accelerometer
uint8_t res; //result of operation
res=MMA7455Init(MMA7455_RANGE_2G);
if(res==0)
{
//Error!
LCDWriteFStringXY(0,0,PSTR("MMA7455 Chip "));
LCDWriteFStringXY(0,1,PSTR("NOT Found ! "));
//Halt!
while(1){}
}
while(1)
{
//wait until data is ready
while(MMA7455IsDataReady()==0){}
//Now take measurements
int8_t x,y,z;
x=MMA7455GetX();
y=MMA7455GetY();
z=MMA7455GetZ();
LCDClear();
LCDWriteFStringXY(0,0,PSTR(" X: Y: Z:"));
//Print the measured values
LCDWriteIntXY(0,1,x,3);
LCDWriteIntXY(5,1,y,3);
LCDWriteIntXY(10,1,z,3);
_delay_ms(250);
}
}
NOTE: To compile this program you need to install the MMA7455 library as described here. Also I2C library and LCD library must also be installed in the same manner.
NOTE: The hardware must be setup as described here.
When the MMA7455Demo1.c is loaded in the editor, copy and paste the above source code and save the file.
Now build the project using any of the following method.
- Using the menu bar. Goto Build > Build Solution.
- Hit F7 key on the keyboard.
If you have done everything as described, the code should compile without any problems. And finally you would get a hex file which you can burn to your xBoard MINI as described here.
NOTE: After compilation of the project, HEX file is created inside the folder Debug inside your project folder. The name of the HEX file is same as the project's name. In our case it is MMA7455Demo1.hex
We cordially thanks the following peoples who shared this page on various social networks and insprided us to develop more quality contents!
Avinash,