Projet proto (LCD + TERMINAL + EEPROM + 2 BTN)

Dependencies:   mbed ADXL345_I2C_my

Committer:
RobotManYt
Date:
Fri May 24 18:02:04 2019 +0000
Revision:
0:04ed5e9a74a4
Projet proto;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RobotManYt 0:04ed5e9a74a4 1 //#include "mbed.h"
RobotManYt 0:04ed5e9a74a4 2 #include "ADXL345_I2C.h"
RobotManYt 0:04ed5e9a74a4 3
RobotManYt 0:04ed5e9a74a4 4 ADXL345_I2C accelerometer(I2C_SDA, I2C_SCL);
RobotManYt 0:04ed5e9a74a4 5 Serial pc(SERIAL_TX, SERIAL_RX);
RobotManYt 0:04ed5e9a74a4 6 Ticker RAM;
RobotManYt 0:04ed5e9a74a4 7 Timer temps;
RobotManYt 0:04ed5e9a74a4 8
RobotManYt 0:04ed5e9a74a4 9 DigitalIn BTN(USER_BUTTON);
RobotManYt 0:04ed5e9a74a4 10 DigitalIn BTNmode(PC_10);
RobotManYt 0:04ed5e9a74a4 11
RobotManYt 0:04ed5e9a74a4 12 int data = 0, j = 0;
RobotManYt 0:04ed5e9a74a4 13
RobotManYt 0:04ed5e9a74a4 14 char X[157][64];
RobotManYt 0:04ed5e9a74a4 15 uint8_t a, b;
RobotManYt 0:04ed5e9a74a4 16 float XYZ[3];
RobotManYt 0:04ed5e9a74a4 17 bool appui = false, Mode = false;
RobotManYt 0:04ed5e9a74a4 18
RobotManYt 0:04ed5e9a74a4 19 void dataWrite(){
RobotManYt 0:04ed5e9a74a4 20 if(j == 0) pc.printf("Bouton appuye\n\r");
RobotManYt 0:04ed5e9a74a4 21 if(j < 10000 && appui){
RobotManYt 0:04ed5e9a74a4 22 X[a][b] = (char)XYZ[0];
RobotManYt 0:04ed5e9a74a4 23 if(b < 64){ //
RobotManYt 0:04ed5e9a74a4 24 b++;
RobotManYt 0:04ed5e9a74a4 25 }else{ // Page + 1;
RobotManYt 0:04ed5e9a74a4 26 b = 0;
RobotManYt 0:04ed5e9a74a4 27 a++;
RobotManYt 0:04ed5e9a74a4 28 }
RobotManYt 0:04ed5e9a74a4 29 j++;
RobotManYt 0:04ed5e9a74a4 30 }else if(appui){
RobotManYt 0:04ed5e9a74a4 31 pc.printf("Ecriture sur la EEPROM\n\r");
RobotManYt 0:04ed5e9a74a4 32 for(a = 0; a < 157; a++){
RobotManYt 0:04ed5e9a74a4 33 char data[66];
RobotManYt 0:04ed5e9a74a4 34 data[0] = 0;
RobotManYt 0:04ed5e9a74a4 35 data[1] = a;
RobotManYt 0:04ed5e9a74a4 36 for(b = 0; b < 64; b++){
RobotManYt 0:04ed5e9a74a4 37 data[b+2] = X[a][b];
RobotManYt 0:04ed5e9a74a4 38 }
RobotManYt 0:04ed5e9a74a4 39 accelerometer.writeEEPROM(data, 66);
RobotManYt 0:04ed5e9a74a4 40 wait(0.01);
RobotManYt 0:04ed5e9a74a4 41 pc.printf("%u : Donnee ecrite\n\r", a);
RobotManYt 0:04ed5e9a74a4 42 }
RobotManYt 0:04ed5e9a74a4 43 pc.printf("Ecriture fini\n\r");
RobotManYt 0:04ed5e9a74a4 44 j = 0;
RobotManYt 0:04ed5e9a74a4 45 RAM.detach();
RobotManYt 0:04ed5e9a74a4 46 }
RobotManYt 0:04ed5e9a74a4 47 }
RobotManYt 0:04ed5e9a74a4 48
RobotManYt 0:04ed5e9a74a4 49 int main() {
RobotManYt 0:04ed5e9a74a4 50 pc.baud(9600);
RobotManYt 0:04ed5e9a74a4 51
RobotManYt 0:04ed5e9a74a4 52 int readings[3] = {0, 0, 0};
RobotManYt 0:04ed5e9a74a4 53
RobotManYt 0:04ed5e9a74a4 54 pc.printf("\n\rStarting ADXL345 test...\n\r");
RobotManYt 0:04ed5e9a74a4 55 pc.printf("Device ID is: 0x%02x\n\r", accelerometer.getDeviceID());
RobotManYt 0:04ed5e9a74a4 56
RobotManYt 0:04ed5e9a74a4 57 //******* Initialisation ADXL ********//
RobotManYt 0:04ed5e9a74a4 58 // These are here to test whether any of the initialization fails. It will print the failure
RobotManYt 0:04ed5e9a74a4 59 if (accelerometer.setPowerControl(0x00)){
RobotManYt 0:04ed5e9a74a4 60 return 0;
RobotManYt 0:04ed5e9a74a4 61 }
RobotManYt 0:04ed5e9a74a4 62 wait(.001);
RobotManYt 0:04ed5e9a74a4 63
RobotManYt 0:04ed5e9a74a4 64 if(accelerometer.setDataFormatControl(0x0B)){
RobotManYt 0:04ed5e9a74a4 65 pc.printf("didn't set data format\n\r");
RobotManYt 0:04ed5e9a74a4 66 return 0;
RobotManYt 0:04ed5e9a74a4 67 }
RobotManYt 0:04ed5e9a74a4 68 wait(.001);
RobotManYt 0:04ed5e9a74a4 69
RobotManYt 0:04ed5e9a74a4 70 //Measurement mode.
RobotManYt 0:04ed5e9a74a4 71
RobotManYt 0:04ed5e9a74a4 72 if(accelerometer.setPowerControl(MeasurementMode)) {
RobotManYt 0:04ed5e9a74a4 73 pc.printf("didn't set the power control to measurement\n\r");
RobotManYt 0:04ed5e9a74a4 74 return 0;
RobotManYt 0:04ed5e9a74a4 75 }
RobotManYt 0:04ed5e9a74a4 76 //****** FIN INITIALISATION ********//
RobotManYt 0:04ed5e9a74a4 77
RobotManYt 0:04ed5e9a74a4 78 while (1) {
RobotManYt 0:04ed5e9a74a4 79 //wait(0.1);
RobotManYt 0:04ed5e9a74a4 80 if(!BTN){
RobotManYt 0:04ed5e9a74a4 81 appui = true;
RobotManYt 0:04ed5e9a74a4 82 RAM.attach(&dataWrite, 0.001);
RobotManYt 0:04ed5e9a74a4 83 }
RobotManYt 0:04ed5e9a74a4 84 accelerometer.getOutput(readings);
RobotManYt 0:04ed5e9a74a4 85 XYZ[0] = readings[0] * 0.004;
RobotManYt 0:04ed5e9a74a4 86 XYZ[1] = readings[1] * 0.004;
RobotManYt 0:04ed5e9a74a4 87 XYZ[2] = readings[2] * 0.004;
RobotManYt 0:04ed5e9a74a4 88
RobotManYt 0:04ed5e9a74a4 89 accelerometer.LCDprint(XYZ[0], XYZ[1], XYZ[2]); // À afficher sur LCD
RobotManYt 0:04ed5e9a74a4 90 //pc.printf("X: %.3fg | Y: %.3fg | Z: %.3fg", XYZ[0],XYZ[1], XYZ[2]); // Affichage sur un terminal plutot que sur LCD
RobotManYt 0:04ed5e9a74a4 91
RobotManYt 0:04ed5e9a74a4 92 if(!BTNmode){
RobotManYt 0:04ed5e9a74a4 93 Mode = true;
RobotManYt 0:04ed5e9a74a4 94 temps.start();
RobotManYt 0:04ed5e9a74a4 95 pc.printf("Temps(s) Accel X(g) Accel Y(g) Accel Z(g)");
RobotManYt 0:04ed5e9a74a4 96 }
RobotManYt 0:04ed5e9a74a4 97
RobotManYt 0:04ed5e9a74a4 98 if(Mode){
RobotManYt 0:04ed5e9a74a4 99 pc.printf("%.3f %.3f %.3f %.3f", temps.read(), XYZ[0], XYZ[1], XYZ[2]);
RobotManYt 0:04ed5e9a74a4 100 }
RobotManYt 0:04ed5e9a74a4 101 }
RobotManYt 0:04ed5e9a74a4 102 }
RobotManYt 0:04ed5e9a74a4 103