Progetto SDI / Mbed 2 deprecated lab3_SPI

Dependencies:   MMA8451Q mbed

Files at this revision

API Documentation at this revision

Comitter:
Thierro
Date:
Fri Jan 09 11:11:45 2015 +0000
Child:
1:1df877428dff
Commit message:
SPI 9/1/2014

Changed in this revision

MMA8451Q.lib 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA8451Q.lib	Fri Jan 09 11:11:45 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/emilmont/code/MMA8451Q/#c4d879a39775
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Jan 09 11:11:45 2015 +0000
@@ -0,0 +1,177 @@
+#include "mbed.h"
+#include "MMA8451Q.h"
+#if   defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
+  PinName const SDA = PTE25;
+  PinName const SCL = PTE24;
+  #else
+  #error TARGET NOT DEFINED
+#endif
+/*
+#if   defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
+  PinName const SDA = PTE25;
+  PinName const SCL = PTE24;
+#elif defined (TARGET_KL05Z)
+  PinName const SDA = PTB4;
+  PinName const SCL = PTB3;
+#elif defined (TARGET_K20D50M)
+  PinName const SDA = PTB1;
+  PinName const SCL = PTB0;
+#else
+  #error TARGET NOT DEFINED
+#endif
+*/
+
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+#define CNTRL_REG_1 0x2A
+#define CNTRL_REG_2 0x2B
+#define X_acc 0x01
+#define Y_acc 0x03
+#define Z_acc 0x05
+#define STATUS 0x00
+
+float conversion(char buf);
+PinName const MOSI = PTD2;
+PinName const MISO = PTD3;
+PinName const SCK = PTD1;
+PinName const SS = PTD0;
+
+int main(void)
+{
+    
+    I2C i2c(SDA,SCL);
+    SPI device(MOSI, MISO, SCK); // mosi, miso, sclk
+    device.format(8,1);
+    device.frequency(500000);
+    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
+    
+    int address = MMA8451_I2C_ADDRESS;
+    float x, y, z;
+    char stato=STATUS;
+    char addr_reg2=CNTRL_REG_2;
+    char data_stato;
+    char stato_reg2;
+    const char X_addr=X_acc;
+    const char Y_addr=Y_acc;
+    const char Z_addr=Z_acc;
+    char z_buffer;
+    char y_buffer;
+    char x_buffer;
+   
+    char data[2] = {CNTRL_REG_1, 0x00};
+    i2c.write(address, data, 2);
+    
+     i2c.write(address,&addr_reg2,1,true);
+       i2c.read(address,&stato_reg2,1,false);
+    
+    char data_reg2[2] = {CNTRL_REG_2, (stato_reg2 |4)};
+    i2c.write(address, data_reg2, 2);
+    
+    char data_reg1[2]={CNTRL_REG_1, 0xFB};
+    i2c.write(address,data_reg1, 2);
+    PwmOut rled(LED1);
+    PwmOut gled(LED2);
+    PwmOut bled(LED3);
+
+    while (true) {
+       
+       i2c.write(address,&stato,1,true);
+       i2c.read(address,&data_stato,1,false);
+       
+       if(data_stato & 8 == 8)
+       {
+        z = 0;
+        x = 0;
+        y = 0;
+       i2c.write(address,&Z_addr,1,true);
+       i2c.read(address,&z_buffer,1,false);
+       printf("z=%d\n\r", z_buffer);
+       
+       
+       i2c.write(address,&X_addr,1,true);
+       i2c.read(address,&x_buffer,1,false);
+       printf("x=%d\n\r", x_buffer);
+       
+       
+       i2c.write(address,&Y_addr,1,true);
+       i2c.read(address,&y_buffer,1,false);
+       printf("y=%d\n\r", y_buffer);
+       
+       printf("\n\r");
+       x = conversion(x_buffer);
+       printf("X= %f\n\r",x);
+       y = conversion(y_buffer);
+       printf("Y= %f\n\r",y);
+       z = conversion(z_buffer);
+       printf("Z= %f\n\r",z);
+       printf("\n\r");
+       rled = 1.0f - abs(x);
+       gled = 1.0f - abs(y);
+       bled = 1.0f - abs(z);
+
+    
+       }
+       
+        char zeta[8];
+       z = -1.62830;
+       sprintf(zeta,"%lf",z);
+       int i;
+       for(i=0; i<8; i++)
+       {
+           DigitalOut  (SS,0);
+            device.write(i);
+            //device.write(85);
+            DigitalOut  (SS,1);
+            DigitalOut  (SS,0);
+            device.write(zeta[i]);
+            DigitalOut  (SS,1);
+        }
+    }
+}
+
+  
+float conversion(char buf)
+{
+    float val=0;
+    if( (buf/128) >= 1)
+       {
+            buf -= 128; 
+            val -= 128;  
+        }
+       
+       if( (buf/64) >= 1)
+       {
+            buf-= 64;
+            val += 64;
+       }
+       if( (buf/32) >= 1)
+       {
+            buf -=32;
+            val += 32;
+       }
+       if( (buf/16) >= 1)
+       {
+            buf -=16;
+            val += 16;
+       }
+       if( (buf/8) >= 1)
+       {
+            buf -=8;
+            val += 8;
+       }
+       if( (buf/4) >= 1)
+       {
+            buf -= 4;
+            val += 4;
+       }
+       if( (buf/2) >= 1)
+       {
+            buf -= 2;
+            val += 2;
+       }
+       if( (buf/1) >= 1)
+       {
+            val+= 1;
+       }
+    val = (float) val/64;
+    return val;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Jan 09 11:11:45 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5
\ No newline at end of file