timer Problem

Hey

I want to use the timer0 on my arduino nano (atmega328) but it doesn't work.

void setup() { 
  //---------------------------------------  
  Serial.begin(115200); 
  //---------------------------------------  
  //intialize Timer
  cli();
  TIMSK0 = _BV(OCIE0A);  // Enable Interrupt TimerCounter0
  TCCR0A = _BV(WGM01);  // Mode = CTC
  TCCR0B = _BV(CS01)|_BV(CS00);   // 64
  OCR0A = 25;          // 0,0001s
  sei();
  //----------------------------------------
  DDRB = DDRB|B00111011;
}

void loop() {

     Serial.print(digitalRead(10));
     Serial.println();
  
}
ISR(SIG_OUTPUT_COMPARE0A)
{
  Serial.println("Test");
}

Where is my mistake?

Where is my mistake?

You didn't tell us what you thought the code was supposed to do, or what it actually does.

nikolausi:
Where is my mistake?

Apart from under-specifying the question, the mistake is possibly that you put a Serial.println inside an interrupt service routine. I wouldn't be doing that.

But saying "it doesn't work" is really unhelpful. Does it print "Test" too often? Too little? Never? Does the board explode into flames?

Does the board explode into flames?

Only once.

It doesn't print test. And it goes only one into the loop. If I delete the sei() the loop works. I think the problem is the atmega328 because if I use the nano atmega168 the code works.

Oh? I pasted your code into the IDE, compiled for my Uno (an Atmega328) and ran it. I got this:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

So that's hardly "one into the loop", whatever that means exactly.

If I delete the sei() the loop works.

In what sense? What do you see?

...if I use the nano atmega168 the code works...

In what sense? What do you see?

PaulS:

Where is my mistake?

You didn't tell us what you thought the code was supposed to do, or what it actually does.

I think I'll have to strongly agree with Paul here. Tell us exactly what you expected to see, and what you actually saw. This talk about "it works" is meaningless.

The code works, it maybe isn't doing what you expect.

ok there must be a problem with the interrupt.
Here is a another code a LED blinking code. On an atmega168 the LED blinks and on the atmega328 nothing happened.

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
  OCR0A = 25;          // 0,0001s
  sei();
  //----------------------------------------
  DDRB = DDRB|B00111111;    
}

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

Amazing as this may be...

In the AVR-libc documentation for interrupt.h, the output compare match A signal flag is not defined for ATmega328. :expressionless:
Could someone check this out? avr-libc: <avr/interrupt.h>: Interrupts

However, I think I know why you're not getting consistent results... if I remember correctly, the IDE will compile using AVR-GCC 3 for the ATmega168, and AVR-GCC4 for the ATmega328.

If you also have a look at the documentation, you should use:

ISR(TIMER0_COMPA_vect) {}

instead of

ISR(SIG_OUTPUT_COMPARE0A){}

Give it a go and see what you get.

Thank you

Now it works

What do you know... I got one right! :slight_smile:

To do list for Sunday:

- Help someone without being difficult.

Come on guys, the timer configuration looked right, the registers matched the intended configuration. The loop() was quite small... and obviously the Serial.print inside the interrupt routine, although wrong for this type of Serial port (not buffered), was to test if the code would go into the ISR.

It's ok to go mental when people don't post their code, or setup or anything like they do sometimes and even though I do like to "be funny", there was enough to notice a problem in this post. I'm not half as experienced or knowledgeable as any of you and got it.

Well done bubulindo! Believe it or not I was on the track, as I was looking at the ISR vector names:

#define TIMER0_COMPA_vect		_VECTOR(13)
#define SIG_OUTPUT_COMPARE0A		_VECTOR(13)
#define SIG_TIMER0_COMPA		_VECTOR(13)

In particular I noted they seemed to vary by processor, exactly what you would expect from the reported symptoms.

I had been plugging in different values but the ones I tried at the time didn't work. Then sleepiness overcame me zzzzzzz.

Good detective work, glad the problem is solved.

What puzzles me a bit is, that although I was pursuing this line, the documentation for the Atmega shows that "Timer/Counter0 Compare Match A" is interrupt vector 15 on both the 168 and the 328. So I wasn't sure I was on the right path. Are the defines themselves wrong? These are the files that define TIMER0_COMPA_vect:

---------- Find in Files ----------
> Searching for the string 'TIMER0_COMPA_vect'...
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\arduino\bootloaders\stk500v2\avrinterruptnames.h(77,8): #ifdef TIMER0_COMPA_vect
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\io90pwm2b.h(1300,9): #define TIMER0_COMPA_vect   _VECTOR(16)  /* Timer/Counter0 Compare Match A */
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\io90pwm3b.h(1300,9): #define TIMER0_COMPA_vect   _VECTOR(16)  /* Timer/Counter0 Compare Match A */
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iom1284p.h(1055,9): #define TIMER0_COMPA_vect _VECTOR(16)  /* Timer/Counter0 Compare Match A */
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iom168p.h(801,9): #define TIMER0_COMPA_vect _VECTOR(14)  /* TimerCounter0 Compare Match A */
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iom328p.h(802,9): #define TIMER0_COMPA_vect _VECTOR(14)  /* TimerCounter0 Compare Match A */
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iom32c1.h(1067,9): #define TIMER0_COMPA_vect   _VECTOR(15)  /* Timer/Counter0 Compare Match A */
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iom32hvb.h(831,9): #define TIMER0_COMPA_vect  _VECTOR(17)  /* Timer 0 Comapre Match A */
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iom32m1.h(1315,9): #define TIMER0_COMPA_vect   _VECTOR(15)  /* Timer/Counter0 Compare Match A */
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iom32u4.h(1415,9): #define TIMER0_COMPA_vect   _VECTOR(21)  /* Timer/Counter0 Compare Match A */
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iom32u6.h(1314,9): #define TIMER0_COMPA_vect      _VECTOR(21)  /* Timer/Counter0 Compare Match A */
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iom48p.h(797,9): #define TIMER0_COMPA_vect _VECTOR(14)  /* TimerCounter0 Compare Match A */
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iom88p.h(801,9): #define TIMER0_COMPA_vect _VECTOR(14)  /* TimerCounter0 Compare Match A */
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iomx8.h(677,9): #define TIMER0_COMPA_vect		_VECTOR(14)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iomxx0_1.h(1386,9): #define TIMER0_COMPA_vect		_VECTOR(21)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iomxx4.h(794,9): #define TIMER0_COMPA_vect		_VECTOR(16)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iomxxhva.h(490,9): #define TIMER0_COMPA_vect		_VECTOR(12)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iotn167.h(761,9): #define TIMER0_COMPA_vect   _VECTOR(10)  /* Timer/Counter0 Compare Match 0A */
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iotn2313.h(556,9): #define TIMER0_COMPA_vect		_VECTOR(13)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iotn48.h(697,9): #define TIMER0_COMPA_vect _VECTOR(12)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iotn88.h(698,9): #define TIMER0_COMPA_vect _VECTOR(12)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iotnx5.h(391,9): #define TIMER0_COMPA_vect		_VECTOR(10)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iotnx61.h(495,9): #define TIMER0_COMPA_vect		_VECTOR(14)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iousbxx2.h(735,9): #define TIMER0_COMPA_vect		_VECTOR(19)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iousbxx6_7.h(1225,9): #define TIMER0_COMPA_vect		_VECTOR(21)
> 25 occurrence(s) have been found.

And these for SIG_OUTPUT_COMPARE0A:

---------- Find in Files ----------
> Searching for the string 'SIG_OUTPUT_COMPARE0A'...
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\io90pwm1.h(1023,9): #define SIG_OUTPUT_COMPARE0A		_VECTOR(16)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\io90pwmx.h(1256,9): #define SIG_OUTPUT_COMPARE0A		_VECTOR(16)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iomx8.h(678,9): #define SIG_OUTPUT_COMPARE0A		_VECTOR(14)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iomxx0_1.h(1387,9): #define SIG_OUTPUT_COMPARE0A		_VECTOR(21)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iomxx4.h(795,9): #define SIG_OUTPUT_COMPARE0A		_VECTOR(16)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iotn13.h(306,9): #define SIG_OUTPUT_COMPARE0A		_VECTOR(6)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iotn2313.h(557,9): #define SIG_OUTPUT_COMPARE0A		_VECTOR(13)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iotnx4.h(420,9): #define SIG_OUTPUT_COMPARE0A		_VECTOR(9)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iotnx5.h(392,9): #define SIG_OUTPUT_COMPARE0A		_VECTOR(10)
C:\Documents and Settings\Owner\Desktop\arduino-0022\hardware\tools\avr\avr\include\avr\iotnx61.h(496,9): #define SIG_OUTPUT_COMPARE0A		_VECTOR(14)
> 10 occurrence(s) have been found.

Actually I went through this when upgrading my board from a 168 to a 328. All of a sudden, my interrupts stopped working. All of them.

So, I had a look in the documentation and renamed my interrupts. That worked.

I think the website I pointed might have a missing chip on that specific interrupt, quite easy to overlook when writing all that info. But the definition is there and it works. I may send an email to the guys that maintain it and see what they say.