Pages: [1]   Go Down
Author Topic: Timer Interrupt Problem  (Read 313 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey

The LED should toggel every second, but the LED needs 4s to switch.
Code:
unsigned int timerCounter = 0;
void setup() {
  //--------------------------------------- 
  //intialize Timer
  TIMSK0 = _BV(OCIE0A);  // Enable Interrupt TimerCounter0
  TCCR0A = _BV(WGM01);  // Mode = CTC
  TCCR0B = _BV(CS01)|_BV(CS00);   // 64 Prescaler
  OCR0A = 125;          // 0,0001s
  sei();
  //----------------------------------------
  pinMode(13, OUTPUT);     
}

void loop() {
 
}
ISR(SIG_OUTPUT_COMPARE0A)
{
 
  timerCounter++;
  if(timerCounter==10000)
  {
   
    digitalWrite(13, HIGH);   // set the LED on
  }
  if(timerCounter>=20000)
  {
    digitalWrite(13, LOW);    // set the LED off
    timerCounter=0;
  }
 
}

My Calculation is: 1/16000000*64*125=0,00001s
Where is the failure?

Thanks, Nikolausi
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14024
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


My calculation is 1/ 16.000.000 * 64 * 125 = 0.0005 so at least something in your math is incorrect.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: