code with new accelerometers
Dependencies: BLE_API i2c-serial-conflict nRF51822
Fork of accel_to_blenano_i2c by
main.cpp@0:6a249a5be3a4, 2017-04-04 (annotated)
- Committer:
- znew711
- Date:
- Tue Apr 04 03:17:10 2017 +0000
- Revision:
- 0:6a249a5be3a4
- Child:
- 1:e2ba28405dd5
it compile;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
znew711 | 0:6a249a5be3a4 | 1 | /* |
znew711 | 0:6a249a5be3a4 | 2 | |
znew711 | 0:6a249a5be3a4 | 3 | Copyright (c) 2012-2014 RedBearLab |
znew711 | 0:6a249a5be3a4 | 4 | |
znew711 | 0:6a249a5be3a4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
znew711 | 0:6a249a5be3a4 | 6 | and associated documentation files (the "Software"), to deal in the Software without restriction, |
znew711 | 0:6a249a5be3a4 | 7 | including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
znew711 | 0:6a249a5be3a4 | 8 | and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
znew711 | 0:6a249a5be3a4 | 9 | subject to the following conditions: |
znew711 | 0:6a249a5be3a4 | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
znew711 | 0:6a249a5be3a4 | 11 | |
znew711 | 0:6a249a5be3a4 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
znew711 | 0:6a249a5be3a4 | 13 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
znew711 | 0:6a249a5be3a4 | 14 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
znew711 | 0:6a249a5be3a4 | 15 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
znew711 | 0:6a249a5be3a4 | 16 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
znew711 | 0:6a249a5be3a4 | 17 | |
znew711 | 0:6a249a5be3a4 | 18 | */ |
znew711 | 0:6a249a5be3a4 | 19 | |
znew711 | 0:6a249a5be3a4 | 20 | #include "mbed.h" |
znew711 | 0:6a249a5be3a4 | 21 | #include "wire.h" |
znew711 | 0:6a249a5be3a4 | 22 | |
znew711 | 0:6a249a5be3a4 | 23 | #define BLE_Nano |
znew711 | 0:6a249a5be3a4 | 24 | //#define nRF_51822 |
znew711 | 0:6a249a5be3a4 | 25 | |
znew711 | 0:6a249a5be3a4 | 26 | |
znew711 | 0:6a249a5be3a4 | 27 | #ifdef nRF_51822 |
znew711 | 0:6a249a5be3a4 | 28 | #define SCL 28 |
znew711 | 0:6a249a5be3a4 | 29 | #define SDA 29 |
znew711 | 0:6a249a5be3a4 | 30 | #endif |
znew711 | 0:6a249a5be3a4 | 31 | |
znew711 | 0:6a249a5be3a4 | 32 | #ifdef BLE_Nano |
znew711 | 0:6a249a5be3a4 | 33 | #define SCL 7 |
znew711 | 0:6a249a5be3a4 | 34 | #define SDA 6 |
znew711 | 0:6a249a5be3a4 | 35 | #endif |
znew711 | 0:6a249a5be3a4 | 36 | |
znew711 | 0:6a249a5be3a4 | 37 | #define DEV_ADDR 0xA0 |
znew711 | 0:6a249a5be3a4 | 38 | #define ADDR_ONE 0x19 |
znew711 | 0:6a249a5be3a4 | 39 | #define ADDR_TWO 0x18 |
znew711 | 0:6a249a5be3a4 | 40 | #define AXIS_X 0x00 |
znew711 | 0:6a249a5be3a4 | 41 | #define AXIS_Y 0x01 |
znew711 | 0:6a249a5be3a4 | 42 | #define AXIS_Z 0x02 |
znew711 | 0:6a249a5be3a4 | 43 | #define REG_OUT_X_L 0x28 |
znew711 | 0:6a249a5be3a4 | 44 | #define REG_CTRL1 0x20 |
znew711 | 0:6a249a5be3a4 | 45 | #define REG_CTRL4 0x23 |
znew711 | 0:6a249a5be3a4 | 46 | #define RANGE_2G 0x00 |
znew711 | 0:6a249a5be3a4 | 47 | |
znew711 | 0:6a249a5be3a4 | 48 | |
znew711 | 0:6a249a5be3a4 | 49 | #define DATARATE_400HZ 0b0111 // 400Hz |
znew711 | 0:6a249a5be3a4 | 50 | #define DATARATE_200HZ 0b0110 // 200Hz |
znew711 | 0:6a249a5be3a4 | 51 | #define DATARATE_100HZ 0b0101 // 100Hz |
znew711 | 0:6a249a5be3a4 | 52 | #define DATARATE_50HZ 0b0100 // 50Hz |
znew711 | 0:6a249a5be3a4 | 53 | #define DATARATE_25HZ 0b0011 // 25Hz |
znew711 | 0:6a249a5be3a4 | 54 | #define DATARATE_10HZ 0b0010 // 10Hz |
znew711 | 0:6a249a5be3a4 | 55 | #define DATARATE_1HZ 0b0001 // 1Hz |
znew711 | 0:6a249a5be3a4 | 56 | #define DATARATE_POWERDOWN 0 // Power down |
znew711 | 0:6a249a5be3a4 | 57 | #define DATARATE_LOWPOWER_1K6HZ 0b1000 // Low power mode (1.6KHz) |
znew711 | 0:6a249a5be3a4 | 58 | #define DATARATE_LOWPOWER_5KHZ 0b1001 // Low power mode (5KHz) / Normal power mode (1.25KHz) |
znew711 | 0:6a249a5be3a4 | 59 | |
znew711 | 0:6a249a5be3a4 | 60 | Serial pc(USBTX, USBRX); |
znew711 | 0:6a249a5be3a4 | 61 | TwoWire Wire = TwoWire(NRF_TWI0); |
znew711 | 0:6a249a5be3a4 | 62 | |
znew711 | 0:6a249a5be3a4 | 63 | void AT24C512_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t length, uint16_t i2cAddr) |
znew711 | 0:6a249a5be3a4 | 64 | { |
znew711 | 0:6a249a5be3a4 | 65 | Wire.beginTransmission(i2cAddr); |
znew711 | 0:6a249a5be3a4 | 66 | Wire.write( (uint8_t)addr>>8 ); |
znew711 | 0:6a249a5be3a4 | 67 | Wire.write( (uint8_t)addr ); |
znew711 | 0:6a249a5be3a4 | 68 | Wire.write(pbuf, length); |
znew711 | 0:6a249a5be3a4 | 69 | Wire.endTransmission(); |
znew711 | 0:6a249a5be3a4 | 70 | } |
znew711 | 0:6a249a5be3a4 | 71 | |
znew711 | 0:6a249a5be3a4 | 72 | void AT24C512_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t length, uint16_t i2cAddr) |
znew711 | 0:6a249a5be3a4 | 73 | { |
znew711 | 0:6a249a5be3a4 | 74 | Wire.beginTransmission(i2cAddr); |
znew711 | 0:6a249a5be3a4 | 75 | Wire.write( (uint8_t)addr>>8 ); |
znew711 | 0:6a249a5be3a4 | 76 | Wire.write( (uint8_t)addr ); |
znew711 | 0:6a249a5be3a4 | 77 | Wire.endTransmission(); |
znew711 | 0:6a249a5be3a4 | 78 | |
znew711 | 0:6a249a5be3a4 | 79 | Wire.requestFrom(i2cAddr+1, length); |
znew711 | 0:6a249a5be3a4 | 80 | while( Wire.available() > 0 ) |
znew711 | 0:6a249a5be3a4 | 81 | { |
znew711 | 0:6a249a5be3a4 | 82 | *pbuf = Wire.read(); |
znew711 | 0:6a249a5be3a4 | 83 | pbuf++; |
znew711 | 0:6a249a5be3a4 | 84 | } |
znew711 | 0:6a249a5be3a4 | 85 | } |
znew711 | 0:6a249a5be3a4 | 86 | |
znew711 | 0:6a249a5be3a4 | 87 | //Set the bit at index 'bit' to 'value' on 'input' and return |
znew711 | 0:6a249a5be3a4 | 88 | uint8_t setBit(uint8_t input, uint8_t bit, uint8_t value) { |
znew711 | 0:6a249a5be3a4 | 89 | uint8_t mask = 1 << bit; |
znew711 | 0:6a249a5be3a4 | 90 | input &= ~mask; |
znew711 | 0:6a249a5be3a4 | 91 | if (value == 1) { |
znew711 | 0:6a249a5be3a4 | 92 | input |= mask; |
znew711 | 0:6a249a5be3a4 | 93 | } |
znew711 | 0:6a249a5be3a4 | 94 | return input; |
znew711 | 0:6a249a5be3a4 | 95 | } |
znew711 | 0:6a249a5be3a4 | 96 | |
znew711 | 0:6a249a5be3a4 | 97 | uint16_t getAxis(uint16_t axis, uint16_t i2cAddr) |
znew711 | 0:6a249a5be3a4 | 98 | { |
znew711 | 0:6a249a5be3a4 | 99 | uint8_t base = REG_OUT_X_L + (2 * axis); |
znew711 | 0:6a249a5be3a4 | 100 | uint8_t* low = new uint8_t[1]; |
znew711 | 0:6a249a5be3a4 | 101 | uint8_t* high = new uint8_t[1]; |
znew711 | 0:6a249a5be3a4 | 102 | AT24C512_ReadBytes(base, low, 1, i2cAddr); |
znew711 | 0:6a249a5be3a4 | 103 | AT24C512_ReadBytes(base + 1, high, 1, i2cAddr); |
znew711 | 0:6a249a5be3a4 | 104 | uint16_t res = low[0] | (high[0] << 8); |
znew711 | 0:6a249a5be3a4 | 105 | return res; |
znew711 | 0:6a249a5be3a4 | 106 | } |
znew711 | 0:6a249a5be3a4 | 107 | |
znew711 | 0:6a249a5be3a4 | 108 | void setRange(uint8_t range, uint16_t i2cAddr) { |
znew711 | 0:6a249a5be3a4 | 109 | uint8_t* val = new uint8_t[1]; |
znew711 | 0:6a249a5be3a4 | 110 | AT24C512_ReadBytes(REG_CTRL4, val, 1, i2cAddr);//get value from the register |
znew711 | 0:6a249a5be3a4 | 111 | val[0] &= ~(0b110000); //zero out lowest 4 bits |
znew711 | 0:6a249a5be3a4 | 112 | val[0] |= (range << 4); // write in our new range |
znew711 | 0:6a249a5be3a4 | 113 | AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr); |
znew711 | 0:6a249a5be3a4 | 114 | } |
znew711 | 0:6a249a5be3a4 | 115 | |
znew711 | 0:6a249a5be3a4 | 116 | //Set whether we want to use high resolution or not |
znew711 | 0:6a249a5be3a4 | 117 | void setHighResolution(bool highRes, uint16_t i2cAddr) { |
znew711 | 0:6a249a5be3a4 | 118 | uint8_t* val = new uint8_t[1]; |
znew711 | 0:6a249a5be3a4 | 119 | AT24C512_ReadBytes(REG_CTRL4, val, 1, i2cAddr);//get value from the register |
znew711 | 0:6a249a5be3a4 | 120 | uint8_t final; |
znew711 | 0:6a249a5be3a4 | 121 | if (highRes) { |
znew711 | 0:6a249a5be3a4 | 122 | final = setBit(val[0], 3, 1); |
znew711 | 0:6a249a5be3a4 | 123 | } else { |
znew711 | 0:6a249a5be3a4 | 124 | final = setBit(val[0], 3, 1); |
znew711 | 0:6a249a5be3a4 | 125 | } |
znew711 | 0:6a249a5be3a4 | 126 | val[0] = final; |
znew711 | 0:6a249a5be3a4 | 127 | AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr); |
znew711 | 0:6a249a5be3a4 | 128 | } |
znew711 | 0:6a249a5be3a4 | 129 | |
znew711 | 0:6a249a5be3a4 | 130 | void setAxisStatus(uint8_t axis, bool enable, uint16_t i2cAddr) { |
znew711 | 0:6a249a5be3a4 | 131 | uint8_t* current = new uint8_t[1]; |
znew711 | 0:6a249a5be3a4 | 132 | AT24C512_ReadBytes(REG_CTRL1, current, 1, i2cAddr);//get value from the register |
znew711 | 0:6a249a5be3a4 | 133 | uint8_t final; |
znew711 | 0:6a249a5be3a4 | 134 | if (enable == 1) { |
znew711 | 0:6a249a5be3a4 | 135 | final = setBit(current[0], axis, 1); |
znew711 | 0:6a249a5be3a4 | 136 | } else { |
znew711 | 0:6a249a5be3a4 | 137 | final = setBit(current[0], axis, 0); |
znew711 | 0:6a249a5be3a4 | 138 | } |
znew711 | 0:6a249a5be3a4 | 139 | current[0] = final; |
znew711 | 0:6a249a5be3a4 | 140 | AT24C512_WriteBytes(REG_CTRL1, current, 1, i2cAddr); |
znew711 | 0:6a249a5be3a4 | 141 | } |
znew711 | 0:6a249a5be3a4 | 142 | |
znew711 | 0:6a249a5be3a4 | 143 | void setDataRate(uint8_t dataRate, uint16_t i2cAddr) { |
znew711 | 0:6a249a5be3a4 | 144 | uint8_t* val = new uint8_t[1]; |
znew711 | 0:6a249a5be3a4 | 145 | AT24C512_ReadBytes(REG_CTRL1, val, 1, i2cAddr); |
znew711 | 0:6a249a5be3a4 | 146 | val[0] &= 0b1111; //mask off lower bits |
znew711 | 0:6a249a5be3a4 | 147 | val[0] |= (dataRate << 4); |
znew711 | 0:6a249a5be3a4 | 148 | AT24C512_WriteBytes(REG_CTRL1, val, 1, i2cAddr); |
znew711 | 0:6a249a5be3a4 | 149 | } |
znew711 | 0:6a249a5be3a4 | 150 | |
znew711 | 0:6a249a5be3a4 | 151 | void setBDU(bool bdu, uint16_t i2cAddr) |
znew711 | 0:6a249a5be3a4 | 152 | { |
znew711 | 0:6a249a5be3a4 | 153 | uint8_t* val = new uint8_t[1]; |
znew711 | 0:6a249a5be3a4 | 154 | AT24C512_ReadBytes(REG_CTRL4, val, 1, i2cAddr);//get value from the register |
znew711 | 0:6a249a5be3a4 | 155 | uint8_t final; |
znew711 | 0:6a249a5be3a4 | 156 | if (bdu == true) { |
znew711 | 0:6a249a5be3a4 | 157 | final = setBit(val[0], 7, 1); |
znew711 | 0:6a249a5be3a4 | 158 | } else { |
znew711 | 0:6a249a5be3a4 | 159 | final = setBit(val[0], 7, 1); |
znew711 | 0:6a249a5be3a4 | 160 | } |
znew711 | 0:6a249a5be3a4 | 161 | val[0] = final; |
znew711 | 0:6a249a5be3a4 | 162 | AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr); |
znew711 | 0:6a249a5be3a4 | 163 | } |
znew711 | 0:6a249a5be3a4 | 164 | |
znew711 | 0:6a249a5be3a4 | 165 | uint16_t getX(uint16_t i2cAddr) |
znew711 | 0:6a249a5be3a4 | 166 | { |
znew711 | 0:6a249a5be3a4 | 167 | return getAxis(AXIS_X, i2cAddr); |
znew711 | 0:6a249a5be3a4 | 168 | } |
znew711 | 0:6a249a5be3a4 | 169 | |
znew711 | 0:6a249a5be3a4 | 170 | uint16_t getY(uint16_t i2cAddr) |
znew711 | 0:6a249a5be3a4 | 171 | { |
znew711 | 0:6a249a5be3a4 | 172 | return getAxis(AXIS_Y, i2cAddr); |
znew711 | 0:6a249a5be3a4 | 173 | } |
znew711 | 0:6a249a5be3a4 | 174 | |
znew711 | 0:6a249a5be3a4 | 175 | uint16_t getZ(uint16_t i2cAddr) |
znew711 | 0:6a249a5be3a4 | 176 | { |
znew711 | 0:6a249a5be3a4 | 177 | return getAxis(AXIS_Z, i2cAddr); |
znew711 | 0:6a249a5be3a4 | 178 | } |
znew711 | 0:6a249a5be3a4 | 179 | |
znew711 | 0:6a249a5be3a4 | 180 | int main(void) |
znew711 | 0:6a249a5be3a4 | 181 | { |
znew711 | 0:6a249a5be3a4 | 182 | pc.baud(9600); |
znew711 | 0:6a249a5be3a4 | 183 | wait(5); |
znew711 | 0:6a249a5be3a4 | 184 | //Wire.begin(); |
znew711 | 0:6a249a5be3a4 | 185 | Wire.begin(SCL, SDA, TWI_FREQUENCY_100K); |
znew711 | 0:6a249a5be3a4 | 186 | pc.printf("IIC Demo Start \r\n"); |
znew711 | 0:6a249a5be3a4 | 187 | |
znew711 | 0:6a249a5be3a4 | 188 | setAxisStatus(AXIS_X, true, ADDR_ONE); |
znew711 | 0:6a249a5be3a4 | 189 | setAxisStatus(AXIS_Y, true, ADDR_ONE); |
znew711 | 0:6a249a5be3a4 | 190 | setAxisStatus(AXIS_Z, true, ADDR_ONE); |
znew711 | 0:6a249a5be3a4 | 191 | setDataRate(DATARATE_400HZ, ADDR_ONE); |
znew711 | 0:6a249a5be3a4 | 192 | setHighResolution(true, ADDR_ONE); |
znew711 | 0:6a249a5be3a4 | 193 | setBDU(true, ADDR_ONE); |
znew711 | 0:6a249a5be3a4 | 194 | setRange(RANGE_2G, ADDR_ONE); |
znew711 | 0:6a249a5be3a4 | 195 | |
znew711 | 0:6a249a5be3a4 | 196 | setAxisStatus(AXIS_X, true, ADDR_TWO); |
znew711 | 0:6a249a5be3a4 | 197 | setAxisStatus(AXIS_Y, true, ADDR_TWO); |
znew711 | 0:6a249a5be3a4 | 198 | setAxisStatus(AXIS_Z, true, ADDR_TWO); |
znew711 | 0:6a249a5be3a4 | 199 | setDataRate(DATARATE_400HZ, ADDR_ONE); |
znew711 | 0:6a249a5be3a4 | 200 | setHighResolution(true, ADDR_TWO); |
znew711 | 0:6a249a5be3a4 | 201 | setBDU(true, ADDR_TWO); |
znew711 | 0:6a249a5be3a4 | 202 | setRange(RANGE_2G, ADDR_TWO); |
znew711 | 0:6a249a5be3a4 | 203 | |
znew711 | 0:6a249a5be3a4 | 204 | wait(0.1); |
znew711 | 0:6a249a5be3a4 | 205 | |
znew711 | 0:6a249a5be3a4 | 206 | while(1) |
znew711 | 0:6a249a5be3a4 | 207 | { |
znew711 | 0:6a249a5be3a4 | 208 | pc.printf("Read data from AT24C512 \r\n"); |
znew711 | 0:6a249a5be3a4 | 209 | uint16_t x1 = getX(ADDR_ONE); |
znew711 | 0:6a249a5be3a4 | 210 | uint16_t y1 = getY(ADDR_ONE); |
znew711 | 0:6a249a5be3a4 | 211 | uint16_t z1 = getZ(ADDR_ONE); |
znew711 | 0:6a249a5be3a4 | 212 | |
znew711 | 0:6a249a5be3a4 | 213 | uint16_t x2 = getX(ADDR_TWO); |
znew711 | 0:6a249a5be3a4 | 214 | uint16_t y2 = getY(ADDR_TWO); |
znew711 | 0:6a249a5be3a4 | 215 | uint16_t z2 = getZ(ADDR_TWO); |
znew711 | 0:6a249a5be3a4 | 216 | pc.printf("Accel one: x %d y %d z %d\r\n", x1, y1, z1); |
znew711 | 0:6a249a5be3a4 | 217 | pc.printf("Accel two: x %d y %d z %d\r\n", x2, y2, z2); |
znew711 | 0:6a249a5be3a4 | 218 | pc.printf("\r\n"); |
znew711 | 0:6a249a5be3a4 | 219 | wait(1); |
znew711 | 0:6a249a5be3a4 | 220 | } |
znew711 | 0:6a249a5be3a4 | 221 | } |