FRDM-K64 I2C-Accelerometer basic example using mbed

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

FRDM-K64 I2C-Accelerometer basic example using mbed

FRDM-K64 I2C-Accelerometer basic example using mbed

Here you will find the code and project files corresponding to the I2C-Accelerometer project. The accelerometer/magnetometer is connected to the I2C port, although bot the accelerometer and magnetometer are contained within a single package, they must be initialized individually. In this example the measurements from both devices (X,Y and Z axis) is performed and displayed at the serial terminal.

In order to compile the project, the following library must be imported:

  • FXOS8700Q.h

Code:

#include "mbed.h"

#include "FXOS8700Q.h"

//I2C lines for FXOS8700Q accelerometer/magnetometer

FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1);

FXOS8700Q_mag mag( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1);

//Temrinal enable

Serial pc(USBTX, USBRX);

MotionSensorDataUnits mag_data;

MotionSensorDataUnits acc_data;

int main()

{

    float faX, faY, faZ;

    float fmX, fmY, fmZ;

    acc.enable();

    printf("\r\n\nFXOS8700Q Who Am I= %X\r\n", acc.whoAmI());

    while (true)

    {

        acc.getAxis(acc_data);

        mag.getAxis(mag_data);

        printf("FXOS8700Q ACC: X=%1.4f Y=%1.4f Z=%1.4f  ", acc_data.x, acc_data.y, acc_data.z);

        printf("    MAG: X=%4.1f Y=%4.1f Z=%4.1f\r\n", mag_data.x, mag_data.y, mag_data.z);

        acc.getX(&faX);

        acc.getY(&faY);

        acc.getZ(&faZ);

        mag.getX(&fmX);

        mag.getY(&fmY);

        mag.getZ(&fmZ);

        printf("FXOS8700Q ACC: X=%1.4f Y=%1.4f Z=%1.4f  ", faX, faY, faZ);

        printf("    MAG: X=%4.1f Y=%4.1f Z=%4.1f\r\n", fmX, fmY, fmZ);

       

        wait(1.0);

    }

}

Attachments
Comments

Hi Pedro,

I'm trying to edit this code so it outputs data every 0.1 seconds instead of 1 second. I changed the wait(1.0); to wait(0.1);. But I'm not sure how to build a binary file from it. Do you think you could help me out?

Thanks!!

%3CLINGO-SUB%20id%3D%22lingo-sub-1127381%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EFRDM-K64%20I2C-Accelerometer%20basic%20example%20using%20mbed%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1127381%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHere%20you%20will%20find%20the%20code%20and%20project%20files%20corresponding%20to%20the%20I2C-Accelerometer%20project.%20The%20accelerometer%2Fmagnetometer%20is%20connected%20to%20the%20I2C%20port%2C%20although%20bot%20the%20accelerometer%20and%20magnetometer%20are%20contained%20within%20a%20single%20package%2C%20they%20must%20be%20initialized%20individually.%20In%20this%20example%20the%20measurements%20from%20both%20devices%20(X%2CY%20and%20Z%20axis)%20is%20performed%20and%20displayed%20at%20the%20serial%20terminal.%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%20In%20order%20to%20compile%20the%20project%2C%20the%20following%20library%20must%20be%20imported%3A%20%3C%2FP%3E%3CUL%3E%3CLI%3EFXOS8700Q.h%3C%2FLI%3E%3C%2FUL%3E%3CP%3E%3C%2FP%3E%3CP%3ECode%3A%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%23include%20%22mbed.h%22%3C%2FP%3E%3CP%3E%23include%20%22FXOS8700Q.h%22%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%2F%2FI2C%20lines%20for%20FXOS8700Q%20accelerometer%2Fmagnetometer%3C%2FP%3E%3CP%3EFXOS8700Q_acc%20acc(%20PTE25%2C%20PTE24%2C%20FXOS8700CQ_SLAVE_ADDR1)%3B%3C%2FP%3E%3CP%3EFXOS8700Q_mag%20mag(%20PTE25%2C%20PTE24%2C%20FXOS8700CQ_SLAVE_ADDR1)%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%2F%2FTemrinal%20enable%20%3C%2FP%3E%3CP%3ESerial%20pc(USBTX%2C%20USBRX)%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3EMotionSensorDataUnits%20mag_data%3B%3C%2FP%3E%3CP%3EMotionSensorDataUnits%20acc_data%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3Eint%20main()%20%3C%2FP%3E%3CP%3E%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20float%20faX%2C%20faY%2C%20faZ%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20float%20fmX%2C%20fmY%2C%20fmZ%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20acc.enable()%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20printf(%22%5Cr%5Cn%5CnFXOS8700Q%20Who%20Am%20I%3D%20%25X%5Cr%5Cn%22%2C%20acc.whoAmI())%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20while%20(true)%20%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20acc.getAxis(acc_data)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20mag.getAxis(mag_data)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20printf(%22FXOS8700Q%20ACC%3A%20X%3D%251.4f%20Y%3D%251.4f%20Z%3D%251.4f%26nbsp%3B%20%22%2C%20acc_data.x%2C%20acc_data.y%2C%20acc_data.z)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20printf(%22%26nbsp%3B%26nbsp%3B%26nbsp%3B%20MAG%3A%20X%3D%254.1f%20Y%3D%254.1f%20Z%3D%254.1f%5Cr%5Cn%22%2C%20mag_data.x%2C%20mag_data.y%2C%20mag_data.z)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20acc.getX(%26amp%3BfaX)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20acc.getY(%26amp%3BfaY)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20acc.getZ(%26amp%3BfaZ)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20mag.getX(%26amp%3BfmX)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20mag.getY(%26amp%3BfmY)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20mag.getZ(%26amp%3BfmZ)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20printf(%22FXOS8700Q%20ACC%3A%20X%3D%251.4f%20Y%3D%251.4f%20Z%3D%251.4f%26nbsp%3B%20%22%2C%20faX%2C%20faY%2C%20faZ)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20printf(%22%26nbsp%3B%26nbsp%3B%26nbsp%3B%20MAG%3A%20X%3D%254.1f%20Y%3D%254.1f%20Z%3D%254.1f%5Cr%5Cn%22%2C%20fmX%2C%20fmY%2C%20fmZ)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20wait(1.0)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1127381%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CLINGO-LABEL%3EFreedom%20Development%20Platform%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EKinetis%20K%20Series%20MCUs%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1127382%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20FRDM-K64%20I2C-Accelerometer%20basic%20example%20using%20mbed%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1127382%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%20Pedro%2C%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3EI'm%20trying%20to%20edit%20this%20code%20so%20it%20outputs%20data%20every%200.1%20seconds%20instead%20of%201%20second.%20I%20changed%20the%20%3CSPAN%20style%3D%22color%3A%20%2351626f%3B%20font-family%3A%20arial%2C%20helvetica%2C%20'helvetica%20neue'%2C%20verdana%2C%20sans-serif%3B%22%3Ewait(1.0)%3B%20to%20%3CSPAN%20style%3D%22color%3A%20%2351626f%3B%20font-family%3A%20arial%2C%20helvetica%2C%20'helvetica%20neue'%2C%20verdana%2C%20sans-serif%3B%22%3Ewait(0.1)%3B.%20But%20I'm%20not%20sure%20how%20to%20build%20a%20binary%20file%20from%20it.%20Do%20you%20think%20you%20could%20help%20me%20out%3F%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%3CSPAN%20style%3D%22color%3A%20%2351626f%3B%20font-family%3A%20arial%2C%20helvetica%2C%20'helvetica%20neue'%2C%20verdana%2C%20sans-serif%3B%22%3EThanks!!%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
No ratings
Version history
Last update:
‎06-27-2014 11:22 AM
Updated by: