base program for tilt measurement
Dependencies: COG4050_ADT7420 ADXL362
Fork of COG4050_adxl355_adxl357-ver2 by
main.cpp@6:45d2393ef468, 2018-08-14 (annotated)
- Committer:
- vtoffoli
- Date:
- Tue Aug 14 06:49:07 2018 +0000
- Revision:
- 6:45d2393ef468
- Parent:
- 5:05af38b6375a
- Child:
- 7:5aaa09c40283
update 14.08.2018
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
APS_Lab | 0:74a0756399ff | 1 | #include "mbed.h" |
vtoffoli | 4:23b53636b576 | 2 | #include <inttypes.h> |
vtoffoli | 2:14dc1ec57f3b | 3 | #include "ADXL355.h" |
vtoffoli | 2:14dc1ec57f3b | 4 | |
vtoffoli | 2:14dc1ec57f3b | 5 | Serial pc(USBTX, USBRX); |
vtoffoli | 2:14dc1ec57f3b | 6 | |
vtoffoli | 2:14dc1ec57f3b | 7 | ADXL355 accl(SPI1_CS0, SPI1_MOSI, SPI1_MISO, SPI1_SCLK); // PMOD port |
vtoffoli | 6:45d2393ef468 | 8 | |
vtoffoli | 2:14dc1ec57f3b | 9 | |
vtoffoli | 6:45d2393ef468 | 10 | float convert(uint32_t data){ |
vtoffoli | 6:45d2393ef468 | 11 | // If a positive value, return it |
vtoffoli | 6:45d2393ef468 | 12 | if ((data & 0x80000) == 0) |
vtoffoli | 6:45d2393ef468 | 13 | { |
vtoffoli | 6:45d2393ef468 | 14 | return float(data); |
vtoffoli | 6:45d2393ef468 | 15 | } |
vtoffoli | 6:45d2393ef468 | 16 | //uint32_t rawValue = data<<(32-nbit); |
vtoffoli | 6:45d2393ef468 | 17 | // Otherwise perform the 2's complement math on the value |
vtoffoli | 6:45d2393ef468 | 18 | return float((~(data - 0x01)) & 0xfffff) * -1; |
vtoffoli | 6:45d2393ef468 | 19 | } |
vtoffoli | 6:45d2393ef468 | 20 | |
vtoffoli | 2:14dc1ec57f3b | 21 | int main(){ |
vtoffoli | 2:14dc1ec57f3b | 22 | pc.baud(9600); |
vtoffoli | 2:14dc1ec57f3b | 23 | pc.printf("SPI ADXL355 and ADXL357 Demo\n"); |
vtoffoli | 2:14dc1ec57f3b | 24 | pc.printf("GET device ID\n"); |
vtoffoli | 4:23b53636b576 | 25 | accl.reset(); |
vtoffoli | 4:23b53636b576 | 26 | uint8_t d; |
vtoffoli | 4:23b53636b576 | 27 | d=accl.read_reg(accl.DEVID_AD); |
vtoffoli | 4:23b53636b576 | 28 | pc.printf("AD id = %x \r\n",d); |
vtoffoli | 4:23b53636b576 | 29 | d=accl.read_reg(accl.DEVID_MST); |
vtoffoli | 4:23b53636b576 | 30 | pc.printf("MEMS id = %x \r\n",d); |
vtoffoli | 4:23b53636b576 | 31 | d=accl.read_reg(accl.PARTID); |
vtoffoli | 4:23b53636b576 | 32 | pc.printf("device id = %x \r\n",d); |
vtoffoli | 4:23b53636b576 | 33 | d=accl.read_reg(accl.REVID); |
vtoffoli | 4:23b53636b576 | 34 | pc.printf("revision id = %x \r\n",d); |
vtoffoli | 4:23b53636b576 | 35 | pc.printf("GET device data [x, y, z, t] \r\n"); |
vtoffoli | 4:23b53636b576 | 36 | accl.set_power_ctl_reg(accl.MEASUREMENT); |
vtoffoli | 4:23b53636b576 | 37 | d=accl.read_reg(accl.POWER_CTL); |
vtoffoli | 4:23b53636b576 | 38 | pc.printf("power control on measurement mode = %x \r\n",d); |
vtoffoli | 6:45d2393ef468 | 39 | float x, y,z; |
vtoffoli | 6:45d2393ef468 | 40 | float t; |
vtoffoli | 2:14dc1ec57f3b | 41 | while(1) { |
vtoffoli | 6:45d2393ef468 | 42 | x = convert(accl.scanx())*accl.axis355_sens; |
vtoffoli | 6:45d2393ef468 | 43 | y = convert(accl.scany())*accl.axis355_sens; |
vtoffoli | 6:45d2393ef468 | 44 | z = convert(accl.scanz())*accl.axis355_sens; |
vtoffoli | 6:45d2393ef468 | 45 | t = 25+float(accl.scant()-1852)/(-9.05); |
vtoffoli | 6:45d2393ef468 | 46 | pc.printf("%f \t %f \t %f \t %f \r\n" , x,y,z,t); |
vtoffoli | 6:45d2393ef468 | 47 | wait(0.50); |
vtoffoli | 2:14dc1ec57f3b | 48 | } |
vtoffoli | 2:14dc1ec57f3b | 49 | } |
vtoffoli | 6:45d2393ef468 | 50 |