FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Thu May 09 14:36:51 2019 +0000
Revision:
140:d8634e76ecce
Parent:
15:b867a6620f96
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jamesheavey 15:b867a6620f96 1 /** @file FXOS8700CQ.h
jamesheavey 15:b867a6620f96 2
jamesheavey 15:b867a6620f96 3 @ brief FXOS8700CQ Library
jamesheavey 15:b867a6620f96 4
jamesheavey 15:b867a6620f96 5 @author Dr Craig A. Evans
jamesheavey 15:b867a6620f96 6 @brief (c) University of Leeds, Jan 2017
jamesheavey 15:b867a6620f96 7
jamesheavey 15:b867a6620f96 8 @code
jamesheavey 15:b867a6620f96 9
jamesheavey 15:b867a6620f96 10 #include "mbed.h"
jamesheavey 15:b867a6620f96 11 #include "FXOS8700CQ.h"
jamesheavey 15:b867a6620f96 12
jamesheavey 15:b867a6620f96 13 // create object and specifiy pins
jamesheavey 15:b867a6620f96 14 FXOS8700CQ device(I2C_SDA,I2C_SCL);
jamesheavey 15:b867a6620f96 15
jamesheavey 15:b867a6620f96 16 int main()
jamesheavey 15:b867a6620f96 17 {
jamesheavey 15:b867a6620f96 18 // call initialisation method
jamesheavey 15:b867a6620f96 19 device.init();
jamesheavey 15:b867a6620f96 20
jamesheavey 15:b867a6620f96 21 while (1) {
jamesheavey 15:b867a6620f96 22
jamesheavey 15:b867a6620f96 23 // poll the sensor and get the values, storing in a struct
jamesheavey 15:b867a6620f96 24 Data values = device.get_values();
jamesheavey 15:b867a6620f96 25
jamesheavey 15:b867a6620f96 26 // print each struct member over serial
jamesheavey 15:b867a6620f96 27 printf("ax = %f ay = %f az = %f | mx = %f my = %f mz = %f\n"
jamesheavey 15:b867a6620f96 28 ,values.ax, values.ay, values.az
jamesheavey 15:b867a6620f96 29 ,values.mx, values.my, values.mz);
jamesheavey 15:b867a6620f96 30
jamesheavey 15:b867a6620f96 31 wait(0.5);
jamesheavey 15:b867a6620f96 32 }
jamesheavey 15:b867a6620f96 33 }
jamesheavey 15:b867a6620f96 34
jamesheavey 15:b867a6620f96 35 @endcode
jamesheavey 15:b867a6620f96 36
jamesheavey 15:b867a6620f96 37 */
jamesheavey 15:b867a6620f96 38
jamesheavey 15:b867a6620f96 39 #ifndef FXOS8700CQ_H
jamesheavey 15:b867a6620f96 40 #define FXOS8700CQ_H
jamesheavey 15:b867a6620f96 41
jamesheavey 15:b867a6620f96 42 #include "mbed.h"
jamesheavey 15:b867a6620f96 43
jamesheavey 15:b867a6620f96 44 // mbed API uses 8-bit addresses so need to left-shift 7-bit addresses by 1
jamesheavey 15:b867a6620f96 45 #define FXOS8700CQ_ADDR (0x1D << 1) // for K64F board
jamesheavey 15:b867a6620f96 46 // values from 13.2 datasheet
jamesheavey 15:b867a6620f96 47 #define FXOS8700CQ_STATUS 0x00
jamesheavey 15:b867a6620f96 48 #define FXOS8700CQ_WHO_AM_I 0x0D
jamesheavey 15:b867a6620f96 49 #define FXOS8700CQ_XYZ_DATA_CFG 0x0E
jamesheavey 15:b867a6620f96 50 #define FXOS8700CQ_CTRL_REG1 0x2A
jamesheavey 15:b867a6620f96 51 #define FXOS8700CQ_M_CTRL_REG1 0x5B
jamesheavey 15:b867a6620f96 52 #define FXOS8700CQ_M_CTRL_REG2 0x5C
jamesheavey 15:b867a6620f96 53 #define FXOS8700CQ_WHO_AM_I_VAL 0xC7
jamesheavey 15:b867a6620f96 54 #define FXOS8700CQ_READ_LEN 13
jamesheavey 15:b867a6620f96 55
jamesheavey 15:b867a6620f96 56 #define PI 3.14159265359f
jamesheavey 15:b867a6620f96 57 #define RAD2DEG 57.2957795131f
jamesheavey 15:b867a6620f96 58
jamesheavey 15:b867a6620f96 59 struct Data {
jamesheavey 15:b867a6620f96 60 float ax;
jamesheavey 15:b867a6620f96 61 float ay;
jamesheavey 15:b867a6620f96 62 float az;
jamesheavey 15:b867a6620f96 63 float mx;
jamesheavey 15:b867a6620f96 64 float my;
jamesheavey 15:b867a6620f96 65 float mz;
jamesheavey 15:b867a6620f96 66 };
jamesheavey 15:b867a6620f96 67
jamesheavey 15:b867a6620f96 68 class FXOS8700CQ
jamesheavey 15:b867a6620f96 69 {
jamesheavey 15:b867a6620f96 70
jamesheavey 15:b867a6620f96 71 public:
jamesheavey 15:b867a6620f96 72 FXOS8700CQ(PinName sda, PinName scl);
jamesheavey 15:b867a6620f96 73 ~FXOS8700CQ();
jamesheavey 15:b867a6620f96 74 void init();
jamesheavey 15:b867a6620f96 75 Data get_values();
jamesheavey 15:b867a6620f96 76 float get_pitch_angle();
jamesheavey 15:b867a6620f96 77 float get_roll_angle();
jamesheavey 15:b867a6620f96 78
jamesheavey 15:b867a6620f96 79 private:
jamesheavey 15:b867a6620f96 80 I2C* i2c;
jamesheavey 15:b867a6620f96 81
jamesheavey 15:b867a6620f96 82 void send_byte_to_reg(char byte,char reg);
jamesheavey 15:b867a6620f96 83 char read_byte_from_reg(char reg);
jamesheavey 15:b867a6620f96 84 void read_bytes_from_reg(char reg,int number_of_bytes,char bytes[]);
jamesheavey 15:b867a6620f96 85 };
jamesheavey 15:b867a6620f96 86
jamesheavey 15:b867a6620f96 87 #endif