0% found this document useful (0 votes)
17 views

Avr Basic Tutorial Series

Uploaded by

klassik kustard
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Avr Basic Tutorial Series

Uploaded by

klassik kustard
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

TUTORIAL SERIES

TUTORIAL 1
EMBEDDED SYSTEMS

Eng George Mugala

THE COPPERBELT UNIVERSITY SCHOOL OF ENGINEERING

January 28, 2019

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMS January 28, 2019 1 / 47
OUTLINE

1 Data Transfer Instructions

2 Arithmetic Instructions

3 Logic Instructions

4 Rotate,shift,Compare,Jump and Branch instructions

5 Subroutine Instructions

6 PORT PROCESSING INSTRUCTIONS

7 AVR STUDIO EXAMPLES FOR INSTRUCTION SETS


8 INDIRECT ADDRESSING WITH PORT PROCESSING
INSTRUCTIONS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMS January 28, 2019 2 / 47
OUTLINE

1 Data Transfer Instructions

2 Arithmetic Instructions

3 Logic Instructions

4 Rotate,shift,Compare,Jump and Branch instructions

5 Subroutine Instructions

6 PORT PROCESSING INSTRUCTIONS

7 AVR STUDIO EXAMPLES FOR INSTRUCTION SETS


8 INDIRECT ADDRESSING WITH PORT PROCESSING
INSTRUCTIONS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMS January 28, 2019 3 / 47
Basic Avr instruction
Assumptions to be made
whenever i write Rd this is destination Register and whenever i write Rr
is source Register and whenever i write K is constant and k will
represent memory location
Data transfer instructions
I MOV Rd,Rr means move the content of Rr to Rd
I Ldi Rd,K move the constant K into Rd, Rd should range from
R16-R31, K should range from 0 to 255
I Ld Rd, X,Y,Z brings data from memory into Rd.
I Ld Rd,X+,Y+,Z+ ;brings data from memory into Rd Register After
the register(X,Y,Z) are post incremented
I Ld Rd,-X,-Y,-Z ;brings data from memory into Rd After the
register(X,Y,Z) are predecremented
I Ldd Rd,Y+q,Z+q; q is an ofset ranging 0q¡63;only Y and Z Register
works here.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMS January 28, 2019 4 / 47
Data Transfer Instructions
Lds Rd,k; bring data directly from memory address into register
ST X,Y,Z,Rr; will get data from Register and store in memory
pointed by X,y,Z
STD Y+q,Z+q,Rr; brings data from Register to memory pointed
by Y+q offset or Z+q;q is an ofset ranging 0¡q¡63;only Y and Z
Register works here.
STs k,Rr; brings Data directly to memory from a Register

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMS January 28, 2019 5 / 47
OUTLINE

1 Data Transfer Instructions

2 Arithmetic Instructions

3 Logic Instructions

4 Rotate,shift,Compare,Jump and Branch instructions

5 Subroutine Instructions

6 PORT PROCESSING INSTRUCTIONS

7 AVR STUDIO EXAMPLES FOR INSTRUCTION SETS


8 INDIRECT ADDRESSING WITH PORT PROCESSING
INSTRUCTIONS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMS January 28, 2019 6 / 47
Arithmetic Instruction
Arithmetic Instructions
I Add Rd,Rr; This adds the content of Rr and Rd and the results are
stored in Rd
I Sub Rd,Rr; this subtracts the content of Rr from Rd and the results
are stored in Rd
I Inc Rd; this increments content of Rd
I Dec Rd; this decrements the content of Rd
I Neg Rd; negates the value stored in Rd
I Mul Rd,Rr; multiplies the values in Rd and Rr and Stores results in
Rd

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMS January 28, 2019 7 / 47
OUTLINE

1 Data Transfer Instructions

2 Arithmetic Instructions

3 Logic Instructions

4 Rotate,shift,Compare,Jump and Branch instructions

5 Subroutine Instructions

6 PORT PROCESSING INSTRUCTIONS

7 AVR STUDIO EXAMPLES FOR INSTRUCTION SETS


8 INDIRECT ADDRESSING WITH PORT PROCESSING
INSTRUCTIONS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMS January 28, 2019 8 / 47
Logic Instructions
Logic Instructions
I AND Rd,Rr; this caries the And operation of Rd and Rr and stores
the result in Rd
I OR Rd,Rr; this caries the OR operation of Rd and Rr and stores the
result in Rd
I EOR Rd,Rr;his caries the XOR operation of Rd and Rr and stores
the result in Rd

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMS January 28, 2019 9 / 47
OUTLINE

1 Data Transfer Instructions

2 Arithmetic Instructions

3 Logic Instructions

4 Rotate,shift,Compare,Jump and Branch instructions

5 Subroutine Instructions

6 PORT PROCESSING INSTRUCTIONS

7 AVR STUDIO EXAMPLES FOR INSTRUCTION SETS


8 INDIRECT ADDRESSING WITH PORT PROCESSING
INSTRUCTIONS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 10 / 47
Rotate,shift,Compare,Jump and Branch instructions
Rotate and shift instruction; these instructions enable us to access
the bits in the middle of the byte.
I LSR/LSL Rd; shift to the right and shift to the Left and the shifted
bit goes to the carry flag.
I ROL/ROR Rd;in this instruction the bits are shifted to the left and
right but the carry bit enters in the end performing a rotation action.
I ASR Rd; shift to the right were the shifted instruction goes to the
carry flag but the most significant figure is replicated.
Compare Instructions
I CPI Rd,K; this instruction performs Rd-K; but the result is not
stored anywhere but reflected in the status register flags.
I CP Rd,Rr; this instruction performs Rd-Rr and the results are not
stored anywhere but reflected in the status register flags.
Jump and Branch cartigory of Instructions
I Jump label; this is unconditional jump instruction, the program
jumps to a label unconditionally
I BREQ/BRNE label; if the previously compared instructions are equal
then branch to label or if the previously compared instructions are
not equal go to label
Eng George Mugala ( THE COPPERBELT UNIVERSITY
TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 11 / 47
OUTLINE

1 Data Transfer Instructions

2 Arithmetic Instructions

3 Logic Instructions

4 Rotate,shift,Compare,Jump and Branch instructions

5 Subroutine Instructions

6 PORT PROCESSING INSTRUCTIONS

7 AVR STUDIO EXAMPLES FOR INSTRUCTION SETS


8 INDIRECT ADDRESSING WITH PORT PROCESSING
INSTRUCTIONS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 12 / 47
Subroutine Instructions
Call label ; this instruction branches to a subroutine under a label:
and performs an instruction under that. When the subroutine has
finished executing the program returnns to the previous section it
should have continued to if the instruction was not called. or the
program continues
Ret; The Ret instruction is put at the end of each subroutine to
make the programm go back

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 13 / 47
OUTLINE

1 Data Transfer Instructions

2 Arithmetic Instructions

3 Logic Instructions

4 Rotate,shift,Compare,Jump and Branch instructions

5 Subroutine Instructions

6 PORT PROCESSING INSTRUCTIONS

7 AVR STUDIO EXAMPLES FOR INSTRUCTION SETS


8 INDIRECT ADDRESSING WITH PORT PROCESSING
INSTRUCTIONS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 14 / 47
SOME USEFUL PORT PROCESSING INSTRUCTIONS
Sbi ioregister,bit; this instruction sets bit mentioned high
I Sbi PORTB,0 set portb0 high
Cbi ioregister,bit;this instruction clears bit mentioned and it
becomes low
I cbi PORTB,0 set portb0 low
Sbic ioregister,bit; this tests a bit in an I/O register and skips the
next instruction if the bit is cleared
I Sbic PIND,0 ; skip the next instruction if the bit in PORTD 0 is
cleared
Sbis ioregister,bit; this tests a bit in an I/O register and skips the
next instruction if the bit is set
I Sbis PIND,0 ; skip the next instruction if the bit in PORTD 0 is set
ldi Register,number ; this loads the immediate number into the
register;note this for register R16-R31

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 15 / 47
PORT PROCESSING INSTRUCTIONS
CONTINUATION
clr Register; this clears the contents of Register. it moves zero into
it
ser Register; this sets the contents of Register. it moves ones into
it.this works with Register R16-R31
out ioreg,Register; this moves the number out from a register and
moves it into IOregister.
in Register,ioreg; this copies the number from I/O Register into a
working register

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 16 / 47
OUTLINE

1 Data Transfer Instructions

2 Arithmetic Instructions

3 Logic Instructions

4 Rotate,shift,Compare,Jump and Branch instructions

5 Subroutine Instructions

6 PORT PROCESSING INSTRUCTIONS

7 AVR STUDIO EXAMPLES FOR INSTRUCTION SETS


8 INDIRECT ADDRESSING WITH PORT PROCESSING
INSTRUCTIONS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 17 / 47
Introduction to AVR STUDIO 6.0

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 18 / 47
CHOOSE ASSEMBLER AND INDICATE PROJECT
NAME

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 19 / 47
CHOOSE YOUR CHIP NAME ATMEGA32

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 20 / 47
AFTER PRESSING OK THE GIVEN PAGE SHOULD
SHOW

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 21 / 47
EXPLANATION
The forward slash and the closing one shows the file name we
created with asm as file extension
The stars between the slashes give the comments to the program
it shows name of file.asm date, created and author
blank space is were the program is written

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 22 / 47
THE PROGRAMING TEMPLATE

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 23 / 47
Examples with Data Transfer instructions
1 Ldi Rd,K; MOV Rd,Rr
I Write a simple program and verify with AVr Studio to perform the
following actions; Load R16 with a number 5 in decimal. Move now
the content of R16 to R17, show this in AVR Studio.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 24 / 47
notice R16 is updated with content of 5 as 0x05 is the hexadecimal
equivalent. In here Ldi command has been used

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 25 / 47
MOVE R17,R16 RESULTS; NOTICE R17 HAS NOW 5

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 26 / 47
Example 2;Ldi,STs,Ld Rd,X,Y,Z Instruction
Write a simple program to store 5 at address location 0060 of
Internal Data SRAM.Upon storing the value Write a program to
retrive the value and store it in R25

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 27 / 47
code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 28 / 47
RESULTS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 29 / 47
Example 3;Ldi,STs,Ld Rd,X+,Y+,Z+ Instruction
Write a simple program to store number 7,8,9 at addresses
0061,0062,0063. Store the values back to registers R17,R18,R19
Respectively

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 30 / 47
Code

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 31 / 47
RESULTS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 32 / 47
Examples on port processing instructions,with CPI,RJMP
and BREQ COMMANDS
Write a simple program to switch on an LED connected to PortA0
in reaction to a button placed on PORTB0

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 33 / 47
OUTLINE

1 Data Transfer Instructions

2 Arithmetic Instructions

3 Logic Instructions

4 Rotate,shift,Compare,Jump and Branch instructions

5 Subroutine Instructions

6 PORT PROCESSING INSTRUCTIONS

7 AVR STUDIO EXAMPLES FOR INSTRUCTION SETS


8 INDIRECT ADDRESSING WITH PORT PROCESSING
INSTRUCTIONS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 34 / 47
INDIRECT ADDRESSING WITH PORT PROCESSING
INSTRUCTIONS
Question
I Write a Simple Programe using Indirect Addressing where by when a
Button zero is pressed a Zero comes on a seven segment and when
button one is pressed a one comes on a seven segmment.

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 35 / 47
programe code and explanation

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 36 / 47
CONTINUATION

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 37 / 47
RESULTS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 38 / 47
RESULTS

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 39 / 47
DESIGNING A COUNTER
Write a simple program that counts and displays numebers on the
seven segment from 0 to 5.
I

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 40 / 47
CODE AND EXPLANATION

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 41 / 47
CODE AND EXPLANATION CONTINUATION

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 42 / 47
CODE AND EXPLANATION CONTINUATION

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 43 / 47
CODE AND EXPLANATION CONTINUATION

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 44 / 47
CODE AND EXPLANATION CONTINUATION

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 45 / 47
CODE AND EXPLANATION CONTINUATION

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 46 / 47
ANALYIS OF MACHINES

Three phase
1 One
2 two
induction motor

Eng George Mugala ( THE COPPERBELT UNIVERSITY


TUTORIAL SERIESTUTORIAL
SCHOOL OF ENGINEERING)
1EMBEDDED SYSTEMSJanuary 28, 2019 47 / 47

You might also like