Full Version : SRAM Code Example (ASM)
avr >>ASSMBLER ROUTINES >>SRAM Code Example (ASM)


Admin5- 04-19-2006
Example ASM code of how to use SRAM

CODE

;***************************************************************************

;* PROGRAM

;* Sram

;* DESCRIPTION

;* to show use of SRAM

;* uses the information stored in Sram

;* and outputs the values to portB

;*************************************************************************

 .include "c:\avrtools\appnotes\8515def.inc"

   

 rjmp Init

               

;**************************************************************************

;* FUNCTION

;* Init

;* DESCRIPTION

;* Intialise the Stackpointer

;* Intialise SRAM

;* Disable the Comparator to save power

;* Intialise Port B for Output

;*************************************************************************



Init:                

; **** Stack Pointer Setup Code ****

 ldi r16,$02 ; Stack Pointer Setup

 out SPH,r16 ; Stack Pointer High Byte

 ldi r16,$5F ; Stack Pointer Setup

 out SPL,r16 ; Stack Pointer Low Byte

                     

; **** SRAM Setup Code ****  

 ldi r16,$C0 ; SRAM Access

 out MCUCR,r16 ;  SRAM:SRE bit set,  SRW bit set,

   

            ; **** Comparator Setup Code ****  

 ldi r16,$80 ;

 out ACSR,r16 ; Comparator Disabled to save power

                                   

; **** Port B Setup Code ****  

 ldi r16,$FF ; I/O Direction Values

 out DDRB,r16 ; Port B Direction Register

 ldi r16,$FF ; Init value

 out PORTB,r16 ; Port B value



;**************************************************************************

;* FUNCTION

;* Main

;* DESCRIPTION

;* Sets up the Y and Z pointer

;* Disable the Comparator to save power

;* sets up R20 with number of elements in the table

;*************************************************************************

       

Main:  

               ldi ZL,$F0 ;init z-pointer low byte  

 clr ZH  ;clear z reg high byte    

   

 ldi ZL,low(TABLE*2);init Z-pointer low byte with TABLE

 ldi  ZH,high(table*2);init Z-pointer High byte with TABLE

 ldi YL,$60 ;init Y-pointer low byte  

 clr YH ;Clear Y-pointer High byte

 ldi r20,$08 ;load r20 with 8 decimal



;*************************************************************************

;* FUNCTION

;* Mem

;* DESCRIPTION

;* Stores the information contained in TABLE into SRAM area

;*************************************************************************



Mem:

 lpm  ;load constant from program

   ;memory pointed to by r30:r31

 st y+,r0 ;store in SRAM and increment Y-pointer

 adiw  ZL,1 ;increment Z-pointer

 dec r20 ; subtract 1 from r16

 cpi r20,$00 ;compare r20 with 00

 brne mem ;if not 00 then loop for more





;*************************************************************************

;* FUNCTION

;* Output

;* DESCRIPTION

;* Re intialises YL

;* Loads r20 with number of elements contained in SRAM

;* Outputs the value from Sram to PortB

;*************************************************************************



Output:

 ldi YL,$60 ;re-init Y-pointer low byte

 ldi  r20,$08 ;load r20 with 8 decimal

out_loop:

 ld r18,y ;load R18 with SRAM loc 60

 adiw YL,1 ;increment Y-pointer

 dec r20  ;decrement r20

 out portb,r18 ;output the value to portb

 rcall delay

 cpi r20,$00 ;compare R20 with 00

 brne out_loop ;if not equal jump to out_loop

 rjmp output ;if equal jump to output

   ;to re-init Y-pointer low byte



;**************************************************************************

;* FUNCTION

;* Delay

;* DESCRIPTION

;* delay time for LED's

;*************************************************************************





delay:  ldi r19,$0a

delay1:  ldi r16,$80

delay2:  ldi r17,$ff

delay3:  dec r17

 brne delay3

 dec r16

 brne delay2

 dec r19

 brne delay1



 ret                  



;**************************************************************************

;* FUNCTION

;* Table

;* DESCRIPTION

;*  Table holds the values to be stored in SRAM  

;*************************************************************************



Table:  

 .db $01,$02,$04,$08,$10,$20,$40,$80

     







Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.