Hallo,
I'm playing with some C code, that sets and clears LED13 on pin PB5 (atmega328p).
Everything goes well and I can see 50% duty waveform on PB5. (see. Picture 1)
The problem raises if I enable interrupt and I run timer1 in CTC mode.
Then the waveform on PB5 is wired (see. Picture 2).
I'm expecting that my interrupt handler is short enough and I should get also symmetrical 50% duty waveform.
What happens if you use the built in timer based delay function instead of delay1()?
With a bunch of interrupts firing, I don't like relying on counting nops to calculate a time. When you know that 1000nops is all you did its fine. But if you might have been running other interrupt code at the same time then your 1000nops might take a lot more than 1000 cycles to run through if they're being interrupted for several cycles at a time.
Thank you for advice.
Problem is solved!
It seems that the Arduino IDE silently starts timer0and adds timer0 int. handler even if you don't use delay() function!
$ avr-objdump -d -m avr5 test.elf
00000090 <setup>:
90: 25 9a sbi 0x04, 5 ; 4
92: 2d 9a sbi 0x05, 5 ; 5
94: 14 bc out 0x24, r1 ; 36 <-- here
96: 15 bc out 0x25, r1 ; 37 <-- here
I can stop it by TCCR0B=0.
I have also added some void handlers for timer1.
The 10000 nops delay is there just for debuging purpose, to show what is happening inside.
But having timer0 running shouldn't cause the symptoms you see since it only
runs every 1.024ms and doesn't do much, a few microseconds jitter is all you should
see.
The timer0 ISR handles millis(), so it has to run from the start.
I think there was one more bug in the first posted code. The timer1 in CTC mode fires two interrupts (OVF and COMPA) and I wrote only COMPA handler.
It seems that accidental unhandled interrupts will cause warm restart :o
frpr666:
Thank you for advice.
Problem is solved!
It seems that the Arduino IDE silently starts timer0and adds timer0 int. handler even if you don't use delay() function!
The function init() (called before setup() ) does that. You can write to only use main() and that problem goes away.