SPH and SPL didn't update (asm for ATmega328p)

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

I used these code:

 

START:
ldi regCommon, LOW(RAMEND)		; init stackpointer
out SPL, regCommon
ldi regCommon, HIGH(RAMEND)
out SPH, regCommon

but, I found in Atmel Studio 7.0, either in 0x3D 0x3E or 0x5D 0x5E are "00 00", why?

 

the whole program is attachment (interrupts vector address problem has been fixed, so needn't think about it).

Attachment(s): 

This topic has a solution.
Last Edited: Tue. Nov 3, 2020 - 02:01 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I found in Atmel Studio 7.0, either in 0x3D 0x3E or 0x5D 0x5E are "00 00"

 how did you "find" that?  SP doesn't even initialize to 0 on Atmega328 (it gets set to RAMEND for you, but hardware.)

 

cseg
.org 0x00
rjmp START; reset
reti; INT0
reti; INT1
reti; PCINT0
reti; PCINT1
reti; PCINT2
reti; WDT

You can't use reti for unused interrupt vectors, because vectors on a 328p are 2 words long.

 

 

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

westfw wrote:
You can't use reti for unused interrupt vectors, because vectors on a 328p are 2 words long.

 

This problem has been solved in last topic ask for code testing (asm for ATmega328p)

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

westfw wrote:
how did you "find" that?  SP doesn't even initialize to 0 on Atmega328 (it gets set to RAMEND for you, but hardware.)

 

You are right, I found the reason: mixed the instructions "sbr cbr" with "sbi cbi", now it was OK.

 

And, where could I "find" the contents in SPL and SPH during debugging in Atmel Studio 7.0?

Last Edited: Fri. Oct 30, 2020 - 04:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MianQi wrote:

And, where could I "find" the contents in SPL and SPH during debugging in Atmel Studio 7.0?

to were you initialized the Stack pointer to point to.

Initially you have to set it to a specific location, which normally is RAMEND, defining the top of the RAM area.

 

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

A couple of things:

 

 

This shows where you can see the current value of SP. As you can see, even before lines 4..7 have run it has already been set to 0x8FF. The 328P (like most modern AVR) does this - it sets SP to RAMEND at power on anyway. So in theory you don't need the code to do it.

 

Another way I could see SP is here which is more obvious if I change the code to write a recognizable value:

 

 

Something interesting has also happened there - I tried to set 0x1234 and got 0x0234. This shows that the SP is not 16 bits. In fact it appears to be 12 bits. If you write 0xFFFF you will find 0x0FFF.

 

 

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

clawson wrote:
even before lines 4..7 have run it has already been set to 0x8FF. The 328P (like most modern AVR) does this - it sets SP to RAMEND at power on anyway.

 

Sure, here it is:

 

initial Stack Pointer value.

 

Attachment(s): 

Last Edited: Mon. Nov 2, 2020 - 09:16 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

11 bits of Stack Pointer.

clawson wrote:
This shows that the SP is not 16 bits. In fact it appears to be 12 bits.

 

I found it was 11 bits in its datasheet, but I can store 0fff into it, I don't know why.

Attachment(s): 

Last Edited: Mon. Nov 2, 2020 - 09:17 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

maybe the reason is here:

" The number of bits actually used is implementation dependent "

The number of bits actually used is implementation dependent

 

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

MianQi wrote:
but I can store 0fff into it,
Well you can't have fractional bits. So number of bits limits counting ranges to things like 0x1FF, 0x3FF, 0x7FF, 0xFFF. As the 328P has a 0x100 offset for SFR and then 0x800 bytes of RAM it means it ends at 0x8FF but you could not have that if the upper limit was 0x7FF so the only logical thing it can be is 0xFFF and that means 12 bits. So if the datasheet says 11 bits it is wrong.

Last Edited: Mon. Nov 2, 2020 - 11:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Atmega328PB stackpointer is 12 bits:

 

 

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

clawson wrote:

fractional bits

 

What does that mean?

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

fractional bits

What does that mean?

If RAM starts at 256 (as it does on some AVR) and goes for 2K, the top address is 2304, which sort-of takes 11.17bits to represent.  But you can't have a fraction of a bit, so you have to use 12 bits.

 

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

2,304.119776642681634396779417722

 

It confused me for minutes.

 

Thanks for your excellent explanation.