Code to get all the data through serial port
Dependents: payload_sensor payload_sensor
FXAS21002.h@0:2741e9e34e8a, 2016-07-22 (annotated)
- Committer:
- Rhyme
- Date:
- Fri Jul 22 08:31:32 2016 +0000
- Revision:
- 0:2741e9e34e8a
First commit before publishing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:2741e9e34e8a | 1 | /** |
Rhyme | 0:2741e9e34e8a | 2 | * FXAS21002 |
Rhyme | 0:2741e9e34e8a | 3 | * 3-Axis Digital Angular Rate Gyroscope |
Rhyme | 0:2741e9e34e8a | 4 | */ |
Rhyme | 0:2741e9e34e8a | 5 | #ifndef FXAS21002_H |
Rhyme | 0:2741e9e34e8a | 6 | #define FXAS21002_H |
Rhyme | 0:2741e9e34e8a | 7 | |
Rhyme | 0:2741e9e34e8a | 8 | #include "mbed.h" |
Rhyme | 0:2741e9e34e8a | 9 | |
Rhyme | 0:2741e9e34e8a | 10 | /** |
Rhyme | 0:2741e9e34e8a | 11 | * |
Rhyme | 0:2741e9e34e8a | 12 | * |
Rhyme | 0:2741e9e34e8a | 13 | * @code |
Rhyme | 0:2741e9e34e8a | 14 | #include "mbed.h" |
Rhyme | 0:2741e9e34e8a | 15 | #include "FXAS21002.h" |
Rhyme | 0:2741e9e34e8a | 16 | #define FXAS21002_I2C_ADDRESS (0x20) |
Rhyme | 0:2741e9e34e8a | 17 | |
Rhyme | 0:2741e9e34e8a | 18 | #if defined (TARGET_KL25Z) |
Rhyme | 0:2741e9e34e8a | 19 | #define PIN_SCL PTE1 |
Rhyme | 0:2741e9e34e8a | 20 | #define PIN_SDA PTE0 |
Rhyme | 0:2741e9e34e8a | 21 | #elif defined (TARGET_KL46Z) |
Rhyme | 0:2741e9e34e8a | 22 | #define PIN_SCL PTE1 |
Rhyme | 0:2741e9e34e8a | 23 | #define PIN_SDA PTE0 |
Rhyme | 0:2741e9e34e8a | 24 | #elif defined (TARGET_K64F) |
Rhyme | 0:2741e9e34e8a | 25 | #define PIN_SCL PTE24 |
Rhyme | 0:2741e9e34e8a | 26 | #define PIN_SDA PTE25 |
Rhyme | 0:2741e9e34e8a | 27 | #elif defined (TARGET_K22F) |
Rhyme | 0:2741e9e34e8a | 28 | #define PIN_SCL PTE1 |
Rhyme | 0:2741e9e34e8a | 29 | #define PIN_SDA PTE0 |
Rhyme | 0:2741e9e34e8a | 30 | #elif defined (TARGET_KL05Z) |
Rhyme | 0:2741e9e34e8a | 31 | #define PIN_SCL PTB3 |
Rhyme | 0:2741e9e34e8a | 32 | #define PIN_SDA PTB4 |
Rhyme | 0:2741e9e34e8a | 33 | #elif defined (TARGET_NUCLEO_F411RE) |
Rhyme | 0:2741e9e34e8a | 34 | #define PIN_SCL PB_8 |
Rhyme | 0:2741e9e34e8a | 35 | #define PIN_SDA PB_9 |
Rhyme | 0:2741e9e34e8a | 36 | #else |
Rhyme | 0:2741e9e34e8a | 37 | #error TARGET NOT DEFINED |
Rhyme | 0:2741e9e34e8a | 38 | #endif |
Rhyme | 0:2741e9e34e8a | 39 | |
Rhyme | 0:2741e9e34e8a | 40 | int main() { |
Rhyme | 0:2741e9e34e8a | 41 | uint16_t result = 0 ; |
Rhyme | 0:2741e9e34e8a | 42 | int16_t temperature = 0 ; |
Rhyme | 0:2741e9e34e8a | 43 | FXAS21002 FXAS21002(PIN_SDA, PIN_SCL, FXAS21002_I2C_ADDRESS) ; |
Rhyme | 0:2741e9e34e8a | 44 | |
Rhyme | 0:2741e9e34e8a | 45 | while(1) { |
Rhyme | 0:2741e9e34e8a | 46 | result = FXAS21002.getValue(&temperature) ; |
Rhyme | 0:2741e9e34e8a | 47 | printf("Temp %d C\n", temperature) ; |
Rhyme | 0:2741e9e34e8a | 48 | wait(1) ; |
Rhyme | 0:2741e9e34e8a | 49 | } |
Rhyme | 0:2741e9e34e8a | 50 | } |
Rhyme | 0:2741e9e34e8a | 51 | * @endcode |
Rhyme | 0:2741e9e34e8a | 52 | */ |
Rhyme | 0:2741e9e34e8a | 53 | class FXAS21002 |
Rhyme | 0:2741e9e34e8a | 54 | { |
Rhyme | 0:2741e9e34e8a | 55 | public: |
Rhyme | 0:2741e9e34e8a | 56 | /** |
Rhyme | 0:2741e9e34e8a | 57 | * FXAS21002 constructor |
Rhyme | 0:2741e9e34e8a | 58 | * |
Rhyme | 0:2741e9e34e8a | 59 | * @param sda SDA pin |
Rhyme | 0:2741e9e34e8a | 60 | * @param sdl SCL pin |
Rhyme | 0:2741e9e34e8a | 61 | * @param addr addr of the I2C peripheral |
Rhyme | 0:2741e9e34e8a | 62 | */ |
Rhyme | 0:2741e9e34e8a | 63 | FXAS21002(PinName sda, PinName scl, int addr); |
Rhyme | 0:2741e9e34e8a | 64 | |
Rhyme | 0:2741e9e34e8a | 65 | /** |
Rhyme | 0:2741e9e34e8a | 66 | * FXAS21002 destructor |
Rhyme | 0:2741e9e34e8a | 67 | */ |
Rhyme | 0:2741e9e34e8a | 68 | ~FXAS21002(); |
Rhyme | 0:2741e9e34e8a | 69 | |
Rhyme | 0:2741e9e34e8a | 70 | /** |
Rhyme | 0:2741e9e34e8a | 71 | * status register |
Rhyme | 0:2741e9e34e8a | 72 | */ |
Rhyme | 0:2741e9e34e8a | 73 | uint8_t getStatus(void) ; |
Rhyme | 0:2741e9e34e8a | 74 | |
Rhyme | 0:2741e9e34e8a | 75 | /** |
Rhyme | 0:2741e9e34e8a | 76 | * getX returns the value of |
Rhyme | 0:2741e9e34e8a | 77 | * REG_OUT_X_MSB |
Rhyme | 0:2741e9e34e8a | 78 | * REG_OUT_X_LSB |
Rhyme | 0:2741e9e34e8a | 79 | * as a signed 16bit integer |
Rhyme | 0:2741e9e34e8a | 80 | */ |
Rhyme | 0:2741e9e34e8a | 81 | int16_t getX(void) ; |
Rhyme | 0:2741e9e34e8a | 82 | |
Rhyme | 0:2741e9e34e8a | 83 | /** |
Rhyme | 0:2741e9e34e8a | 84 | * getY returns the value of |
Rhyme | 0:2741e9e34e8a | 85 | * REG_OUT_Y_MSB |
Rhyme | 0:2741e9e34e8a | 86 | * REG_OUT_Y_LSB |
Rhyme | 0:2741e9e34e8a | 87 | * as a signed 16bit integer |
Rhyme | 0:2741e9e34e8a | 88 | */ |
Rhyme | 0:2741e9e34e8a | 89 | int16_t getY(void) ; |
Rhyme | 0:2741e9e34e8a | 90 | |
Rhyme | 0:2741e9e34e8a | 91 | /** |
Rhyme | 0:2741e9e34e8a | 92 | * getZ returns the value of |
Rhyme | 0:2741e9e34e8a | 93 | * REG_OUT_Z_MSB |
Rhyme | 0:2741e9e34e8a | 94 | * REG_OUT_Z_LSB |
Rhyme | 0:2741e9e34e8a | 95 | * as a signed 16bit integer |
Rhyme | 0:2741e9e34e8a | 96 | */ |
Rhyme | 0:2741e9e34e8a | 97 | int16_t getZ(void) ; |
Rhyme | 0:2741e9e34e8a | 98 | |
Rhyme | 0:2741e9e34e8a | 99 | /** |
Rhyme | 0:2741e9e34e8a | 100 | * activate/deactivate the sensor |
Rhyme | 0:2741e9e34e8a | 101 | * |
Rhyme | 0:2741e9e34e8a | 102 | * @param mode true: Active false: Standby |
Rhyme | 0:2741e9e34e8a | 103 | */ |
Rhyme | 0:2741e9e34e8a | 104 | void activate(bool mode) ; |
Rhyme | 0:2741e9e34e8a | 105 | |
Rhyme | 0:2741e9e34e8a | 106 | /** |
Rhyme | 0:2741e9e34e8a | 107 | * Self-Test enable |
Rhyme | 0:2741e9e34e8a | 108 | * |
Rhyme | 0:2741e9e34e8a | 109 | * @param mode true: Self-Test enabled, false: Self-Test disabled |
Rhyme | 0:2741e9e34e8a | 110 | */ |
Rhyme | 0:2741e9e34e8a | 111 | void selftest(bool mode) ; |
Rhyme | 0:2741e9e34e8a | 112 | |
Rhyme | 0:2741e9e34e8a | 113 | /** |
Rhyme | 0:2741e9e34e8a | 114 | * Standby/Ready mode selection |
Rhyme | 0:2741e9e34e8a | 115 | * |
Rhyme | 0:2741e9e34e8a | 116 | * @param mode true: Ready, false: Standby |
Rhyme | 0:2741e9e34e8a | 117 | */ |
Rhyme | 0:2741e9e34e8a | 118 | void ready(bool mode) ; |
Rhyme | 0:2741e9e34e8a | 119 | |
Rhyme | 0:2741e9e34e8a | 120 | /** |
Rhyme | 0:2741e9e34e8a | 121 | * get value of CTRL_REG1 |
Rhyme | 0:2741e9e34e8a | 122 | */ |
Rhyme | 0:2741e9e34e8a | 123 | uint8_t getCTRL1(void) ; |
Rhyme | 0:2741e9e34e8a | 124 | |
Rhyme | 0:2741e9e34e8a | 125 | /** |
Rhyme | 0:2741e9e34e8a | 126 | * set value to CTRL_REG1 |
Rhyme | 0:2741e9e34e8a | 127 | * |
Rhyme | 0:2741e9e34e8a | 128 | * @param value value for CTRL_REG1 |
Rhyme | 0:2741e9e34e8a | 129 | */ |
Rhyme | 0:2741e9e34e8a | 130 | void setCTRL1(uint8_t value) ; |
Rhyme | 0:2741e9e34e8a | 131 | |
Rhyme | 0:2741e9e34e8a | 132 | /** |
Rhyme | 0:2741e9e34e8a | 133 | * get value of CTRL_REG2 |
Rhyme | 0:2741e9e34e8a | 134 | */ |
Rhyme | 0:2741e9e34e8a | 135 | uint8_t getCTRL2(void) ; |
Rhyme | 0:2741e9e34e8a | 136 | |
Rhyme | 0:2741e9e34e8a | 137 | /** |
Rhyme | 0:2741e9e34e8a | 138 | * set value to CTRL_REG2 |
Rhyme | 0:2741e9e34e8a | 139 | * |
Rhyme | 0:2741e9e34e8a | 140 | * @param value value for CTRL_REG2 |
Rhyme | 0:2741e9e34e8a | 141 | */ |
Rhyme | 0:2741e9e34e8a | 142 | void setCTRL2(uint8_t value) ; |
Rhyme | 0:2741e9e34e8a | 143 | |
Rhyme | 0:2741e9e34e8a | 144 | /** |
Rhyme | 0:2741e9e34e8a | 145 | * get value of CTRL_REG3 |
Rhyme | 0:2741e9e34e8a | 146 | */ |
Rhyme | 0:2741e9e34e8a | 147 | uint8_t getCTRL3(void) ; |
Rhyme | 0:2741e9e34e8a | 148 | |
Rhyme | 0:2741e9e34e8a | 149 | /** |
Rhyme | 0:2741e9e34e8a | 150 | * set value to CTRL_REG3 |
Rhyme | 0:2741e9e34e8a | 151 | * |
Rhyme | 0:2741e9e34e8a | 152 | * @param value value for CTRL_REG3 |
Rhyme | 0:2741e9e34e8a | 153 | */ |
Rhyme | 0:2741e9e34e8a | 154 | void setCTRL3(uint8_t value) ; |
Rhyme | 0:2741e9e34e8a | 155 | |
Rhyme | 0:2741e9e34e8a | 156 | |
Rhyme | 0:2741e9e34e8a | 157 | |
Rhyme | 0:2741e9e34e8a | 158 | private: |
Rhyme | 0:2741e9e34e8a | 159 | I2C m_i2c; |
Rhyme | 0:2741e9e34e8a | 160 | int m_addr; |
Rhyme | 0:2741e9e34e8a | 161 | void readRegs(int addr, uint8_t * data, int len); |
Rhyme | 0:2741e9e34e8a | 162 | void writeRegs(uint8_t * data, int len); |
Rhyme | 0:2741e9e34e8a | 163 | |
Rhyme | 0:2741e9e34e8a | 164 | }; |
Rhyme | 0:2741e9e34e8a | 165 | |
Rhyme | 0:2741e9e34e8a | 166 | #endif |