Skip to main content
Corrected spelling of "interrupt" in thread title.
Link
Nick Gammon
  • 38.7k
  • 13
  • 67
  • 125

Timer 2 interruput atmega328pinterrupt with ATmega328p and make measurement in ISR

Added C++ language formatting tag.
Source Link
Nick Gammon
  • 38.7k
  • 13
  • 67
  • 125
 

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

I read in some article that using delay instruction is not as good as using timers'Interruptions so, I 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(' ');

}

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(' ');
             
}

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(' ');

}

 

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(' ');
             
}
added 1 character in body
Source Link
omran
  • 31
  • 1
  • 6

I am working in my project and i want to use Timer2 interrupt every 50ms100ms 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 = 49;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(' ');

}

I am working in my project and i want to use Timer2 interrupt every 50ms 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 = 49; 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(' ');

}

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(' ');

}

Source Link
omran
  • 31
  • 1
  • 6
Loading