Utilisation de Timer2

This commit is contained in:
Grouch 2018-03-19 18:53:27 +01:00
parent 94fb67ba07
commit f20197f5a9
2 changed files with 46 additions and 38 deletions

View File

@ -1,8 +1,9 @@
#include <TimerOne.h>
//#include <TimerOne.h>
#include <SPI.h>
#include <SD.h>
#include <SparkFun_ADXL345.h>
#include <SoftwareSerial.h>
#include <MsTimer2.h>
//----------------------------------------------------------------------------------
// CONSTANTES
@ -15,6 +16,7 @@
#define MAX6675_SCK 9
#define START "Start"
#define STOP "Stop"
#define CONFIG "Config"
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
ADXL345 adxl = ADXL345(); // Capteur ADXL
volatile int x,y,z; // Valeurs ADXL
volatile double tc1;
File dataFile; // Fichier sur carte SD
String dataString = ""; // String courante à écrire
unsigned long pm_flush; // Précédent millis() pour flush
volatile bool flag = false; // Flag acquisition
bool flag_run = false; // Flag acquis en cours
unsigned long cm; // millis() courant
bool flag_run = false; // Flag acquisition en route
bool flag = false; // Flag mesure faite
int x,y,z; // Valeurs ADXL
//----------------------------------------------------------------------------------
// PARAMETRES
//----------------------------------------------------------------------------------
volatile bool Voie_ON[]= { // Voies actives
bool Voie_ON[]= { // Voies actives
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
//----------------------------------------------------------------------------------
// ISR ACQUISITION
// ISR MESURE
//----------------------------------------------------------------------------------
void isrTimer1(){
Timer1.stop();
void mesure(){
dataString = "";
Timer1.initialize(T);
// 0 = millis
// 1 = RTC
// 2,3,4 = X,Y,Z
@ -75,16 +76,16 @@ void menu(){
void setup() {
Serial.begin(9600);
BT.begin(9600);
Serial.print("Initializing SD card...");
BT.print("Initializing SD card...");
Serial.print(F("Initializing SD card..."));
BT.print(F("Initializing SD card..."));
if (!SD.begin(PIN_SD_CS)) {
Serial.println("Card failed");
BT.println("Card failed");
while (1);
}
Serial.println("card initialized.");
BT.println("card initialized.");
Serial.println(F("card initialized."));
BT.println(F("card initialized."));
pinMode(MAX6675_CS, OUTPUT);
pinMode(MAX6675_SO, INPUT);
@ -94,17 +95,10 @@ void setup() {
adxl.setRangeSetting(4);
dataFile = SD.open("datalog.txt", FILE_WRITE);
Timer1.stop();
menu();
//Timer1.attachInterrupt(isrTimer1);
Timer1.stop();
//isrTimer1();
//Timer1.start();
//Timer1.initialize(1000000);
MsTimer2::set(T, mesure); // 500ms period
MsTimer2::stop();
}
double readTC() {
@ -133,41 +127,54 @@ double readTC() {
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() {
if (flag) {
cm = millis();
if (flag) {
dataFile.println(dataString);
Serial.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;
if (Serial.available()>0){in = Serial.read();}
if ( BT.available()>0){in = BT.read();}
if (!flag_run && in == 48) {
config();
}
if (!flag_run && in == 49) {
Timer1.attachInterrupt(isrTimer1);
Timer1.initialize(T);
Serial.println(START);
BT.println(START);
flag_run = true;
MsTimer2::set(T, mesure);
MsTimer2::start();
}
if (flag_run && in == 50) {
Timer1.stop();
Serial.println(STOP);
BT.println(STOP);
flag_run = false;
MsTimer2::stop();
dataFile.flush();
menu();
}
if ((millis()-pm_flush)>T_FLUSH) {
if (flag_run && ((cm-pm_flush)>T_FLUSH)) {
dataFile.flush();
pm_flush=millis();
pm_flush=cm;
}
if (Voie_ON[2] || Voie_ON[3] || Voie_ON[4]){
adxl.readAccel(&x, &y, &z);
}
}
}

View File

@ -1,2 +1,3 @@
Datalogger sur base Arduino.
En cours de déeloppement.
En cours de dév..