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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "FXOS8700CQ.h"
00003 
00004 #define DATA_RECORD_TIME_MS 1000
00005 
00006 Serial pc(USBTX, USBRX); // Primary output to demonstrate library
00007 
00008 // Pin names for FRDM-K64F
00009 FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)
00010 
00011 DigitalOut green(LED_GREEN); // waiting light
00012 DigitalOut blue(LED_BLUE); // collection-in-progress light
00013 DigitalOut red(LED_RED); // completed/error ligt
00014 
00015 Timer t; // Microsecond timer, 32 bit int, maximum count of ~30 minutes
00016 //InterruptIn fxos_int1(PTC6); // unused, common with SW2 on FRDM-K64F
00017 InterruptIn fxos_int2(PTC13); // should just be the Data-Ready interrupt
00018 //InterruptIn start_sw(PTA4); // switch SW3
00019 
00020 // Interrupt status flags and data
00021 bool fxos_int1_triggered = false;
00022 bool fxos_int2_triggered = false;
00023 uint32_t us_ellapsed = 0;
00024 bool start_sw_triggered = false;
00025 
00026 // Storage for the data from the sensor
00027 SRAWDATA accel_data;
00028 SRAWDATA magn_data;
00029 
00030 void print_reading()
00031 {
00032 //    pc.printf("%d A X:%5d,Y:%5d,Z:%5d   M X:%5d,Y:%5d,Z:%5d\r\n",
00033 //              us_ellapsed,
00034 //             accel_data.x, accel_data.y, accel_data.z,
00035 //              magn_data.x, magn_data.y, magn_data.z);
00036     pc.printf("%d A X:%5d,Y:%5d,Z:%5d \r\n",
00037     //pc.printf("%d A X:%5x,Y:%5x,Z:%5x \r\n",
00038               us_ellapsed/1000,
00039               accel_data.x, accel_data.y, accel_data.z);
00040 }
00041 
00042 
00043 void trigger_fxos_int2(void)
00044 {
00045     us_ellapsed = t.read_us();
00046     fxos.get_data(&accel_data);
00047     print_reading(); 
00048 }
00049 
00050 int main(void)
00051 {
00052     // Setup
00053     t.reset();
00054     pc.baud(115200); // Print quickly! 200Hz x line of output data!
00055 
00056     // Lights off (FRDM-K64F has active-low LEDs)
00057     green.write(1);
00058     red.write(1);
00059     blue.write(1);
00060 
00061     // Diagnostic printing of the FXOS WHOAMI register value
00062     pc.printf("\r\n\nFXOS8700Q Who Am I= %X\r\n", fxos.get_whoami());
00063 
00064     // Iterrupt for active-low interrupt line from FXOS
00065     // Configured with only one interrupt on INT2 signaling Data-Ready
00066     fxos_int2.fall(&trigger_fxos_int2);
00067 
00068     pc.printf("Started data collection. Accelerometer at max %dg.\r\n", fxos.get_accel_scale());
00069 
00070     pc.printf("FXOS8700CQ_CTRL_REG1    0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_CTRL_REG1));
00071     pc.printf("FXOS8700CQ_CTRL_REG2    0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_CTRL_REG2));
00072     pc.printf("FXOS8700CQ_CTRL_REG3    0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_CTRL_REG3));
00073     pc.printf("FXOS8700CQ_CTRL_REG4    0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_CTRL_REG4));
00074     pc.printf("FXOS8700CQ_CTRL_REG5    0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_CTRL_REG5));
00075 
00076     pc.printf("FXOS8700CQ_M_CTRL_REG1  0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_M_CTRL_REG1));
00077     pc.printf("FXOS8700CQ_M_CTRL_REG2  0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_M_CTRL_REG2));
00078     pc.printf("FXOS8700CQ_M_CTRL_REG3  0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_M_CTRL_REG3));
00079 
00080     pc.printf("FXOS8700CQ_XYZ_DATA_CFG 0x%02x\r\n", fxos.read_single_reg(FXOS8700CQ_XYZ_DATA_CFG));
00081 
00082 
00083 
00084     fxos.get_data(&accel_data); // clear interrupt from device
00085 
00086     t.start(); // start timer and enter collection loop
00087 
00088     fxos.enable();
00089 
00090     while(1) {
00091         wait_us(500); // 1/10th the period of the 200Hz sample rate
00092     }
00093 
00094 }