Timer0 overflow interrupt on Atmega8 in assembler

Go To Last Post
17 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone.
I tried to arrange timer0 overflow interrupt on Atmega8, but it dosn't work.
I checked it in Atmel Studio 7.
I would be very happy, if someone points me to my error(s), please.

.include	"m8def.inc"
.list
.def		temp0 = R16

.cseg
.org			0

rjmp			init			; Переход на начало программы
rjmp			return			; External Interrupt 0
rjmp			return			; External Interrupt 1
rjmp			return			; Timer2 Compare A Handler
rjmp			return			; Timer2 Overflow Handler
rjmp			return			; Timer1 Capture Handler
rjmp			return			; Timer1 Compare A Handler
rjmp			return			; Timer1 Compare B Handler
rjmp			return			; Timer1 Overflow Handler
rjmp			TIM0			; Timer0 Overflow Handler
rjmp			return			; SPI Transfer Complete Handler
rjmp			return			; USART, RX Complete Handler
rjmp			return			; USART, UDR Empty Handler
rjmp			return			; USART, TX Complete Handler
rjmp			return			; ADC Conversion Complete Handler
rjmp			return			; EEPROM Ready Handler
rjmp			return			; Analog Comparator Handler
rjmp			return			; TWI Two-wire Serial Interface
rjmp			return			; SPM_RDY Store Program Memory Ready

init:
ldi			temp0, low(RAMEND)
out			SPL, temp0
ldi			temp0, high(RAMEND)
out			SPH, temp0

ldi			temp0, 0xFF
out			DDRD, temp0
ldi			temp0, 0x00
out			PORTD, temp0

;ldi			temp0, 0b00000000
;out			GICR, temp0

ldi			temp0, 0b00000001
out			TIMSK, temp0

ldi			temp0, 0b11111100
out			TCNT0, temp0

ldi			temp0, 0b00000001
out			TCCR0, temp0

sei

Loop:
rjmp  Loop

; --------------------Interrupts Handlers
return:
reti

TIM0:
ldi			temp0, 0xff
out			PORTD, temp0
reti

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Forget interrupts...does the chip work at all?  Can you toggle any pins, blink an led?

 

try setting portD to FF in your init...do the pins go high? 

 

....now back to interrupts

    note you didn't save/restore status  sreg register in your IRQ...probably  "OK" for your short IRQ   & do-nothing main waiting loop...however please include now .....so you don't forget later.

 

Do you not see the port D go high & stay high when you run your IRQ program?

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Offhand I don't see any errors in your code (but that's harder to check when you use magic numbers like 0b00000001 instead of identifiers as found in the data sheet).  Is there a reason you're loading TCNT0 with 0xFC?  

 

So start backing up.

 

Can you blink an LED?  Or, do you have a scope where you can see output pin activity?

 

If so, can you get the timer overflow working in polled mode?  Set the longest prescaler for LED, otherwise any prescaler if you have a scope.  Start the timer and sit in a loop waiting for the overflow flag to be set.  Then toggle an output pin, clear the overflow flag (by writing a '1' to the bit) and repeat.  You should see a square wave.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for replies.

I don't have real circuit assembled, but I tried this code in latest Atmel Studio and not t latest Proteus 8.

The LED, connected in port D didn't blink (in Proteus).

I didn't save/restore status register intentionally to do code less complicated, and also because this code (TIM0:...) never starts.

After Timer0 overflows, bit TOV0 becomes set, but don't going to TIM0: and continues duing Loop:

I loading TCNT0 with 0xFC just to speed up step-by-step checking code in Atmel Studio (overflow becomes quickly).

I am in in desperation to loose a whole day for this matter.....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The problem is the Timer0 overflow routine never starts, but in the main code everything executes properly (if I write to PORTD 0xff, whole PORT D will high).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, it would have been good if you had included all that extra info in your first post, instead of just saying it doesn't work.

 

Now we're getting down into the strange stuff.

 

Can you see in the debugger that TOIE0 is actually set?

 

Can you put a breakpoint in your default interrupt handler?  (though it seems you are selecting the correct vector)

 

Are you sure you're building for the right chip?

 

Can you get ANY interrupts to work in the simulator?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:
try setting portD to FF in your init...do the pins go high? 

Are you connected to something active-high or active-low?

 

How are you determining that it "doesn't work"?

 

To be surer, most would make the vector table with an .ORG for each entry.  As it is, to go further down that path I'd need to count all your RJMPs and compare to datasheet.  Easier for you with the .LST file.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

s it is, to go further down that path I'd need to count all your RJMPs and compare to datashee

I did when I looked...it was good to at least his IRQ.

 

===================================

 

Why are you not using a chip?  How do you know it isn't a problem with your simulator settings?  What are your simulator settings?

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I do not see any mode setting for Timer0. But, then again, who can tell with all those magic numbers? Certainly nothing to any of the timer control registers! And, that also means the prescaler is not set and default is NO CLOCK (e.g. timer is not clocked). No clock, no overflow, no interrupt.

 

Jim

 

 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sat. Jun 22, 2019 - 10:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your sketch, as included in your original message, seems to work fine in the AS7 simulator, but NOT if you just single-step through the code.

Are you letting the Proteus simulator "run", or are you also trying to single-step?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No clock, no overflow, no interrupt

A common oversight, however one of his bits is setting the clock to divide by 1

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I thoroughly checked the code work, and discovered, that it works properly in Proteus 8 - interrupt routine code executes.
But in latest Atmel Studio I tried to execute code step-by-step it anyway doesn't come to interrupt routine code TIM0:
Just execute code in Loop:
So, I decided, that "timer0 overflow interrupt on Atmega8 doesn't work"
Why it is so different in Proteus 8 and Atmel Studio I don't know.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Because stepping in Atmel Studio disables interrupts. Set a breakpoint in the interrupt handler and run, or turn off interrupt masking by going to Tools/Options/Tools/Mask interrupts while stepping

:: Morten

 

(yes, I work for Atmel, yes, I do this in my spare time, now stop sending PMs)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So, I decided, that "timer0 overflow interrupt on Atmega8 doesn't work"

Well, it certainly works in the chip....perhaps time to try one! 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Interrupts work definitely with my AS7 simulator.

 

 

Oh, is that a thing about this?

http://microchip.force.com/micro...

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kabasan wrote:
Oh, is that a thing about this?

Try this link without trailing punctuation:

http://microchip.force.com/micro...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Or y'all could just read #13 perhaps ;-)