Simple program to send ADXL345 accelerometer readings to a PC/Laptop over serial port. Also wrote an ADXL345 library
ADXL345sb/ADXL345sb.cpp@0:48c465d803f7, 2012-05-12 (annotated)
- Committer:
- simonbarker
- Date:
- Sat May 12 16:16:37 2012 +0000
- Revision:
- 0:48c465d803f7
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simonbarker | 0:48c465d803f7 | 1 | #include "ADXL345sb.h" |
simonbarker | 0:48c465d803f7 | 2 | |
simonbarker | 0:48c465d803f7 | 3 | ADXL345sb::ADXL345sb(PinName mosi, PinName miso, PinName sck, PinName cs) : spi_(mosi, miso, sck), nCS_(cs) { |
simonbarker | 0:48c465d803f7 | 4 | |
simonbarker | 0:48c465d803f7 | 5 | nCS_ = 1; |
simonbarker | 0:48c465d803f7 | 6 | |
simonbarker | 0:48c465d803f7 | 7 | spi_.frequency(1000000); |
simonbarker | 0:48c465d803f7 | 8 | spi_.format(8,3); |
simonbarker | 0:48c465d803f7 | 9 | |
simonbarker | 0:48c465d803f7 | 10 | wait_us(500); |
simonbarker | 0:48c465d803f7 | 11 | |
simonbarker | 0:48c465d803f7 | 12 | } |
simonbarker | 0:48c465d803f7 | 13 | |
simonbarker | 0:48c465d803f7 | 14 | int ADXL345sb::adxlread(int address){ |
simonbarker | 0:48c465d803f7 | 15 | int d; |
simonbarker | 0:48c465d803f7 | 16 | //address MSB must be one as it's a read |
simonbarker | 0:48c465d803f7 | 17 | address = address | 0x80; |
simonbarker | 0:48c465d803f7 | 18 | nCS_ = 0; |
simonbarker | 0:48c465d803f7 | 19 | spi_.write(address); |
simonbarker | 0:48c465d803f7 | 20 | d = spi_.write(0x00); |
simonbarker | 0:48c465d803f7 | 21 | nCS_ = 1; |
simonbarker | 0:48c465d803f7 | 22 | return d; |
simonbarker | 0:48c465d803f7 | 23 | } |
simonbarker | 0:48c465d803f7 | 24 | void ADXL345sb::adxlmultibyteread(int address, int numOfBytes, unsigned char *buf){ |
simonbarker | 0:48c465d803f7 | 25 | |
simonbarker | 0:48c465d803f7 | 26 | //address MSB must be one as it's a read |
simonbarker | 0:48c465d803f7 | 27 | address = address | 0xC0; |
simonbarker | 0:48c465d803f7 | 28 | nCS_ = 0; |
simonbarker | 0:48c465d803f7 | 29 | spi_.write(address); |
simonbarker | 0:48c465d803f7 | 30 | for(int i = 0; i < numOfBytes; i++) |
simonbarker | 0:48c465d803f7 | 31 | buf[i] = spi_.write(0x00); |
simonbarker | 0:48c465d803f7 | 32 | nCS_ = 1; |
simonbarker | 0:48c465d803f7 | 33 | } |
simonbarker | 0:48c465d803f7 | 34 | void ADXL345sb::adxlreadXYZ(float *buf){ |
simonbarker | 0:48c465d803f7 | 35 | //address MSB must be one as it's a read |
simonbarker | 0:48c465d803f7 | 36 | int address = DATAX0 | 0xC0; |
simonbarker | 0:48c465d803f7 | 37 | //set up response array and temp storeage vars |
simonbarker | 0:48c465d803f7 | 38 | unsigned char response[6]; |
simonbarker | 0:48c465d803f7 | 39 | int16_t x = 0, y = 0, z = 0; //this need to be in a 16bit int as the adxl is 10 bit res with MSB signed |
simonbarker | 0:48c465d803f7 | 40 | |
simonbarker | 0:48c465d803f7 | 41 | nCS_ = 0; |
simonbarker | 0:48c465d803f7 | 42 | spi_.write(address); |
simonbarker | 0:48c465d803f7 | 43 | for(int i = 0; i < 6; i++) |
simonbarker | 0:48c465d803f7 | 44 | response[i] = spi_.write(0x00); |
simonbarker | 0:48c465d803f7 | 45 | nCS_ = 1; |
simonbarker | 0:48c465d803f7 | 46 | |
simonbarker | 0:48c465d803f7 | 47 | x = (response[1]<<8)|response[0]; |
simonbarker | 0:48c465d803f7 | 48 | y = (response[3]<<8)|response[2]; |
simonbarker | 0:48c465d803f7 | 49 | z = (response[5]<<8)|response[4]; |
simonbarker | 0:48c465d803f7 | 50 | |
simonbarker | 0:48c465d803f7 | 51 | buf[0] = x * 0.0078; |
simonbarker | 0:48c465d803f7 | 52 | buf[1] = y * 0.0078; |
simonbarker | 0:48c465d803f7 | 53 | buf[2] = z * 0.0078; |
simonbarker | 0:48c465d803f7 | 54 | } |
simonbarker | 0:48c465d803f7 | 55 | void ADXL345sb::adxlwrite(int address, int data){ |
simonbarker | 0:48c465d803f7 | 56 | nCS_ = 0; |
simonbarker | 0:48c465d803f7 | 57 | |
simonbarker | 0:48c465d803f7 | 58 | spi_.write(address); |
simonbarker | 0:48c465d803f7 | 59 | spi_.write(data); |
simonbarker | 0:48c465d803f7 | 60 | |
simonbarker | 0:48c465d803f7 | 61 | nCS_ = 1; |
simonbarker | 0:48c465d803f7 | 62 | } |