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:adb624063b97, committed 2015-01-20
- Comitter:
- giogal
- Date:
- Tue Jan 20 08:17:10 2015 +0000
- Parent:
- 0:ef6b25a481a5
- Child:
- 2:620019419136
- Commit message:
- boh
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Dec 19 10:38:18 2014 +0000
+++ b/main.cpp Tue Jan 20 08:17:10 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
@@ -28,40 +15,53 @@
#define Z_acc 0x05
#define STATUS 0x00
+//funzione di conversione dell'accelerazione da un numero in CA2 al valore espresso in m/s
+//viene richiamata per le accelerazioni su ognuno dei tre assi
float conversion(char buf);
int main(void)
{
-
+ //Classe I2C presente nella libreria mbed
I2C i2c(SDA,SCL);
-
+
+ //Inizializzazione dell'accelerometro configurando pin e l'indirizzo
MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
+ int address = MMA8451_I2C_ADDRESS;
- int address = MMA8451_I2C_ADDRESS;
+ //variabili che ci servono nel programma
float x, y, z;
char stato=STATUS;
char data_stato;
+
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;
-
+
+ //inizializziamo il registro di controllo a 0 per poterlo settare
char data[2] = {CNTRL_REG_1, 0x00};
i2c.write(address, data, 2);
+ //settiamo il registro di controllo
char data_wr_2[2]={CNTRL_REG_1, 0x3B};
i2c.write(address,data_wr_2, 2);
+
+
PwmOut rled(LED1);
PwmOut gled(LED2);
PwmOut bled(LED3);
while (true) {
+ //Si legge l'indirizzo di stato nell'accelerometro per vedere se ha
+ //convertito nuovi valori di accelerazione
i2c.write(address,&stato,1,true);
i2c.read(address,&data_stato,1,false);
+ //Se sono presenti nuovi valori
if(data_stato & 8 == 8)
{
z = 0;
@@ -69,27 +69,28 @@
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");
+ //Richiamiamo la funzione di conversione per esprimere i
+ //valori letti in float
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);
+
+ //Regolo l'accensione dei led per verificare la presenza di accelerazioni
+ rled = 1.0f - abs(x);
gled = 1.0f - abs(y);
bled = 1.0f - abs(z);
}
@@ -101,7 +102,7 @@
float conversion(char buf)
{
- float val;
+ float val = 0;
if( (buf/128) >= 1)
{
buf -= 128;