Lets Learn AVR - Step by Step Tutorial
Lets Learn AVR - Step by Step Tutorial
http://www.8051projects.net/avr-microcontroller-tutorial.html
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
Lets Learn AVR - Step by step tutorial Moderators: Ajay Bhargav, Arun Kumar V, pdi33, Shailesh NAYAK, TPS , shyam, sashijoseph, ExperimenterUK, DavesGarage, majoka
<< Previous thread | Next thread >>
Welcome
Post
Username:
Author
Ajay Bhargav Rickey's World Admin
Sat Aug 18 2007, 05:56PM
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.
Password:
Login
Chatbox
Lets Learn AVR - Step 1:
Registered Member #1 Joined: Fri Feb 24 2006, 06:26PM Location (Home Town): Punjab, India Posts: 12399
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:
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?
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.
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)
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
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
1 of 8
3/30/2013 5:21 PM
Forums / AVR / Lets Learn AVR - Step by step tutorial - Rickey's World ...
http://www.8051projects.net/avr-microcontroller-tutorial.html
Photography in Kolkata
2&4 months comprehensive Foundation Course in photography in Kolkata udaan.org.in/kolkata-Ph-9836968988
kibbutz4success | 21 Mar : 02:42 how can i get model for components component that are not find in the libary
Ravindra21 | 19 Mar : 07:10 In this time controlled solar tracker system ,there are 3 separate circuits given, I don't know how to interconnect those 3 .Can somebody please help?
Tags: AVR Tutorial, AVR programming, AVR studio, AVR instruction set, AVR ISP Programmer
Back to top
ExperimenterUK | 18 Mar : 18:00 Hi 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
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" of microcontrollers.. AVR Tutorial - Step 2 Note: The program i wrote is for ATMEGA8515 and there is not much difference in other AVRs except some has extra features. View all posts (26902)
Link to Us!
Put this button on your website to link with us! New York Lawyers
Info Panel
Registered Member #1 Joined: Fri Feb 24 2006, 06:26PM Location (Home Town): Punjab, India Posts: 12399
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 with their 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, $35 the 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,$35 is a valid statement. You can move values from one register to other by using MOV R15,R16 Not only the registers from R0 to R15 has restriction on LDI but also other commands where the use of 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 like CODE:
LDI YH,HIGH(LABEL) ; Set the MSB LDI YL,LOW(LABEL) ; Set the LSB
where LABEL is address for any lookup table or any memory location. Some important notes for using registers 1. Define names for registers with the .DEF directive, never use them with their direct name Rx. This helps you making better use of registers and you will never confuse yourself while using them. 2. If you need pointer access reserve R26 to R31 for that purpose. 3. 16-bit-counter are best located R25:R24. 4. If you need to read from the program memory, e.g. fixed tables, reserve Z (R31:R30) and R0 for that purpose. 5. If you plan to have access to single bits within certain registers (e.g. for testing flags), use R16 to R23 for that purpose Now coming to ports, The information about a specific port of a certain type of AVR can be easily obtained 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:
.def output = R16 LDI output, $FF OUT DDRA, output ; making as o/p Select Language
Recommended
2 of 8
3/30/2013 5:21 PM
Forums / AVR / Lets Learn AVR - Step by step tutorial - Rickey's World ...
http://www.8051projects.net/avr-microcontroller-tutorial.html
LDI output, $00 OUT PORTA, output ; clear all PORTA pins
Reading from port CODE:
.def input = R17 LDI input, $00 OUT DDRA,input IN input, PINA
We are finished with the basics of AVR, lets try programming with simplest program. Blinking an LED! CODE:
.include "8515def.inc" RJMP MAIN vector MAIN: ldi R16,low(RAMEND) out SPL,R16 highest value ldi R16,high(RAMEND) out SPH,R16 SBI DDRA,0 Pin 0 as o/p DO: SBI PORTA,0 of PORTA RCALL DELAY some time CBI PORTA,0 0 of PORTA RCALL DELAY some time RJMP DO loop! DELAY: routine LDI R16,$20 delay value LOOP1: SER R17 as $FF LOOP: DEC R17 R17 BRNE LOOP not zero DEC R16 R16 BRNE LOOP1 not zero RET ;Return
Please post your problems in the forum Doubts in AVR tutorial
;Set Pin 0 ;Wait for ;Cleare Pin ;Wait for ;Forever ;The delay ;Load some ;Make R17 ;Decrement ;Jump if ;Decrement ;Jump if
Photography in Kolkata
2&4 months comprehensive Foundation Course in photography in Kolkata udaan.org.in/kolkata-Ph-9836968988
Tags: AVR Tutorial, AVR port programming, writing program AVR studio, AVR instruction set, AVR register set
Back to top
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 learn 1. Reading from Program memory 2. Reading from RAM 3. Writing to RAM 4. Practice programs
Registered Member #1 Joined: Fri Feb 24 2006, 06:26PM Location (Home Town): Punjab, India Posts: 12399
Select Language
Here is the summary of Load and store instructions that are used for dealing with SRAM of AVR Microcontroller i. 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) ii. 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 iii. LD Rn, -X/-Y/-Z
Recommended
3 of 8
3/30/2013 5:21 PM
Forums / AVR / Lets Learn AVR - Step by step tutorial - Rickey's World ...
http://www.8051projects.net/avr-microcontroller-tutorial.html
>Load Rn with value stored at location pointed by pre-decrement of address stored in X/Y/Z iv. 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 v. ST X/Y/Z, Rn >Store the value of Rn to location pointed by X or Y or Z vi. ST X+/Y+/Z+, Rn >Store the value in Rn to location pointed by X or Y or Z and increment the address pointer vii. STD Y/Z+displacement, Rn >Store the value in Rn to location pointed by Y or Z + Displacement viii. LDS Rn, SRAM_Address >Load value from SRAM Address to the Rn register >SRAM Address is the immediate value e.g. LDS R0,0x0100 ix. STS SRAM_Address, Rn >Store Rn to immediate SRAM location To read from Program memory we have special instructions like i. 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] ii. LPM Rn, Z >Load Rn from program memory pointed by register Z >This instruction is not supported by all AVRs e.g ATMega8515, AT90S8515 iii. 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 .def count = R17 ldi ZH,HIGH(2*data) where ldi ZL,LOW(2*data) ldi XL,$60 Destination RAM location ldi XH,$0 ldi count,$A again: lpm value from program memory inc ZL memory location st X+,Temp the RAM location dec count brne again bytes moved end: rjmp end ;our data is stored ;Load ;Load count 10 Bytes ;Load ;Increment ;Store byte to ;Decrement Count ;Check if all ;End of program ;Temprary variable ;Byte Count ;Load Z with address
;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 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 number .def num2 = r1 number .def answer = R2 answer .org $0 ldi ZL, Low(2*Data) address ldi ZH, High(2*data) lpm first number mov num2,num1 inc ZL address lpm Select Language ;Load ;Move it to num2 ;Increment the ;Load
Recommended
4 of 8
3/30/2013 5:21 PM
Forums / AVR / Lets Learn AVR - Step by step tutorial - Rickey's World ...
http://www.8051projects.net/avr-microcontroller-tutorial.html
second number cp num1,num2 brlt next num1<num2 mov num2,num1 num1 next: location inc ZL address lpm the third number cp num1,num2 brlt final num1<num2 mov answer,num1 answer final: mov answer,num2 our answer end: rjmp end Data: .db $23,$23,$14 see results
;Compare them ;Jump if ;If num1>num2 then move ; to num2 ;Increment the ;Load ;Again compare ;Check if ;If No, then num1 is ;If Yes, then num2 is ;End of program ;Our 3 numbers ;Try changing them and
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: A. Swap two numbers stored in RAM B. Find Greatest of 5 numbers C. Copy memory block from RAM to RAM D. Sorting of 10 numbers E. Clear SRAM area from 0x60 to RAMEND
*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 assembly Programming example, AVR Greatest of 3 numbers, AVR Copy memory block
Back to top
I think the wait is over... now as i am not getting any questions from your side.. so i can post the answer to the assignment i gave.. which is 5 programs.. So here are the programs.. Please take a look! Swap two numbers stored in RAM CODE:
;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 stored ldi ZL,0x90 ;at location 0x0090 in RAM ld num1,Z+ ;Load first number ld num2,Z ;Load second number mov temp,num1 ;copy num1 to temp mov num1,num2 ;copy num2 to num1 mov num2,temp ;copy temp to num2 ldi ZL,0x90 ;load the RAM location back st Z+,num1 ;store the first number st Z, num2 ;store the second number end: rjmp end ;end of prog
Find Greatest of 5 numbers CODE:
Registered Member #1 Joined: Fri Feb 24 2006, 06:26PM Location (Home Town): Punjab, India Posts: 12399
;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 .def .def .def num1 = r0 num2 = r1 answer = R2 count = r16 ;Location for First number ;Location for second number ;location for Final answer ;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
5 of 8
3/30/2013 5:21 PM
Forums / AVR / Lets Learn AVR - Step by step tutorial - Rickey's World ...
http://www.8051projects.net/avr-microcontroller-tutorial.html
lpm mov num2,num1 adiw Z,1 again: lpm cp num1,num2 brlt next mov num2,num1 next: adiw Z,1 dec count brne again final: mov answer,num2 end: rjmp end
;Load first number ;Move it to num2 ;Load second number ;Compare them ;Jump if num1<num2 ;If num1>num2 then move num1 ; to num2 location ;Increment the address ;are we done with all 5 numbers? ;if no then jump back ;If Yes, then num2 is our answer ;End of program
;Try changing them and see results Data: ;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 RAM CODE:
;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 ldi ZH,0x00 ldi count,10 fill: st Z+,count dec count brne fill ldi ldi ldi ldi ldi copy: ld temp,Z+ st Y+,temp dec count brne copy end: rjmp end
Sorting of 10 numbers CODE:
;Lets fill the RAM with some numbers ;to copy, so we can check is it working ;Load count ;Store value to RAM location
;Load memory location to copy from ;Load destination memory location ;Load count ;Load value to temporary register ;Store it to location ;decrement counter
;End of program...
;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 .def .def .def .def count1 count2 temp = num1 = num2 = = r17 = r18 r0 r1 r2 ;First Count ;Second count ;Temp reg for swap ;Num 1 ;Num 2 ;Code segment starts ;Load memory add where ;data is stored in ROM ;Destination location ;0x0060 in RAM ;Load count ;Load from program mem ;Store it to RAM ;Increment Z ;Decrement counter ;copy all 10 bytes ;Load first pointer ;Load counter for it ;Load second pointer ;Increment it ;load first number
.cseg .org 0 ldi Zh,high(2*mydata) ldi Zl,low(2*mydata) ldi Yh,0x00 ldi Yl,0x60 ldi count1,10 copy: lpm st Y+,temp adiw Z,1 dec count1 brne copy ldi ZH,0x00 ldi ZL,0x60 ldi count1,10 sort: mov YL,ZL mov YH,ZH adiw Y,1 ld num1,Z
Select Language
Recommended
6 of 8
3/30/2013 5:21 PM
Forums / AVR / Lets Learn AVR - Step by step tutorial - Rickey's World ...
http://www.8051projects.net/avr-microcontroller-tutorial.html
mov count2,count1 ;load count2 subi count2,1 ;Count2 = Count1-1 breq end ;end if last num again: ld num2,Y cp num1,num2 brlo next mov temp,num2 mov num2,num1 mov num1,temp st Z,num1 st Y,num2 next: adiw Y,1 dec count2 brne again adiw Z,1 dec count1 brne sort end: rjmp end ;End of program mydata: .db $10,$90,$50,$20,$91,$23,$55,$62,$39,$80
Clear SRAM area from 0x60 to RAMEND CODE:
;Load second number ;Compare them ;Jump if num1<num2 ;If num1>num2 then swap ;and store them on their ;respective locations ;Increment the address ;dec count2 for Y pointer ;check if count is zero ;increment Z pointer ;Dec count1 for Z pointer ;finish if zero
;Program to clear RAM ;this program is for ATMega8515 So ram area is from ;0x60 to RAMEND .include "8515def.inc" .def temp = r16 .def cnth = r25 .def cntl = r24 .org 0 ldi ldi clr ldi ldi 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 cnth,0x02 cntl,0x00 temp ZH,0x00 ZL,0x60 ;Load count 0x200 to clear ;memory from 0x00 to 0x1FF ;clear temp ;Load starting address of ;RAM 0x0060 in Z pointer ;Temporary variable ;Counter High byte ;Counter Low byte
Tags: AVR assembly programing, AVR programing example, AVR Swap two numbers, AVR Find Greatest numbers, AVR Copy memory block, AVR Sorting 10 numbers, AVR Clear SRAM
Back to top
Back to top
Select Language
Recommended
7 of 8
3/30/2013 5:21 PM
Forums / AVR / Lets Learn AVR - Step by step tutorial - Rickey's World ...
http://www.8051projects.net/avr-microcontroller-tutorial.html
Registered Member #1 Joined: Fri Feb 24 2006, 06:26PM Location (Home Town): Punjab, India Posts: 12399
Back to top
Registered Member #1 Joined: Fri Feb 24 2006, 06:26PM Location (Home Town): Punjab, India Posts: 12399
Back to top
Go
Back to top
Select Language
Recommended
8 of 8
3/30/2013 5:21 PM