Atmega324p read from SRAM

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

I've been trying to learn about micro controllers and assembly for more than a week now. as i learn more I'm trying to improve the code I've written. I had a code that would display numbers on 7 segment display and it worked as intended but I wanted to have shorter code so after I learned few things about storing data in sram i made something like this: 

.equ startPtr = 0x0700

init:
	ldi r16,0b00111111
	sts startPtr+0,r16
	ldi r16,0b00000110
	sts startPtr+1,r16
	ldi r16,0b01011011
	sts startPtr+2,r16
	ldi r16,0b01001111
	sts startPtr+3,r16
	ldi r16,0b01100110
	sts startPtr+4,r16
	ldi r16,0b01101101
	sts startPtr+5,r16
	ldi r16,0b01111101
	sts startPtr+6,r16
	ldi r16,0b00000111
	sts startPtr+7,r16
	ldi r16,0b01111111
	sts startPtr+8,r16
	ldi r16,0b01101111
	sts startPtr+9,r16
	ret

basically I'm storing binary code for every number. I can load them whenever I want by typing lds r16,startPtr+[Number I Want To Display]. but I have to type the number by hand.

instead im trying to figure out if something like this is possible 

ldi r20,3
lds r16,startPtr+r20

so i would just have variable for the number i want to display instead of making lots of "cpi" commands like i did here 

cpi r20,0x00
breq zero
cpi r20,0x01
breq one
cpi r20,0x02
breq two
cpi r20,0x03
breq three
cpi r20,0x04
breq four
cpi r20,0x05
breq five
cpi r20,0x06
breq six
cpi r20,0x07
breq seven
cpi r20,0x08
breq eight
cpi r20,0x09
breq nine

 

This topic has a solution.
Last Edited: Thu. Jun 10, 2021 - 05:42 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

  if something like this is possible 

Yes, probably.  It depends on exactly which AVR you have, but there is an "indirect with displacement" addressing mode that uses the Y(r29:r28) or Z (r31:30) index registers with an offset.

(actually, for your application, you'd probably add the offset manually and just use indirect addressing, since the "offset" can only be <64.)

 

Looks like there is a good tutorial here: http://www.rjhcoding.com/avr-asm...

 

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

thanks, that link helped a lot. if anyone needs this for the future this is what i did:

ldi r21,LOW(startPtr) // store last 2 digits of address in r21
add r21,r20 // increase r21 by whatever value we have in r20
mov	XL,r21	// initialize X pointer
ldi	XH,HIGH(startPtr)
ld r20,X // write whatver value we have at X address in r20
ret //return 

 

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

Bos5 wrote:

	ldi r16,0b00111111
	sts startPtr+0,r16
	ldi r16,0b00000110
	sts startPtr+1,r16
	ldi r16,0b01011011
	sts startPtr+2,r16
	ldi r16,0b01001111
	sts startPtr+3,r16
	ldi r16,0b01100110
	sts startPtr+4,r16
	ldi r16,0b01101101
	sts startPtr+5,r16
	ldi r16,0b01111101
	sts startPtr+6,r16
	ldi r16,0b00000111
	sts startPtr+7,r16
	ldi r16,0b01111111
	sts startPtr+8,r16
	ldi r16,0b01101111
	sts startPtr+9,r16

I know you have a solution but I can't help noticing in this sequence the +N offset increases by 1 in each case. So maybe:

    ldi R30, low(startPtr)
    ldi R31, high(startPtr)
    ldi r16,0b00111111
    sts Z+,r16
    ldi r16,0b00000110
    sts Z+,r16
    ldi r16,0b01011011
    sts Z+,r16
    ldi r16,0b01001111
    sts Z+,r16
    ldi r16,0b01100110
    sts Z+,r16
    ldi r16,0b01101101
    sts Z+,r16
    ldi r16,0b01111101
    sts Z+,r16
    ldi r16,0b00000111
    sts Z+,r16
    ldi r16,0b01111111
    sts Z+,r16
    ldi r16,0b01101111
    sts Z+,r16

But also have you considered, rather than all those LDIs, storing the data bytes in a memory array:

start:
    rjmp over_data

    thedata: .db 0b00111111, 0b00000110, 0b01011011, 0b01001111, 0b01100110, 0b01101101, 0b01111101, 0b00000111, 0b01111111, 0b01101111

over_data:
    ldi R26, low(thedata << 1)
    ldi R27, high(thedata << 1)
    ldi R28, low(startPtr)
    ldi R29, high(startPtr)
    ldi R17, 10
loop:
    ld R16, X+
    st Y+, R16
    dec R17
    BRNE loop

I'll leave you to work out what's going on there.

 

(hope I got it right - I haven't actually tried that!)