Bonjour à tous,
Actuellement en stage de master sur un sujet d'électronique, j'ai du mal à me sortir d'un problème depuis quelques jours.
J'ai à ma disposition une platine arduino pro mini reliée à un accéléromètre ADXL345 et à une mémoire EEPROM 24LC256.
Mon code est censé lire les valeurs x y et z de l'accéléromètre et de les mettre en mémoire. Dans un second temps, et avec un autre programme, je lis les valeurs de la mémoire en question.
Problème : Quand je compare les valeurs x y et z qui s'affichent avec celles de la mémoire, soit je récupère les bonnes valeurs avec de mauvaises valeurs qui suivent, soit je ne récupère qu'une partie des valeurs, soit j'ai des valeurs qui ne correspondent pas.
J'ai essayé avec et sans bouton poussoir pour faire des reset et remettre la mémoire à zéro mais rien ne change.
Auriez-vous une idée de ce qui pourrait clocher dans le code ?
Merci à vous.
Code:// Ce programme affiche les données de x y et z de l'accéléromètre quand le système détecte un déplacement. Une fois arrêté, le système n'affiche rien. // Il enregistre les valeurs de x, y et z quand il y a un déplacement ainsi que le temps (en secondes) quand le système s'arrête et redémarre. //Add the SPI library so we can communicate with the ADXL345 sensor and I²C for the EEPROM chip #include <SPI.h> #include <Wire.h> //Assign the Chip Select signal to pin 10. #define CS 10 #define LED 5 #define RAZ 4 //Address of 24LC256 eeprom chip #define disk1 0x50 //This is a list of some of the registers available on the ADXL345. //To learn more about these and the rest of the registers on the ADXL345, read the datasheet! #define POWER_CTL 0x2D //Power Control Register #define THRESH_ACT 0x24 // Activity threshold Register #define THRESH_INACT 0x25 // InActivity threshold Register #define ACT_INACT_CTL 0x27 // axis enable #define DATA_FORMAT 0x31 #define DATAX0 0x32 //X-Axis Data 0 #define DATAX1 0x33 //X-Axis Data 1 #define DATAY0 0x34 //Y-Axis Data 0 #define DATAY1 0x35 //Y-Axis Data 1 #define DATAZ0 0x36 //Z-Axis Data 0 #define DATAZ1 0x37 //Z-Axis Data 1 #define INT_SOURCE 0x30 #define INT_ENABLE 0x2E #define TIME_INACT 0x26 //This buffer will hold values read from the ADXL345 registers. char values[10]; //These variables will be used to hold the x,y and z axis accelerometer values. short int x,y,z; float xg,yg,zg; // flag d'arret deplacement boolean bouge,arret; // hrologe initiale unsigned long debut,temps_courant; // current memory address unsigned int address = 0; unsigned int nb_data = 0; int i=0; void setup(){ //Initiate an SPI communication instance. SPI.begin(); //Configure the SPI connection for the ADXL345. SPI.setDataMode(SPI_MODE3); //Create a serial connection to display the data on the terminal. Serial.begin(9600); //Set up the Chip Select pin to be an output from the Arduino. pinMode(CS, OUTPUT); //Before communication starts, the Chip Select pin needs to be set high. digitalWrite(CS, HIGH); pinMode(LED, OUTPUT); //Init LED off digitalWrite(LED, LOW); //Init RAZ pinMode(RAZ, INPUT_PULLUP); Wire.begin(); //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register. writeRegister(DATA_FORMAT, 0x00); //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register. writeRegister(POWER_CTL, 0x08); //Measurement mode // Axis configuration writeRegister(ACT_INACT_CTL,0xFF); // AC mode X and Y enables // accelaration Thresold writeRegister(THRESH_ACT,5); // Inaction Thresold writeRegister(THRESH_INACT,4); // inactivity time thresold writeRegister(TIME_INACT,5); // Enable Activity interrupt writeRegister(INT_ENABLE,0x18); // Backup current time debut = millis(); address = 0; nb_data = 0; writeEEPROM(disk1, address, 0); address++; writeEEPROM(disk1, address, 0); address++; address =0; // recupere le nombre de data nb_data = 256*readEEPROM(disk1, 0); nb_data = nb_data + readEEPROM(disk1, 1); address = (nb_data*3)+2; /*address = 0; nb_data = 0; writeEEPROM(disk1, address, 0); address++; writeEEPROM(disk1, address, 0); address++;*/ /* init */ bouge=false; arret=true; } void loop(){ char flags; readRegister(DATAX0, 6, values); x = (values[1]<<8)|(values[0]&0xFF); xg = x * 4.0 / 1024.0; //The Y value is stored in values[2] and values[3]. y = (values[3]<<8)|(values[2]&0xFF); yg = y * 4.0 / 1024.0; //The Z value is stored in values[4] and values[5]. z = (values[5]<<8)|(values[4]&0xFF); zg = z * 4.0 / 1024.0; readRegister(INT_SOURCE, 1, values); flags=values[0]; //Print the results to the terminal. if ((flags & 0x10)==0x10) { if (bouge==false) { Serial.print("Deplacement "); temps_courant = millis(); temps_courant -= debut; temps_courant = temps_courant / 1000; Serial.println(temps_courant); bouge=true; arret=false; digitalWrite(LED, HIGH); // sauvegarde dans EEPROM nb_data++; Sauve_sample(0, temps_courant,nb_data); } } else { if ((flags & 0x08)==0x08) { if (arret==false) { Serial.print("Arret"); temps_courant = millis(); temps_courant -= debut; temps_courant = temps_courant / 1000; Serial.println(temps_courant); bouge=false; arret=true; digitalWrite(LED, LOW); // sauvegarde dans EEPROM nb_data++; Sauve_sample(0, temps_courant,nb_data); } } } if ((arret==false)&&(bouge==true)){ Serial.print(x, DEC); nb_data++; Sauve_data(1, x, nb_data); Serial.print(','); Serial.print(y, DEC); nb_data++; Sauve_data(1, y, nb_data); Serial.print(','); Serial.println(z, DEC); nb_data++; Sauve_data(1, z, nb_data); } delay(100); /*if (digitalRead(RAZ)==0) { address = 0; nb_data = 0; writeEEPROM(disk1, address, 0); address++; writeEEPROM(disk1, address, 0); address++; for (i=0;i<32000;i++) { writeEEPROM(disk1, i, 0); } address = 2; }*/ } void Sauve_data(char etat, int valeur_data, int data) { char datal,datah,valeur_datal,valeur_datah; char addl,addh; datah = (char)(data / 256); // poids fort datal = (char)data; // poids faible valeur_datah = (char) (valeur_data>>8); valeur_datal = (char) (valeur_data & 0xFF); addh = (char) (address / 256); addl = (char)address; writeEEPROM(disk1, 0, datah); writeEEPROM(disk1, 1, datal); writeEEPROM(disk1, address, etat); address++; writeEEPROM(disk1, address, valeur_datah); address++; writeEEPROM(disk1, address, valeur_datal); address++; } void Sauve_sample(char etat, unsigned int temps,unsigned int data) { char datal,datah,tempsl,tempsh; char addl,addh; datah = (char)(data / 256); // poids fort datal = (char)data; // poids faible tempsh = (char) (temps / 256); tempsl = (char) temps; addh = (char) (address / 256); addl = (char)address; writeEEPROM(disk1, 0, datah); writeEEPROM(disk1, 1, datal); writeEEPROM(disk1, address, etat); address++; writeEEPROM(disk1, address, tempsh); address++; writeEEPROM(disk1, address, tempsl); address++; } //This function will write a value to a register on the ADXL345. //Parameters: // char registerAddress - The register to write a value to // char value - The value to be written to the specified register. void writeRegister(char registerAddress, char value){ //Set Chip Select pin low to signal the beginning of an SPI packet. digitalWrite(CS, LOW); //Transfer the register address over SPI. SPI.transfer(registerAddress); //Transfer the desired register value over SPI. SPI.transfer(value); //Set the Chip Select pin high to signal the end of an SPI packet. digitalWrite(CS, HIGH); } //This function will read a certain number of registers starting from a specified address and store their values in a buffer. //Parameters: // char registerAddress - The register addresse to start the read sequence from. // int numBytes - The number of registers that should be read. // char * values - A pointer to a buffer where the results of the operation should be stored. void readRegister(char registerAddress, int numBytes, char * values){ //Since we're performing a read operation, the most significant bit of the register address should be set. char address = 0x80 | registerAddress; //If we're doing a multi-byte read, bit 6 needs to be set as well. if(numBytes > 1)address = address | 0x40; //Set the Chip select pin low to start an SPI packet. digitalWrite(CS, LOW); //Transfer the starting register address that needs to be read. SPI.transfer(address); //Continue to read registers until we've read the number specified, storing the results to the input buffer. for(int i=0; i<numBytes; i++){ values[i] = SPI.transfer(0x00); } //Set the Chips Select pin high to end the SPI packet. digitalWrite(CS, HIGH); } void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) { Wire.beginTransmission(deviceaddress); Wire.write((int)(eeaddress >> 8)); // MSB Wire.write((int)(eeaddress & 0xFF)); // LSB Wire.write(data); Wire.endTransmission(); delay(5); } byte readEEPROM(int deviceaddress, unsigned int eeaddress ) { byte rdata = 0xFF; Wire.beginTransmission(deviceaddress); Wire.write((int)(eeaddress >> 8)); // MSB Wire.write((int)(eeaddress & 0xFF)); // LSB Wire.endTransmission(); Wire.requestFrom(deviceaddress,1); if (Wire.available()) rdata = Wire.read(); return rdata;
-----