180622 HJM : 10 Count sensing data RF send 180718 HJM : 8 Count sensing data RF send, certTest, Temperature(cold) test
Fork of ADXL362 by
ADXL362.cpp@2:dd9450370643, 2018-07-18 (annotated)
- Committer:
- jmhong
- Date:
- Wed Jul 18 05:43:56 2018 +0000
- Revision:
- 2:dd9450370643
- Parent:
- 1:bf56b783747e
cubebite hjm 180718
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rmcwilliam101 | 0:d9853774f233 | 1 | #include "mbed.h" |
rmcwilliam101 | 0:d9853774f233 | 2 | #include "ADXL362.h" |
jmhong | 1:bf56b783747e | 3 | #include "main.h" |
rmcwilliam101 | 0:d9853774f233 | 4 | |
rmcwilliam101 | 0:d9853774f233 | 5 | // Class |
rmcwilliam101 | 0:d9853774f233 | 6 | |
rmcwilliam101 | 0:d9853774f233 | 7 | ADXL362::ADXL362(PinName mosi, PinName miso, PinName sclk, PinName cbs) |
jmhong | 1:bf56b783747e | 8 | : SPI_m(mosi, miso, sclk) |
jmhong | 1:bf56b783747e | 9 | , CBS_m(cbs) { |
jmhong | 1:bf56b783747e | 10 | CBS_m=1; |
jmhong | 1:bf56b783747e | 11 | } |
jmhong | 1:bf56b783747e | 12 | |
rmcwilliam101 | 0:d9853774f233 | 13 | // SPI |
rmcwilliam101 | 0:d9853774f233 | 14 | |
rmcwilliam101 | 0:d9853774f233 | 15 | void ADXL362::init_spi(){ |
jmhong | 1:bf56b783747e | 16 | // spi 8 bits, mode 0, 1 MHz for adxl362 |
rmcwilliam101 | 0:d9853774f233 | 17 | SPI_m.format(8,0); |
rmcwilliam101 | 0:d9853774f233 | 18 | // 5 MHz, max for acc - works fine |
rmcwilliam101 | 0:d9853774f233 | 19 | SPI_m.frequency(5000000); |
rmcwilliam101 | 0:d9853774f233 | 20 | } |
rmcwilliam101 | 0:d9853774f233 | 21 | |
rmcwilliam101 | 0:d9853774f233 | 22 | |
rmcwilliam101 | 0:d9853774f233 | 23 | |
jmhong | 1:bf56b783747e | 24 | void ADXL362::init_adxl362() |
jmhong | 1:bf56b783747e | 25 | { |
jmhong | 1:bf56b783747e | 26 | uint8_t reg; |
jmhong | 1:bf56b783747e | 27 | // reset the adxl362 |
rmcwilliam101 | 0:d9853774f233 | 28 | wait_ms(200); |
rmcwilliam101 | 0:d9853774f233 | 29 | ACC_WriteReg(RESET, 0x52); |
rmcwilliam101 | 0:d9853774f233 | 30 | wait_ms(200); |
rmcwilliam101 | 0:d9853774f233 | 31 | |
rmcwilliam101 | 0:d9853774f233 | 32 | // set FIFO |
rmcwilliam101 | 0:d9853774f233 | 33 | ACC_WriteReg(FIFO_CTL,0x0A); // stream mode, AH bit |
jmhong | 1:bf56b783747e | 34 | |
jmhong | 1:bf56b783747e | 35 | /////////////////////////////////////////////////////////////////////////////////////// |
jmhong | 1:bf56b783747e | 36 | reg = ACC_ReadReg(FILTER_CTL); |
jmhong | 2:dd9450370643 | 37 | printf("[Vibration] Init start"); |
jmhong | 2:dd9450370643 | 38 | // printf("1 ------------------Start : FILTER_CTL = 0x%X\r\n\n", reg); |
jmhong | 1:bf56b783747e | 39 | |
jmhong | 1:bf56b783747e | 40 | |
jmhong | 1:bf56b783747e | 41 | // printf("2 ------------------Input : FILTER_CTL = 0x17\r\n"); |
jmhong | 1:bf56b783747e | 42 | wait_ms(25); |
jmhong | 1:bf56b783747e | 43 | |
jmhong | 1:bf56b783747e | 44 | /////////////////////////////////////////////////////////////////////////////////////// |
jmhong | 1:bf56b783747e | 45 | |
rmcwilliam101 | 0:d9853774f233 | 46 | //ACC_WriteReg(FIFO_CTL,0x02); // stream mode, no AH bit |
rmcwilliam101 | 0:d9853774f233 | 47 | //reg = ACC_ReadReg(FIFO_CTL); |
jmhong | 1:bf56b783747e | 48 | //printf("FIFO_CTL = 0x%X\r\n", reg); |
rmcwilliam101 | 0:d9853774f233 | 49 | |
jmhong | 1:bf56b783747e | 50 | // Not used but keep in case it is important to set FIFO parameters. |
rmcwilliam101 | 0:d9853774f233 | 51 | //ACC_WriteReg(FIFO_SAM,SAMPLE_SET * 3); // fifo depth |
rmcwilliam101 | 0:d9853774f233 | 52 | //reg = ACC_ReadReg(FIFO_SAM); |
rmcwilliam101 | 0:d9853774f233 | 53 | //pc.printf("FIFO_SAM = 0x%X\r\n", reg); |
rmcwilliam101 | 0:d9853774f233 | 54 | |
jmhong | 1:bf56b783747e | 55 | |
jmhong | 2:dd9450370643 | 56 | // printf("2 ------------------Connect accelerometer"); |
rmcwilliam101 | 0:d9853774f233 | 57 | |
jmhong | 1:bf56b783747e | 58 | while(reg != 0x17) |
jmhong | 1:bf56b783747e | 59 | { |
jmhong | 2:dd9450370643 | 60 | // printf(" vib while...\n"); |
jmhong | 1:bf56b783747e | 61 | |
jmhong | 1:bf56b783747e | 62 | // set adxl362 to 4g range, 25Hz |
jmhong | 1:bf56b783747e | 63 | //ACC_WriteReg(FILTER_CTL,0x51); |
jmhong | 1:bf56b783747e | 64 | // 2g, 25Hz |
jmhong | 1:bf56b783747e | 65 | ACC_WriteReg(FILTER_CTL,0x17); |
jmhong | 1:bf56b783747e | 66 | //reg = ACC_ReadReg(FILTER_CTL); |
jmhong | 1:bf56b783747e | 67 | //printf("FILTER_CTL = 0x%X\r\n", reg); |
jmhong | 1:bf56b783747e | 68 | |
jmhong | 1:bf56b783747e | 69 | reg = ACC_ReadReg(FILTER_CTL); |
jmhong | 1:bf56b783747e | 70 | |
jmhong | 1:bf56b783747e | 71 | if(reg == 0x17) |
jmhong | 1:bf56b783747e | 72 | { |
jmhong | 2:dd9450370643 | 73 | // printf(" Success!!\n\n"); |
jmhong | 1:bf56b783747e | 74 | // printf("2 ------------------Output : FILTER_CTL = 0x%X\r\n\n\n", reg); |
jmhong | 1:bf56b783747e | 75 | } |
jmhong | 1:bf56b783747e | 76 | else |
jmhong | 1:bf56b783747e | 77 | { |
jmhong | 1:bf56b783747e | 78 | printf("."); |
jmhong | 1:bf56b783747e | 79 | } |
jmhong | 1:bf56b783747e | 80 | wait_ms(500); |
jmhong | 1:bf56b783747e | 81 | } |
jmhong | 1:bf56b783747e | 82 | |
rmcwilliam101 | 0:d9853774f233 | 83 | // map adxl362 interrupts |
rmcwilliam101 | 0:d9853774f233 | 84 | //ACC_WriteReg(INTMAP1,0x01); //data ready |
rmcwilliam101 | 0:d9853774f233 | 85 | ACC_WriteReg(INTMAP1,0x04); //watermark |
rmcwilliam101 | 0:d9853774f233 | 86 | //reg = ACC_ReadReg(INTMAP1); |
rmcwilliam101 | 0:d9853774f233 | 87 | //pc.printf("INTMAP1 = 0x%X\r\n", reg); |
rmcwilliam101 | 0:d9853774f233 | 88 | |
rmcwilliam101 | 0:d9853774f233 | 89 | // set adxl362 to measurement mode, ultralow noise |
rmcwilliam101 | 0:d9853774f233 | 90 | ACC_WriteReg(POWER_CTL,0x22); |
rmcwilliam101 | 0:d9853774f233 | 91 | //reg = ACC_ReadReg(POWER_CTL); |
rmcwilliam101 | 0:d9853774f233 | 92 | //pc.printf("POWER_CTL = 0x%X\r\n", reg); |
jmhong | 2:dd9450370643 | 93 | printf("(OK)\n"); |
rmcwilliam101 | 0:d9853774f233 | 94 | } |
rmcwilliam101 | 0:d9853774f233 | 95 | |
jmhong | 1:bf56b783747e | 96 | void ADXL362::ACC_GetXYZ16(int16_t* x16, int16_t* y16, int16_t* z16) |
rmcwilliam101 | 0:d9853774f233 | 97 | { |
jmhong | 1:bf56b783747e | 98 | int8_t x8=0; |
jmhong | 1:bf56b783747e | 99 | int8_t y8=0; |
jmhong | 1:bf56b783747e | 100 | int8_t z8=0; |
jmhong | 1:bf56b783747e | 101 | |
rmcwilliam101 | 0:d9853774f233 | 102 | CBS_m = DOWN; |
rmcwilliam101 | 0:d9853774f233 | 103 | SPI_m.write(RD_SPI); |
jmhong | 1:bf56b783747e | 104 | |
jmhong | 1:bf56b783747e | 105 | SPI_m.write(0x0E); |
jmhong | 1:bf56b783747e | 106 | x8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 107 | *x16 = x8 << 0; |
jmhong | 1:bf56b783747e | 108 | x8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 109 | // *x16 |= x8 << 8; |
jmhong | 1:bf56b783747e | 110 | *x16 = x8 << 8; |
jmhong | 1:bf56b783747e | 111 | printf("[X Hex value]\n"); |
jmhong | 1:bf56b783747e | 112 | printf("1. x8 : [%02X]\n", x8); |
jmhong | 1:bf56b783747e | 113 | printf("2. *x16 : [%04X]\n", *x16); |
jmhong | 1:bf56b783747e | 114 | |
jmhong | 1:bf56b783747e | 115 | printf("[X Decimal value]\n"); |
jmhong | 1:bf56b783747e | 116 | printf("1. x8 : [%d]\n", x8); |
jmhong | 1:bf56b783747e | 117 | printf("2. *x16 : [%d]\n", *x16); |
rmcwilliam101 | 0:d9853774f233 | 118 | |
jmhong | 1:bf56b783747e | 119 | y8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 120 | *y16 = y8 << 0; |
jmhong | 1:bf56b783747e | 121 | y8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 122 | // *y16 |= y8 << 8; |
jmhong | 1:bf56b783747e | 123 | *y16 = y8 << 8; |
jmhong | 1:bf56b783747e | 124 | printf("\n[Y Hex value]\n"); |
jmhong | 1:bf56b783747e | 125 | printf("1. y8 : [%02X]\n", y8); |
jmhong | 1:bf56b783747e | 126 | printf("2. *y16 : [%04X]\n", *y16); |
rmcwilliam101 | 0:d9853774f233 | 127 | |
jmhong | 1:bf56b783747e | 128 | z8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 129 | *z16 = z8 << 0; |
jmhong | 1:bf56b783747e | 130 | z8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 131 | // *z16 |= z8 << 8; |
jmhong | 1:bf56b783747e | 132 | *z16 = z8 << 8; |
jmhong | 1:bf56b783747e | 133 | printf("\n[Z Hex value]\n"); |
jmhong | 1:bf56b783747e | 134 | printf("1. z8 : [%02X]\n", z8); |
jmhong | 1:bf56b783747e | 135 | printf("2. *z16 : [%04X]\n", *z16); |
jmhong | 1:bf56b783747e | 136 | |
jmhong | 1:bf56b783747e | 137 | CBS_m = UP; |
rmcwilliam101 | 0:d9853774f233 | 138 | } |
rmcwilliam101 | 0:d9853774f233 | 139 | |
jmhong | 1:bf56b783747e | 140 | void ADXL362::ACC_GetXYZ8(int8_t* x8, int8_t* y8, int8_t* z8) |
jmhong | 1:bf56b783747e | 141 | { |
jmhong | 1:bf56b783747e | 142 | |
jmhong | 1:bf56b783747e | 143 | CBS_m = DOWN; |
jmhong | 1:bf56b783747e | 144 | SPI_m.write(RD_SPI); |
jmhong | 1:bf56b783747e | 145 | //-> 레지스터 주소바꿈 |
jmhong | 1:bf56b783747e | 146 | SPI_m.write(0x08); |
jmhong | 1:bf56b783747e | 147 | |
jmhong | 1:bf56b783747e | 148 | //-> 인자에 가속도 값 바로저장 |
jmhong | 1:bf56b783747e | 149 | *x8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 150 | *y8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 151 | *z8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 152 | |
jmhong | 1:bf56b783747e | 153 | CBS_m = UP; |
jmhong | 1:bf56b783747e | 154 | } |
rmcwilliam101 | 0:d9853774f233 | 155 | |
rmcwilliam101 | 0:d9853774f233 | 156 | uint8_t ADXL362::ACC_ReadReg( uint8_t reg ) |
rmcwilliam101 | 0:d9853774f233 | 157 | { |
rmcwilliam101 | 0:d9853774f233 | 158 | CBS_m = DOWN; |
rmcwilliam101 | 0:d9853774f233 | 159 | SPI_m.write(RD_SPI); |
rmcwilliam101 | 0:d9853774f233 | 160 | SPI_m.write(reg); |
rmcwilliam101 | 0:d9853774f233 | 161 | uint8_t val = SPI_m.write(0x00); |
rmcwilliam101 | 0:d9853774f233 | 162 | CBS_m = UP; |
rmcwilliam101 | 0:d9853774f233 | 163 | return (val); |
rmcwilliam101 | 0:d9853774f233 | 164 | } |
rmcwilliam101 | 0:d9853774f233 | 165 | |
rmcwilliam101 | 0:d9853774f233 | 166 | void ADXL362::ACC_WriteReg( uint8_t reg, uint8_t cmd ) |
rmcwilliam101 | 0:d9853774f233 | 167 | { |
rmcwilliam101 | 0:d9853774f233 | 168 | CBS_m = DOWN; |
rmcwilliam101 | 0:d9853774f233 | 169 | SPI_m.write(WR_SPI); |
rmcwilliam101 | 0:d9853774f233 | 170 | SPI_m.write(reg); |
rmcwilliam101 | 0:d9853774f233 | 171 | SPI_m.write(cmd); |
rmcwilliam101 | 0:d9853774f233 | 172 | CBS_m = UP; |
rmcwilliam101 | 0:d9853774f233 | 173 | } |