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 0:ef6b25a481a5, committed 2014-12-19
- Comitter:
- Thierro
- Date:
- Fri Dec 19 10:38:18 2014 +0000
- Child:
- 1:adb624063b97
- Commit message:
- venerd? 19 dicembre
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA8451Q.lib Fri Dec 19 10:38:18 2014 +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 Dec 19 10:38:18 2014 +0000
@@ -0,0 +1,147 @@
+#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 X_acc 0x01
+#define Y_acc 0x03
+#define Z_acc 0x05
+#define STATUS 0x00
+
+float conversion(char buf);
+
+int main(void)
+{
+
+ I2C i2c(SDA,SCL);
+
+ MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
+
+ int address = MMA8451_I2C_ADDRESS;
+ 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;
+
+ char data[2] = {CNTRL_REG_1, 0x00};
+ i2c.write(address, data, 2);
+
+ 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) {
+
+ 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);
+ }
+
+
+ }
+}
+
+
+float conversion(char buf)
+{
+ float val;
+ 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 Dec 19 10:38:18 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5 \ No newline at end of file