Stop timer1 interrupt arduino Leonardo (ATmega16u4/32u4)

Viewed 21

For an applycation a need to make some measure with the adc from arduino. I need to start measurment with a ready from serial port.

I make my measure within an interval made by overflow timer interrupt 1. When i finish to take 500 points. I want to stop the timer1 interrupt.

I can start the interrupt when i read the good caractere ("#" for my application) on serial port. But i can't stop the timer interrupt after i finished my 500 measurement.

Here is my code :

#include <Arduino.h>
const byte Test = 10;
volatile int i;
volatile int cnt_mes = 0;
volatile int valADC = 0;
volatile int flag = 0;
float valVolt;
const int TempoTimer = 41;
volatile int tab_Measure[500]={};
char data;

#define TestToggle digitalWrite (Test, !digitalRead(Test))

ISR(TIMER1_OVF_vect)
{
  TCNT1 = 65536 - TempoTimer;
  tab_Measure[cnt_mes] = analogRead(A0);
  cnt_mes = cnt_mes + 1;
  if(cnt_mes == 500)
  { 
    flag = 1;
  }
}

void setupTimer1()
{
  bitClear (TCCR1A, WGM10); // WGM10 = 0
  bitClear (TCCR1A, WGM11); // WGM11 = 0
  bitClear (TCCR1B, WGM12); // WGM10 = 0
  bitClear (TCCR1B, WGM13); // WGM11 = 0  
  TCCR1B = 0b00000011; // Clock / 64 soit 4us
  TIMSK1 = 0b00000001; // Local Interrupt TOIE1
  TCNT1 = 65536 - TempoTimer; 
}
void setup() 
{
  /* Port Setup */
  pinMode(A0,INPUT);      //Pin à Mesurer
  cli(); // Desable global interrupt

  /* Timer 1 Setup */
  setupTimer1();
  sei(); // Enable global interrupt 
  Serial.begin(115200);
}
void loop() 
{
  if(Serial.available()==1)
  {
    data = Serial.read();
  }
  if(data  == '#')
  {
    TCCR1B = 0b00000011; // Start timer with Clock/64 (4us)
  }
  if(flag)
  {
    //TestToggle;
    TCCR1B = 0b00000000; // Stop timer
    cnt_mes = 0;
    flag = 0;
    for (i = 0; i <500; i = i+1)
    {
      Serial.println(String(tab_Measure[i]));
    }
    //TestToggle;  
  }
}

I have tried to stop the timer1 interrupt with TIMSK1 register. But it doesn't work too.

Thanks in advance for your answers.

0 Answers
Related