Simple program to send ADXL345 accelerometer readings to a PC/Laptop over serial port. Also wrote an ADXL345 library

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
simonbarker
Date:
Sat May 12 16:16:37 2012 +0000
Commit message:

Changed in this revision

ADXL345sb/ADXL345sb.cpp Show annotated file Show diff for this revision Revisions of this file
ADXL345sb/ADXL345sb.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 48c465d803f7 ADXL345sb/ADXL345sb.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADXL345sb/ADXL345sb.cpp	Sat May 12 16:16:37 2012 +0000
@@ -0,0 +1,62 @@
+#include "ADXL345sb.h"
+
+ADXL345sb::ADXL345sb(PinName mosi, PinName miso, PinName sck, PinName cs) : spi_(mosi, miso, sck), nCS_(cs) {
+
+    nCS_ = 1;
+    
+    spi_.frequency(1000000);
+    spi_.format(8,3);
+
+    wait_us(500);
+
+}
+
+int ADXL345sb::adxlread(int address){
+    int d;
+    //address MSB must be one as it's a read
+    address = address | 0x80;
+    nCS_ = 0;
+    spi_.write(address);
+    d = spi_.write(0x00);
+    nCS_ = 1;   
+    return d;
+}
+void ADXL345sb::adxlmultibyteread(int address, int numOfBytes, unsigned char *buf){
+
+    //address MSB must be one as it's a read
+    address = address | 0xC0;
+    nCS_ = 0;
+    spi_.write(address);
+    for(int i = 0; i < numOfBytes; i++)
+        buf[i] = spi_.write(0x00);
+    nCS_ = 1;   
+}
+void ADXL345sb::adxlreadXYZ(float *buf){
+     //address MSB must be one as it's a read
+    int address = DATAX0 | 0xC0;
+    //set up response array and temp storeage vars
+    unsigned char response[6];
+    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
+    
+    nCS_ = 0;
+    spi_.write(address);
+    for(int i = 0; i < 6; i++)
+        response[i] = spi_.write(0x00);
+    nCS_ = 1;
+    
+    x = (response[1]<<8)|response[0];
+    y = (response[3]<<8)|response[2];
+    z = (response[5]<<8)|response[4];
+    
+    buf[0] = x * 0.0078;
+    buf[1] = y * 0.0078;
+    buf[2] = z * 0.0078;
+}
+void ADXL345sb::adxlwrite(int address, int data){
+    nCS_ = 0;
+    
+    spi_.write(address);
+    spi_.write(data);
+    
+    nCS_ = 1;
+}
\ No newline at end of file
diff -r 000000000000 -r 48c465d803f7 ADXL345sb/ADXL345sb.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADXL345sb/ADXL345sb.h	Sat May 12 16:16:37 2012 +0000
@@ -0,0 +1,51 @@
+#ifndef ADXL345sb_H
+#define ADXL345sb_H
+
+#include "mbed.h"
+
+//register codes
+#define DEVID           0x00
+#define THRESH_TAP      0x1D
+#define OFSX            0x1E
+#define OFSY            0x1F
+#define OFSZ            0x20
+#define DUR             0x21
+#define LATENT          0x22
+#define WINDOW          0x23
+#define THRESH_ACT      0x24
+#define THRESH_INACT    0x25
+#define TIME_INACT      0x26
+#define ACT_INACT_CTL   0x27
+#define THRESH_FF       0x28
+#define TIME_FF         0x29
+#define TAP_AXES        0x2A
+#define ACT_TAP_STATUS  0x2B
+#define BW_RATE         0x2C
+#define POWER_CTL       0x2D
+#define INT_ENABLE      0x2E
+#define INT_MAP         0x2F
+#define INT_SOURCE      0x30
+#define DATA_FORMAT     0x31
+#define DATAX0          0x32
+#define DATAX1          0x33
+#define DATAY0          0x34
+#define DATAY1          0x35
+#define DATAZ0          0x36
+#define DATAZ1          0x37
+#define FIFO_CTL        0x38
+#define FIFO_STATUS     0x39
+
+class ADXL345sb{
+
+public:
+    ADXL345sb(PinName mosi, PinName miso, PinName sck, PinName cs);
+    int adxlread(int address);
+    void adxlmultibyteread(int address, int numOfBytes, unsigned char *buf);
+    void adxlreadXYZ(float *buf);
+    void adxlwrite(int address, int data);
+
+private:
+    SPI        spi_;
+    DigitalOut nCS_;
+};
+#endif
\ No newline at end of file
diff -r 000000000000 -r 48c465d803f7 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat May 12 16:16:37 2012 +0000
@@ -0,0 +1,34 @@
+#include "mbed.h"
+#include "ADXL345sb.h"
+
+DigitalOut myled(LED1);
+ADXL345sb accel(p5, p6, p7, p8); // mosi, miso, sclk, ncs
+
+Serial pc(USBTX, USBRX);         // tx, rx
+
+int main() {
+
+    float readings[3];
+    
+    while(1) {       
+        wait(0.1);
+       
+        accel.adxlwrite(DATA_FORMAT,0x01);
+        accel.adxlwrite(POWER_CTL ,0x08);
+        
+        accel.adxlreadXYZ(readings);
+        
+        float angleX = acos(readings[0]);
+        float angleY = acos(readings[1]);
+        float angleZ = acos(readings[2]);
+        
+        if(isnan(angleX))
+            angleX = 0;
+        if(isnan(angleY))
+            angleY = 0;
+        if(isnan(angleZ))
+            angleZ = 0;
+               
+        printf("%f,%f,%f,\r\n",angleX,angleY,angleZ);
+    }
+}
diff -r 000000000000 -r 48c465d803f7 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat May 12 16:16:37 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/737756e0b479