# Help with Nano TMR0 COMPARE interrupt example

**URL:** <https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158>\
**Category:** Classic Nano\
**Created:** [July 29, 2024, 3:29am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158 "2024-07-29T03:29:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![WB6EYV\_Bob](https://avatars.discourse-cdn.com/v4/letter/w/d6d6ee/32.png) [@WB6EYV\_Bob](https://forum.arduino.cc/u/WB6EYV_Bob)\
**Post date:** [July 29, 2024, 3:29am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/1 "2024-07-29T03:29:38Z")

</div>

Novice to ATMega 328 Nano but not PIC assembly. Can't get simple TMR0 interrupt on COMPARE routine to work... toggles an output pin... that's all.

Compiles OK, downloads OK, runs OK but changing the timer values in code ( and installing in the Nano ) changes nothing in the output signal... always about 500 Hz. Program ( with any changes ) is always SAVEd before new compile and download...

Code follows :

```arduino

// Definitions
#define OutputPin 12 // output pin for TMR0 signal 
int TMR0_COMPARE_VALUE; // Variable ( integer ) for TMR0 COMPARE match 

// TMR0 Interrupt Service Routine
ISR(TIMER0_COMPA_vect)
{
  digitalWrite(OutputPin, digitalRead(OutputPin) ^ 1); // toggle the output pin
}

// Startup Code 
void setup()
{
  pinMode(OutputPin, OUTPUT); // digital pin 12 = output 
  noInterrupts(); // disable all interrupts ( temporary ) 
  TCNT0 = 0; // clear TMR0 8 bit count register
  TMR0_COMPARE_VALUE = 50; // Count up to 50
  OCR0A = TMR0_COMPARE_VALUE; // load Output COMPARE register A of Timer 0
  TCCR0A |= (1 << WGM01); // enable CTC counter mode ( mode 2 ) 
  TCCR0B |= (1 << CS00); // set TMR0 clock prescaler to divide 16 MHz by 1
  TCCR0B |= (0 << CS01);
  TCCR0B |= (0 << CS02);
  TIMSK0 |= (1 << OCIE0A); // enable TMR0 interrupts for COMPARE register A  
  interrupts(); // enable all unmasked interrupts
}

// Main Program Loop
void loop()
{
  
}

```

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [July 29, 2024, 3:39am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/2 "2024-07-29T03:39:00Z")

</div>

> [@WB6EYV\_Bob](#):
>
> // Definitions  
> #define OutputPin 12 // output pin for TMR0 signal

Do not use this. Use instead

```arduino
// Definitions
volatile byte OutputPin 12 // output pin for TMR0 signal

```

All variables used inside and outside of an ISR must be defined as a volatile type.

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [July 29, 2024, 3:44am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/3 "2024-07-29T03:44:31Z")

</div>

```arduino
ISR(TIMER2_COMPA_vect) {
  PORTB&(1 << 4) ? PORTB &= ~(1 << 4) : PORTB |= (1 << 4); // toggle the output pin
}

void setup()
{
  pinMode(12, OUTPUT); // digital pin 12 = output
  noInterrupts(); // disable all interrupts ( temporary )
  TCCR2A = 0;// set entire TCCR2A register to 0
  TCCR2B = 0;// same for TCCR2B
  TCNT2 = 0;//initialize counter value to 0

  OCR2A = 50;

  TCCR2A |= (1 << WGM21);// turn on CTC mode
  TCCR2B |= (1 << CS20); // Set CS20, no prescaler

  TIMSK2 |= (1 << OCIE2A);// enable timer compare interrupt
  interrupts(); // enable all unmasked interrupts
}

void loop() {}

```

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 29, 2024, 3:46am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/4 "2024-07-29T03:46:23Z")

</div>

I did see your attempt at code tags; it did not quite work out 😉 It's not `<CODE>...</CODE>` but `[CODE]...[/CODE]`

Alternatively use three backticks as shown below; they need to be on their own line.  
```  
your code here  
```

---

<div class="post-metadata">

**Author:** ![WB6EYV\_Bob](https://avatars.discourse-cdn.com/v4/letter/w/d6d6ee/32.png) [@WB6EYV\_Bob](https://forum.arduino.cc/u/WB6EYV_Bob)\
**Post date:** [July 29, 2024, 3:59am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/5 "2024-07-29T03:59:10Z")

</div>

OK, thanks... but it did seem to work... I'm new to C coding also... Appreciate the help

---

<div class="post-metadata">

**Author:** ![WB6EYV\_Bob](https://avatars.discourse-cdn.com/v4/letter/w/d6d6ee/32.png) [@WB6EYV\_Bob](https://forum.arduino.cc/u/WB6EYV_Bob)\
**Post date:** [July 29, 2024, 4:02am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/6 "2024-07-29T04:02:51Z")

</div>

Thanks, I'm new to C code also... this syntax is alien to me, my original code seemed to work so I'll leave it alone until I can understand the syntax you suggested...

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [July 29, 2024, 4:04am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/7 "2024-07-29T04:04:52Z")

</div>

not a single try?

---

<div class="post-metadata">

**Author:** ![WB6EYV\_Bob](https://avatars.discourse-cdn.com/v4/letter/w/d6d6ee/32.png) [@WB6EYV\_Bob](https://forum.arduino.cc/u/WB6EYV_Bob)\
**Post date:** [July 29, 2024, 4:05am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/8 "2024-07-29T04:05:20Z")

</div>

Yeah, this is my first post so I'm blundering through it all... I tried using the code "button" but it did not show up in the final draft so I just added my own...

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [July 29, 2024, 4:09am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/9 "2024-07-29T04:09:31Z")

</div>

You do realise that changing stuff on Timer 0 will screw up all the timing for all the delay functions and the use of millis()?

It is normal not to mess with this timer but use another one.

---

<div class="post-metadata">

**Author:** ![WB6EYV\_Bob](https://avatars.discourse-cdn.com/v4/letter/w/d6d6ee/32.png) [@WB6EYV\_Bob](https://forum.arduino.cc/u/WB6EYV_Bob)\
**Post date:** [July 29, 2024, 4:16am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/10 "2024-07-29T04:16:04Z")

</div>

I was not aware of that, thanks... I'm actually a hardware guy, all my embedded coding til now was in assembly... I'm new to Arduinos and to C... but otherwise very proficient. Thanks.

---

<div class="post-metadata">

**Author:** ![WB6EYV\_Bob](https://avatars.discourse-cdn.com/v4/letter/w/d6d6ee/32.png) [@WB6EYV\_Bob](https://forum.arduino.cc/u/WB6EYV_Bob)\
**Post date:** [July 29, 2024, 4:48am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/11 "2024-07-29T04:48:55Z")

</div>

I tried the changes you suggested, TCCR0B, CS01 AND CS02 = 1, that killed the output completely.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [July 29, 2024, 4:51am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/12 "2024-07-29T04:51:37Z")

</div>

> [@Grumpy\_Mike](#):
>
> All variables used inside and outside of an ISR must be defined as a volatile type.

No, you don't need declare as volatile constants, like the pin number:)  
And even not to all variables.

The 'volatile' qualifer only needs to variables, that changes the value inside the interrupt, as I know.

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [July 29, 2024, 5:01am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/13 "2024-07-29T05:01:32Z")

</div>

> [@WB6EYV\_Bob](#):
>
> ```arduino
> TCCR0B |= (0 << CS01);
> TCCR0B |= (0 << CS02);
> 
> ```

is equivalent to:

```arduino
TCCR0B = TCCR0B;

```

---

<div class="post-metadata">

**Author:** ![WB6EYV\_Bob](https://avatars.discourse-cdn.com/v4/letter/w/d6d6ee/32.png) [@WB6EYV\_Bob](https://forum.arduino.cc/u/WB6EYV_Bob)\
**Post date:** [July 29, 2024, 5:24am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/14 "2024-07-29T05:24:15Z")

</div>

I think you spotted the problem, I am new to C... will investigate.

---

<div class="post-metadata">

**Author:** ![WB6EYV\_Bob](https://avatars.discourse-cdn.com/v4/letter/w/d6d6ee/32.png) [@WB6EYV\_Bob](https://forum.arduino.cc/u/WB6EYV_Bob)\
**Post date:** [July 29, 2024, 5:56am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/15 "2024-07-29T05:56:26Z")

</div>

Well that helped, but still not working right... syntax of C boolean operators is new to me, so I just assigned equivalent numeric values to the registers instead. That made a difference, but changes to OCR0A register still do not change the signal frequency.

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [July 29, 2024, 6:09am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/16 "2024-07-29T06:09:30Z")

</div>

> [@Grumpy\_Mike](#):
>
> > [@WB6EYV\_Bob](#):
> >
> > #define OutputPin 12 // output pin for TMR0 signal
> 
> Do not use this. Use instead
> 
> ```arduino
> // Definitions
> volatile byte OutputPin 12 // output pin for TMR0 signal
> 
> ```
> 
> All variables used inside and outside of an ISR must be defined as a volatile type.

It's not a variable, it's a constant.

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [July 29, 2024, 6:19am UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/17 "2024-07-29T06:19:59Z")

</div>

Timer0 is initialized by the Arduino core, so the initial content of registers may not be what you think it is.  
For that reason, you should use full byte-wide assignments, rather than setting individual bits:

```arduino
 TCCR0A = 1<<WGM01;
 TCC0B = (1<<CS00);

```

(also, timer0 is used for the millisecond clock (millis() and etc.) Experimentation would be better done with one of the other timers (timer2 is similar.)

---

<div class="post-metadata">

**Author:** ![WB6EYV\_Bob](https://avatars.discourse-cdn.com/v4/letter/w/d6d6ee/32.png) [@WB6EYV\_Bob](https://forum.arduino.cc/u/WB6EYV_Bob)\
**Post date:** [July 29, 2024, 1:15pm UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/18 "2024-07-29T13:15:39Z")

</div>

Thanks, someone else pointed out that TMR0 is ( otherwise ) committed to other tasks.

It also seems I don’t yet understand the Boolean syntax of C statements, so I have switched to byte-wide register loads, as you suggested.

That yielded some improvements, but it is still not working right…changes to the COMPARE A register do not yield timing changes in the output pulses I see on my scope.

Running out of prospects here, and most suggestions from this forum have been “cosmetic” and do not address the core problem I have…

I suppose I should use a different TMR ( as you suggest ) just to eliminate the prospect of interference from some other TMR0 routine, running in the background s/w.

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [July 29, 2024, 4:55pm UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/19 "2024-07-29T16:55:32Z")

</div>

sketch in post#3 changed. use `OCR2A `

---

<div class="post-metadata">

**Author:** ![WB6EYV\_Bob](https://avatars.discourse-cdn.com/v4/letter/w/d6d6ee/32.png) [@WB6EYV\_Bob](https://forum.arduino.cc/u/WB6EYV_Bob)\
**Post date:** [July 29, 2024, 5:28pm UTC](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158/20 "2024-07-29T17:28:22Z")

</div>

OK, I’ll migrate everything to TMR2… it will take several minutes to do that and test it.

[Next page](https://forum.arduino.cc/t/help-with-nano-tmr0-compare-interrupt-example/1286158.md?page=2)
