LED

Dependencies:   mbed

Fork of LED2 by Charlie Bailey

Files at this revision

API Documentation at this revision

Comitter:
cbailey1994
Date:
Thu Oct 29 11:14:56 2015 +0000
Commit message:
LED;

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
BAILEY_CWS.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADXL362.cpp	Thu Oct 29 11:14:56 2015 +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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADXL362.h	Thu Oct 29 11:14:56 2015 +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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BAILEY_CWS.cpp	Thu Oct 29 11:14:56 2015 +0000
@@ -0,0 +1,235 @@
+/*Charlie Bailey
+ Hild Bede*/
+
+#include "mbed.h"
+#include "ADXL362.h"
+/* This program fetches samples from the AXDL362 and prints the readings in various formats. */
+
+/* Serial device declaration */
+Serial pc(USBTX,USBRX); // serial tx, rx
+ADXL362 adxl362(p11, p12, p13, p10);  /* Accelerometer (mosi, miso, sclk, cs) */
+DigitalOut led1(LED1); /* LED1 */
+DigitalOut led2(LED2); /* LED2 */
+DigitalOut led3(LED3); /* LED3 */
+DigitalOut led4(LED4); /* LED4 */
+
+/* Main loop */
+int main()
+{
+
+float meanx=0, meany=0, meanz=0, stddevx=0, stddevy=0, stddevz=0, sumvarx=0, sumvary=0, sumvarz=0, variancex=0, variancey=0, variancez =0;
+float xval[100]; /* array to store readings */
+float yval[100];
+float zval[100];
+int8_t xdata, ydata, zdata;
+float max_valx, max_valy, max_valz, min_valx, min_valy, min_valz, sumx, sumy, sumz;
+int N=10;
+float T=0.1;
+float L=60;
+int i;
+	int user_cmd; /* User command is stored in this variable */
+
+	pc.printf("\n\nThis program analyses readings from an accelerometer.");
+	
+	adxl362.init_spi();/* set up SPI interface */
+	adxl362.init_adxl362();/* Set up accelerometer */
+	wait(0.1);/* wait 100ms for accelerometer to initialise */
+	
+	while(1) 
+	
+	{ 
+		
+		/* Print a simple menu */ 
+		pc.printf("\nMenu for anaysing accelerometer readings\n"); 
+		pc.printf("1: Take Readings\n"); 
+		pc.printf("2: Display maximum and minimum values\n"); 
+		pc.printf("3: Display mean values\n"); 
+		pc.printf("4: Display standard deviation of values\n"); 
+		pc.printf("5: Change sample rate \n"); 
+		pc.printf("6: Illuminate LEDs when acceleration exceeds your chosen value \n"); 
+		pc.printf("7: Exit Program\n"); 
+		pc.printf("Command: "); 
+		pc.scanf("%i",&user_cmd); /* Get command from PuTTY terminal */
+		
+		/* Process user command */ 
+		switch(user_cmd) 
+		{ 
+			case(1):
+					
+				pc.printf("Chose number of readings: "); 
+				pc.scanf("%i",&N); /*takes user input */
+				pc.printf("Taking %i readings...\n",N);
+				
+				for (i=0;i<100;i=i+1) /* clears all arrays */
+				{
+				xval[i]=0;
+				yval[i]=0;
+				zval[i]=0;
+				}
+				{
+				max_valx=-1000;
+				max_valy=-1000;
+				max_valz=-1000;
+				min_valx=1000;
+				min_valy=1000;
+				min_valz=1000;
+				sumx=0;
+				sumy=0;
+				sumz=0;
+				}
+				for(i=0;i<N;i=i+1)
+				{ 
+					{
+					adxl362.ACC_GetXYZ8(&xdata, &ydata, &zdata);/* fetch readings */
+					xval[i]=float(xdata); /*store x reading */
+					yval[i]=float(ydata); /*store y reading */
+					zval[i]=float(zdata); /*store z reading */
+					
+					pc.printf("Reading no. %i\t\t x= %i\t\t y= %i\t\t z= %i\n",i+1 ,xdata, ydata, zdata);/* print X-reading */
+					
+					if(max_valx<xval[i])max_valx=xdata;/* update x maximum if necessary */
+					if(max_valy<yval[i])max_valy=ydata;/* update y maximum if necessary */						
+					if(max_valz<zval[i])max_valz=zdata;/* update z maximum if necessary */
+						
+					if(min_valx>xval[i])min_valx=xdata;/* update x minimum if necessary */
+					if(min_valy>yval[i])min_valy=ydata;/* update y minimum if necessary */
+					if(min_valz>zval[i])min_valz=zdata;/* update z minimum if necessary */
+					
+					{
+					sumx = sumx + xval[i];		/* sums all values */
+					sumy = sumy + yval[i];					
+					sumz = sumz + zval[i];
+					}
+					
+					{
+					meanx = sumx/N; /*calcualtes means of values*/
+					meany = sumy/N;
+					meanz = sumz/N;
+					}
+					
+								
+					
+					wait(T);/* wait some period of time before next sample */
+					
+					}
+				}
+					
+					for(i=0;i<N;i=i+1) /* sums to allow calculation of standard deviation later */
+					{
+					sumvarx = sumvarx + (xval[i]-meanx)*(xval[i]-meanx) ;
+					sumvary = sumvary + (yval[i]-meany)*(yval[i]-meany) ; 					
+					sumvarz = sumvarz + (zval[i]-meanz)*(zval[i]-meanz) ; 			
+					}
+				 
+				
+				
+				break;
+			case(2): 
+				pc.printf("Maximum and minimum values are:\n"); /* prints maximum and minimum x and y values*/
+				
+				{ 
+					pc.printf("Maximum x reading was %+04f\n",max_valx);/* print max x result */
+					pc.printf("Maximum y reading was %+04f\n",max_valy);/* print max y result */
+					pc.printf("Maximum z reading was %+04f\n",max_valz);/* print max z result */
+					pc.printf("Minimum x reading was %+04f\n",min_valx);/* print min x result */
+					pc.printf("Minimum y reading was %+04f\n",min_valy);/* print min y result */
+					pc.printf("Minimum z reading was %+04f\n",min_valz);/* print min z result */
+				} 
+				break;
+				
+			case(3): 
+				pc.printf("Mean values are:\n"); /* prints mean values */
+				{ 
+					pc.printf("Mean of x values was %+04f\n",meanx);/* print mean of x values */
+					pc.printf("Mean of y values was %+04f\n",meany);/* print mean of y values */
+					pc.printf("Mean of z values was %+04f\n",meanz);/* print mean of z values */	
+				} 
+				break;
+			case(4): 
+				pc.printf("Calculating standard deviation...\n"); /* calcuates standard deviation */
+					{
+					variancex = sumvarx/(N-1);
+					variancey = sumvary/(N-1);
+					variancez = sumvarz/(N-1);
+					}
+					
+					{
+					stddevx = sqrt( variancex );
+					stddevy = sqrt( variancey );
+					stddevz = sqrt( variancez );
+					}
+				
+				{ 
+				pc.printf("Standard deviation of x values: %+04f\n",stddevx);/* print mean of x values */
+				pc.printf("Standard deviation of y values: %+04f\n",stddevy);/* print mean of y values */
+				pc.printf("Standard deviation of z values: %+04f\n",stddevz);/* print mean of z values */			
+				} 
+				
+				break;
+				
+			case(5): 
+				pc.printf("Choose sample rate (readings/s), T: \n"); /*allows user to change sample rate*/
+				pc.scanf("%f",&T); /*takes users input*/
+				pc.printf("You chose to take one sample every %f seconds\n",T);
+				break;
+				
+			case(6): 
+				pc.printf("Chose threshold value:\n"); /*allows user to change threshold value*/
+				pc.scanf("%f",&L);
+				pc.printf("You chose %f as your threshold acceleration. \n",L);
+				
+				while (2)
+				{
+					adxl362.ACC_GetXYZ8(&xdata, &ydata, &zdata);/* fetch readings */
+					/* Set LEDs depending upon a threshold value of acceleration */ 
+					if(xdata>=L) 
+					{ 
+						led1=1; 
+					} 
+					else 
+					{
+					led1=0; 
+					}
+					if(ydata>=L) 
+					{ 
+						led2=1; 
+					} 
+					else 
+					{
+					led2=0; 
+					}
+					if(zdata>=L) 
+					{ 
+						led3=1; 
+					} 
+					else 
+					{
+					led3=0; 
+					}
+					wait(T);/* wait some period of time before next sample */
+				}
+				
+				
+				
+				break;
+				
+			case(7): 
+				
+				pc.printf("Program stopping...\n"); 
+				return(0); /* Return from main, ending the program */ 
+				break;
+				
+			default: 
+				pc.printf("Error, invalid command...\n"); 
+		} 
+	}
+
+
+return(0);/* normal exit */
+
+	}
+	
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.lib	Thu Oct 29 11:14:56 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/gokmenascioglu/code/mbed/#a8fa94490a0a