this is program how build nRF51822 to get ADXL345 data

Dependencies:   BLE_API mbed nRF51822

Fork of ADXL345_I2C by Peter Swanson

Committer:
asyrofi
Date:
Thu Dec 07 06:08:01 2017 +0000
Revision:
8:a682144e97a8
Parent:
7:31412349a322
Child:
9:1e04b80d7199
spi mantaplah;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
peterswanson87 0:d0adb548714f 1 #include "ADXL345_I2C.h"
asyrofi 2:99e00e9c5035 2 #include "mbed.h"
asyrofi 2:99e00e9c5035 3 #include "ble/BLE.h"
asyrofi 3:1749778af065 4 #include "ble/services/UARTService.h"
asyrofi 3:1749778af065 5 #include "Serial.h"
asyrofi 2:99e00e9c5035 6
asyrofi 3:1749778af065 7 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
asyrofi 2:99e00e9c5035 8 * it will have an impact on code-size and power consumption. */
asyrofi 2:99e00e9c5035 9
asyrofi 2:99e00e9c5035 10 #if NEED_CONSOLE_OUTPUT
asyrofi 2:99e00e9c5035 11 #define DEBUG(...) { printf(__VA_ARGS__); }
asyrofi 2:99e00e9c5035 12 #else
asyrofi 2:99e00e9c5035 13 #define DEBUG(...) /* nothing */
asyrofi 2:99e00e9c5035 14 #endif /* #if NEED_CONSOLE_OUTPUT */
peterswanson87 0:d0adb548714f 15
asyrofi 4:a57b495be9fa 16 ADXL345_I2C accelerometer(p30, p7);
asyrofi 2:99e00e9c5035 17 BLEDevice ble;
asyrofi 2:99e00e9c5035 18 DigitalOut led1(LED1);
asyrofi 3:1749778af065 19 Serial uart1(USBTX,USBRX);
asyrofi 2:99e00e9c5035 20 UARTService *uartServicePtr;
asyrofi 6:bc835d0f686f 21
asyrofi 6:bc835d0f686f 22
asyrofi 6:bc835d0f686f 23
asyrofi 2:99e00e9c5035 24
asyrofi 2:99e00e9c5035 25 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
asyrofi 2:99e00e9c5035 26 {
asyrofi 2:99e00e9c5035 27 DEBUG("Disconnected!\n\r");
asyrofi 2:99e00e9c5035 28 DEBUG("Restarting the advertising process\n\r");
asyrofi 2:99e00e9c5035 29 ble.startAdvertising();
asyrofi 2:99e00e9c5035 30 }
asyrofi 2:99e00e9c5035 31
asyrofi 3:1749778af065 32 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
asyrofi 3:1749778af065 33
asyrofi 3:1749778af065 34 DEBUG("Connected!\n\r");
asyrofi 3:1749778af065 35
asyrofi 3:1749778af065 36 }
asyrofi 3:1749778af065 37
asyrofi 3:1749778af065 38 uint8_t b[40]={'a','d','q','w'};
asyrofi 2:99e00e9c5035 39 void onDataWritten(const GattWriteCallbackParams *params)
asyrofi 2:99e00e9c5035 40 {
asyrofi 2:99e00e9c5035 41 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
asyrofi 2:99e00e9c5035 42 uint16_t bytesRead = params->len;
asyrofi 3:1749778af065 43 DEBUG("received %u bytes %s\n\r", bytesRead,params->data);
asyrofi 3:1749778af065 44 /*uint16_t bytesRead = params->len;
asyrofi 2:99e00e9c5035 45 DEBUG("received %u bytes\n\r", bytesRead);
asyrofi 3:1749778af065 46 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);*/
asyrofi 2:99e00e9c5035 47 }
asyrofi 2:99e00e9c5035 48 }
asyrofi 2:99e00e9c5035 49
asyrofi 2:99e00e9c5035 50 void periodicCallback(void)
asyrofi 2:99e00e9c5035 51 {
asyrofi 2:99e00e9c5035 52 led1 = !led1;
asyrofi 2:99e00e9c5035 53 }
asyrofi 7:31412349a322 54 /*
asyrofi 6:bc835d0f686f 55 int threshold =80;
asyrofi 6:bc835d0f686f 56 int xval[100]={0};
asyrofi 6:bc835d0f686f 57 int yval[100]={0};
asyrofi 6:bc835d0f686f 58 int zval[100]={0};
asyrofi 6:bc835d0f686f 59 int xavg;
asyrofi 6:bc835d0f686f 60 int yavg;
asyrofi 6:bc835d0f686f 61 int zavg;
asyrofi 6:bc835d0f686f 62 int steps, flag=0;
asyrofi 6:bc835d0f686f 63 void calibrate(void)
asyrofi 5:8242f251bcac 64 {
asyrofi 6:bc835d0f686f 65 led1=1;
asyrofi 6:bc835d0f686f 66 int sum1 = 0;
asyrofi 6:bc835d0f686f 67 int sum2 = 0;
asyrofi 6:bc835d0f686f 68 int sum3 = 0;
asyrofi 6:bc835d0f686f 69 for (int i=0; i<100; i++)
asyrofi 6:bc835d0f686f 70 {
asyrofi 6:bc835d0f686f 71 xval[i]=(1); //nilai x adxl345
asyrofi 6:bc835d0f686f 72 sum1 = xval[i]+sum1;
asyrofi 6:bc835d0f686f 73 }
asyrofi 6:bc835d0f686f 74 wait(0.1);
asyrofi 6:bc835d0f686f 75 xavg=sum1/100.0;
asyrofi 6:bc835d0f686f 76 uart1.printf("%i\n", (int16_t)xavg); //nilai hasil kalibrasi x
asyrofi 6:bc835d0f686f 77
asyrofi 6:bc835d0f686f 78
asyrofi 6:bc835d0f686f 79 for (int i=0; i<100; i++)
asyrofi 6:bc835d0f686f 80 {
asyrofi 6:bc835d0f686f 81 yval[i]=(1); //nilai y adxl345
asyrofi 6:bc835d0f686f 82 sum2 = yval[i]+sum2;
asyrofi 6:bc835d0f686f 83 }
asyrofi 6:bc835d0f686f 84 wait(0.1);
asyrofi 6:bc835d0f686f 85 yavg=sum2/100.0;
asyrofi 6:bc835d0f686f 86 uart1.printf("%i\n", (int16_t)yavg); //nilai hasil kalibrasi y
asyrofi 6:bc835d0f686f 87
asyrofi 6:bc835d0f686f 88 for (int i=0; i<100; i++)
asyrofi 6:bc835d0f686f 89 {
asyrofi 6:bc835d0f686f 90 zval[i]=(1); //nilai z adxl345
asyrofi 6:bc835d0f686f 91 sum3 = zval[i]+sum3;
asyrofi 6:bc835d0f686f 92 }
asyrofi 6:bc835d0f686f 93 wait(0.1);
asyrofi 6:bc835d0f686f 94 zavg=sum3/100.0;
asyrofi 6:bc835d0f686f 95 uart1.printf("%i\n", (int16_t)zavg); //nilai hasil kalibrasi z
asyrofi 6:bc835d0f686f 96 led1=0;
asyrofi 5:8242f251bcac 97 }
asyrofi 7:31412349a322 98 */
asyrofi 3:1749778af065 99
asyrofi 6:bc835d0f686f 100 int main()
asyrofi 6:bc835d0f686f 101 {
asyrofi 4:a57b495be9fa 102 uart1.baud(9600);
peterswanson87 0:d0adb548714f 103 int readings[3] = {0, 0, 0};
asyrofi 8:a682144e97a8 104
asyrofi 7:31412349a322 105 //calibrate();
asyrofi 4:a57b495be9fa 106 uart1.printf("Starting ADXL345 test...\n");
peterswanson87 0:d0adb548714f 107 wait(.001);
asyrofi 4:a57b495be9fa 108 uart1.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
asyrofi 3:1749778af065 109 wait(.001);
asyrofi 6:bc835d0f686f 110
peterswanson87 0:d0adb548714f 111
asyrofi 2:99e00e9c5035 112 //Go into standby mode to configure the device.
asyrofi 2:99e00e9c5035 113 accelerometer.setPowerControl(0x00);
asyrofi 2:99e00e9c5035 114
asyrofi 2:99e00e9c5035 115 //Full resolution, +/-16g, 4mg/LSB.
asyrofi 2:99e00e9c5035 116 accelerometer.setDataFormatControl(0x0B);
asyrofi 2:99e00e9c5035 117
asyrofi 2:99e00e9c5035 118 //3.2kHz data rate.
asyrofi 2:99e00e9c5035 119 accelerometer.setDataRate(ADXL345_3200HZ);
asyrofi 2:99e00e9c5035 120
asyrofi 2:99e00e9c5035 121 //Measurement mode.
asyrofi 2:99e00e9c5035 122 accelerometer.setPowerControl(0x08);
asyrofi 2:99e00e9c5035 123
asyrofi 7:31412349a322 124 /*
asyrofi 6:bc835d0f686f 125 int acc=0;
asyrofi 6:bc835d0f686f 126 int totvect [100] = {0};
asyrofi 6:bc835d0f686f 127 int totave [100] = {0};
asyrofi 6:bc835d0f686f 128 int totvel [100] = {0};
asyrofi 6:bc835d0f686f 129 int totdist [100] = {0};
asyrofi 6:bc835d0f686f 130 int totheight [100] = {0};
asyrofi 6:bc835d0f686f 131
asyrofi 6:bc835d0f686f 132
asyrofi 6:bc835d0f686f 133 //float sum1, sum2, sum3 = 0
asyrofi 6:bc835d0f686f 134 double xaccl[100] = {0};
asyrofi 6:bc835d0f686f 135 double yaccl[100] = {0};
asyrofi 6:bc835d0f686f 136 double zaccl[100] = {0};
asyrofi 6:bc835d0f686f 137
asyrofi 6:bc835d0f686f 138 //float x,y,z
asyrofi 6:bc835d0f686f 139 for (int i=0; i<100; i++)
asyrofi 6:bc835d0f686f 140 {
asyrofi 6:bc835d0f686f 141 xaccl[i]=(1);
asyrofi 6:bc835d0f686f 142 wait(0.1);
asyrofi 6:bc835d0f686f 143 yaccl[i]=(1);
asyrofi 6:bc835d0f686f 144 wait(0.1);
asyrofi 6:bc835d0f686f 145 zaccl[i]=(1);
asyrofi 6:bc835d0f686f 146 wait(0.1);
asyrofi 6:bc835d0f686f 147
asyrofi 6:bc835d0f686f 148 //formula
asyrofi 6:bc835d0f686f 149
asyrofi 6:bc835d0f686f 150 totvect[i] = sqrt(((xaccl[i]-xavg)* (xaccl[i]-xavg))+ ((yaccl[i] - yavg)*(yaccl[i] - yavg)) + ((zaccl[i] - zavg)*(zaccl[i] - zavg)));
asyrofi 6:bc835d0f686f 151 totave[i] = (totvect[i] + totvect[i-1]) / 2 ;
asyrofi 6:bc835d0f686f 152 totvel[i]=(totave[i]*0.2);
asyrofi 6:bc835d0f686f 153 //totave1[i]=(totvel[i]+totvel[i-1])/2;
asyrofi 6:bc835d0f686f 154 totdist[i]=(totvect[i]*(0.2*0.2)/2);
asyrofi 6:bc835d0f686f 155 totheight[i]=sqrt((((xaccl[i]-xavg)* (xaccl[i]-xavg))+ ((yaccl[i] - yavg)*(yaccl[i] - yavg)))*(0.2*0.2)/2);
asyrofi 6:bc835d0f686f 156 //totave3[i]=((totheight[i]+totheight[i-1])/2);
asyrofi 6:bc835d0f686f 157 acc=acc+totave[i];
asyrofi 6:bc835d0f686f 158
asyrofi 6:bc835d0f686f 159 uart1.printf("%i\n", (int16_t)totave[i]);
asyrofi 6:bc835d0f686f 160 uart1.printf("%i\n", (int16_t)totvel[i]);
asyrofi 6:bc835d0f686f 161 uart1.printf("%i\n", (int16_t)totdist[i]);
asyrofi 6:bc835d0f686f 162 uart1.printf("%i\n", (int16_t)totheight[i]);
asyrofi 6:bc835d0f686f 163
asyrofi 6:bc835d0f686f 164 wait(0.1);
asyrofi 6:bc835d0f686f 165
asyrofi 6:bc835d0f686f 166 if (steps == 10)
asyrofi 6:bc835d0f686f 167 {
asyrofi 6:bc835d0f686f 168 uart1.printf("capek");
asyrofi 6:bc835d0f686f 169 }
asyrofi 6:bc835d0f686f 170
asyrofi 6:bc835d0f686f 171 else
asyrofi 6:bc835d0f686f 172 {
asyrofi 6:bc835d0f686f 173 //nothing
asyrofi 6:bc835d0f686f 174 }
asyrofi 6:bc835d0f686f 175
asyrofi 6:bc835d0f686f 176 //cal steps
asyrofi 6:bc835d0f686f 177 if (totave[i] > threshold && flag==0)
asyrofi 6:bc835d0f686f 178 {
asyrofi 6:bc835d0f686f 179 steps = steps+1;
asyrofi 6:bc835d0f686f 180 flag=1;
asyrofi 6:bc835d0f686f 181 }
asyrofi 6:bc835d0f686f 182 else if (totave[i] > threshold && flag == 1)
asyrofi 6:bc835d0f686f 183 {
asyrofi 6:bc835d0f686f 184 // do nothing
asyrofi 6:bc835d0f686f 185 }
asyrofi 6:bc835d0f686f 186 if (totave[i] < threshold && flag == 1)
asyrofi 6:bc835d0f686f 187 {flag=0;}
asyrofi 6:bc835d0f686f 188 uart1.printf("%i\n", (int16_t)steps);
asyrofi 6:bc835d0f686f 189
asyrofi 6:bc835d0f686f 190
asyrofi 6:bc835d0f686f 191 }
asyrofi 7:31412349a322 192 */
asyrofi 6:bc835d0f686f 193
asyrofi 2:99e00e9c5035 194 led1 = 1;
asyrofi 3:1749778af065 195 uart1.baud(9600);
asyrofi 2:99e00e9c5035 196 Ticker ticker;
asyrofi 2:99e00e9c5035 197 ticker.attach(periodicCallback, 1);
asyrofi 6:bc835d0f686f 198
asyrofi 4:a57b495be9fa 199
asyrofi 4:a57b495be9fa 200
asyrofi 2:99e00e9c5035 201 DEBUG("Initialising the nRF51822\n\r");
asyrofi 2:99e00e9c5035 202 ble.init();
asyrofi 2:99e00e9c5035 203 ble.onDisconnection(disconnectionCallback);
asyrofi 3:1749778af065 204 ble.onConnection(connectionCallback);
asyrofi 2:99e00e9c5035 205 ble.onDataWritten(onDataWritten);
asyrofi 2:99e00e9c5035 206
asyrofi 2:99e00e9c5035 207 /* setup advertising */
asyrofi 2:99e00e9c5035 208 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
asyrofi 2:99e00e9c5035 209 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
asyrofi 2:99e00e9c5035 210 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
asyrofi 2:99e00e9c5035 211 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
asyrofi 2:99e00e9c5035 212 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
asyrofi 2:99e00e9c5035 213 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
asyrofi 2:99e00e9c5035 214
asyrofi 2:99e00e9c5035 215 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
asyrofi 2:99e00e9c5035 216 ble.startAdvertising();
asyrofi 2:99e00e9c5035 217
asyrofi 2:99e00e9c5035 218 UARTService uartService(ble);
asyrofi 2:99e00e9c5035 219 uartServicePtr = &uartService;
peterswanson87 0:d0adb548714f 220
asyrofi 4:a57b495be9fa 221 while (1)
asyrofi 4:a57b495be9fa 222 {
asyrofi 2:99e00e9c5035 223 ble.waitForEvent();
peterswanson87 0:d0adb548714f 224 wait(0.1);
peterswanson87 0:d0adb548714f 225 accelerometer.getOutput(readings);
asyrofi 5:8242f251bcac 226 uart1.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
asyrofi 6:bc835d0f686f 227 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[0]/*params->data*/, sizeof(readings),false/*bytesRead*/);
asyrofi 6:bc835d0f686f 228 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[1]/*params->data*/, sizeof(readings),false/*bytesRead*/);
asyrofi 6:bc835d0f686f 229 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[2]/*params->data*/, sizeof(readings),false/*bytesRead*/);
asyrofi 6:bc835d0f686f 230
asyrofi 6:bc835d0f686f 231 }
peterswanson87 0:d0adb548714f 232
peterswanson87 0:d0adb548714f 233 }
asyrofi 6:bc835d0f686f 234