Lets Learn AVR - Step by Step Tutorial

8
Home Forum Tutorials Downloads LCD Pinouts Jet Engine Games Project Demos More Saturday, 30 March 2013 17:20 Forums Rickey's World of Microcontrollers & Microprocessors :: Forums :: Discuss and Learn :: E-books and Tutorials :: AVR << Previous thread | Next thread >> Lets Learn AVR - Step by step tutorial Moderators: Ajay Bhargav, Arun Kumar V, pdi33, Shailesh NAYAK, ۞ TPS ۞, shyam, sashijoseph, ExperimenterUK, DavesGarage, majoka Author Post Ajay Bhargav Sat Aug 18 2007, 05:56PM Rickey's World Admin Registered Member #1 Joined: Fri Feb 24 2006, 06:26PM Location (Home Town): Punjab, India Posts: 12399 I have heard many people on forum asking for learning AVR Microcontroller. So i got this idea to start a new thread where I will post step by step information which will help you get started with AVR microcontroller. This thread not only help you understand AVR, but also it will cover all the details regarding interfacing and other stuff.. so you can say its a complete E-book for you to learn AVR. We will also discuss problems you face while working on with the assignments. So i get started with the essentials of AVR. Note: The tutorials will be in assembly language. We can discuss about C later on. Lets Learn AVR - Step 1: The first step in the development of any micrcontroller is to know about its architecture and instruction set. So i advice you all to keep a copy of instruction set and architecture manual. You can download them from the link below. AVR Instruction Set (User Guide, 150 pages) Architecture Manual or Datasheet for your AVR Microcontroller The above document will help you to get familiar with the instruction set and to know about your AVR in a better way. After this, we now have to decide the IDE on which you are going to work or write program for your AVR. I advice you to use AVR Studio 4 from Atmel Corporation, which is a free IDE for AVR and it has many features like programming, debugging etc. You can get your free copy of AVR studio from link below. AVR Studio 4.12 (build 460) (45 MB, updated 11/05) - Registration needed AVR Studio 4.12 Service Pack 4 (build 498) (25 MB, updated 10/06) - No registration AVR Studio 4.13 (build 528) (73 MB, updated 03/07) - Registration needed So you can download any of the copy above as per your need more information about AVR studio can be obtained from AVR Studio 4 All documents and IDE for writing program is ready.. only thing left is a programmer, with which we are going to program our AVR. As we know almost all AVR comes with ISP (In System Programmable) ports. So you don't need any special hardware to program your AVR. Please see the link below for ISP programmer for AVR. Download PonyProg - serial device programmer Interfacing Schematic: Note: Do not forget to run the setup after installing PonyProg. Setup Information: In interface setup, select parallel and then from the drop down select AVR ISP I/O. slect the LPT Port (parallel port) available on your PC. Then click ok! To load Hex file: Go to File-> Open Program (FLASH) file then from the drop down where ".e2p" is show, select ".hex" and load your hex file. The first step of our AVR tutorial is over... if you have any doubts please post in the link below Doubts in AVR tutorial - Step 1 Support Rickey's World $ 5.00 Custom Name & Comments! Welcome Username: Password: Login Remember me [ Signup ] [ Forgot password? ] [ Resend Activation Email ] Chatbox sjangra100 | 30 Mar : 09:25 hello sir i have observed that there are a lot of extra components in the image of circuit than as shown on circuit diagram. Now i am in confusion.....i need your help. i want this project to be working for me.......for this i need the final list of electronics components required. u are requested to mail me the list of components or comment here below. my email id is- [email protected] Ajay Bhargav | 29 Mar : 06:02 please post in forum... we will be glad to help kibbutz4success | 26 Mar : 11:52 PLEASE CAN SOMEONE HELP ME OUT WITH CD4047 IC PROTEUS MODEL Anonymous | 25 Mar : 19:44 Has anyone worked with the CC2540 implementing a dual BT and SD card setup? appucool | 25 Mar : 14:56 Hello sir.my project is automatic railway gate control system.So in that i am using a stepper motor which would rotate by 90 degree after sensing the arrival of the gate.how to make the stepper stop after 12 steps(we are using a 7.5 degree stepper motor) priyankatnp | 23 Mar : 11:30 hello sir...my project is gas leakage detection and auto on off gas system...so in that i m using stepper motor which would rotate by 90degree after sensing d gas leak so how to write a code in code vision avr to interface stepper motor with ATmega16 Select Language Forums / AVR / Lets Learn AVR - Step by step tutorial - Rickey's World ... http://www.8051projects.net/avr-microcontroller-tutorial.html 1 of 8 3/30/2013 5:21 PM

description

Lets Learn AVR - Step by Step Tutorial

Transcript of Lets Learn AVR - Step by Step Tutorial

Page 1: Lets Learn AVR - Step by Step Tutorial

Home Forum Tutorials Downloads LCD Pinouts Jet Engine Games Project Demos More

Saturday, 30 March 2013 17:20

Forums

Rickey's World of Microcontrollers & Microprocessors :: Forums :: Discuss andLearn :: E-books and Tutorials :: AVR

<< Previous thread | Nextthread >>

Lets Learn AVR - Step by step tutorial

Moderators: Ajay Bhargav, Arun Kumar V, pdi33, Shailesh NAYAK, ۞ TPS ۞,shyam, sashijoseph, ExperimenterUK, DavesGarage, majoka

Author Post

Ajay Bhargav Sat Aug 18 2007, 05:56PM

Rickey's World Admin

Registered Member #1

Joined: Fri Feb 24 2006,06:26PMLocation (Home Town):Punjab, IndiaPosts: 12399

I have heard many people on forum asking for learning AVR Microcontroller. So i got this idea to start anew thread where I will post step by step information which will help you get started with AVRmicrocontroller.This thread not only help you understand AVR, but also it will cover all the details regarding interfacingand other stuff.. so you can say its a complete E-book for you to learn AVR.We will also discuss problems you face while working on with the assignments. So i get started with theessentials of AVR.

Note: The tutorials will be in assembly language. We can discuss about C later on.

Lets Learn AVR - Step 1:

The first step in the development of any micrcontroller is to know about its architecture andinstruction set. So i advice you all to keep a copy of instruction set and architecture manual. You candownload them from the link below.AVR Instruction Set (User Guide, 150 pages)Architecture Manual or Datasheet for your AVR Microcontroller

The above document will help you to get familiar with the instruction set and to know about yourAVR in a better way.After this, we now have to decide the IDE on which you are going to work or write program for yourAVR. I advice you to use AVR Studio 4 from Atmel Corporation, which is a free IDE for AVR and it hasmany features like programming, debugging etc.You can get your free copy of AVR studio from link below.AVR Studio 4.12 (build 460) (45 MB, updated 11/05) - Registration neededAVR Studio 4.12 Service Pack 4 (build 498) (25 MB, updated 10/06) - No registrationAVR Studio 4.13 (build 528) (73 MB, updated 03/07) - Registration needed

So you can download any of the copy above as per your need more information about AVR studio canbe obtained fromAVR Studio 4

All documents and IDE for writing program is ready.. only thing left is a programmer, with which weare going to program our AVR. As we know almost all AVR comes with ISP (In SystemProgrammable) ports. So you don't need any special hardware to program your AVR. Please see thelink below for ISP programmer for AVR.Download PonyProg - serial device programmer

Interfacing Schematic:

Note: Do not forget to run the setup after installing PonyProg.

Setup Information:In interface setup, select parallel and then from the drop down select AVR ISP I/O.slect the LPT Port (parallel port) available on your PC. Then click ok!

To load Hex file:Go to File-> Open Program (FLASH) filethen from the drop down where ".e2p" is show, select ".hex" and load your hex file.

The first step of our AVR tutorial is over... if you have any doubts please post in the link belowDoubts in AVR tutorial - Step 1

Support Rickey's World

$ 5.00 Custom

Name & Comments!

Welcome

Username:

Password:

Login

Remember me

[ Signup ][ Forgot password? ]

[ Resend Activation Email ]

Chatbox

sjangra100 | 30 Mar : 09:25hello sir

i have observed that there are a lot of extracomponents in the image of circuit than as shownon circuit diagram.Now i am in confusion.....i need your help.i want this project to be working for me.......forthis i need the final list of electronics componentsrequired.u are requested to mail me the list ofcomponents or comment here below.my email id is- [email protected]

Ajay Bhargav | 29 Mar : 06:02please post in forum... we will be glad to help

kibbutz4success | 26 Mar : 11:52PLEASE CAN SOMEONE HELP ME OUT WITHCD4047 IC PROTEUS MODEL

Anonymous | 25 Mar : 19:44Has anyone worked with the CC2540implementing a dual BT and SD card setup?

appucool | 25 Mar : 14:56Hello sir.my project is automatic railway gatecontrol system.So in that i am using a steppermotor which would rotate by 90 degree aftersensing the arrival of the gate.how to make thestepper stop after 12 steps(we are using a 7.5degree stepper motor)

priyankatnp | 23 Mar : 11:30hello sir...my project is gas leakage detection andauto on off gas system...so in that i m usingstepper motor which would rotate by 90degreeafter sensing d gas leak so how to write a codein code vision avr to interface stepper motor withATmega16

mohansaini | 22 Mar : 05:59Select Language▲

Forums / AVR / Lets Learn AVR - Step by step tutorial - Rickey's World ... http://www.8051projects.net/avr-microcontroller-tutorial.html

1 of 8 3/30/2013 5:21 PM

Page 2: Lets Learn AVR - Step by Step Tutorial

[ Edited Sat Aug 18 2007, 05:57PM ]

MATHIVANAN, Arun Kumar, fique and 11 others like this.

Tags: AVR Tutorial, AVR programming, AVR studio, AVR instruction set, AVR ISPProgrammer

Back to top

Ajay Bhargav Mon Aug 20 2007, 08:25PM

Rickey's World Admin

Registered Member #1

Joined: Fri Feb 24 2006,06:26PMLocation (Home Town):Punjab, IndiaPosts: 12399

I hope you've all gone through the step 1 and ready to work on the AVR Microcontroller.

So lets start with the programming and writing our first program which is "hello world" ofmicrocontrollers..

AVR Tutorial - Step 2

Note: The program i wrote is for ATMEGA8515 and there is not much difference in other AVRsexcept some has extra features.

Before programming AVR, first you need to know few important programming tips and AVR registers.

CODE:

.include "8515def.inc"

This include the definition file for ATmega8515, so that we can directly address the registers withtheir names.All AVRs have 32 general purpose registers from R0 - R32.R0-R15 registers have certain restrictions of use, i.e. they are not allowed to load immediate value.e.g. LDI R15, $35the above statement will give you an error, saying "Invalid Register"Where as registers from R16-R32 can be used for this purpose i.e.LDI R16,$35is a valid statement.You can move values from one register to other by usingMOV R15,R16

Not only the registers from R0 to R15 has restriction on LDI but also other commands where the useof R0-R16 is not allowed. usually all commands having immediate operands are not allowed.For the ease of programming, you can also give names to the register likeCODE:

.def myregister = R16

There are some special registers like X,Y and Z for 16-Bit operations. They are used to read andwrite to XRAM. They are also used for reading from program memory like reading from a lookup table(we will discuss about them when we use them). These special registers are actually combination oftwo 8-bit General purpose registers. i.e. X is actually R26:R27, Y is R28:R29 and Z is R30:R31.The lower byte of the 16-bit-adress is located in the lower register, the higher byte in the upperregister. Both parts have their own names, e.g. the higher byte of Z is named as ZH (R31) and thelower Byte is ZL (R30). Similarly for X (XL and XH) and for Y (YL and YH). These names are definedin the standard header file for the chips (which we include while writing program). Dividing these16-bit-pointer-names into two different bytes is done like follows:CODE:

LDI YH,HIGH(LABEL) ; Set the MSBLDI YL,LOW(LABEL) ; Set the LSB

where LABEL is address for any lookup table or any memory location.

Some important notes for using registersDefine names for registers with the .DEF directive, never use them with their direct nameRx. This helps you making better use of registers and you will never confuse yourself whileusing them.

1.

If you need pointer access reserve R26 to R31 for that purpose.2.16-bit-counter are best located R25:R24.3.If you need to read from the program memory, e.g. fixed tables, reserve Z (R31:R30) and R0for thatpurpose.

4.

If you plan to have access to single bits within certain registers (e.g. for testing flags), useR16 toR23 for that purpose

5.

Now coming to ports, The information about a specific port of a certain type of AVR can be easilyobtained in the AVR Datasheet. Port names are defined in the include file of the CPU..if you don't have an include file then you can define yourself as..CODE:

.equ PORTA = $1B ;incase of ATmega8515

So if you are not able to find an include file you can use the .EQU directive to define ports and otherregisters.Making port as i/p or o/p is purely dependent on data direction register called DDRx (DDRA for portA etc.) The DDxn bit in the DDRx Register selects the direction of this pin. If DDxn is writtenlogic one, Pxn is configured as an output pin. If DDxn is written logic zero, Pxn is configuredas an input pin.for writing and reading data to Ports, PORTx registers are there. and to read from ports PINxregisters are there.for example..writing to portCODE:

.def output = R16LDI output, $FFOUT DDRA, output ; making as o/p

kibbutz4success | 21 Mar : 02:42how can i get model for components componentthat are not find in the libary

Ravindra21 | 19 Mar : 07:10In this time controlled solar tracker system ,thereare 3 separate circuits given, I don't know how tointerconnect those 3 .Can somebody please help?

ExperimenterUK | 18 Mar : 18:00Hi everybody.If you can help in any of the threads please do.You don't have to be an expert..or even right most the time We appreciate the effort

View all posts (26902)

Link to Us!

Put this button on your website to link with us!

New York Lawyers

Info Panel

Photography in Kolkata2&4 months comprehensive Foundation Course in photography in Kolkata

udaan.org.in/kolkata-Ph-9836968988

Select Language▲ Recommended

Forums / AVR / Lets Learn AVR - Step by step tutorial - Rickey's World ... http://www.8051projects.net/avr-microcontroller-tutorial.html

2 of 8 3/30/2013 5:21 PM

Page 3: Lets Learn AVR - Step by Step Tutorial

LDI output, $00OUT PORTA, output ; clear all PORTA pins

Reading from portCODE:

.def input = R17LDI input, $00OUT DDRA,inputIN input, PINA

We are finished with the basics of AVR, lets try programming with simplest program. Blinking anLED!

CODE:

.include "8515def.inc" ;Include file

RJMP MAIN ;Resetvector

MAIN: ldi R16,low(RAMEND) ;Load stack with out SPL,R16 ;RAMEND -highest value ldi R16,high(RAMEND) ;of internal SRAM out SPH,R16 SBI DDRA,0 ;Make PORTAPin 0 as o/p

DO: SBI PORTA,0 ;Set Pin 0of PORTA RCALL DELAY ;Wait forsome time CBI PORTA,0 ;Cleare Pin0 of PORTA RCALL DELAY ;Wait forsome time RJMP DO ;Foreverloop!

DELAY: ;The delayroutine LDI R16,$20 ;Load somedelay valueLOOP1: SER R17 ;Make R17as $FFLOOP: DEC R17 ;DecrementR17 BRNE LOOP ;Jump ifnot zero DEC R16 ;DecrementR16 BRNE LOOP1 ;Jump ifnot zero RET ;Return

Please post your problems in the forum Doubts in AVR tutorial

[ Edited Wed Nov 21 2007, 02:24PM ]

MATHIVANAN, Ganesh Babu, iwan and 3 others like this.

Tags: AVR Tutorial, AVR port programming, writing program AVR studio, AVR instruction set,AVR register set

Back to top

Ajay Bhargav Mon Aug 27 2007, 05:47PM

Rickey's World Admin

Registered Member #1

Joined: Fri Feb 24 2006,06:26PMLocation (Home Town):Punjab, IndiaPosts: 12399

I hope you are now familiar with the register set of AVR and instructions that deals with these registers.So let move on to our next step of the tutorial. I named it as"Dealing with load and store instructions of AVR"

Lets learn AVR Tutorial - Step 3

In this part of tutorial you will learnReading from Program memory1.Reading from RAM2.Writing to RAM3.Practice programs4.

Here is the summary of Load and store instructions that are used for dealing with SRAM of AVRMicrocontroller

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 thedestination register Rn (can be R0, R1.. any etc)

i.

LD Rn,X+/Y+/Z+>This instruction will load the value which is stored in memory at location pointed by X/Y/Zregisters and then increment the memory location by 1>This is a post increment instruction

ii.

LD Rn, -X/-Y/-Ziii.

Photography in Kolkata2&4 months comprehensive Foundation Course in photography in Kolkata

udaan.org.in/kolkata-Ph-9836968988

Select Language▲ Recommended

Forums / AVR / Lets Learn AVR - Step by step tutorial - Rickey's World ... http://www.8051projects.net/avr-microcontroller-tutorial.html

3 of 8 3/30/2013 5:21 PM

Page 4: Lets Learn AVR - Step by Step Tutorial

>Load Rn with value stored at location pointed by pre-decrement of address stored in X/Y/ZLDD 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 at0x0090+0x10 = 0x0100

iv.

ST X/Y/Z, Rn>Store the value of Rn to location pointed by X or Y or Z

v.

ST X+/Y+/Z+, Rn>Store the value in Rn to location pointed by X or Y or Z and increment the address pointer

vi.

STD Y/Z+displacement, Rn>Store the value in Rn to location pointed by Y or Z + Displacement

vii.

LDS Rn, SRAM_Address>Load value from SRAM Address to the Rn register>SRAM Address is the immediate value e.g. LDS R0,0x0100

viii.

STS SRAM_Address, Rn>Store Rn to immediate SRAM location

ix.

To read from Program memory we have special instructions likeLPM>Load form program memory, This instruction is used in most of the AVRs and its hard codedin the architecture.>This instruction will load R0 with the address specified by register Z [This is hardcoded]

i.

LPM Rn, Z>Load Rn from program memory pointed by register Z>This instruction is not supported by all AVRs e.g ATMega8515, AT90S8515

ii.

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

iii.

Note: load from program memory instructions are not supported by all AVRarchitectures. Most of the architectures support LPM instruction which is hard coded toload 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 Datamemory (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 addresswhere ldi ZL,LOW(2*data) ;our data is stored ldi XL,$60 ;LoadDestination RAM location ldi XH,$0 ldi count,$A ;Load count 10 Bytesagain: lpm ;Loadvalue from program memory inc ZL ;Incrementmemory location st X+,Temp ;Store byte tothe RAM location dec count ;Decrement Count brne again ;Check if allbytes movedend: rjmp end ;End of program

;Our data which we will copy from Program Memory to RAMData:.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, imultiplied 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 eachcommand, So the address has to be multiplied by 2. You can try running these programs and seeits 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 Firstnumber.def num2 = r1 ;Location for secondnumber.def answer = R2 ;location for Finalanswer

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

lpm ;Loadfirst number mov num2,num1 ;Move it to num2 inc ZL ;Increment theaddress lpm ;Load

Select Language▲ Recommended

Forums / AVR / Lets Learn AVR - Step by step tutorial - Rickey's World ... http://www.8051projects.net/avr-microcontroller-tutorial.html

4 of 8 3/30/2013 5:21 PM

Page 5: Lets Learn AVR - Step by Step Tutorial

second number cp num1,num2 ;Compare them brlt next ;Jump ifnum1<num2 mov num2,num1 ;If num1>num2 then movenum1next: ; to num2location inc ZL ;Increment theaddress lpm ;Loadthe third number cp num1,num2 ;Again compare brlt final ;Check ifnum1<num2 mov answer,num1 ;If No, then num1 isanswerfinal: mov answer,num2 ;If Yes, then num2 isour answerend: rjmp end ;End of program

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

Now you can try yourself writing some programs to make yourself more easy with load and storeoperations.Here is the list of programs you can try out:

Swap two numbers stored in RAMA.Find Greatest of 5 numbersB.Copy memory block from RAM to RAMC.Sorting of 10 numbersD.Clear SRAM area from 0x60 to RAMENDE.

*Happy programming with your AVR*

Any Doubts? Post here...Doubts in AVR Tutorial - Step 3

MATHIVANAN, allamovich_1 like this.

Tags: AVR Tutorial, AVR assembly programming, AVR load store instructions, AVR assemblyProgramming example, AVR Greatest of 3 numbers, AVR Copy memory block

Back to top

Ajay Bhargav Mon Sep 03 2007, 05:02PM

Rickey's World Admin

Registered Member #1

Joined: Fri Feb 24 2006,06:26PMLocation (Home Town):Punjab, IndiaPosts: 12399

I think the wait is over... now as i am not getting any questions from your side.. so i can post the answerto the assignment i gave.. which is 5 programs..So here are the programs.. Please take a look!Swap two numbers stored in RAMCODE:

;Program to swap two numbers

.include "8515def.inc"

.def temp = R16 ;Temporary register

.def num1 = R17 ;Number one location

.def num2 = R18 ;Location for second number

.cseg

.org $0

ldi ZH,0x00 ;Assuming the two numbers are storedldi ZL,0x90 ;at location 0x0090 in RAMld num1,Z+ ;Load first numberld num2,Z ;Load second numbermov temp,num1 ;copy num1 to tempmov num1,num2 ;copy num2 to num1mov num2,temp ;copy temp to num2ldi ZL,0x90 ;load the RAM location backst Z+,num1 ;store the first numberst Z, num2 ;store the second numberend: rjmp end ;end of prog

Find Greatest of 5 numbersCODE:

;Program to find greatest of 5 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

.def count = r16 ;Count

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

Select Language▲ Recommended

Forums / AVR / Lets Learn AVR - Step by step tutorial - Rickey's World ... http://www.8051projects.net/avr-microcontroller-tutorial.html

5 of 8 3/30/2013 5:21 PM

Page 6: Lets Learn AVR - Step by Step Tutorial

lpm ;Load first number mov num2,num1 ;Move it to num2 adiw Z,1again: lpm ;Load second number cp num1,num2 ;Compare them brlt next ;Jump if num1<num2 mov num2,num1 ;If num1>num2 then move num1next: ; to num2 location adiw Z,1 ;Increment the address dec count ;are we done with all 5 numbers? brne again ;if no then jump backfinal: mov answer,num2 ;If Yes, then num2 is our answerend: rjmp end ;End of program

;Try changing them and see resultsData: ;Our 5 numbers.db $3,$23,$14,$50,$20,$0;last zero is to aligh the bytes in word manner;we have 5 numbers so i am padding with a 0 to make it;even. if we dont't do this.. compiler will do it;automatically to prevent misalignment.

Copy memory block from RAM to RAMCODE:

;Program to copy a block of memory (10 Bytes); from one RAM location to another RAM location.

.include "8515def.inc"

.def temp = r0

.def count = r16

.org 0 ldi ZL,0x60 ;Lets fill the RAM with some numbers ldi ZH,0x00 ;to copy, so we can check is it working ldi count,10 ;Load countfill: st Z+,count ;Store value to RAM location dec count brne fill

ldi ZL,0x60 ;Load memory location to copy from ldi ZH,0x00 ldi YL,0x90 ;Load destination memory location ldi YH,0x00 ldi count,10 ;Load count

copy: ld temp,Z+ ;Load value to temporary register st Y+,temp ;Store it to location dec count ;decrement counter brne copy

end: rjmp end ;End of program...

Sorting of 10 numbersCODE:

;Program to sort 10 numbers;in ascending order;10 numbers are stored in ROM;and sorted answer is stored in RAM;at location 0x0060

.include "8515def.inc"

.def count1 = r17 ;First Count

.def count2 = r18 ;Second count

.def temp = r0 ;Temp reg for swap

.def num1 = r1 ;Num 1

.def num2 = r2 ;Num 2

.cseg ;Code segment starts

.org 0ldi Zh,high(2*mydata) ;Load memory add whereldi Zl,low(2*mydata) ;data is stored in ROMldi Yh,0x00 ;Destination locationldi Yl,0x60 ;0x0060 in RAMldi count1,10 ;Load count

copy:lpm ;Load from program memst Y+,temp ;Store it to RAMadiw Z,1 ;Increment Zdec count1 ;Decrement counterbrne copy ;copy all 10 bytes

ldi ZH,0x00 ;Load first pointerldi ZL,0x60ldi count1,10 ;Load counter for it

sort: mov YL,ZL ;Load second pointer mov YH,ZH adiw Y,1 ;Increment it ld num1,Z ;load first number

Select Language▲ Recommended

Forums / AVR / Lets Learn AVR - Step by step tutorial - Rickey's World ... http://www.8051projects.net/avr-microcontroller-tutorial.html

6 of 8 3/30/2013 5:21 PM

Page 7: Lets Learn AVR - Step by Step Tutorial

mov count2,count1 ;load count2 subi count2,1 ;Count2 = Count1-1 breq end ;end if last num

again: ld num2,Y ;Load second number cp num1,num2 ;Compare them brlo next ;Jump if num1<num2 mov temp,num2 mov num2,num1 ;If num1>num2 then swap mov num1,temp st Z,num1 ;and store them on their st Y,num2 ;respective locationsnext: adiw Y,1 ;Increment the address dec count2 ;dec count2 for Y pointer brne again ;check if count is zero adiw Z,1 ;increment Z pointer dec count1 ;Dec count1 for Z pointer brne sort ;finish if zero

end: rjmp end ;End of program

mydata:.db $10,$90,$50,$20,$91,$23,$55,$62,$39,$80

Clear SRAM area from 0x60 to RAMENDCODE:

;Program to clear RAM;this program is for ATMega8515 So ram area is from;0x60 to RAMEND

.include "8515def.inc"

.def temp = r16 ;Temporary variable

.def cnth = r25 ;Counter High byte

.def cntl = r24 ;Counter Low byte

.org 0

ldi cnth,0x02 ;Load count 0x200 to clear ldi cntl,0x00 ;memory from 0x00 to 0x1FF clr temp ;clear temp ldi ZH,0x00 ;Load starting address of ldi ZL,0x60 ;RAM 0x0060 in Z pointer

clrram: st Z+,temp ;Store 0 in current Z location sbiw cnth:cntl,1 ;Decrement the counter brne clrram ;end if zero end: rjmp end ;End of program

[ Edited Thu Nov 29 2007, 01:52PM ]

MATHIVANAN, Micr0n, iwan and 3 others like this.

Tags: AVR assembly programing, AVR programing example, AVR Swap two numbers, AVRFind Greatest numbers, AVR Copy memory block, AVR Sorting 10 numbers, AVR Clear SRAM

Back to top

Ajay Bhargav Sun Oct 28 2007, 02:19AM

Rickey's World Admin

Registered Member #1

Joined: Fri Feb 24 2006,06:26PMLocation (Home Town):Punjab, IndiaPosts: 12399

AVR Serial Programming Tutorial

AVR UART Programming

Learn to program AVR in C as well as assembly for serial Programming or UART programming.

Interfacing ADC with AVR

ADC0804 interfacing tutorial

Interface an 8-bit ADC (ADC0804) with AVR microcontroller.

[ Edited Wed Nov 21 2007, 02:03PM ]

iwan, amiicool, priyatendulkar and 1 other like this.

Back to top

Ajay Bhargav Thu Nov 29 2007, 03:35PM

Rickey's World Admin Interfacing Keypad with AVR microcontroller

Keypad TutorialLearn to interface a 4x4 matrix keypad with AVR microcontroller.

zatar, iwan, amiicool and 6 others like this.

Select Language▲ Recommended

Forums / AVR / Lets Learn AVR - Step by step tutorial - Rickey's World ... http://www.8051projects.net/avr-microcontroller-tutorial.html

7 of 8 3/30/2013 5:21 PM

Page 8: Lets Learn AVR - Step by Step Tutorial

Registered Member #1

Joined: Fri Feb 24 2006,06:26PMLocation (Home Town):Punjab, IndiaPosts: 12399

Back to top

Ajay Bhargav Tue Jul 13 2010, 12:49AM

Rickey's World Admin

Registered Member #1

Joined: Fri Feb 24 2006,06:26PMLocation (Home Town):Punjab, IndiaPosts: 12399

Facing problem deciding fuse bits?? Use this handy tool:

Atmel AVR® Fuse Calculator:http://www.engbedded.com/fusecalc/

Back to top

Jump: Site News And Announcements Go Back to top

Powered by e107 Forum System

© 2010 Rickey's World

Select Language▲ Recommended

Forums / AVR / Lets Learn AVR - Step by step tutorial - Rickey's World ... http://www.8051projects.net/avr-microcontroller-tutorial.html

8 of 8 3/30/2013 5:21 PM