Library for the ADXL362 Accelerometer

Dependents:   mbed_Y2PROJECT_FIRST mbed_protoy2project mbed_savedataproto mbed_menu ... more

Files at this revision

API Documentation at this revision

Comitter:
rmcwilliam101
Date:
Thu Mar 03 13:49:03 2016 +0000
Commit message:
AXDL library

Changed in this revision

ADXL362.cpp Show annotated file Show diff for this revision Revisions of this file
ADXL362.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r d9853774f233 ADXL362.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADXL362.cpp	Thu Mar 03 13:49:03 2016 +0000
@@ -0,0 +1,91 @@
+#include "mbed.h"
+#include "ADXL362.h"
+ 
+// Class
+ 
+ADXL362::ADXL362(PinName mosi, PinName miso, PinName sclk, PinName cbs)
+	: SPI_m(mosi, miso, sclk) 
+	, CBS_m(cbs) {
+	CBS_m=1;
+	}
+	
+// SPI 
+
+void ADXL362::init_spi(){
+	// spi 8 bits, mode 0, 1 MHz for adxl362
+    SPI_m.format(8,0);
+    // 5 MHz, max for acc - works fine
+    SPI_m.frequency(5000000);
+}
+
+
+
+void ADXL362::init_adxl362(){
+	//uint8_t reg;
+	// reset the adxl362
+    wait_ms(200);
+    ACC_WriteReg(RESET, 0x52);
+    wait_ms(200);
+
+    // set FIFO
+    ACC_WriteReg(FIFO_CTL,0x0A);  // stream mode, AH bit
+    //ACC_WriteReg(FIFO_CTL,0x02);  // stream mode, no AH bit
+    //reg = ACC_ReadReg(FIFO_CTL);
+    //pc.printf("FIFO_CTL = 0x%X\r\n", reg);
+
+	// Not used but keep in case it is important to set FIFO parameters.
+    //ACC_WriteReg(FIFO_SAM,SAMPLE_SET * 3);   // fifo depth
+    //reg = ACC_ReadReg(FIFO_SAM);
+    //pc.printf("FIFO_SAM = 0x%X\r\n", reg);
+
+    // set adxl362 to 4g range, 25Hz
+    //ACC_WriteReg(FILTER_CTL,0x51);
+    // 2g, 25Hz
+    ACC_WriteReg(FILTER_CTL,0x11);
+    //reg = ACC_ReadReg(FILTER_CTL);
+    //printf("FILTER_CTL = 0x%X\r\n", reg);
+
+    // map adxl362 interrupts
+    //ACC_WriteReg(INTMAP1,0x01); //data ready
+    ACC_WriteReg(INTMAP1,0x04); //watermark
+    //reg = ACC_ReadReg(INTMAP1);
+    //pc.printf("INTMAP1 = 0x%X\r\n", reg);
+
+    // set adxl362 to measurement mode, ultralow noise
+    ACC_WriteReg(POWER_CTL,0x22);
+    //reg = ACC_ReadReg(POWER_CTL);
+    //pc.printf("POWER_CTL = 0x%X\r\n", reg);
+}
+
+void ADXL362::ACC_GetXYZ8(int8_t* x, int8_t* y, int8_t* z)
+{
+    CBS_m = DOWN;
+    SPI_m.write(RD_SPI);
+    SPI_m.write(0x08);
+
+    *x = SPI_m.write(0x00);
+    *y = SPI_m.write(0x00);
+    *z = SPI_m.write(0x00);
+
+    CBS_m = UP;
+}
+
+
+uint8_t ADXL362::ACC_ReadReg( uint8_t reg )
+{
+    CBS_m = DOWN;
+    SPI_m.write(RD_SPI);
+    SPI_m.write(reg);
+    uint8_t val = SPI_m.write(0x00);
+    CBS_m = UP;
+    return (val);
+}
+
+void ADXL362::ACC_WriteReg( uint8_t reg, uint8_t cmd )
+{
+    CBS_m = DOWN;
+    SPI_m.write(WR_SPI);
+    SPI_m.write(reg);
+    SPI_m.write(cmd);
+    CBS_m = UP;
+}
diff -r 000000000000 -r d9853774f233 ADXL362.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADXL362.h	Thu Mar 03 13:49:03 2016 +0000
@@ -0,0 +1,85 @@
+#ifndef _ADXL362_H_
+#define _ADXL362_H_
+
+#include "mbed.h"
+
+// ACC Registers
+#define ID0 0x00
+#define STATUS 0x0b
+#define FIFO_EL 0x0c
+#define FIFO_EH 0x0d
+#define RESET 0x1f
+#define FIFO_CTL 0x28
+#define FIFO_SAM 0x29
+#define INTMAP1 0x2a
+#define INTMAP2 0x2b
+#define FILTER_CTL 0x2c
+#define POWER_CTL 0x2d
+#define WR_SPI 0x0a
+#define RD_SPI 0x0b
+#define RD_FIFO 0x0d
+#define DOWN 0
+#define UP 1
+#define SAMPLE_SET 128
+
+/*		Class ADXL362: configure and connect to ADXL362 3-axis accelerometer.
+ *		Richard McWilliam
+ *
+ *		Example:
+ *
+ *		#include "mbed.h"
+ *		#include "ADXL362.h"
+ *
+ *		ADXL362 adxl362(p11, p12, p13, p10);  // Accelerometer (mosi, miso, sclk, cs)
+ *
+ *		int main()
+ *		{
+ *		// local variables
+ *		int8_t x8 = 0;
+ *		int8_t y8 = 0;
+ *		int8_t z8 = 0;	
+ *		uint8_t reg;
+ *
+ *		// set up SPI interface
+ *		adxl362.init_spi();
+ *		// Set up accelerometer
+ *		adxl362.init_adxl362();
+ *
+ *		// Check settings
+ *		reg = adxl362.ACC_ReadReg(FILTER_CTL);
+ * 		printf("FILTER_CTL = 0x%X\r\n", reg);
+ *
+ *		adxl362.ACC_GetXYZ8(&x8, &y8, &z8); // Fetch sample from ADXL362
+ *		wait(0.1); // Wait is required in this mode
+ *			
+ *		}
+*/
+class ADXL362 {
+	
+	public:
+		// Set up object for communcation with ADXL362. Pins are mosi, miso, sclk, cs
+		ADXL362(PinName mosi, PinName miso, PinName sclk, PinName cbs);
+		//~ADXL362() {};
+		
+		// Initialise the SPI interface for ADXL362
+		void init_spi();
+		
+		// Initialise ADXL362 in basic capture mode, 8 bit pcakets.
+		void init_adxl362();
+		
+		// Fetch a single set of x,y,z packets indicating acceleration
+		void ACC_GetXYZ8(int8_t* x, int8_t* y, int8_t* z);
+		
+		// Read specified register of ADXL362
+		uint8_t ACC_ReadReg( uint8_t reg );
+		
+		// Write to register of ADXL362
+		void ACC_WriteReg( uint8_t reg, uint8_t cmd );
+	
+	private:
+		SPI SPI_m;
+        DigitalOut CBS_m;		
+
+};
+
+#endif
\ No newline at end of file