[ASM] Mega644 && Timer2 = StackOverflow?!

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

Hey Freaks,
I've read abc's, Timers ROCK and some of the 644's Datasheet.

My final aim is to produce a bunch of independant PWM's ... say 20. To get a start I tried to translate miniusers CTC stuff into ASM. It didn't work so "hey let's start with counting again" I read through the datasheet and configured every T2 reg as I pleased. I came up with this:

Setup:
- AVR Studio 4.13
- Assembler
- AVR Simulator
- ATMega644

current aim:
- run Timer2 in normal mode, no prescale, preload 0x55

Problem is: if I click on "run" or "auto step" I get the messages
AVR Simulator: Unititializes Stackpointer used at 0x003B
invalid opcode 0x00B2 at adress 0x00002A
stack overflow at 0x003B (repeats endlessly)

and the PC jumps from

rjmp main

to

;[OCR2A]	OutputCompare Register
	clr w

If I comment out "sei" it doesn't appear and jumps properly. But TCNT2 isn't increasing.

Is the overflow a problem of AVRsim or is it my fault? Is there anything wrong with my timer config?
Here's my complete code:

;===========================================================================
; 					     Processor specific stuff
;===========================================================================
.device ATmega644
.include "m644def.inc"
;===========================================================================
;						 	work registers
;===========================================================================
;[work registers]
.def   	u		=	r16
.def	w		=	r17 

;===========================================================================
;						     Vector Table
;===========================================================================
.cseg
.org   	$0000	RJMP reset
.org	$0016	RJMP TIM2_OVF
RETI

reset:
	RJMP init
;===========================================================================
;							Initialization
;===========================================================================
init:
;[INIT STACK]
	ldi w, high(RAMEND)
	sts SPH, w
	ldi w, low(RAMEND)
	sts SPL, w
;[INIT PORTD]
	ser w					
	out DDRD, w				;all PORTD Pins are output
	clr w
	out PORTD, w			;all PORTD Pins are High
;[INIT TIMER 2]			
	;[TCCR2A] 0 0 0 0 0 0 0 0 TimerCounterControll Register
	clr w
	sts TCCR2A, w
	;[TCCR2B] 0 0 0 0 0 0 0 0 no prescale
	sts TCCR2B, w
	;[TCNT2]	Timer/Counter Register
	ldi w, 0x55
	sts TCNT2, w
	;[OCR2A]	OutputCompare Register
	clr w
	sts OCR2A, w
	;[OCR2B]	
	sts OCR2B, w
	;[ASSR] 	Asynchronus Status Register
	sts ASSR, w
	;[TIMSK2]	Interrupt on Overflow
	ldi w, 0x01
	sts TIMSK2, w
	;[TIFR2]	TimerInterruptFlag Register
	clr w
	sts TIFR2, w
	;[GTCCR]	GeneralTimer/Counter Controll Register
	sts GTCCR, w
sei
jmp main

;===========================================================================
;							        MAIN
;===========================================================================
main:
rjmp main
;===========================================================================
;							     Interrupts
;===========================================================================
TIM2_OVF:
	inc w
	out PORTD, w
reti 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
.device ATmega644
Not needed. By the way are you using M644 or M644P?
.org   $0016   RJMP TIM2_OVF 
RETI 

What's that RETI doing there?

 RJMP init 
;=========================================================================== 
;                     Initialization 
;=========================================================================== 
init:

Not really needed, just rename init: to reset:

All rjmp should be jmp in the vectors. Somehow you are triggering INT0 etc. (code jumps to 0x0002 which is 0xffff) so do a bit more init in your code.

DO NOT TRUST THE SIMULATOR. :?

edit simulator2 behaves a little better but the INT0 problem is still there.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
;[INIT STACK] 
   ldi w, high(RAMEND) 
   sts SPH, w 
   ldi w, low(RAMEND) 
   sts SPL, w 

What makes you think you can use STS with the stack pointer anyway? :) Have a good look at the rest of your init also.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I orderd ATMega644-20PU-ND. That's M644, isn't it?

The RETI was supposed to handle unexpected interrupts. Better: I'll write down the complete vector table and do jumps to some kind of error ISR.

I ususally used OUT instead of STS. Since OUT doesn't work with some config registers of the M644 I figured I'd use STS for all.

Ok, next step will be a complete Init for all config regs ... good thing is that it only needs to be once. Plus it's a reason to start with multiple file assembly ^^

Thanks for your help, John :)

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

Try using the macros in AVR001: Conditional Assembly and portability macros:
http://www.atmel.com/dyn/resourc...
http://www.atmel.com/dyn/resourc...

The LOAD and STORE macros will keep you from ever making that type of sts vs. out mistake ever again.

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

Quote:
That's M644, isn't it?
Yes but it is NOT the same as the Mega644p, just in case you wonder in the future. :)
Quote:
I figured I'd use STS for all.
Apart from using the macros as suggested above you CAN use sts >> IF << you offset the address by 0x20 as shown in the datasheet (addresses in parenthesis). The address for ports in the 0x00 - 0x3f range will be defined for the OUT instruction otherwise.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

That macro stuff is very helpfull :)
I've had chosen the wrong mega at the simulater menue. After making a new project and fixing a init error TCNT2 is running. The IRQ bug is gone as well.

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

Go and buy yourself a drink!! :)

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly