Docstoc

Tutorial AVR - Part 2

Document Sample
Tutorial AVR - Part 2 Powered By Docstoc
					AVR Learning Part 2– Original from http://8051projects.net

Lets learn AVR Tutorial - Step 3

In this part of tutorial you will learn
     1. Reading from Program memory
     2. Reading from RAM
     3. Writing to RAM
     4. Practice programs

Here is the summary of Load and store instructions that are used for dealing with SRAM of AVR
Microcontroller
    1. LD Rn,X/Y/Z
       >either X or Y or Z register can be used
       >this will load the value which is stored in memory location pointed by register X/Y/Z to the
       destination register Rn (can be R0, R1.. any etc)
    2. LD Rn,X+/Y+/Z+
       >This instruction will load the value which is stored in memory at location pointed by X/Y/Z
       registers and then increment the memory location by 1
       >This is a post increment instruction
    3. LD Rn, -X/-Y/-Z
       >Load Rn with value stored at location pointed by pre-decrement of address stored in X/Y/Z
    4. LDD Rn,Y/Z+displacement
       >Load Rn with value at address Z or Y + displacement
       >e.g. Z is 0x0090, Displacement is 0x10 so Rn will be loaded with value stored at 0x0090+0x10
       = 0x0100
    5. ST X/Y/Z, Rn
       >Store the value of Rn to location pointed by X or Y or Z
    6. ST X+/Y+/Z+, Rn
       >Store the value in Rn to location pointed by X or Y or Z and increment the address pointer
    7. STD Y/Z+displacement, Rn
       >Store the value in Rn to location pointed by Y or Z + Displacement
    8. LDS Rn, SRAM_Address
       >Load value from SRAM Address to the Rn register
       >SRAM Address is the immediate value e.g. LDS R0,0x0100
    9. STS SRAM_Address, Rn
       >Store Rn to immediate SRAM location


To read from Program memory we have special instructions like
    1. LPM
       >Load form program memory, This instruction is used in most of the AVRs and its hard coded
       in the architecture.
       >This instruction will load R0 with the address specified by register Z [This is hardcoded]
    2. LPM Rn, Z
       >Load Rn from program memory pointed by register Z
       >This instruction is not supported by all AVRs e.g ATMega8515, AT90S8515
    3. LPM Rn,Z+
       >Load Rn from program memory and increment the memory location pointed by Z
       >This instruction is also not supported by all AVRs


Note: load from program memory instructions are not supported by all AVR architectures. Most
of the architectures support LPM instruction which is hard coded to load R0 from location in Z.
where as in some AVR this is also not implemented.

Now we are done with the instructions overview.. now lets practice them..
Program 1: Copy 10 Bytes memory block stored in Program memory(ROM) to Data memory
(SRAM)
CODE:
;This program is to copy memory block from Program
;memory to AVR RAM (10 Bytes)

.include "8515def.inc"

.org $0
.def Temp = R0                         ;Temprary variable
.def count = R17                       ;Byte Count

         ldi   ZH,HIGH(2*data)         ;Load Z with address where
         ldi   ZL,LOW(2*data)          ;our data is stored
         ldi   XL,$60                          ;Load Destination RAM location
         ldi   XH,$0
         ldi   count,$A                ;Load count 10 Bytes
again:
         lpm                                             ;Load value from program memory
         inc ZL                                  ;Increment memory location
         st X+,Temp                              ;Store byte to the RAM location
         dec count                               ;Decrement Count
         brne again                              ;Check if all bytes moved
end:
         rjmp end                                ;End of program

;Our data which we will copy from Program Memory to RAM
Data:
.db $10,$20,$30,$40,$50,$60,$70,$80,$90,$95

In the above code.. you can see while loading the address of program memory location, i multiplied it
with 2, i.e. LDI ZH,High(2*Data)
The reason is, the program memory is organized in word manner i.e. two bytes for each command, So
the address has to be multiplied by 2. You can try running these programs and see its working in the
Simulator of AVR Studio.
Program 1: Find Greatest of 3 numbers Stored in program memory
CODE:
;Program to find greatest of 3 numbers
;
;numbers are stored in ROM and the final
;result will be stored in a register

.include "8515def.inc"

.def num1 = r0                        ;Location for First number
.def num2 = r1                        ;Location for second number
.def answer = R2                      ;location for Final answer

.org $0
          ldi ZL, Low(2*Data)         ;Load the program memory address
          ldi ZH, High(2*data)

          lpm                                            ;Load first number
          mov num2,num1               ;Move it to num2
          inc ZL                              ;Increment the address
          lpm                                          ;Load second number
          cp num1,num2                ;Compare them
          brlt next                           ;Jump if num1<num2
          mov num2,num1               ;If num1>num2 then move num1
next:                                         ; to num2 location
          inc ZL                              ;Increment the address
          lpm                                          ;Load the third number
          cp num1,num2                ;Again compare
          brlt final                          ;Check if num1<num2
          mov answer,num1             ;If No, then num1 is answer
final:
          mov answer,num2             ;If Yes, then num2 is our answer
end:
          rjmp end                             ;End of program


Data:                                         ;Our 3 numbers
.db $23,$23,$14                       ;Try changing them and see results


Now you can try yourself writing some programs to make yourself more easy with load and store
operations.
Here is the list of programs you can try out:
    1.   Swap two numbers stored in RAM
    2.   Find Greatest of 5 numbers
    3.   Copy memory block from RAM to RAM
    4.   Sorting of 10 numbers
    5.   Clear SRAM area from 0x60 to RAMEND

				
DOCUMENT INFO
Shared By:
Stats:
views:60
posted:9/25/2011
language:English
pages:4
Description: Tutorial AVR and KEil compiler