MCU : ATTiny85
Fuses: (low) e2; (high) df.
A stripped-down program is shown below.
The datasheet says that if, in TCCR0A, COM0A[1:0] = 0b11, then the output pin (OC0A) is set high on compare match - and so it is. I just can't figure out how to set it back to zero.
In AS-7, the interrupt works OK, and TCNT0 is reset to zero. PIN0 (on pin 5) goes high and stays high. That looks suspicious to me - I had expected PORTB0 to high, not PIN0. When I run it on a chip, OC0A (on pin 5) goes high and stays high. I tried setting it low in the ISR by doing
... but that doesn't work. The output compare setting seems to override it.
How can I set PORTB0 back to a zero, so it can go high again next compare interrupt?
The program :
.org 0x0 rjmp main ; Reset - Address 0 rjmp INT0_handler ; vector for INT0 (address 01) reti reti reti rjmp TCNT0_overflow ; Vector for Timer/Counter0 Overflow reti reti reti reti rjmp TCNT0_compare_a INT0_handler: ; INT0 vector points here in R15,SREG ; save the status register ; lpm R4,Z+ ; out TCNT0,R4 clr R4 out PORTB,R4 out SREG,R15 reti TCNT0_overflow: ; increment overflow counter in R15,SREG ; out SREG,R15 ; restore SREG reti TCNT0_compare_a: in R15,SREG ; clr R2 out PORTB,R2 ; >>>>>> THIS DOESN'T WORK! <<<<<<< out SREG,R15 ; restore SREG reti ; ================================================== main: ;Initialize the AVR microcontroller stack pointer ldi R16, low(RAMEND) out SPL, R16 ldi R16, high(RAMEND) out SPH, R16 ; MCU setup ldi R16,0x03 out DDRB,R16 ; PB0, PB1 OUT. All other pins IN. PB0 is OC0A ldi R16,0xc2 ; "c" sets OC0A high on compare. "2" sets CTC mode out TCCR0A,R16 ; Pin 5 = PB0. Set to 0xc0 to make pin5 = OC0A ldi R16,0x02 ; <<==== 02 = Clk/8; Change to 04 for Clk/256 out TCCR0B,R16 ; CLK prescaler ldi R16,0x80 out GTCCR,R16 ; Synch mode in CTC mode protects timer prescaler from being reset by hardware ldi R16,0x40 out GIMSK,R16 ; enable external INT0 ldi R16,0x10 ; OCIE0A out TIMSK,R16 ; Enable the Compare interrupt ldi R16,0x03 ; Trigger INT0 on rising edge out MCUCR,R16 ; Enable INT0 interrupt on rising edge ldi R16,4 ; aritrary value for AS-7 simulation out OCR0A,R16 sei ; Enable interrupts again: rjmp again