180622 HJM : 10 Count sensing data RF send
Fork of ADXL362 by
ADXL362.cpp@1:bf56b783747e, 2018-06-22 (annotated)
- Committer:
- jmhong
- Date:
- Fri Jun 22 03:41:47 2018 +0000
- Revision:
- 1:bf56b783747e
- Parent:
- 0:d9853774f233
180622 HJM : 10 Count sensing data RF send
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 | 1:bf56b783747e | 37 | printf("1 ------------------Start : FILTER_CTL = 0x%X\r\n\n", reg); |
jmhong | 1:bf56b783747e | 38 | |
jmhong | 1:bf56b783747e | 39 | |
jmhong | 1:bf56b783747e | 40 | // printf("2 ------------------Input : FILTER_CTL = 0x17\r\n"); |
jmhong | 1:bf56b783747e | 41 | wait_ms(25); |
jmhong | 1:bf56b783747e | 42 | |
jmhong | 1:bf56b783747e | 43 | /////////////////////////////////////////////////////////////////////////////////////// |
jmhong | 1:bf56b783747e | 44 | |
rmcwilliam101 | 0:d9853774f233 | 45 | //ACC_WriteReg(FIFO_CTL,0x02); // stream mode, no AH bit |
rmcwilliam101 | 0:d9853774f233 | 46 | //reg = ACC_ReadReg(FIFO_CTL); |
jmhong | 1:bf56b783747e | 47 | //printf("FIFO_CTL = 0x%X\r\n", reg); |
rmcwilliam101 | 0:d9853774f233 | 48 | |
jmhong | 1:bf56b783747e | 49 | // Not used but keep in case it is important to set FIFO parameters. |
rmcwilliam101 | 0:d9853774f233 | 50 | //ACC_WriteReg(FIFO_SAM,SAMPLE_SET * 3); // fifo depth |
rmcwilliam101 | 0:d9853774f233 | 51 | //reg = ACC_ReadReg(FIFO_SAM); |
rmcwilliam101 | 0:d9853774f233 | 52 | //pc.printf("FIFO_SAM = 0x%X\r\n", reg); |
rmcwilliam101 | 0:d9853774f233 | 53 | |
jmhong | 1:bf56b783747e | 54 | |
jmhong | 1:bf56b783747e | 55 | printf("2 ------------------Connect accelerometer"); |
rmcwilliam101 | 0:d9853774f233 | 56 | |
jmhong | 1:bf56b783747e | 57 | while(reg != 0x17) |
jmhong | 1:bf56b783747e | 58 | { |
jmhong | 1:bf56b783747e | 59 | printf(" vib while...\n"); |
jmhong | 1:bf56b783747e | 60 | |
jmhong | 1:bf56b783747e | 61 | // set adxl362 to 4g range, 25Hz |
jmhong | 1:bf56b783747e | 62 | //ACC_WriteReg(FILTER_CTL,0x51); |
jmhong | 1:bf56b783747e | 63 | // 2g, 25Hz |
jmhong | 1:bf56b783747e | 64 | ACC_WriteReg(FILTER_CTL,0x17); |
jmhong | 1:bf56b783747e | 65 | //reg = ACC_ReadReg(FILTER_CTL); |
jmhong | 1:bf56b783747e | 66 | //printf("FILTER_CTL = 0x%X\r\n", reg); |
jmhong | 1:bf56b783747e | 67 | |
jmhong | 1:bf56b783747e | 68 | reg = ACC_ReadReg(FILTER_CTL); |
jmhong | 1:bf56b783747e | 69 | |
jmhong | 1:bf56b783747e | 70 | if(reg == 0x17) |
jmhong | 1:bf56b783747e | 71 | { |
jmhong | 1:bf56b783747e | 72 | printf(" Success!!\n\n"); |
jmhong | 1:bf56b783747e | 73 | // printf("2 ------------------Output : FILTER_CTL = 0x%X\r\n\n\n", reg); |
jmhong | 1:bf56b783747e | 74 | } |
jmhong | 1:bf56b783747e | 75 | else |
jmhong | 1:bf56b783747e | 76 | { |
jmhong | 1:bf56b783747e | 77 | printf("."); |
jmhong | 1:bf56b783747e | 78 | } |
jmhong | 1:bf56b783747e | 79 | wait_ms(500); |
jmhong | 1:bf56b783747e | 80 | } |
jmhong | 1:bf56b783747e | 81 | |
rmcwilliam101 | 0:d9853774f233 | 82 | // map adxl362 interrupts |
rmcwilliam101 | 0:d9853774f233 | 83 | //ACC_WriteReg(INTMAP1,0x01); //data ready |
rmcwilliam101 | 0:d9853774f233 | 84 | ACC_WriteReg(INTMAP1,0x04); //watermark |
rmcwilliam101 | 0:d9853774f233 | 85 | //reg = ACC_ReadReg(INTMAP1); |
rmcwilliam101 | 0:d9853774f233 | 86 | //pc.printf("INTMAP1 = 0x%X\r\n", reg); |
rmcwilliam101 | 0:d9853774f233 | 87 | |
rmcwilliam101 | 0:d9853774f233 | 88 | // set adxl362 to measurement mode, ultralow noise |
rmcwilliam101 | 0:d9853774f233 | 89 | ACC_WriteReg(POWER_CTL,0x22); |
rmcwilliam101 | 0:d9853774f233 | 90 | //reg = ACC_ReadReg(POWER_CTL); |
rmcwilliam101 | 0:d9853774f233 | 91 | //pc.printf("POWER_CTL = 0x%X\r\n", reg); |
rmcwilliam101 | 0:d9853774f233 | 92 | } |
rmcwilliam101 | 0:d9853774f233 | 93 | |
jmhong | 1:bf56b783747e | 94 | void ADXL362::ACC_GetXYZ16(int16_t* x16, int16_t* y16, int16_t* z16) |
rmcwilliam101 | 0:d9853774f233 | 95 | { |
jmhong | 1:bf56b783747e | 96 | int8_t x8=0; |
jmhong | 1:bf56b783747e | 97 | int8_t y8=0; |
jmhong | 1:bf56b783747e | 98 | int8_t z8=0; |
jmhong | 1:bf56b783747e | 99 | |
rmcwilliam101 | 0:d9853774f233 | 100 | CBS_m = DOWN; |
rmcwilliam101 | 0:d9853774f233 | 101 | SPI_m.write(RD_SPI); |
jmhong | 1:bf56b783747e | 102 | |
jmhong | 1:bf56b783747e | 103 | SPI_m.write(0x0E); |
jmhong | 1:bf56b783747e | 104 | x8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 105 | *x16 = x8 << 0; |
jmhong | 1:bf56b783747e | 106 | x8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 107 | // *x16 |= x8 << 8; |
jmhong | 1:bf56b783747e | 108 | *x16 = x8 << 8; |
jmhong | 1:bf56b783747e | 109 | printf("[X Hex value]\n"); |
jmhong | 1:bf56b783747e | 110 | printf("1. x8 : [%02X]\n", x8); |
jmhong | 1:bf56b783747e | 111 | printf("2. *x16 : [%04X]\n", *x16); |
jmhong | 1:bf56b783747e | 112 | |
jmhong | 1:bf56b783747e | 113 | printf("[X Decimal value]\n"); |
jmhong | 1:bf56b783747e | 114 | printf("1. x8 : [%d]\n", x8); |
jmhong | 1:bf56b783747e | 115 | printf("2. *x16 : [%d]\n", *x16); |
rmcwilliam101 | 0:d9853774f233 | 116 | |
jmhong | 1:bf56b783747e | 117 | y8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 118 | *y16 = y8 << 0; |
jmhong | 1:bf56b783747e | 119 | y8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 120 | // *y16 |= y8 << 8; |
jmhong | 1:bf56b783747e | 121 | *y16 = y8 << 8; |
jmhong | 1:bf56b783747e | 122 | printf("\n[Y Hex value]\n"); |
jmhong | 1:bf56b783747e | 123 | printf("1. y8 : [%02X]\n", y8); |
jmhong | 1:bf56b783747e | 124 | printf("2. *y16 : [%04X]\n", *y16); |
rmcwilliam101 | 0:d9853774f233 | 125 | |
jmhong | 1:bf56b783747e | 126 | z8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 127 | *z16 = z8 << 0; |
jmhong | 1:bf56b783747e | 128 | z8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 129 | // *z16 |= z8 << 8; |
jmhong | 1:bf56b783747e | 130 | *z16 = z8 << 8; |
jmhong | 1:bf56b783747e | 131 | printf("\n[Z Hex value]\n"); |
jmhong | 1:bf56b783747e | 132 | printf("1. z8 : [%02X]\n", z8); |
jmhong | 1:bf56b783747e | 133 | printf("2. *z16 : [%04X]\n", *z16); |
jmhong | 1:bf56b783747e | 134 | |
jmhong | 1:bf56b783747e | 135 | CBS_m = UP; |
rmcwilliam101 | 0:d9853774f233 | 136 | } |
rmcwilliam101 | 0:d9853774f233 | 137 | |
jmhong | 1:bf56b783747e | 138 | void ADXL362::ACC_GetXYZ8(int8_t* x8, int8_t* y8, int8_t* z8) |
jmhong | 1:bf56b783747e | 139 | { |
jmhong | 1:bf56b783747e | 140 | |
jmhong | 1:bf56b783747e | 141 | CBS_m = DOWN; |
jmhong | 1:bf56b783747e | 142 | SPI_m.write(RD_SPI); |
jmhong | 1:bf56b783747e | 143 | //-> 레지스터 주소바꿈 |
jmhong | 1:bf56b783747e | 144 | SPI_m.write(0x08); |
jmhong | 1:bf56b783747e | 145 | |
jmhong | 1:bf56b783747e | 146 | //-> 인자에 가속도 값 바로저장 |
jmhong | 1:bf56b783747e | 147 | *x8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 148 | *y8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 149 | *z8 = SPI_m.write(0x00); |
jmhong | 1:bf56b783747e | 150 | |
jmhong | 1:bf56b783747e | 151 | CBS_m = UP; |
jmhong | 1:bf56b783747e | 152 | } |
rmcwilliam101 | 0:d9853774f233 | 153 | |
rmcwilliam101 | 0:d9853774f233 | 154 | uint8_t ADXL362::ACC_ReadReg( uint8_t reg ) |
rmcwilliam101 | 0:d9853774f233 | 155 | { |
rmcwilliam101 | 0:d9853774f233 | 156 | CBS_m = DOWN; |
rmcwilliam101 | 0:d9853774f233 | 157 | SPI_m.write(RD_SPI); |
rmcwilliam101 | 0:d9853774f233 | 158 | SPI_m.write(reg); |
rmcwilliam101 | 0:d9853774f233 | 159 | uint8_t val = SPI_m.write(0x00); |
rmcwilliam101 | 0:d9853774f233 | 160 | CBS_m = UP; |
rmcwilliam101 | 0:d9853774f233 | 161 | return (val); |
rmcwilliam101 | 0:d9853774f233 | 162 | } |
rmcwilliam101 | 0:d9853774f233 | 163 | |
rmcwilliam101 | 0:d9853774f233 | 164 | void ADXL362::ACC_WriteReg( uint8_t reg, uint8_t cmd ) |
rmcwilliam101 | 0:d9853774f233 | 165 | { |
rmcwilliam101 | 0:d9853774f233 | 166 | CBS_m = DOWN; |
rmcwilliam101 | 0:d9853774f233 | 167 | SPI_m.write(WR_SPI); |
rmcwilliam101 | 0:d9853774f233 | 168 | SPI_m.write(reg); |
rmcwilliam101 | 0:d9853774f233 | 169 | SPI_m.write(cmd); |
rmcwilliam101 | 0:d9853774f233 | 170 | CBS_m = UP; |
rmcwilliam101 | 0:d9853774f233 | 171 | } |