Is it load stack is a must when using subroutine?

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

hi all.

My asm code just to blink the leds. For my first code, i didnt add in the load stack step which is as follow:

	ldi R16,low(RAMEND)
	out SPL,R16

	ldi R16,high(RAMEND)
	out SPH,R16

the LEDs wont blink.But once i add in these steps.it works and start to blink.

From what i had experience with other chip like M**rochip.the asm code dont have to consider about this when applying subroutine. So can anyone explain why could this happen?

thank you.

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

Newer AVRs set up the stack for you at power up like M**rochip :shock: but you MUST set up the stack yourself for older AVRs. PICs only have a limited stack anyway.

It is a good habit to setup the stack even if the hardware does it for you, it will save you headache when you change chips or brand of chips.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

hi js,
thanks for the reply.I'm using ATmega32L for my testing.As what i know, ATmega32L should be not included in older AVR list.Or is it?
because when i quote the four instruction steps, the leds wont manage to blink.But yes if i add in.So is it ATmega32L is older AVR?

thank you.

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

Quote:
ATmega32L should be not included in older AVR list.Or is it?
Yep,pretty old in micro years. :) Use a Mega324p.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

hi js,
but if i wrote the blinking code in C.i wont have to worry about this steps.it will work without take care of the load stack part.Any idea what happen in C?

thank you.

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

If you have no subroutines or interrupts (yet), you probably don't need a stack.

If you show the entire program, and perhaps how you are linking it and downloading it, perhaps we can solve the problem.

That code segment you posted doesn't show the org or the loop and delay between toggling the LED.

The beginners' tutorials here would help.

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

hi,
thanks for the reply again.

.include "m32def.inc"

.def temp= r19

	rjmp	start

start:
	ldi R16,low(RAMEND)
	out SPL,R16

	ldi R16,high(RAMEND)
	out SPH,R16

	ldi	temp,0xff
	out DDRB, temp
loop:
	ldi	temp,0x00
	out	PORTB,temp
	rcall delay
	ldi	temp,0xFF
	out	PORTB,temp
	rcall delay
	rjmp loop


delay:
	ldi R16,0xff
loop1:
	ldi R21,0xff
loop2:
	dec R21
	brne loop2

	dec R16
	BRNE loop1
	
	ret


above is my blinking asm code.

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

js wrote:
Quote:
ATmega32L should be not included in older AVR list.Or is it?
Yep,pretty old in micro years. :) Use a Mega324p.

hi js,

thanks for this info.After comparing the price.The newer avr is much more cheaper than the oldest avr.thanks.

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

Quote:
but if i wrote the blinking code in C.i wont have to worry about this steps.it will work without take care of the load stack part.Any idea what happen in C

With C, the compiler will set up the stack for you.

Regards,
Steve A.

The Board helps those that help themselves.

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

If you have masochistic tendencies, you may write the application in assembly language.