Today, I -again- tried to get fast PWM working on my ATmega16. Only real hardware as the simulator now seems to repeatedly random-jump into all ISRs in all AVR firmware I write. I guess something's broken there, but re-install? Took me a whole day to get the bi*** running. So: no way!
The code I'm talking about:
.def acc = r16 ; accumulator for calculation ; make D and A outputs ldi acc, 0xFF out DDRD, acc out DDRA, acc ; init timer 0 ldi acc, 0b01001001 ; fast pwm, no prescaler out TCCR0, acc ; enable timer interrupts ldi acc, 0b00000011 ; oc, tof interrupt for timer 0 out TIMSK, acc ; set PWM value ldi acc, 0x20 out OCR0, acc ; clear debug port A ldi acc, 0x00 out PORTA, acc ; enable interrupts sei mainloop:rjmp mainloop timer_overflow_0: ldi acc, 0xFF out PORTD, acc out PORTA, acc reti timer_oc_0: ldi acc, 0x00 out PORTD, acc reti end:rjmp end ; just in case
This code looks fine to me. I checked it several times.
-make ports D and A outputs, D for PWM, A for debug
-init timer 0 as fast PWM with no prescaler (I did test other prescalers - no good)
-enable both interrupts for timer 0
-write OCR0 value to *something*
-clear debug port, just in case
-enable all interrupts
-on overflow: set both ports to 0xFF
-on compare: set portD to 0x00
Here the funny result: the PWM does work. BUT: not only PortD, but also PortA is doing the PWM. PortA is never set back to 0x00, though, it's resetted before the main loop and setted in the overflow-ISR. Should remain 0xFF then, but it doesn't.
I also double checked the wires to and from the ATmega16. Pins 4-6 of PORTD and pins 0-2 of PORTA are routed to the base of a transistor each (with 2k2 ohms resistor), all 6 emitters go to ground (we have NPN transistors), all collectors go to LEDs (with 100 ohms resistor to vcc). Did work previously and the LEDs do light up and they do PWM, so I guess that's still okay.
P.S: I do use the right hex file, I do recompile after every change manually, I always verify the Flash.
Does anyone have *any* idea why PORTA is being reset in timer_oc_0 ISR without even being mentioned?