Bonjour,
Je me mets sur un nouveau topic, car je fais face à un problème de gestion de plusieurs modules SPI :
- J'ai une horloge que je règle afin de prendre des mesures toutes les minutes (par interruptions sur la pin SQW)
- J'ai un stockage sd effectué à chaque fois que je fais la moyenne de ma mesure.
Problème lorsque j'initialise la carte SD de mon module ethernet, et que je met ma partie enregistrement sur SD, le programme plante, les interruptions ne sont plus générées, et l’horloge perd l'heure..
Je met mon code en PJ car il est un peu long..
Si une âme charitable voulait bien m'aider un peu... ?
#include <AcceleroMMA7361.h>
#include <SD.h>
#include <SPI.h>
#include <DS3234.h>
#include "Enerlib.h"
#define BUFF_MAX 256
const int chipSelectSD = 4;
const int chipSelectHorl = 8; // chip select pin
uint8_t sleep_period = 1; // the sleep interval in minutes between 2 consecutive alarms
Energy energy;
int time_interrupt=0;
AcceleroMMA7361 accelero;
float comparaison;
int i = 0;
void set_next_alarm(void)
{
struct ts t;
unsigned char wakeup_min;
DS3234_get(chipSelectHorl, &t);
// calculate the minute when the next alarm will be triggered
wakeup_min = (t.min / sleep_period + 1) * sleep_period;
if (wakeup_min > 59) {
wakeup_min -= 60;
}
// flags define what calendar component to be checked against the current time in order
// to trigger the alarm
// A2M2 (minutes) (0 to enable, 1 to disable)
// A2M3 (hour) (0 to enable, 1 to disable)
// A2M4 (day) (0 to enable, 1 to disable)
// DY/DT (dayofweek == 1/dayofmonth == 0)
boolean flags[4] = { 0, 1, 1, 1 };
// set Alarm2. only the minute is set since we ignore the hour and day component
DS3234_set_a2(chipSelectHorl, wakeup_min, 0, 0, flags);
// activate Alarm2
DS3234_set_creg(chipSelectHorl, DS3234_INTCN | DS3234_A2IE);
}
void INT0_ISR(void)
{
//detach interrupt and set time_interrupt=1
//interrupt must be attached again
detachInterrupt(0);
time_interrupt=1;
}
void setup()
{
Serial.begin(9600);
Serial.println("Entering setup");
pinMode(2, INPUT);
pinMode(10, OUTPUT); //broche SS de l'arduino, mise en sortie pour qu'il reste maitre
digitalWrite(10, HIGH); //desactive le module ethernet W5100
pinMode(chipSelectHorl, OUTPUT);
// pinMode(chipSelectSD, OUTPUT);
delay(5000);
DS3234_init(chipSelectHorl, DS3234_INTCN);
DS3234_clear_a2f(chipSelectHorl);
set_next_alarm();
attachInterrupt(0, INT0_ISR, LOW);
//======================================================
/* Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
// see if the card is present and can be initialized:
if (!SD.begin(chipSelectSD)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
*/
//===========================================================
accelero.begin(6, 3, 5, 9, A0, A1, A2);
accelero.setARefVoltage(3.3); //sets the AREF voltage to 3.3V
accelero.setSensitivity(HIGH); //sets the sensitivity to +/-6G 206mV/G
accelero.calibrate();
}
//===================================================================
void loop()
{
// open the file. note that only one file can be open at a time,
/* // so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(analogRead(A0));
dataFile.close();
// digitalWrite(chipSelectSD,HIGH);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
if(time_interrupt==1){
Serial.println(" time_interrupt==1");
time_interrupt=0;
// set next alarm
set_next_alarm();
// clear a2 alarm flag and let INT go into hi-z
DS3234_clear_a2f(chipSelectHorl);
//Attach interrupt again
attachInterrupt(0, INT0_ISR, LOW);
if (energy.WasSleeping())
{
Serial.println(" Interrupt and was sleeping");
}
else
{
/*
The IRQ happened in awake state.
This code is for the "normal" ISR.
*/
Serial.println(" Interrupt and was NOT sleeping");
}
}
Serial.println("Entering loop");
delay(1000);
/*
//For debuf only
char buff[BUFF_MAX];
struct ts t;
DS3234_get(chipSelectHorl, &t);
// display current time
snprintf(buff, BUFF_MAX, "%d.%02d.%02d %02d:%02d:%02d", t.year, t.mon, t.mday, t.hour, t.min, t.sec);
Serial.println(buff);
*/
delay(10);
Serial.println("Powering down");
energy.PowerDown();
}