# TIMER1 Good, Timer0 Bad

**URL:** <https://forum.arduino.cc/t/timer1-good-timer0-bad/912357>\
**Category:** Programming\
**Created:** [October 6, 2021, 4:50pm UTC](https://forum.arduino.cc/t/timer1-good-timer0-bad/912357 "2021-10-06T16:50:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![BanksySan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/banksysan/32/676763_2.png) [@BanksySan](https://forum.arduino.cc/u/BanksySan)\
**Post date:** [October 6, 2021, 4:50pm UTC](https://forum.arduino.cc/t/timer1-good-timer0-bad/912357/1 "2021-10-06T16:50:42Z")

</div>

I thought that the following code would give me change in pin 8's state at about 125KHz.

I think it's setting the overflow interrupt on Timer0.

```cpp
bool state = HIGH;

void setup() {
  pinMode(8, OUTPUT);

  noInterrupts();
  TCCR0A = 0;

  TCCR0B = 0;
  TCCR0B |= (1 << CS02);
  
  TIFR0 |= (1 << TOV0);
  TIMSK0 |= (1 << TOIE0);
  interrupts();
}

void loop() { ; }

ISR(Timer0_OVF_vect) {
  static bool state = LOW;
  digitalWrite(8, state);
  state = !state;
}

```

I don't get any signal (other that some noise). An identical sketch working on Timer1 runs as expected:

```cpp
bool state = HIGH;

void setup() {
  pinMode(8, OUTPUT);

  noInterrupts();
  TCCR1A = 0;
  TCCR1B = 0;
  TCCR1C = 0;

  TCCR1B |= (1 << CS10);

  TIFR1 |= (1 << TOV1);
  TIMSK1 |= (1 << TOIE1);
  interrupts();
}

void loop() { ; }

ISR(TIMER1_OVF_vect) {
  static bool state = LOW;
  digitalWrite(8, state);
  state = !state;
}

```

I thought it could be that the `TIMERx_OVF_vect` casing might be the cause, the camel-case form of `TIMER1` compiles but doesn't produce an output either, however the uppercase version of `Timer1` causes a compilation error.

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [October 6, 2021, 5:05pm UTC](https://forum.arduino.cc/t/timer1-good-timer0-bad/912357/2 "2021-10-06T17:05:41Z")

</div>

Timer0 is used to keep millis() going.

Can you use the output of the hardware timer for the 125kHz, or do you need the interrupt for other things ?

---

<div class="post-metadata">

**Author:** ![BanksySan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/banksysan/32/676763_2.png) [@BanksySan](https://forum.arduino.cc/u/BanksySan)\
**Post date:** [October 6, 2021, 5:07pm UTC](https://forum.arduino.cc/t/timer1-good-timer0-bad/912357/3 "2021-10-06T17:07:34Z")

</div>

I know that, I thought that if I didn't care about millis(), delay() etc. then I could still use it.

I'm not actually changing it at all here, just trying to hook into the overflow interrupt.

---

<div class="post-metadata">

**Author:** ![BanksySan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/banksysan/32/676763_2.png) [@BanksySan](https://forum.arduino.cc/u/BanksySan)\
**Post date:** [October 6, 2021, 5:08pm UTC](https://forum.arduino.cc/t/timer1-good-timer0-bad/912357/4 "2021-10-06T17:08:23Z")

</div>

> [@Koepel](#):
>
> Can you use the output of the hardware timer for the 125kHz, or do you need the interrupt for other things ?

I'm just playing with it, trying to learn what's what.

---

<div class="post-metadata">

**Author:** ![chrisknightley](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chrisknightley/32/637257_2.png) [@chrisknightley](https://forum.arduino.cc/u/chrisknightley)\
**Post date:** [October 6, 2021, 5:15pm UTC](https://forum.arduino.cc/t/timer1-good-timer0-bad/912357/5 "2021-10-06T17:15:17Z")

</div>

Even if you don't need millis() function, you can't use `TIMER0_OVF_vect` because the Arduino Core has defined a Timer0 overflow interrupt (a.k.a. `TIMER0_OVF_vect`).

If you write multiple ISRs of the same vector, a compile error will occur.

Try using `TIMER0_COMPA_vect` instead of `TIMER0_OVF_vect`.  
The overflow interrupt must be released and the compare match A interrupt must be enabled.

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [October 6, 2021, 5:15pm UTC](https://forum.arduino.cc/t/timer1-good-timer0-bad/912357/6 "2021-10-06T17:15:37Z")

</div>

I think that you do not learn by messing around with the Arduino basics. Use Timer1 and Timer2. Learn, for example, about all the different PWM modes, that will keep you busy for a while.

Timer0 is also used by analogWrite() when a PWM pin is selected that is a output of Timer0.

---

<div class="post-metadata">

**Author:** ![BanksySan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/banksysan/32/676763_2.png) [@BanksySan](https://forum.arduino.cc/u/BanksySan)\
**Post date:** [October 6, 2021, 5:19pm UTC](https://forum.arduino.cc/t/timer1-good-timer0-bad/912357/7 "2021-10-06T17:19:08Z")

</div>

> [@chrisknightley](#):
>
> Arduino Core has defined a Timer0 overflow interrupt

That makes sense. Thanks.

---

<div class="post-metadata">

**Author:** ![hmeijdam](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hmeijdam/32/218564_2.png) [@hmeijdam](https://forum.arduino.cc/u/hmeijdam)\
**Post date:** [October 6, 2021, 5:39pm UTC](https://forum.arduino.cc/t/timer1-good-timer0-bad/912357/8 "2021-10-06T17:39:31Z")

</div>

> [@BanksySan](#):
>
> `ISR(Timer0_OVF_vect) {`

I think that should have been spelled  
ISR(TIMER0\_OVF\_vect) {

Then you will get the compilation error that interrupt vector 16 is already defined.

---

<div class="post-metadata">

**Author:** ![hmeijdam](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hmeijdam/32/218564_2.png) [@hmeijdam](https://forum.arduino.cc/u/hmeijdam)\
**Post date:** [October 6, 2021, 5:46pm UTC](https://forum.arduino.cc/t/timer1-good-timer0-bad/912357/9 "2021-10-06T17:46:27Z")

</div>

Without all the Arduino stuff pulled in, it can work

```cpp
bool state = HIGH;

int main(void) {
  pinMode(8, OUTPUT);

  noInterrupts();
  TCCR0A = 0;

  TCCR0B = 0;
  TCCR0B |= (1 << CS02);
  
  TIFR0 |= (1 << TOV0);
  TIMSK0 |= (1 << TOIE0);
  interrupts();
  while(1){};
}

ISR(TIMER0_OVF_vect) {
  static bool state = LOW;
  digitalWrite(8, state);
  state = !state;
}
```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 4, 2022, 5:47pm UTC](https://forum.arduino.cc/t/timer1-good-timer0-bad/912357/10 "2022-04-04T17:47:17Z")

</div>

Closed on April 4, 2022, 5:47pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
