ADXL355

Committer:
peng103617
Date:
Thu Aug 06 06:53:20 2020 +0000
Revision:
1:c7e291816833
Parent:
0:df16df7ba53f
ADXL355

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ppcanuck2 0:df16df7ba53f 1 /* Copyright (c) 2019 Analog Devices, Inc. All rights reserved.
ppcanuck2 0:df16df7ba53f 2
ppcanuck2 0:df16df7ba53f 3 Redistribution and use in source and binary forms, with or without modification,
ppcanuck2 0:df16df7ba53f 4 are permitted provided that the following conditions are met:
ppcanuck2 0:df16df7ba53f 5 - Redistributions of source code must retain the above copyright notice,
ppcanuck2 0:df16df7ba53f 6 this list of conditions and the following disclaimer.
ppcanuck2 0:df16df7ba53f 7 - Redistributions in binary form must reproduce the above copyright notice,
ppcanuck2 0:df16df7ba53f 8 this list of conditions and the following disclaimer in the documentation
ppcanuck2 0:df16df7ba53f 9 and/or other materials provided with the distribution.
ppcanuck2 0:df16df7ba53f 10 - Modified versions of the software must be conspicuously marked as such.
ppcanuck2 0:df16df7ba53f 11 - This software is licensed solely and exclusively for use with processors/products
ppcanuck2 0:df16df7ba53f 12 manufactured by or for Analog Devices, Inc.
ppcanuck2 0:df16df7ba53f 13 - This software may not be combined or merged with other code in any manner
ppcanuck2 0:df16df7ba53f 14 that would cause the software to become subject to terms and conditions which
ppcanuck2 0:df16df7ba53f 15 differ from those listed here.
ppcanuck2 0:df16df7ba53f 16 - Neither the name of Analog Devices, Inc. nor the names of its contributors
ppcanuck2 0:df16df7ba53f 17 may be used to endorse or promote products derived from this software without
ppcanuck2 0:df16df7ba53f 18 specific prior written permission.
ppcanuck2 0:df16df7ba53f 19 - The use of this software may or may not infringe the patent rights of one or
ppcanuck2 0:df16df7ba53f 20 more patent holders. This license does not release you from the requirement
ppcanuck2 0:df16df7ba53f 21 that you obtain separate licenses from these patent holders to use this software.
ppcanuck2 0:df16df7ba53f 22
ppcanuck2 0:df16df7ba53f 23 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND
ppcanuck2 0:df16df7ba53f 24 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
ppcanuck2 0:df16df7ba53f 25 TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
ppcanuck2 0:df16df7ba53f 26 NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
ppcanuck2 0:df16df7ba53f 27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES
ppcanuck2 0:df16df7ba53f 28 (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL
ppcanuck2 0:df16df7ba53f 29 PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
ppcanuck2 0:df16df7ba53f 30 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
ppcanuck2 0:df16df7ba53f 31 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
ppcanuck2 0:df16df7ba53f 32 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
ppcanuck2 0:df16df7ba53f 33 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ppcanuck2 0:df16df7ba53f 34
ppcanuck2 0:df16df7ba53f 35 2019-01-10-7CBSD SLA
ppcanuck2 0:df16df7ba53f 36
ppcanuck2 0:df16df7ba53f 37 */
ppcanuck2 0:df16df7ba53f 38
ppcanuck2 0:df16df7ba53f 39 #include "mbed.h"
ppcanuck2 0:df16df7ba53f 40 #include "platform/mbed_thread.h"
ppcanuck2 0:df16df7ba53f 41 #include "ADXL355.h"
ppcanuck2 0:df16df7ba53f 42
ppcanuck2 0:df16df7ba53f 43 /* Global Data */
ppcanuck2 0:df16df7ba53f 44
ppcanuck2 0:df16df7ba53f 45 float LSB_scale_factor=0.0000039f;
ppcanuck2 0:df16df7ba53f 46
ppcanuck2 0:df16df7ba53f 47 int32_t volatile i32SensorX;
ppcanuck2 0:df16df7ba53f 48 int32_t volatile i32SensorY;
ppcanuck2 0:df16df7ba53f 49 int32_t volatile i32SensorZ;
ppcanuck2 0:df16df7ba53f 50 int32_t volatile i32SensorT;
ppcanuck2 0:df16df7ba53f 51
ppcanuck2 0:df16df7ba53f 52 uint8_t volatile ui8SensorX;
ppcanuck2 0:df16df7ba53f 53 uint8_t volatile ui8SensorX2;
ppcanuck2 0:df16df7ba53f 54 uint8_t volatile ui8SensorX3;
ppcanuck2 0:df16df7ba53f 55 uint32_t volatile ui32SensorX;
ppcanuck2 0:df16df7ba53f 56
ppcanuck2 0:df16df7ba53f 57 uint8_t volatile ui8SensorY;
ppcanuck2 0:df16df7ba53f 58 uint8_t volatile ui8SensorY2;
ppcanuck2 0:df16df7ba53f 59 uint8_t volatile ui8SensorY3;
ppcanuck2 0:df16df7ba53f 60 uint32_t volatile ui32SensorY;
ppcanuck2 0:df16df7ba53f 61
ppcanuck2 0:df16df7ba53f 62 uint8_t volatile ui8SensorZ;
ppcanuck2 0:df16df7ba53f 63 uint8_t volatile ui8SensorZ2;
ppcanuck2 0:df16df7ba53f 64 uint8_t volatile ui8SensorZ3;
ppcanuck2 0:df16df7ba53f 65 uint32_t volatile ui32SensorZ;
ppcanuck2 0:df16df7ba53f 66
ppcanuck2 0:df16df7ba53f 67
ppcanuck2 0:df16df7ba53f 68 uint8_t volatile ui8SensorT;
ppcanuck2 0:df16df7ba53f 69 uint8_t volatile ui8SensorT2;
ppcanuck2 0:df16df7ba53f 70 uint32_t volatile ui32SensorT;
ppcanuck2 0:df16df7ba53f 71
ppcanuck2 0:df16df7ba53f 72 uint32_t volatile ui32Result = 0;
ppcanuck2 0:df16df7ba53f 73
ppcanuck2 0:df16df7ba53f 74 // Blinking rate in milliseconds
ppcanuck2 0:df16df7ba53f 75 #define SLEEP_TIME 500
ppcanuck2 0:df16df7ba53f 76 // Setup the SPI peripheral to talk to the ADXL35x
peng103617 1:c7e291816833 77 //SPI spi(D11, D12, D13);
peng103617 1:c7e291816833 78 // initialize chip select
peng103617 1:c7e291816833 79 //DigitalOut cs(D10);
peng103617 1:c7e291816833 80
peng103617 1:c7e291816833 81 SPI spi(PB_15, PB_14, PB_13);
ppcanuck2 0:df16df7ba53f 82 // initialize chip select
peng103617 1:c7e291816833 83 DigitalOut cs(PB_1);
peng103617 1:c7e291816833 84
peng103617 1:c7e291816833 85
peng103617 1:c7e291816833 86
peng103617 1:c7e291816833 87
ppcanuck2 0:df16df7ba53f 88
ppcanuck2 0:df16df7ba53f 89 // Initialise the digital pin LED1 as an output
ppcanuck2 0:df16df7ba53f 90 DigitalOut led1(LED1);
ppcanuck2 0:df16df7ba53f 91 // Initialise the serial object with TX and RX pins
ppcanuck2 0:df16df7ba53f 92 Serial pc(USBTX, USBRX);
peng103617 1:c7e291816833 93 Timer t;
ppcanuck2 0:df16df7ba53f 94 int id=0;
peng103617 1:c7e291816833 95 int i=1;
ppcanuck2 0:df16df7ba53f 96
ppcanuck2 0:df16df7ba53f 97 // main() runs in its own thread in the OS
ppcanuck2 0:df16df7ba53f 98 int main()
ppcanuck2 0:df16df7ba53f 99 {
ppcanuck2 0:df16df7ba53f 100 pc.printf("Hello World!");
peng103617 1:c7e291816833 101 pc.baud (115200) ;
ppcanuck2 0:df16df7ba53f 102
ppcanuck2 0:df16df7ba53f 103 spi.frequency(1000000);
ppcanuck2 0:df16df7ba53f 104
ppcanuck2 0:df16df7ba53f 105 cs=0;
ppcanuck2 0:df16df7ba53f 106 spi.write(DEVID_AD<<1|0x01);
ppcanuck2 0:df16df7ba53f 107 id=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 108 cs=1;
ppcanuck2 0:df16df7ba53f 109 pc.printf("ID register = 0x%X\n", id);
ppcanuck2 0:df16df7ba53f 110 // This should output the ID register as 0xAD
ppcanuck2 0:df16df7ba53f 111
ppcanuck2 0:df16df7ba53f 112 cs=0;
ppcanuck2 0:df16df7ba53f 113 spi.write(DEVID_MST<<1|0x01);
ppcanuck2 0:df16df7ba53f 114 id=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 115 cs=1;
ppcanuck2 0:df16df7ba53f 116 pc.printf("DEVID register = 0x%X\n", id);
ppcanuck2 0:df16df7ba53f 117 // This should output the ID register as 0x1D
ppcanuck2 0:df16df7ba53f 118
ppcanuck2 0:df16df7ba53f 119 cs=0;
ppcanuck2 0:df16df7ba53f 120 spi.write(PARTID<<1|0x01);
ppcanuck2 0:df16df7ba53f 121 id=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 122 cs=1;
ppcanuck2 0:df16df7ba53f 123 pc.printf("PARTID register = 0x%X\n", id);
ppcanuck2 0:df16df7ba53f 124 // This should output the ID register as 0xED
ppcanuck2 0:df16df7ba53f 125
ppcanuck2 0:df16df7ba53f 126 cs=0;
ppcanuck2 0:df16df7ba53f 127 spi.write(REVID<<1|0x01);
ppcanuck2 0:df16df7ba53f 128 id=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 129 cs=1;
ppcanuck2 0:df16df7ba53f 130 pc.printf("REVID register = 0x%X\n", id);
ppcanuck2 0:df16df7ba53f 131 // This should output the ID register as 0x01
ppcanuck2 0:df16df7ba53f 132
ppcanuck2 0:df16df7ba53f 133 // Turn on the sensor
ppcanuck2 0:df16df7ba53f 134 ADXL355_Start_Sensor();
ppcanuck2 0:df16df7ba53f 135
ppcanuck2 0:df16df7ba53f 136 // Now dump the temp and accelerometer data to the port
ppcanuck2 0:df16df7ba53f 137
ppcanuck2 0:df16df7ba53f 138 while (true) {
peng103617 1:c7e291816833 139 t.start();
peng103617 1:c7e291816833 140
ppcanuck2 0:df16df7ba53f 141 // Blink LED and wait 500 ms
peng103617 1:c7e291816833 142 //led1 = !led1;
peng103617 1:c7e291816833 143 //thread_sleep_for(SLEEP_TIME);
ppcanuck2 0:df16df7ba53f 144
ppcanuck2 0:df16df7ba53f 145 //the acceleration data is 20 bits and needs to come in via three register transactions
ppcanuck2 0:df16df7ba53f 146 cs=0;
ppcanuck2 0:df16df7ba53f 147 spi.write(XDATA3<<1|0x01);
ppcanuck2 0:df16df7ba53f 148 ui8SensorX=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 149 ui8SensorX2=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 150 ui8SensorX3=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 151 cs=1;
ppcanuck2 0:df16df7ba53f 152 // Put it back together
ppcanuck2 0:df16df7ba53f 153
ppcanuck2 0:df16df7ba53f 154 ui32Result = ((ui8SensorX << 16) | (ui8SensorX2 << 8) | ui8SensorX3); /* Set read result*/
ppcanuck2 0:df16df7ba53f 155 //pc.printf("X acceleration data = 0x%X\n", ui32Result);
ppcanuck2 0:df16df7ba53f 156 // This should output the ID register as 0x01
ppcanuck2 0:df16df7ba53f 157 i32SensorX = ADXL355_Acceleration_Data_Conversion(ui32Result);
ppcanuck2 0:df16df7ba53f 158 //pc.printf("X acceleration formatted data = %d\n", i32SensorX);
peng103617 1:c7e291816833 159 // pc.printf("%2.6f \n\r", i32SensorX*LSB_scale_factor);
ppcanuck2 0:df16df7ba53f 160
ppcanuck2 0:df16df7ba53f 161 //the acceleration data is 20 bits and needs to come in via three register transactions
ppcanuck2 0:df16df7ba53f 162 cs=0;
ppcanuck2 0:df16df7ba53f 163 spi.write(YDATA3<<1|0x01);
ppcanuck2 0:df16df7ba53f 164 ui8SensorY=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 165 ui8SensorY2=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 166 ui8SensorY3=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 167 cs=1;
ppcanuck2 0:df16df7ba53f 168 // Put it back together
ppcanuck2 0:df16df7ba53f 169
ppcanuck2 0:df16df7ba53f 170 ui32Result = ((ui8SensorY << 16) | (ui8SensorY2 << 8) | ui8SensorY3); /* Set read result*/
ppcanuck2 0:df16df7ba53f 171 //pc.printf("Y acceleration data = 0x%X\n", ui32Result);
ppcanuck2 0:df16df7ba53f 172 // This should output the ID register as 0x01
ppcanuck2 0:df16df7ba53f 173 i32SensorY = ADXL355_Acceleration_Data_Conversion(ui32Result);
ppcanuck2 0:df16df7ba53f 174 //pc.printf("Y acceleration formatted data = %d\n", i32SensorY);
peng103617 1:c7e291816833 175 // pc.printf("%2.6f \n\r", i32SensorY*LSB_scale_factor);
peng103617 1:c7e291816833 176
peng103617 1:c7e291816833 177
ppcanuck2 0:df16df7ba53f 178
ppcanuck2 0:df16df7ba53f 179
ppcanuck2 0:df16df7ba53f 180 //the acceleration data is 20 bits and needs to come in via three register transactions
ppcanuck2 0:df16df7ba53f 181 cs=0;
ppcanuck2 0:df16df7ba53f 182 spi.write(ZDATA3<<1|0x01);
ppcanuck2 0:df16df7ba53f 183 ui8SensorZ=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 184 ui8SensorZ2=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 185 ui8SensorZ3=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 186 cs=1;
ppcanuck2 0:df16df7ba53f 187 // Put it back together
ppcanuck2 0:df16df7ba53f 188
peng103617 1:c7e291816833 189
peng103617 1:c7e291816833 190
peng103617 1:c7e291816833 191
peng103617 1:c7e291816833 192
ppcanuck2 0:df16df7ba53f 193 ui32Result = ((ui8SensorZ << 16) | (ui8SensorZ2 << 8) | ui8SensorZ3); /* Set read result*/
ppcanuck2 0:df16df7ba53f 194 //pc.printf("Z acceleration data = 0x%X\n", ui32Result);
ppcanuck2 0:df16df7ba53f 195 // This should output the ID register as 0x01
ppcanuck2 0:df16df7ba53f 196 i32SensorZ = ADXL355_Acceleration_Data_Conversion(ui32Result);
ppcanuck2 0:df16df7ba53f 197 //pc.printf("Z acceleration formatted data = %d \n", i32SensorZ);
peng103617 1:c7e291816833 198 // pc.printf("%2.6f \n\r", i32SensorZ*LSB_scale_factor);
peng103617 1:c7e291816833 199
peng103617 1:c7e291816833 200
peng103617 1:c7e291816833 201
ppcanuck2 0:df16df7ba53f 202
peng103617 1:c7e291816833 203
peng103617 1:c7e291816833 204 t.stop();
peng103617 1:c7e291816833 205 //pc.printf(" %d us\n\r", t.read_us()); wait1.5ms
peng103617 1:c7e291816833 206
peng103617 1:c7e291816833 207 wait(0.01); //max0.0016
peng103617 1:c7e291816833 208
peng103617 1:c7e291816833 209
peng103617 1:c7e291816833 210
ppcanuck2 0:df16df7ba53f 211 }
ppcanuck2 0:df16df7ba53f 212 }
ppcanuck2 0:df16df7ba53f 213 int32_t ADXL355_Acceleration_Data_Conversion (uint32_t ui32SensorData)
ppcanuck2 0:df16df7ba53f 214 {
ppcanuck2 0:df16df7ba53f 215 int32_t volatile i32Conversion = 0;
ppcanuck2 0:df16df7ba53f 216
ppcanuck2 0:df16df7ba53f 217 ui32SensorData = (ui32SensorData >> 4);
ppcanuck2 0:df16df7ba53f 218 ui32SensorData = (ui32SensorData & 0x000FFFFF);
ppcanuck2 0:df16df7ba53f 219
ppcanuck2 0:df16df7ba53f 220 if((ui32SensorData & 0x00080000) == 0x00080000){
ppcanuck2 0:df16df7ba53f 221
ppcanuck2 0:df16df7ba53f 222 i32Conversion = (ui32SensorData | 0xFFF00000);
ppcanuck2 0:df16df7ba53f 223
ppcanuck2 0:df16df7ba53f 224 }
ppcanuck2 0:df16df7ba53f 225 else{
ppcanuck2 0:df16df7ba53f 226 i32Conversion = ui32SensorData;
ppcanuck2 0:df16df7ba53f 227 }
ppcanuck2 0:df16df7ba53f 228
ppcanuck2 0:df16df7ba53f 229 return i32Conversion;
ppcanuck2 0:df16df7ba53f 230 }
ppcanuck2 0:df16df7ba53f 231
ppcanuck2 0:df16df7ba53f 232 void ADXL355_Start_Sensor(void)
ppcanuck2 0:df16df7ba53f 233 {
ppcanuck2 0:df16df7ba53f 234 cs=0;
ppcanuck2 0:df16df7ba53f 235 spi.write(POWER_CTL<<1|0x01);
ppcanuck2 0:df16df7ba53f 236 id=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 237 cs=1;
ppcanuck2 0:df16df7ba53f 238 pc.printf("POWER_CTL register = 0x%X\n", id);
ppcanuck2 0:df16df7ba53f 239
ppcanuck2 0:df16df7ba53f 240 id = id & 0xFE; // Clear measurement bit in POWER_CTL register and turn it on
ppcanuck2 0:df16df7ba53f 241
ppcanuck2 0:df16df7ba53f 242 // Write the power control register, which involves add a '1' to the 8th bit
ppcanuck2 0:df16df7ba53f 243 // address for the ADXL355
ppcanuck2 0:df16df7ba53f 244
ppcanuck2 0:df16df7ba53f 245 int volatile tmp=(POWER_CTL<<1);
ppcanuck2 0:df16df7ba53f 246
ppcanuck2 0:df16df7ba53f 247 cs=0;
ppcanuck2 0:df16df7ba53f 248 spi.write(tmp);
ppcanuck2 0:df16df7ba53f 249 spi.write(id);
ppcanuck2 0:df16df7ba53f 250 cs=1;
ppcanuck2 0:df16df7ba53f 251
ppcanuck2 0:df16df7ba53f 252
ppcanuck2 0:df16df7ba53f 253 //SPI_Write(POWER_CTL, ui8temp, 0x00, SPI_WRITE_ONE_REG); /* Write the new value to POWER_CTL register */
ppcanuck2 0:df16df7ba53f 254 }
ppcanuck2 0:df16df7ba53f 255
ppcanuck2 0:df16df7ba53f 256