1

I am working in my project and i want to use Timer2 interrupt every 100ms to get new measurement from the output of my 5 sensors which connected to pins A1,A2,A3,A4,A5.

I read in some article that using delay instruction is not as good as using timers'Interruptions so, I tried this code put it didn't work properly :

void setup(){

cli();

  TCCR2A = 0;
  TCCR2B = 0;
  TCNT2  = 0;
  OCR2A = 99;
  TCCR2A |= (1 << WGM21);
  TCCR2B |= (1 << CS21);   
  TIMSK2 |= (1 << OCIE2A);
sei();

Serial.begin(9600);  
}
void loop()
{
Serial.print(digitalRead(1));
Serial.print(' ');
Serial.print(digitalRead(2));
Serial.print(' ');
Serial.print(digitalRead(3));
Serial.print(' ');
Serial.print(digitalRead(4));
Serial.print(' ');
Serial.print(digitalRead(5));
Serial.println(' ');

}
  • 1
    Where is your ISR? – Ignacio Vazquez-Abrams Aug 17 '15 at 8:20
  • in fact I dont know where i put the ISR interupt service routine where i have to put measurement instruction . – omran Aug 17 '15 at 8:36
  • 1
    Enabling the interrupt without writing a ISR is pointless. – Ignacio Vazquez-Abrams Aug 17 '15 at 8:38
  • i know that but where in the code i put the ISR?I think i have to put it in the loop – omran Aug 17 '15 at 8:52
  • 2
    ISR(TIMER2_COMPA_vect){ ..code here.. }. However with your current setting the ISR gets called once every 0.5ms. – Gerben Aug 17 '15 at 14:34
1

Well let's start from the start ...


Analog readings

to get new measurement from the output of my 5 sensors which connected to pins A1,A2,A3,A4,A5.

Serial.print(digitalRead(1));

That is not reading analog pin A1, it is reading digital pin 1. If you want to read A1 you need:

Serial.print(analogRead(1));

Check the time

By far the simplest thing would be to simply check the time and take a reading when 100 ms is up.

Example:

void setup()
{
  Serial.begin(115200);  
}  // end of setup

unsigned long lastReading;
const unsigned long INTERVAL = 100;  // ms

void loop()
{

  // is time up?
  if (millis () - lastReading >= INTERVAL)
    {
    lastReading = millis ();  // when we took this reading
    Serial.print(analogRead(1));
    Serial.print(" ");
    Serial.print(analogRead(2));
    Serial.print(" ");
    Serial.print(analogRead(3));
    Serial.print(" ");
    Serial.print(analogRead(4));
    Serial.print(" ");
    Serial.print(analogRead(5));
    Serial.println();
    }  // end of if time up

  // do other things here

}  // end of loop

I increased the baud rate to 115200. Why use 9600 when you are trying to print 5 x 4 characters every 100 ms?


Using timers

If you really want to use timers (and I don't see what they achieve here) I have a page about timers that describes using them, and interrupts, in some depth.

The very least you would need is a timer 2 "compare A" interrupt like this:

ISR (TIMER2_COMPA_vect) 
  {
  // Timer 2 has reached its comparison value
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.