Utilisation de Timer2
This commit is contained in:
parent
94fb67ba07
commit
f20197f5a9
|
@ -1,8 +1,9 @@
|
||||||
#include <TimerOne.h>
|
//#include <TimerOne.h>
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <SD.h>
|
#include <SD.h>
|
||||||
#include <SparkFun_ADXL345.h>
|
#include <SparkFun_ADXL345.h>
|
||||||
#include <SoftwareSerial.h>
|
#include <SoftwareSerial.h>
|
||||||
|
#include <MsTimer2.h>
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// CONSTANTES
|
// CONSTANTES
|
||||||
|
@ -15,6 +16,7 @@
|
||||||
#define MAX6675_SCK 9
|
#define MAX6675_SCK 9
|
||||||
#define START "Start"
|
#define START "Start"
|
||||||
#define STOP "Stop"
|
#define STOP "Stop"
|
||||||
|
#define CONFIG "Config"
|
||||||
|
|
||||||
const char* ITEM_MENU[] = {"0. Paramètres", "1. Start" , "2. Stop"};
|
const char* ITEM_MENU[] = {"0. Paramètres", "1. Start" , "2. Stop"};
|
||||||
|
|
||||||
|
@ -23,29 +25,28 @@ const char* ITEM_MENU[] = {"0. Paramètres", "1. Start" , "2. Stop"};
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
SoftwareSerial BT(PIN_BT_RX,PIN_BT_TX); // BLUETOOTH
|
SoftwareSerial BT(PIN_BT_RX,PIN_BT_TX); // BLUETOOTH
|
||||||
ADXL345 adxl = ADXL345(); // Capteur ADXL
|
ADXL345 adxl = ADXL345(); // Capteur ADXL
|
||||||
volatile int x,y,z; // Valeurs ADXL
|
|
||||||
volatile double tc1;
|
|
||||||
File dataFile; // Fichier sur carte SD
|
File dataFile; // Fichier sur carte SD
|
||||||
String dataString = ""; // String courante à écrire
|
String dataString = ""; // String courante à écrire
|
||||||
unsigned long pm_flush; // Précédent millis() pour flush
|
unsigned long pm_flush; // Précédent millis() pour flush
|
||||||
volatile bool flag = false; // Flag acquisition
|
unsigned long cm; // millis() courant
|
||||||
bool flag_run = false; // Flag acquis en cours
|
bool flag_run = false; // Flag acquisition en route
|
||||||
|
bool flag = false; // Flag mesure faite
|
||||||
|
int x,y,z; // Valeurs ADXL
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// PARAMETRES
|
// PARAMETRES
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
volatile bool Voie_ON[]= { // Voies actives
|
bool Voie_ON[]= { // Voies actives
|
||||||
true, true, true, true, true, true};
|
true, true, true, true, true, true};
|
||||||
volatile double T = 500000; // Période acquisition µs
|
unsigned long T = 500; // Période acquisition µs
|
||||||
unsigned long T_FLUSH = 2000; // Période écriture fichier ms
|
unsigned long T_FLUSH = 2000; // Période écriture fichier ms
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// ISR ACQUISITION
|
// ISR MESURE
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
void isrTimer1(){
|
void mesure(){
|
||||||
Timer1.stop();
|
|
||||||
dataString = "";
|
dataString = "";
|
||||||
Timer1.initialize(T);
|
|
||||||
// 0 = millis
|
// 0 = millis
|
||||||
// 1 = RTC
|
// 1 = RTC
|
||||||
// 2,3,4 = X,Y,Z
|
// 2,3,4 = X,Y,Z
|
||||||
|
@ -75,16 +76,16 @@ void menu(){
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
BT.begin(9600);
|
BT.begin(9600);
|
||||||
Serial.print("Initializing SD card...");
|
Serial.print(F("Initializing SD card..."));
|
||||||
BT.print("Initializing SD card...");
|
BT.print(F("Initializing SD card..."));
|
||||||
|
|
||||||
if (!SD.begin(PIN_SD_CS)) {
|
if (!SD.begin(PIN_SD_CS)) {
|
||||||
Serial.println("Card failed");
|
Serial.println("Card failed");
|
||||||
BT.println("Card failed");
|
BT.println("Card failed");
|
||||||
while (1);
|
while (1);
|
||||||
}
|
}
|
||||||
Serial.println("card initialized.");
|
Serial.println(F("card initialized."));
|
||||||
BT.println("card initialized.");
|
BT.println(F("card initialized."));
|
||||||
|
|
||||||
pinMode(MAX6675_CS, OUTPUT);
|
pinMode(MAX6675_CS, OUTPUT);
|
||||||
pinMode(MAX6675_SO, INPUT);
|
pinMode(MAX6675_SO, INPUT);
|
||||||
|
@ -94,17 +95,10 @@ void setup() {
|
||||||
adxl.setRangeSetting(4);
|
adxl.setRangeSetting(4);
|
||||||
|
|
||||||
dataFile = SD.open("datalog.txt", FILE_WRITE);
|
dataFile = SD.open("datalog.txt", FILE_WRITE);
|
||||||
Timer1.stop();
|
|
||||||
menu();
|
menu();
|
||||||
|
|
||||||
//Timer1.attachInterrupt(isrTimer1);
|
|
||||||
Timer1.stop();
|
|
||||||
|
|
||||||
//isrTimer1();
|
|
||||||
//Timer1.start();
|
|
||||||
|
|
||||||
//Timer1.initialize(1000000);
|
|
||||||
|
|
||||||
|
MsTimer2::set(T, mesure); // 500ms period
|
||||||
|
MsTimer2::stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
double readTC() {
|
double readTC() {
|
||||||
|
@ -133,41 +127,54 @@ double readTC() {
|
||||||
return v*0.25; // The remaining bits are the number of 0.25 degree (C) counts
|
return v*0.25; // The remaining bits are the number of 0.25 degree (C) counts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void config(){
|
||||||
|
affich(CONFIG);
|
||||||
|
affich(String("Période acquisition [" + String(T) + "]"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void affich(String x){
|
||||||
|
Serial.println(x);
|
||||||
|
BT.println(x);
|
||||||
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (flag) {
|
cm = millis();
|
||||||
|
if (flag) {
|
||||||
dataFile.println(dataString);
|
dataFile.println(dataString);
|
||||||
Serial.println(dataString);
|
Serial.println(dataString);
|
||||||
BT.println(dataString);
|
BT.println(dataString);
|
||||||
flag = false;
|
if (Voie_ON[2] || Voie_ON[3] || Voie_ON[4]){adxl.readAccel(&x, &y, &z);}
|
||||||
|
flag=false;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte in = 0;
|
byte in = 0;
|
||||||
|
|
||||||
if (Serial.available()>0){in = Serial.read();}
|
if (Serial.available()>0){in = Serial.read();}
|
||||||
if ( BT.available()>0){in = BT.read();}
|
if ( BT.available()>0){in = BT.read();}
|
||||||
|
|
||||||
|
if (!flag_run && in == 48) {
|
||||||
|
config();
|
||||||
|
}
|
||||||
|
|
||||||
if (!flag_run && in == 49) {
|
if (!flag_run && in == 49) {
|
||||||
Timer1.attachInterrupt(isrTimer1);
|
|
||||||
Timer1.initialize(T);
|
|
||||||
Serial.println(START);
|
Serial.println(START);
|
||||||
BT.println(START);
|
BT.println(START);
|
||||||
flag_run = true;
|
flag_run = true;
|
||||||
|
MsTimer2::set(T, mesure);
|
||||||
|
MsTimer2::start();
|
||||||
}
|
}
|
||||||
if (flag_run && in == 50) {
|
if (flag_run && in == 50) {
|
||||||
Timer1.stop();
|
|
||||||
Serial.println(STOP);
|
Serial.println(STOP);
|
||||||
BT.println(STOP);
|
BT.println(STOP);
|
||||||
flag_run = false;
|
flag_run = false;
|
||||||
|
MsTimer2::stop();
|
||||||
|
dataFile.flush();
|
||||||
menu();
|
menu();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((millis()-pm_flush)>T_FLUSH) {
|
if (flag_run && ((cm-pm_flush)>T_FLUSH)) {
|
||||||
dataFile.flush();
|
dataFile.flush();
|
||||||
pm_flush=millis();
|
pm_flush=cm;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Voie_ON[2] || Voie_ON[3] || Voie_ON[4]){
|
}
|
||||||
adxl.readAccel(&x, &y, &z);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue