Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 1:1df877428dff, committed 2015-01-20
- Comitter:
- giogal
- Date:
- Tue Jan 20 08:13:03 2015 +0000
- Parent:
- 0:03ec17aefbcc
- Child:
- 2:4a0b5f2947d1
- Commit message:
- Ciao imperatore ho effettuato delle modifiche;
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Jan 09 11:11:45 2015 +0000
+++ b/main.cpp Tue Jan 20 08:13:03 2015 +0000
@@ -1,25 +1,12 @@
#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
@@ -29,23 +16,38 @@
#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;
+float conversion(char buf);
+
+//funzione che trasmette ad uno schermo in SPI i valori delle accelerazioni in codice ascii
+void VAL_TO_SPI( char tens, char unit, int pos_unit);
+
+//inizializzazione dell protocollo SPI
+SPI device(MOSI, MISO, SCK); // mosi, miso, clock
+
+float x, y, z;
+
int main(void)
{
I2C i2c(SDA,SCL);
- SPI device(MOSI, MISO, SCK); // mosi, miso, sclk
+
+
+ //trasmettiamo 8 bit alla volta
+ //POL = 0, PHA = 1
device.format(8,1);
+ //frequenza della trasmissione SPI
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;
@@ -56,21 +58,24 @@
char z_buffer;
char y_buffer;
char x_buffer;
+ char x_str[8];
+ char y_str[8];
+ char z_str[8];
+ //inizializziamo il registro di controllo a 0 per poterlo settare
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);
+ //settiamo il registro di controllo
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) {
@@ -84,47 +89,33 @@
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);
-
}
+ x = x*2.551 + 50;
+ sprintf(x_str,"%f",x);
+ VAL_TO_SPI( x_str[0], x_str[1], 134);
+ printf("ciao");
- 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);
- }
+ y = y*2.551 + 50;
+ sprintf(y_str,"%f",y);
+ VAL_TO_SPI( y_str[0], y_str[1], 132);
+
+ z = z*2.551 + 50;
+ sprintf(z_str,"%f",z);
+ VAL_TO_SPI( z_str[0], z_str[1], 130);
+
}
}
@@ -175,3 +166,18 @@
val = (float) val/64;
return val;
}
+
+void VAL_TO_SPI( char tens, char unit, int pos_unit)
+{
+ int pos_tens;
+ pos_tens = pos_unit + 1;
+ DigitalOut (SS,0);
+ device.write(pos_unit);
+ device.write(unit);
+ DigitalOut (SS,1);
+ DigitalOut (SS,0);
+ device.write(pos_tens);
+ device.write(tens);
+ DigitalOut (SS,1);
+ return;
+}