An example demonstrating the usage of the default FXOS8700CQ firmware configuration by Thomas Murphy. Uses interrupts to control flow and to cue access to the peripheral.

Dependencies:   FXOS8700CQ-AccOnly50hz mbed

Fork of fxos8700cq_example by Thomas Murphy

main.cpp

Committer:
julianhigginson
Date:
2016-02-05
Revision:
1:d006ce66f663
Parent:
0:6c6060a8a2f6

File content as of revision 1:d006ce66f663:

#include "mbed.h"
#include "FXOS8700CQ.h"

#define DATA_RECORD_TIME_MS 1000

Serial pc(USBTX, USBRX); // Primary output to demonstrate library

// Pin names for FRDM-K64F
FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)

DigitalOut green(LED_GREEN); // waiting light
DigitalOut blue(LED_BLUE); // collection-in-progress light
DigitalOut red(LED_RED); // completed/error ligt

Timer t; // Microsecond timer, 32 bit int, maximum count of ~30 minutes
//InterruptIn fxos_int1(PTC6); // unused, common with SW2 on FRDM-K64F
InterruptIn fxos_int2(PTC13); // should just be the Data-Ready interrupt
//InterruptIn start_sw(PTA4); // switch SW3

// Interrupt status flags and data
bool fxos_int1_triggered = false;
bool fxos_int2_triggered = false;
uint32_t us_ellapsed = 0;
bool start_sw_triggered = false;

// Storage for the data from the sensor
SRAWDATA accel_data;
SRAWDATA magn_data;

void print_reading()
{
//    pc.printf("%d A X:%5d,Y:%5d,Z:%5d   M X:%5d,Y:%5d,Z:%5d\r\n",
//              us_ellapsed,
//             accel_data.x, accel_data.y, accel_data.z,
//              magn_data.x, magn_data.y, magn_data.z);
    pc.printf("%d A X:%5d,Y:%5d,Z:%5d \r\n",
    //pc.printf("%d A X:%5x,Y:%5x,Z:%5x \r\n",
              us_ellapsed/1000,
              accel_data.x, accel_data.y, accel_data.z);
}


void trigger_fxos_int2(void)
{
    us_ellapsed = t.read_us();
    fxos.get_data(&accel_data);
    print_reading(); 
}

int main(void)
{
    // Setup
    t.reset();
    pc.baud(115200); // Print quickly! 200Hz x line of output data!

    // Lights off (FRDM-K64F has active-low LEDs)
    green.write(1);
    red.write(1);
    blue.write(1);

    // Diagnostic printing of the FXOS WHOAMI register value
    pc.printf("\r\n\nFXOS8700Q Who Am I= %X\r\n", fxos.get_whoami());

    // Iterrupt for active-low interrupt line from FXOS
    // Configured with only one interrupt on INT2 signaling Data-Ready
    fxos_int2.fall(&trigger_fxos_int2);

    pc.printf("Started data collection. Accelerometer at max %dg.\r\n", fxos.get_accel_scale());

    pc.printf("FXOS8700CQ_CTRL_REG1    0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_CTRL_REG1));
    pc.printf("FXOS8700CQ_CTRL_REG2    0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_CTRL_REG2));
    pc.printf("FXOS8700CQ_CTRL_REG3    0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_CTRL_REG3));
    pc.printf("FXOS8700CQ_CTRL_REG4    0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_CTRL_REG4));
    pc.printf("FXOS8700CQ_CTRL_REG5    0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_CTRL_REG5));

    pc.printf("FXOS8700CQ_M_CTRL_REG1  0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_M_CTRL_REG1));
    pc.printf("FXOS8700CQ_M_CTRL_REG2  0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_M_CTRL_REG2));
    pc.printf("FXOS8700CQ_M_CTRL_REG3  0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_M_CTRL_REG3));

    pc.printf("FXOS8700CQ_XYZ_DATA_CFG 0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_XYZ_DATA_CFG));



    fxos.get_data(&accel_data); // clear interrupt from device

    t.start(); // start timer and enter collection loop

    fxos.enable();

    while(1) {
        wait_us(500); // 1/10th the period of the 200Hz sample rate
    }

}