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.
Dependencies: mbed
Fork of APP1_customProtocole by
Diff: main.cpp
- Revision:
- 2:451888674389
- Parent:
- 1:7b43594a95f3
- Child:
- 3:b3574c385012
--- a/main.cpp Thu Jan 07 20:43:57 2016 +0000
+++ b/main.cpp Sun Jan 10 19:39:16 2016 +0000
@@ -1,20 +1,78 @@
#include "mbed.h"
-#define CLR_DISPLAY 0x76
+const int ACCELERO_WRITE_ADRESS = 0x3A;
+const int ACCELERO_READ_ADRESS = 0x3B;
+
+const char ACCELERO_REGISTER_WHO_AM_I = 0x0D;
+const char ACCELERO_RESPONSE_WHO_AM_I = 0x2A;
+const char ACCELERO_REGISTER_CTRL_REG1 = 0x2A;
+
+const char ACCELERO_REGISTER_OUT_X_MSB = 0x01;
+
+Serial pc(USBTX, USBRX);
Serial afficheur(p13, p14);
+I2C accelero(p28, p27);
void display2decimal(int number);
+int getAcceleroZValue();
+
+void acceleroInit(int frequency);
int main() {
- int compteur = 0;
+
+ acceleroInit(100000);
+
+ int zValue = 0;
while(1) {
- display2decimal(compteur);
- compteur++;
+ zValue = getAcceleroZValue();
+ display2decimal(zValue);
wait(0.5);
}
}
+void acceleroInit(int frequency)
+{
+ char command[2];
+ command[0] = 0x2B;
+ command[1] = 0x40;
+
+ accelero.write(ACCELERO_WRITE_ADRESS, command, 1, false);
+ wait(0.1);
+
+ char response = 0x00;
+ accelero.frequency(frequency);
+ accelero.write(ACCELERO_WRITE_ADRESS, &ACCELERO_REGISTER_WHO_AM_I, 1, true);
+ accelero.read(ACCELERO_READ_ADRESS, &response, 1, false);
+
+ if(response == ACCELERO_RESPONSE_WHO_AM_I)
+ {
+ DigitalOut led4(LED4);
+ led4 = 1;
+ }
+
+ command[0] = ACCELERO_REGISTER_CTRL_REG1;
+ command[1] = 0x01; //Put accelro in active mode
+
+ accelero.write(ACCELERO_WRITE_ADRESS, command, 1, false);
+}
+
+int getAcceleroZValue()
+{
+ char data[6];
+ accelero.write(ACCELERO_WRITE_ADRESS, &ACCELERO_REGISTER_OUT_X_MSB, 1, true);
+ accelero.read(ACCELERO_READ_ADRESS, data, 6, false);
+
+ int X = (data[0] << 4) + (data[1] >> 4);
+ int Y = (data[2] << 4) + (data[3] >> 4);
+ int Z = (data[4] << 4) + (data[5] >> 4);
+ pc.printf("X: %i \n\r", X);
+ pc.printf("Y: %i \n\r", Y);
+ pc.printf("Z: %i \n\r", Z);
+ return Z;
+}
+
+
//Affiche automatique un nombre avec 2 dgits
//Pour display 12.35 il faut envoyer 1235
void display2decimal(int number)
