Output onboard accelerometer data of FRDM-KL25Z board on serial.

Dependencies:   MMA8451Q mbed

Committer:
DWeng
Date:
Tue Jul 18 18:35:27 2017 +0000
Revision:
0:effa4c81f363
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DWeng 0:effa4c81f363 1 /**
DWeng 0:effa4c81f363 2 * +----------------------+
DWeng 0:effa4c81f363 3 * | Accelerometer Output |
DWeng 0:effa4c81f363 4 * +----------------------+
DWeng 0:effa4c81f363 5 *
DWeng 0:effa4c81f363 6 * Sample accelerometer at x Hz and output data onto serial port. Used for
DWeng 0:effa4c81f363 7 * UC Merced data acquisition and filtering project.
DWeng 0:effa4c81f363 8 *
DWeng 0:effa4c81f363 9 * Leverages Antonio Quevedo's MMA8451Q library for interfacing with the
DWeng 0:effa4c81f363 10 * accelerometer over I2C, with slight modification to configuration.
DWeng 0:effa4c81f363 11 *
DWeng 0:effa4c81f363 12 * Acceleration should be sampled no faster than 800Hz, but we will output at
DWeng 0:effa4c81f363 13 * 100Hz. -- ADD ODR MODIFIER
DWeng 0:effa4c81f363 14 *
DWeng 0:effa4c81f363 15 * Output Data Packet:
DWeng 0:effa4c81f363 16 * [ start_byte, acc_xH, acc_xL, acc_yH, acc_yL, acc_zH, acc_zL, checksum1, checksum2, end_byte ]
DWeng 0:effa4c81f363 17 *
DWeng 0:effa4c81f363 18 * Output all axes, but really only interested in y and z axes.
DWeng 0:effa4c81f363 19 *
DWeng 0:effa4c81f363 20 * Darrel Weng
DWeng 0:effa4c81f363 21 *
DWeng 0:effa4c81f363 22 */
DWeng 0:effa4c81f363 23
DWeng 0:effa4c81f363 24 #include "mbed.h"
DWeng 0:effa4c81f363 25 #include "MMA8451Q.h"
DWeng 0:effa4c81f363 26
DWeng 0:effa4c81f363 27 #include <string.h>
DWeng 0:effa4c81f363 28
DWeng 0:effa4c81f363 29 int period = 8; // data reads before output; set at 50Sps
DWeng 0:effa4c81f363 30 int acc_flag = 0; // flag to super loop to read data
DWeng 0:effa4c81f363 31 int flag_count = 0; // counter for how many times data has been read
DWeng 0:effa4c81f363 32
DWeng 0:effa4c81f363 33 void
DWeng 0:effa4c81f363 34 mma8451q_int()
DWeng 0:effa4c81f363 35 {
DWeng 0:effa4c81f363 36 acc_flag = 1;
DWeng 0:effa4c81f363 37 }
DWeng 0:effa4c81f363 38
DWeng 0:effa4c81f363 39 int main()
DWeng 0:effa4c81f363 40 {
DWeng 0:effa4c81f363 41 /* objects */
DWeng 0:effa4c81f363 42 DigitalOut laser(PTE2);
DWeng 0:effa4c81f363 43 Serial pc(USBTX, USBRX);
DWeng 0:effa4c81f363 44 //Timer timer;
DWeng 0:effa4c81f363 45 MMA8451Q acc(PTE25, PTE24);
DWeng 0:effa4c81f363 46 InterruptIn accINT1(PTA14);
DWeng 0:effa4c81f363 47
DWeng 0:effa4c81f363 48 /* init */
DWeng 0:effa4c81f363 49 laser = 0; // turn laser on; pin is active sinking
DWeng 0:effa4c81f363 50 pc.baud(115200); // high baudrate for lower latency comm
DWeng 0:effa4c81f363 51 accINT1.fall(&mma8451q_int);
DWeng 0:effa4c81f363 52
DWeng 0:effa4c81f363 53 /* local variables */
DWeng 0:effa4c81f363 54 int16_t acc_data[4]; // 4th entry contains checksum
DWeng 0:effa4c81f363 55 uint8_t output[10];
DWeng 0:effa4c81f363 56
DWeng 0:effa4c81f363 57 /* Start and Stop delimiting Bytes */
DWeng 0:effa4c81f363 58 output[0] = 0xBE;
DWeng 0:effa4c81f363 59 output[9] = 0xEF;
DWeng 0:effa4c81f363 60
DWeng 0:effa4c81f363 61 while(1)
DWeng 0:effa4c81f363 62 {
DWeng 0:effa4c81f363 63 // if data ready, read data
DWeng 0:effa4c81f363 64 if (acc_flag)
DWeng 0:effa4c81f363 65 {
DWeng 0:effa4c81f363 66 acc_flag = 0;
DWeng 0:effa4c81f363 67 acc.getAccAllAxis(acc_data);
DWeng 0:effa4c81f363 68 for (int i=0;i<3;i++)
DWeng 0:effa4c81f363 69 acc_data[i] /= 4;
DWeng 0:effa4c81f363 70 flag_count++;
DWeng 0:effa4c81f363 71 }
DWeng 0:effa4c81f363 72 // if time to output
DWeng 0:effa4c81f363 73 if (flag_count >= period)
DWeng 0:effa4c81f363 74 {
DWeng 0:effa4c81f363 75 flag_count = 0;
DWeng 0:effa4c81f363 76 //pc.printf("%d %d %d \r\n", acc_data[0], acc_data[1], acc_data[2]);
DWeng 0:effa4c81f363 77 // Build packet
DWeng 0:effa4c81f363 78 acc_data[3] = acc_data[0] + acc_data[1] - acc_data[2];
DWeng 0:effa4c81f363 79 memcpy(output+1, acc_data, 8);
DWeng 0:effa4c81f363 80 for (int i=0; i<10; i++)
DWeng 0:effa4c81f363 81 pc.putc(output[i]);
DWeng 0:effa4c81f363 82 }
DWeng 0:effa4c81f363 83 }
DWeng 0:effa4c81f363 84 }