code with new accelerometers

Dependencies:   BLE_API i2c-serial-conflict nRF51822

Fork of accel_to_blenano_i2c by Nicholas Kosarek

Committer:
znew711
Date:
Mon Apr 10 02:19:13 2017 +0000
Revision:
1:e2ba28405dd5
Parent:
0:6a249a5be3a4
Child:
2:2082f0f50590
hey there mr nick who am i

Who changed what in which revision?

UserRevisionLine numberNew 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 1:e2ba28405dd5 33 #define SCL 8
znew711 1:e2ba28405dd5 34 #define SDA 10
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 1:e2ba28405dd5 46 #define REG_WHOAMI 0x0F
znew711 0:6a249a5be3a4 47 #define RANGE_2G 0x00
znew711 1:e2ba28405dd5 48 #define DEVICE_ID 0x33
znew711 0:6a249a5be3a4 49
znew711 0:6a249a5be3a4 50
znew711 0:6a249a5be3a4 51 #define DATARATE_400HZ 0b0111 // 400Hz
znew711 0:6a249a5be3a4 52 #define DATARATE_200HZ 0b0110 // 200Hz
znew711 0:6a249a5be3a4 53 #define DATARATE_100HZ 0b0101 // 100Hz
znew711 0:6a249a5be3a4 54 #define DATARATE_50HZ 0b0100 // 50Hz
znew711 0:6a249a5be3a4 55 #define DATARATE_25HZ 0b0011 // 25Hz
znew711 0:6a249a5be3a4 56 #define DATARATE_10HZ 0b0010 // 10Hz
znew711 0:6a249a5be3a4 57 #define DATARATE_1HZ 0b0001 // 1Hz
znew711 0:6a249a5be3a4 58 #define DATARATE_POWERDOWN 0 // Power down
znew711 0:6a249a5be3a4 59 #define DATARATE_LOWPOWER_1K6HZ 0b1000 // Low power mode (1.6KHz)
znew711 0:6a249a5be3a4 60 #define DATARATE_LOWPOWER_5KHZ 0b1001 // Low power mode (5KHz) / Normal power mode (1.25KHz)
znew711 0:6a249a5be3a4 61
znew711 0:6a249a5be3a4 62 Serial pc(USBTX, USBRX);
znew711 0:6a249a5be3a4 63 TwoWire Wire = TwoWire(NRF_TWI0);
znew711 0:6a249a5be3a4 64
znew711 0:6a249a5be3a4 65 void AT24C512_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t length, uint16_t i2cAddr)
znew711 0:6a249a5be3a4 66 {
znew711 0:6a249a5be3a4 67 Wire.beginTransmission(i2cAddr);
znew711 1:e2ba28405dd5 68 int err = Wire.write( (uint8_t)addr>>8 );
znew711 0:6a249a5be3a4 69 Wire.write( (uint8_t)addr );
znew711 0:6a249a5be3a4 70 Wire.write(pbuf, length);
znew711 1:e2ba28405dd5 71 if (err != 0) {
znew711 1:e2ba28405dd5 72 pc.printf("error on write write! %d\n", err);
znew711 1:e2ba28405dd5 73 }
znew711 1:e2ba28405dd5 74 uint8_t err8 = Wire.endTransmission();
znew711 1:e2ba28405dd5 75 if (err8 != 0) {
znew711 1:e2ba28405dd5 76 pc.printf("error on write end transmission! %d\n", err8);
znew711 1:e2ba28405dd5 77 }
znew711 0:6a249a5be3a4 78 }
znew711 0:6a249a5be3a4 79
znew711 0:6a249a5be3a4 80 void AT24C512_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t length, uint16_t i2cAddr)
znew711 0:6a249a5be3a4 81 {
znew711 0:6a249a5be3a4 82 Wire.beginTransmission(i2cAddr);
znew711 1:e2ba28405dd5 83 int err = Wire.write( (uint8_t)addr>>8 );
znew711 1:e2ba28405dd5 84 Wire.write( (uint8_t)addr );
znew711 1:e2ba28405dd5 85 Wire.write(pbuf, length);
znew711 1:e2ba28405dd5 86 if (err != 0) {
znew711 1:e2ba28405dd5 87 pc.printf("error on read write! %d\n", err);
znew711 1:e2ba28405dd5 88 }
znew711 1:e2ba28405dd5 89 uint8_t err8 = Wire.endTransmission();
znew711 1:e2ba28405dd5 90 if (err8 != 0) {
znew711 1:e2ba28405dd5 91 pc.printf("error on read end transmission! %d\n", err8);
znew711 1:e2ba28405dd5 92 }
znew711 0:6a249a5be3a4 93
znew711 1:e2ba28405dd5 94 err8 = Wire.requestFrom(i2cAddr+1, length);
znew711 1:e2ba28405dd5 95 if (err != 0) {
znew711 1:e2ba28405dd5 96 pc.printf("error on read request from! %d\n", err8);
znew711 1:e2ba28405dd5 97 }
znew711 0:6a249a5be3a4 98 while( Wire.available() > 0 )
znew711 0:6a249a5be3a4 99 {
znew711 0:6a249a5be3a4 100 *pbuf = Wire.read();
znew711 0:6a249a5be3a4 101 pbuf++;
znew711 0:6a249a5be3a4 102 }
znew711 0:6a249a5be3a4 103 }
znew711 0:6a249a5be3a4 104
znew711 0:6a249a5be3a4 105 //Set the bit at index 'bit' to 'value' on 'input' and return
znew711 0:6a249a5be3a4 106 uint8_t setBit(uint8_t input, uint8_t bit, uint8_t value) {
znew711 0:6a249a5be3a4 107 uint8_t mask = 1 << bit;
znew711 0:6a249a5be3a4 108 input &= ~mask;
znew711 0:6a249a5be3a4 109 if (value == 1) {
znew711 0:6a249a5be3a4 110 input |= mask;
znew711 0:6a249a5be3a4 111 }
znew711 0:6a249a5be3a4 112 return input;
znew711 0:6a249a5be3a4 113 }
znew711 0:6a249a5be3a4 114
znew711 0:6a249a5be3a4 115 uint16_t getAxis(uint16_t axis, uint16_t i2cAddr)
znew711 0:6a249a5be3a4 116 {
znew711 0:6a249a5be3a4 117 uint8_t base = REG_OUT_X_L + (2 * axis);
znew711 0:6a249a5be3a4 118 uint8_t* low = new uint8_t[1];
znew711 0:6a249a5be3a4 119 uint8_t* high = new uint8_t[1];
znew711 0:6a249a5be3a4 120 AT24C512_ReadBytes(base, low, 1, i2cAddr);
znew711 0:6a249a5be3a4 121 AT24C512_ReadBytes(base + 1, high, 1, i2cAddr);
znew711 0:6a249a5be3a4 122 uint16_t res = low[0] | (high[0] << 8);
znew711 0:6a249a5be3a4 123 return res;
znew711 0:6a249a5be3a4 124 }
znew711 0:6a249a5be3a4 125
znew711 0:6a249a5be3a4 126 void setRange(uint8_t range, uint16_t i2cAddr) {
znew711 0:6a249a5be3a4 127 uint8_t* val = new uint8_t[1];
znew711 0:6a249a5be3a4 128 AT24C512_ReadBytes(REG_CTRL4, val, 1, i2cAddr);//get value from the register
znew711 0:6a249a5be3a4 129 val[0] &= ~(0b110000); //zero out lowest 4 bits
znew711 0:6a249a5be3a4 130 val[0] |= (range << 4); // write in our new range
znew711 0:6a249a5be3a4 131 AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr);
znew711 0:6a249a5be3a4 132 }
znew711 0:6a249a5be3a4 133
znew711 0:6a249a5be3a4 134 //Set whether we want to use high resolution or not
znew711 0:6a249a5be3a4 135 void setHighResolution(bool highRes, uint16_t i2cAddr) {
znew711 0:6a249a5be3a4 136 uint8_t* val = new uint8_t[1];
znew711 0:6a249a5be3a4 137 AT24C512_ReadBytes(REG_CTRL4, val, 1, i2cAddr);//get value from the register
znew711 0:6a249a5be3a4 138 uint8_t final;
znew711 0:6a249a5be3a4 139 if (highRes) {
znew711 0:6a249a5be3a4 140 final = setBit(val[0], 3, 1);
znew711 0:6a249a5be3a4 141 } else {
znew711 0:6a249a5be3a4 142 final = setBit(val[0], 3, 1);
znew711 0:6a249a5be3a4 143 }
znew711 0:6a249a5be3a4 144 val[0] = final;
znew711 0:6a249a5be3a4 145 AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr);
znew711 0:6a249a5be3a4 146 }
znew711 0:6a249a5be3a4 147
znew711 0:6a249a5be3a4 148 void setAxisStatus(uint8_t axis, bool enable, uint16_t i2cAddr) {
znew711 0:6a249a5be3a4 149 uint8_t* current = new uint8_t[1];
znew711 0:6a249a5be3a4 150 AT24C512_ReadBytes(REG_CTRL1, current, 1, i2cAddr);//get value from the register
znew711 0:6a249a5be3a4 151 uint8_t final;
znew711 0:6a249a5be3a4 152 if (enable == 1) {
znew711 0:6a249a5be3a4 153 final = setBit(current[0], axis, 1);
znew711 0:6a249a5be3a4 154 } else {
znew711 0:6a249a5be3a4 155 final = setBit(current[0], axis, 0);
znew711 0:6a249a5be3a4 156 }
znew711 0:6a249a5be3a4 157 current[0] = final;
znew711 0:6a249a5be3a4 158 AT24C512_WriteBytes(REG_CTRL1, current, 1, i2cAddr);
znew711 0:6a249a5be3a4 159 }
znew711 0:6a249a5be3a4 160
znew711 0:6a249a5be3a4 161 void setDataRate(uint8_t dataRate, uint16_t i2cAddr) {
znew711 0:6a249a5be3a4 162 uint8_t* val = new uint8_t[1];
znew711 0:6a249a5be3a4 163 AT24C512_ReadBytes(REG_CTRL1, val, 1, i2cAddr);
znew711 0:6a249a5be3a4 164 val[0] &= 0b1111; //mask off lower bits
znew711 0:6a249a5be3a4 165 val[0] |= (dataRate << 4);
znew711 0:6a249a5be3a4 166 AT24C512_WriteBytes(REG_CTRL1, val, 1, i2cAddr);
znew711 0:6a249a5be3a4 167 }
znew711 0:6a249a5be3a4 168
znew711 0:6a249a5be3a4 169 void setBDU(bool bdu, uint16_t i2cAddr)
znew711 0:6a249a5be3a4 170 {
znew711 0:6a249a5be3a4 171 uint8_t* val = new uint8_t[1];
znew711 0:6a249a5be3a4 172 AT24C512_ReadBytes(REG_CTRL4, val, 1, i2cAddr);//get value from the register
znew711 0:6a249a5be3a4 173 uint8_t final;
znew711 0:6a249a5be3a4 174 if (bdu == true) {
znew711 0:6a249a5be3a4 175 final = setBit(val[0], 7, 1);
znew711 0:6a249a5be3a4 176 } else {
znew711 0:6a249a5be3a4 177 final = setBit(val[0], 7, 1);
znew711 0:6a249a5be3a4 178 }
znew711 0:6a249a5be3a4 179 val[0] = final;
znew711 0:6a249a5be3a4 180 AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr);
znew711 0:6a249a5be3a4 181 }
znew711 0:6a249a5be3a4 182
znew711 0:6a249a5be3a4 183 uint16_t getX(uint16_t i2cAddr)
znew711 0:6a249a5be3a4 184 {
znew711 0:6a249a5be3a4 185 return getAxis(AXIS_X, i2cAddr);
znew711 0:6a249a5be3a4 186 }
znew711 0:6a249a5be3a4 187
znew711 0:6a249a5be3a4 188 uint16_t getY(uint16_t i2cAddr)
znew711 0:6a249a5be3a4 189 {
znew711 0:6a249a5be3a4 190 return getAxis(AXIS_Y, i2cAddr);
znew711 0:6a249a5be3a4 191 }
znew711 0:6a249a5be3a4 192
znew711 0:6a249a5be3a4 193 uint16_t getZ(uint16_t i2cAddr)
znew711 0:6a249a5be3a4 194 {
znew711 0:6a249a5be3a4 195 return getAxis(AXIS_Z, i2cAddr);
znew711 0:6a249a5be3a4 196 }
znew711 0:6a249a5be3a4 197
znew711 0:6a249a5be3a4 198 int main(void)
znew711 0:6a249a5be3a4 199 {
znew711 0:6a249a5be3a4 200 pc.baud(9600);
znew711 0:6a249a5be3a4 201 wait(5);
znew711 0:6a249a5be3a4 202 //Wire.begin();
znew711 0:6a249a5be3a4 203 Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);
znew711 0:6a249a5be3a4 204 pc.printf("IIC Demo Start \r\n");
znew711 0:6a249a5be3a4 205
znew711 1:e2ba28405dd5 206 uint8_t* whoami = new uint8_t[1];
znew711 1:e2ba28405dd5 207 AT24C512_ReadBytes(REG_WHOAMI, whoami, 1, ADDR_ONE);
znew711 1:e2ba28405dd5 208 pc.printf("Whoami: %d\r\n", whoami[1]);
znew711 1:e2ba28405dd5 209
znew711 1:e2ba28405dd5 210 wait(15);
znew711 1:e2ba28405dd5 211
znew711 0:6a249a5be3a4 212 setAxisStatus(AXIS_X, true, ADDR_ONE);
znew711 0:6a249a5be3a4 213 setAxisStatus(AXIS_Y, true, ADDR_ONE);
znew711 0:6a249a5be3a4 214 setAxisStatus(AXIS_Z, true, ADDR_ONE);
znew711 0:6a249a5be3a4 215 setDataRate(DATARATE_400HZ, ADDR_ONE);
znew711 0:6a249a5be3a4 216 setHighResolution(true, ADDR_ONE);
znew711 0:6a249a5be3a4 217 setBDU(true, ADDR_ONE);
znew711 0:6a249a5be3a4 218 setRange(RANGE_2G, ADDR_ONE);
znew711 0:6a249a5be3a4 219
znew711 0:6a249a5be3a4 220 setAxisStatus(AXIS_X, true, ADDR_TWO);
znew711 0:6a249a5be3a4 221 setAxisStatus(AXIS_Y, true, ADDR_TWO);
znew711 0:6a249a5be3a4 222 setAxisStatus(AXIS_Z, true, ADDR_TWO);
znew711 0:6a249a5be3a4 223 setDataRate(DATARATE_400HZ, ADDR_ONE);
znew711 0:6a249a5be3a4 224 setHighResolution(true, ADDR_TWO);
znew711 0:6a249a5be3a4 225 setBDU(true, ADDR_TWO);
znew711 0:6a249a5be3a4 226 setRange(RANGE_2G, ADDR_TWO);
znew711 0:6a249a5be3a4 227
znew711 0:6a249a5be3a4 228 wait(0.1);
znew711 0:6a249a5be3a4 229
znew711 0:6a249a5be3a4 230 while(1)
znew711 0:6a249a5be3a4 231 {
znew711 0:6a249a5be3a4 232 pc.printf("Read data from AT24C512 \r\n");
znew711 0:6a249a5be3a4 233 uint16_t x1 = getX(ADDR_ONE);
znew711 0:6a249a5be3a4 234 uint16_t y1 = getY(ADDR_ONE);
znew711 0:6a249a5be3a4 235 uint16_t z1 = getZ(ADDR_ONE);
znew711 0:6a249a5be3a4 236
znew711 0:6a249a5be3a4 237 uint16_t x2 = getX(ADDR_TWO);
znew711 0:6a249a5be3a4 238 uint16_t y2 = getY(ADDR_TWO);
znew711 0:6a249a5be3a4 239 uint16_t z2 = getZ(ADDR_TWO);
znew711 0:6a249a5be3a4 240 pc.printf("Accel one: x %d y %d z %d\r\n", x1, y1, z1);
znew711 0:6a249a5be3a4 241 pc.printf("Accel two: x %d y %d z %d\r\n", x2, y2, z2);
znew711 0:6a249a5be3a4 242 pc.printf("\r\n");
znew711 0:6a249a5be3a4 243 wait(1);
znew711 0:6a249a5be3a4 244 }
znew711 0:6a249a5be3a4 245 }