Amanda Travieso
/
SerialComms
Export for Dan
Diff: main.cpp
- Revision:
- 6:aa3219001d18
- Parent:
- 5:14a21c3812ed
- Child:
- 7:79a10204cb3b
diff -r 14a21c3812ed -r aa3219001d18 main.cpp --- a/main.cpp Thu Oct 29 23:04:14 2015 +0000 +++ b/main.cpp Fri Oct 30 18:51:47 2015 +0000 @@ -3,6 +3,7 @@ #include "mbed.h" #include "MODSERIAL.h" #include "HIH_5030.h" +#include "ADXL345.h" #define MESSAGE_BUFFER_SIZE 100 @@ -15,6 +16,7 @@ //AnalogIn ain_UseC(p17); //AnalogIn ain_UseD(p18); //AnalogIn ain_UseE(p19); +ADXL345 accelerometer(p5, p6, p7, p8); MODSERIAL messageSystem(p13, p14); @@ -213,6 +215,52 @@ return (float)(ain_UseA.read_u16()) * 3.3 / 65536; } +void ADXL345::getOutput(int* readings){ + + char buffer[6]; + + multiByteRead(ADXL345_DATAX0_REG, buffer, 6); + + readings[0] = (int)buffer[1] << 8 | (int)buffer[0]; + readings[1] = (int)buffer[3] << 8 | (int)buffer[2]; + readings[2] = (int)buffer[5] << 8 | (int)buffer[4]; + +} + +void ADXL345::multiByteRead(int startAddress, char* buffer, int size) { + + int tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F)); + + nCS_ = 0; + //Send address to start reading from. + spi_.write(tx); + + for (int i = 0; i < size; i++) { + buffer[i] = spi_.write(0x00); + } + + nCS_ = 1; + +} + +//ACCELEROMETER + +ADXL345::ADXL345(PinName mosi, + PinName miso, + PinName sck, + PinName cs) : spi_(mosi, miso, sck), nCS_(cs) { + + //2MHz, allowing us to use the fastest data rates. + spi_.frequency(2000000); + spi_.format(8,3); + + nCS_ = 1; + + wait_us(500); + +} + +//ACCELEROMETER int main() { @@ -246,7 +294,38 @@ wait(2.0); led3 = 0; wait(2.0); + + ///ACCELEROMETER + + int readings[3] = {0, 0, 0}; + + // messageSystem.printf("Starting ADXL345 test...\n"); + // messageSystem.printf("Device ID is: 0x%02x\n", accelerometer.getDevId()); + + //Go into standby mode to configure the device. + // accelerometer.setPowerControl(0x00); + + //Full resolution, +/-16g, 4mg/LSB. + // accelerometer.setDataFormatControl(0x0B); + + //3.2kHz data rate. Changed from 3.2k to 25Hz + // accelerometer.setDataRate(ADXL345_25HZ); + + //Measurement mode. + // accelerometer.setPowerControl(0x08); + + wait(0.1); + + accelerometer.getOutput(readings); + + //13-bit, sign extended values. + messageSystem.printf("X:%i, Y:%i, Z:%i\r\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]); + led4 = !led4; + + ///ACCELEROMETER } + + if (messageReceived) { //led2 = !led2;