Assembly Language Programming Part 1
Assembly Language Programming Part 1
Objectives
List the registers of AVR architecture
Examine the data memory of AVR architecture.
Perform simple operations such as ADD, LOAD and access internal RAM memory.
Explain the purpose of status register.
Discuss data RAM memory space allocation.
Code simple AVR assembly language instructions.
Describe AVR data types and directives.
Assemble and run AVR program using Atmel Studio.
Describe the sequence of events that occur up-on AVR power on.
Detail the execution of AVR Assembly language instructions.
Examine the AVR registers and data RAM using Atmel studio Simulator.
1
General purpose registers in AVR
CPU use registers to store data temporarily.
So, to program in assembly language, we must understand:
Registers and architectures of a given CPU and the role they play in processing data.
AVR microcontrollers have many registers for arithmetic and logical operations.
In the CPU, registers are used to store information temporarily. That information could be a byte of data
to be processed. Or an address pointing to the data to be fetched.
The vast majority of AVR registers are 8-bit registers.
The range from MSB(most significant bit (D7)) to the LSB(List significant bit (D0)).
Arranged with 8-bit data type.
any data larger than 8-bits must be broken in to 8-bit chunks before it is processed.
2
General purpose registers in AVR
In AVR there are 32 general purpose registers. They are R0-R31 and are located in the
lowest location of memory address.
3
Some simple instructions
LDI (Load Immediate): copies 8-bit data into the general purpose registers.
LDI Rd, k ; Its equivalent in high level languages: Rd = k
Rd can be any of the GPR’s R16 to R31
K can be 8-bit number.
To write comment in assembly language we use ;(semicolon).
We can not load values in to registers R0-R15 using the LDI instruction.
When programming the GPRs of the AVR microcontroller with an intermediate value the
following points should be noted.
If we want to present a number in hex we must put dollar sign($) or 0x in front of it.
If values from 0 to F are moved to 8-bit registers such as GPRs the rest of the bits assumed to be
all zero. E.g. LDI R16, 0x5 the result will be R16 = 0x05. or R16 = 00000101 in binary.
Moving a value larger than 255(FF in hex in to GPRs will cause an error.) E.g. LDI R17, 0x7F2.
4
Some simple instructions
LDI (Load Immediate): copies 8-bit data into the general purpose registers.
LDI Rd, k ; Its equivalent in high level languages: Rd = k
Rd can be any of the GPR’s R16 to R31
K can be 8-bit number.
Example:
LDI R16,53
R16 = 53
LDI R19,132
• R19=132
LDI R23,0x27
R23 = 0x27
We can not load values in to registers R0-R15 using the LDI instruction.
5
Some simple instructions
6
Some simple instructions
ADD instruction
Example:
Execute the above lines result in R16 = 0x59 (0x25 + 0x34 = 0x59)
7
The AVR Data Registers
In AVR microcontroller there are
two kinds of memory spaces.
Which are code memory space
and data memory space.
Our program is stored in code
memory space. Where as data
memory space stores data.
Data memory space composed of
three parts. These are GPRs, I/O
memory and internal data SRAM.
8
The AVR Data Registers
GPRs
As we have discussed earlier GPRs uses 32 bytes of data memory space.
They always take the address location $00-$1F in the data memory space.
The I/O data memory
is dedicated to specific functions such as status register, timer, serial communications, I/O port, ADC and so on.
The function of each I/O memory is fixed by CPU designer at design time. Because it is used to control the
microcontroller or peripherals.
I/O memory made up of 8-bit registers.
In some microcontrollers I/O memory called special purpose registers.
Internal SRAM
Used for storing data and parameters by AVR programmers and C compilers. Each location is 8-bit wide.
Generally it is called scratch pad.
The sizes of SRAM vary from chip to chip even in the same family.
9
Using instructions with the Data Memory
LDS Instruction(Load Direct from Data Space)
LDS Rd, K;
K can be $0000to $FFFF address locations Rd can be any 0-31 GPR’s.
Example:
LDS R0, 0x300
LDS R1,0x302
ADD R0,R1
The instruction tells the CPU to load(Copy) one byte from an address in data memory to
the GPRs.
After the instruction is executed, the GPR will have the same value as the location in the
data memory.
10
Using instructions with the Data Memory
Table: The effect of ADD R1,R0 (-assuming $300 and $302 have initially α and β respectively.
11
Using instructions with the Data Memory
STS Instruction(Store Direct to Data Space)
STS K, Rd;
K can be $0000 to $FFFF address locations and Rd can be any 0-31 GPR’s.
Example:
STS 0x230, R25;
The instruction tells the CPU to store(Copy) the content of GPR to an address in data memory space.
After the instruction is executed, the location in the data space will have the same value as the GPR.
Note: We can not copy(store) an immediate value directly into the SRAM location in AVR. This must be
done via GPRs.
Example:
12
Using instructions with the Data Memory
IN & OUT Instructions
IN Rd, A
load contents of an IO location to register
Rd can be 0-31 GPR’s
A can be 0-63 SFR’s or IO registers
OUT A, Rr
send contents of a register content to IO location
A can be 0-63 SFR’s or IO registers
Rr can be 0-31 GPR’s
IN vs LDS
IN is 2 bytes instruction, executes in 1 m/c cycle
IN can use IO register names instead of their addresses
IN is available in all AVR chips, LDS may or may not
13
Using instructions with the Data Memory
MOV instructions
Used to copy data among the GPRs of R0=R31 with the following format.
MOV Rd, Rr, where Rd and Rr can be any of the GPRs.
Example: MOV R10, R20, R10 = R20
For instance, if R20 contains 60, after execution of the above instruction both R20
and R10 will contain 60.
Example 2: The following program adds 0x19 to the contents of location 0x220 and
stores the result in location 0x221.
LDI R20, 0x19
LDS R21, 0x220
ADD R21, R20
STS 0x221, R21
14
Using instructions with the Data Memory
INC instruction: It increment the content of the given register.
INC Rd; it increment the contents of the Rd by one(0<=d<=31)
Example:
LDS R20, 0x430
INC R20
STS 0x430, R20
SUB Instruction
Tells the CPU to subtract the value of Rr from Rd and put the result back into the Rd register.
SUB Rd, Rr, Rd – Rr
Example:
LDI R20, 0x34
LDI R21, 0x25
SUB R20, R21
15
Using instructions with the Data Memory
DEC Instruction
Format: DEC Rd
It decrement(subtract 1) from the content of Rd and puts the result back
Example:
LDI R30, 3
DEC R30
DEC R30
16
Using instructions with the Data Memory
DEC Instruction
Format: DEC Rd
It decrement(subtract 1) from the content of Rd and puts the result back
Example:
LDI R30, 3
DEC R30
DEC R30
Example 2(COM instruction)
LDI R16, 0x55; R16 = 0x55
OUT PORTB, R16; copy R16 to PORT B SFR (PB = 0x55)
COM, R16; Complement R16 (R16 = 0xAA)
OUT PORTB, R16; copy R16 to Port B SFR (PB = 0xAA)
17
AVR Status Register
The AVR has a flag register to indicate arithmetic conditions such as the carry bit.
The status register (flag register) in the AVR is an 8-bit register.
In the status register the bits C, Z, N, V, S and H are called conditional flags. Meaning
that they indicate some conditions that result after an instruction is executed.
Each of the conditional flags can be used to perform a conditional branch(jump).
The Status register contains information about the result of the most recently executed
arithmetic instruction. This information can be used for altering program flow in order
to perform conditional operations. The Status register is updated after all the Arithmetic
Logical Unit (ALU) operations.
18
AVR Status Register Bits
I (bit 7): Global Interrupt Enable: If you have ever worked with interrupts in C, you
should be familiar with the Global Interrupt Enable flag. By setting this flag, interrupts
are enabled in the system. By clearing it, interrupts will not disrupt your program flow.
In C, you used special functions to set and clear the Global Interrupt Flag, sei() and cli().
In AVR Assembly, the same is accomplished with the instructions sei and cli.
sei ; enable interrupts
cli ; disable interrupts
T Flag(bit 6) : Copy Storage: is not actually set by any arithmetic or logical operations.
Instead, it is yours to set and clear based on how you see fit, using
the set and clt instructions.
set ; set T flag
clt ; clear T flag
19
AVR Status Register Bits
H (bit 5): Half Carry Flag:
is set when an overflow occurs between the lower and upper 4-bits of a register.
ldi r16, 0x0F ; load r16 with 0x0F
inc r16 ; increment r16 (half carry set)
The half carry is most useful when dealing with Binary Coded Digits, where two digits are stored in the
upper and lower nibbles (4-bits) of a register.
S (bit 4): Sign Flag, The Sign Flag is an extra bit of logic that can be used to determine if the result of a
previous operation is positive or negative. It is just the exclusive OR of the Negative and Two's
Complement Overflow flags. It will be set if one or the other is set, but not both. Consider
subtracting 10 from -120. It is obvious that the result it -130, but if we attempt this.
ldi r16,0x88 ; load r16 with 0x88 (-120)
ldi r17,0x0A ; load r17 with 0x0A (10)
sub r16,r17 ; subtract r17 from r16 (result = 0x7E = 126)
The result is not -130 since that value is too large for an 8-bit signed number. After
the sub instruction, the Negative Flag will not be set since bit 7 of the result is cleared.
However, the Sign Flag will be set, correctly indicating the result should be negative.
20
AVR Status Register Bits
S (bit 4): Sign Flag,
Consider addition instead
ldi r16,0x78 ; load r16 with 0x78 (120)
ldi r17,0x0A ; load r17 with 0x0A (10)
add r16,r17 ; add r17 to r16 (result = 0x82 = 130)
Here the Negative Flag will be set even though the result is clearly positive. However,
because a two's complement overflow occurred, the Sign Flag will not be set, correctly
indicating the result is positive.
Using the Sign flag can give you a better idea of whether a result is positive or negative.
It is very useful when creating alternate branches to handle cases like were shown
above.
21
AVR Status Register Bits
V(bit 3) : Overflow Flag: This flag is set whenever the result of a signed number
operation is too large, causing the high order bit to overflow into the sign bit. In general,
the carry flag used to detect errors in unsigned arithmetic operations while the overflow
flag is used to detect errors in signed arithmetic operations.
N (bit 2): Negative Flag: binary representation of signed numbers uses D7 as the sign
bit. The negative flag reflects the result of an arithmetic operation. If the D7 bit of the
result is zero, then N = 0 and the result is positive. If the D7 bit of the result is one, then
N = 1 and the result is negative.
Z (bit 1): Zero Flag: The zero flag reflects the result of an arithmetic or logic
operation. If the result is zero, then Z = 1. If the result is not zero, then Z = 0.
C (bit 0): Carry Flag: this flag is set whenever there is a carry from the D7 bit. This
flag bit is affected after an 8-bit addition or subtraction.
22
Status Register (SREG)
SREG: I T H S V N Z C
Carry
Interrupt oVerflow Zero
Temporary Negative Data Address
Sign Space
Half carry N+V
$0000
Example:Show
Example:
Example:
Example: Showthe
Show
Show thestatus
the
the statusof
status
status ofthe
of
of theC,
the
the C,H,
C,
C, H,and
H,
H, andZZ
and
and ZZflags
flagsafter
flags
flags after
after
after theGeneral
the
$0001 the
the addition
addition
subtraction
subtraction
of
of 0x9C
0x38 and of
of 0x9C
0x23
0x73 from
from
from 0x9C
0xA5
0x52 in
in the
the following
following
0x64 in the following instructions: instructions:
instructions: Purpose
R0 0x2F
...
Registers
ALU LDI LDILDI 0x38
R1
LDI
R16, R20, 0x9C
R20,
R20, 0x9C
0xA5
0x52;R16 = 0x38 $001F IO Address
R2 $0020 TWBR $00
...
SUB R17R20,
R20, R21;add R17;add
;subtract R21 from R20
...
SREG: I T H S V N Z C ADD SUB
ADD
R16, R20, R21
R21 ;subtract
R21 toR21
to R16 R20from R20
Registers
CPU
R15 $005F
SPH
SREG
$3E
$3F
R16
Solution: $0060
Solution:R17
Solution:
Solution: 11 General
$52
$9C
$A5 0101 0010
1001 1100
1010 1100
0101
$38
$9C 0011
1001 1000 purpose
...
PC
…
24
Assembler Directives
While instructions tell the CPU what to do, directives also called pseudo-instructions give
directions to the assembler.
.EQU(equate)
Used to define a constant value or a fixed memory address. It does not set aside
storage for a data item, but associate a constant number with a data or an address label
so that when the label appears in the program, its constant will be substituted for the
label.
Example:- .EQU COUNT = 0x25
LDI R21, COUNT
After executing the above instruction R21 will be loaded with the value 25 hex.
We can also use the names of the I/O registers instead of their address with .EQU
directive.
.EQU PORTB = 0x1B
.SET directive is identical with .EQU with the only difference that the value assigned by
the .SET directive may be reassigned later.
25
Assembler Directives
.ORG(Origin)
Used to indicate the beginning of the address. It can be used for both code and data.
.INCLUDE directives
Tells the AVR assembler to add the content of a file to our program.(like #include
directive in C/C++).
For example when you want to use ATmega32, you must write the following instruction
at the beginning of your program.
.INCLUDE “M32DEF.INC”
26
Introduction to AVR Assembly Programming
It uses mnemonics to represent the operations that a processor has to do. Which is an
intermediate language between high-level languages like C++ and the binary language.
The operands are the data items being manipulated, and the mnemonics are the
commands to the CPU, telling it what to do with those items.
27
Introduction to AVR Assembly Programming
An assembler is used to convert assembly code into machine language. That machine
code is stored in an executable file for the sake of execution.
It enables the programmer to communicate directly with the hardware such as registers,
memory locations, input/output devices or any other hardware components. Which
could help the programmer to directly control hardware components and to manage the
resources in an efficient manner.
28
Assembling an AVR Program
29
Assembling an AVR Program
30
Branch, Call, and Time Delay Loop
BIHE university
Objectives
Code AVR assembly language instructions to create loops.
Code AVR assembly language conditional branch instructions.
Explain the conditions that determine each conditional branch instruction.
Code JMP(Long Jump) instructions for unconditional jumps.
Code AVR subroutines.
Describe stack and its use in subroutines.
Discuss pipelining in the AVR.
Discuss crystal frequency versus instruction cycle time in the AVR.
Code AVR Programs to generate a time delay.
In the sequence of instructions to be executed, it is often necessary to transfer program control to different
location. There are many instructions in AVR to achieve this.
• Again, line 4 7 }
8
• Again line 5 9
• Line 6
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 34Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Introduction (Continued)
BIHE university
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 35Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Conditional Jump in AVR
SREG: I T H S V N Z C
BIHE university
• Write a program that if R20 is equal to R21 then R22 increases. if (R20 == R21)
No
• Solution:
Yes
SUB R20,R21 ;Z will be set if R20 == R21
BRNE NEXT ;if Not Equal jump to next
INC R22 increment R22
NEXT:
• Solution: Yes
INC R22
L1:
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 38Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Example 3
BIHE university
R17 = 5;
if (R20 > R21)
R22++;
else
R22--;
R17 ++;
LDI R17,5
SUB R21,R20
BRCS IF_LABEL
DEC R22
JMP NEXT
IF_LABEL:
INC R22
NEXT:
INC R17
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 40Education,
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Loop
BIHE university
• Example 1: Write a program that executes the instruction “ADD R30,R31” 9 times.
• Solution:
.ORG 00
LDI R16,9 ;R16 = 9 R16 = 9
L1: ADD R30,R31
DEC R16 ;R16 = R16 - 1
ADD R30,R31
BRNE L1 ;if Z = 0
L2: RJMP L2 ;Wait here forever
R16 = R16 - 1
Yes
if (R16 > 0)
No
END
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 41Education,
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Loop
BIHE university
• Solution: R16 = 9
R17 = 0
.ORG 00
LDI R16, 9 ;R16 = 9 R17 = R17 + R16
LDI R17, 0 ;R17 = 0
L1: ADD R17,R16 ;R17 = R17 + R16
R16 = R16 - 1
DEC R16 ;R16 = R16 - 1
BRNE L1 ;if Z = 0
L2: RJMP L2 ;Wait here forever
Yes
if (R16 > 0)
No
END
• Solution: R16 = 20
R17 = 0
.ORG 00
LDI R16, 20 ;R16 = 20 R17 = R17 + R16
LDI R17, 0 ;R17 = 0
L1: ADD R17,R16 ;R17 = R17 + R16
R16 = R16 - 1
DEC R16 ;R16 = R16 - 1
BRNE L1 ;if Z = 0
L2: RJMP L2 ;Wait here forever
Yes
if (R16 > 0)
No
END
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 43Education,
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Loop
BIHE university
{
do something Do something
}
calculation
Yes
Condition
No
END
LDI R20,0
LDI R16,1 R20 = R20 + R16
L1:ADD R20,R16
LDI R17,2 R16 = R16 + 2
END
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 45Education,
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Branch instructions and Looping(3)
Example 5: Write a program to clear R20, then add 3 to R20 ten times and send the sum to PORTB. Use the zero
BIHE university
Because the location R16 is 8-bit register it can hold a maximum of 0xFF(255 decimal), therefore, the loop can be
repeated a maximum of 255 times.
How we solve this problem?
AVR Microcontroller and Embedded System Using Assembly and C
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi
46
© 2011 Pearson Higher Education,
Upper Saddle River, NJ 07458. • All Rights Reserved.
Branch instructions and Looping(4)
BIHE university
In this instruction, the Z flag is checked. If it is high, the CPU jumps to the target address.
Example:
– TST R20
– BREQ OVER
In this program, if PINB is zero, the CPU jumps to the label OVER. It says in the loop until PINB
has a value other than zero. Notice that the TST instruction can be examine a register and set the
flags according to the contents of the register without performing an arithmetic instruction such
as decrement.
When the TST instruction executes, if the register contains the zero value, the zero flag is set;
otherwise, it is cleared. It also sets the N flag high if the D7 bit of the register is high, otherwise
N=0.
AVR Microcontroller and Embedded System Using Assembly and C
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi
49
© 2011 Pearson Higher Education,
Upper Saddle River, NJ 07458. • All Rights Reserved.
Other Conditional Jumps
Example 6: write a program to determine if RAM location 0x200 contains the value 0. if so, put 0x55 into it.
BIHE university
Write a program to find the sum of the values 0x79, 0xF5 and 0xE2. Put the sum into R20(lower
The unconditional branch is a jump in which control is transferred unconditionally to the target location.
In the AVR there are three unconditional branches. These are JMP(Jump), RJMP(Relative Jump) and
IJMP(indirect Jump).
In is unconditional jump that can go to any memory location in the 4M(word) address space of the AVR.
In it a 4-byte(32 bit) instruction in which 10 bits are used for the opcode, and the other 22 bits represent
The 22-bit target address allows a jump to 4M of memory locations from 000000 to $3FFFFF.
• Jump changes the Program Counter (PC) and causes the CPU to execute an instruction
other than the next instruction.
There are 2 kinds of Jump
– Unconditional Jump: When CPU executes an unconditional jump, it jumps
unconditionally (without checking any condition) to the target location.
• Example: RJMP and JMP instructions
– Conditional Jump: When CPU executes a conditional jump, it checks a condition, if the
condition is true then it jumps to the target location; otherwise, it executes the next
instruction.
1 LDI R16, 0
RJMP, JMP, and IJMP 2 LDI R17, 2
JMP Instruction:
JMP is an unconditional jump instruction. It
is 4 byte in size.
10 bits are used for opcode, and rest 22 bits
target address allows a jump to 4M of
memory location: $000000 to $3FFFFF
• JMP PC = operand
1001 010X XXXX 110X XXXX XXXX XXXX XXXX XXXX XXXX
– Example:
1001 0100 0000 1100 0000 0000 0000 0110
• Operand = 0000000000000000000110
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 57Education,
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
JMP
BIHE university
Addres Code
• In JMP, the operand, contains the PC: 0007
0002
0001
0000 s
address of the destination 0000 .ORG 0
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 58Education,
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
RJMP
BIHE university
• RJMP PC = PC + operand
1100 XXXX XXXX XXXX
• Operand = 000000000110
• PC = PC + 000000000110
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 60Education,
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
RJMP
BIHE university
PC: 0003
0007
0006
0001
0000
0002 Addres Code
• When RJMP is executed: +0
+F
s
0000 .ORG 0
0005 0000
– The operand will be added to the Machine 0001
LDI
LDI
R16, 15
R17, 5
code:
current value of PC 002
C002 0002 RJMP LBL_NAME
opCod operan 0003 LDI R18, 4
e d 0004 ADD R18, R17
0005 LBL_NAME:
0005
Machine
0005 ADD R16,R17
code: 0006 RJMP LBL_NAME
FFE
CFFE 0007
opCod operan
e d
• STACK POINTER
– A section of the RAM which store data or address temporarily since there are
limited number of registers.
– SP must be wide enough address all the RAM
– Conventionally SP is initialized with highest address of the RAM
– In AVR, RAMEND represents the last address of the RAM
– SP decrements when a PUSH takes place
– SP increments when a POP takes place
• Push instruction store such as program • POP instruction loads the content of the
counter to the stack stack back to the CPU.
• PUSH Rr • POP Rd
[SP] = Rr
SP = SP + 1
SP = SP - 1
Rd = [SP]
S
P
Stac
k
Addres Code
s
ORG 0
0000 LDI R16,HIGH(RAMEND)
0001 OUT SPH,R16
R20: $10
$00 R22 $30
$00
0002 LDI R16,LOW(RAMEND)
: OUT SPL,R16
R21 $00
$20 R0: $00 0003
: 0004 LDI R20,0x10
0005 LDI R21, 0x20
0006 LDI R22,0x30
0007 PUSH R20
SP 0000 $10
0008 PUSH R21
$20
0009 PUSH R22
$30
POP R21
000A
000B POP R0
000C POP R20
000D L1: RJMP L1
Memor
AVR Microcontroller and Embedded System Using Assembly and Cy
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi
66
© 2011 Pearson Higher Education,
Upper Saddle River, NJ 07458. • All Rights Reserved.
Usage of SP – An Example
BIHE university
• CALL Instruction:
– 4 bytes instruction
– Uses 10 bits for opcode, 22 bits for target address
– Saves the address of the immediate instruction next to CALL in the stack.
• To execute a call:
– Address of the next instruction is saved
– PC is loaded with the appropriate value Addres
s
Code
0000 LDI R16,HIGH(RAMEND)
0001 OUT SPH,R16
0002 LDI R16,LOW(RAMEND)
0003 OUT SPL,R16
0004 LDI R20,15
Stac
k
AVR Microcontroller and Embedded System Using Assembly and C
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi
69
© 2011 Pearson Higher Education,
Upper Saddle River, NJ 07458. • All Rights Reserved.
RET
BIHE university
• RET Instruction:
– When executed, the top of the stack is copied back to the PC and SP is
incremented.
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 70Education,
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Time delay
BIHE university
• AVR microprocessors are able to execute an instruction in one cycle. There are three
ways of doing this.
– Use Harvard architecture to get the maximum amount of code and data into the
CPU.
– Use RISC architecture features such as fixed-size instructions.
– Use pipelining to overlap fetching and execution of instructions.
• Pipelining :
– In the early microprocessors, the CPU could either fetch or execute at a given time.
In other words, the CPU had to fetch an instruction from the memory, then execute
it then again fetch the next instruction, execute it, and so on.
– Pipelining allows the CPU to fetch and execute the given instruction at the same
time.
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 72Education,
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Time delay …
BIHE university
• We can use the pipeline to speed up execution of instructions. In pipelines, the process
of execution is split up into smaller steps that are all executed in parallel. In the
execution of instructions, we must make sure that the sequence of instructions is kept
intact and that there is no different execution.
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 73Education,
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Instruction cycle time for the AVR
BIHE university
• It takes a certain amount of time for the CPU to execute an instruction. This time is
referred to as machine cycles.
• All the instructions in the AVR are either 2-byte or 4-byte and hence most of the
instructions take no more than 2 machine cycles to execute (some instructions may
take up 3 to 4 machine cycles to execute like JMP CALL, RET).
• In the AVR family, the duration of the machine cycle depends upon the frequency of
the oscillator connected to the AVR system.
• In the AVR, one machine cycle consists of one oscillator period, which means that
with each oscillator clock, one machine cycle passes.
• Therefore, to calculate the machine cycle for the AVR, we take the inverse of the
crystal frequency.
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 74Education,
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Instruction cycle time for the AVR …
• Instruction cycles required by different instructions (considering 1 MHz as the crystal frequency) :
BIHE university
LDI 1 1 us
DEC 1 1 us
OUT 1 1 us
ADD 1 1 us
NOP 1 1 us
JMP 3 3 us
CALL 4 4 us
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 75Education,
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Instruction cycle time for the AVR …
BIHE university
• Example-1 :
• For the given crystal frequencies, calculate the period of the instruction cycles.
a) 8 MHz
b) 16 MHz
• Solution :
a) instruction cycle = 1/ 8 MHz = 0.125 us (microsecond)
b) instruction cycle = 1/ 16 MHz = 0.0625 us
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 76Education,
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Instruction cycle time for the AVR …
• Example-2 :
BIHE university
• Find the delay in us of the code snippet below if the crystal frequency is 10 MHz
Instruction Cycles
DELAY : LDI COUNT, 0XFF 0
Again : NOP 1
NOP 1
NOP 1
DEC COUNT 1
BRNE AGAIN 2/1
RET 4
Note: Conditional Branch instruction such as BRNE, BRLO, can take only one
machine cycle if it does not jump.
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher 77Education,
Embedded Systems by Course owner
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Branch Penalty
BIHE university
machine cycle
LDI R16, 19 1
LDI R20, 95 1
LDI R21, 5 1
ADD R16, R20 1
ADD R16, R21 1
5
machine cycle
LDI R16, 100
1
AGAIN: ADD R17,R16 *100
1
DEC R16 *100
1
BRNE AGAIN *100
1/2
Branch penalty
machine cycle
LDI R17, 20
1
L1: LDI R16, 50
1 *20
L2: NOP
1 *20 * 50
NOP *20 * 50
1
DEC R16 *20 * 50
1
BRNE L2 *20 * 50
1/2
DEC R17 *20
1
BRNE L1 *20
1/2
• Delay loop
• Objectives
– List all ports of AVR microcontroller.
– Describe the dual role of AVR PINs
– Code assembly language to use the ports as input and output.
– Explain the dual role of ports A, B, C and D.
– Code AVR instructions for I/O handling.
– Code I/O bit manipulation programs for the AVRs
• In the AVR family, there are many ports for I/O operations, depending on the family member. In case of
Atmega32, in is 40-pin chip. A total of 32 pins are set aside for the four ports A, B, C and D.
• The rest of the pins are designed as VCC, GND, XTAL1, XTAL2, RESET, AREF, AGND and AVCC.
Mega32/Mega16
(XCK/T0) PB0 0 00 00 0 PA0 (ADC0)
(T1) PB1 1 11 11 1 PA1 (ADC1)
(INT2/AIN0) PB2 2 22 22 2 PA2 (ADC2)
(OC0/AIN1) PB3 3 33 33 3 PA3 (ADC3)
(SS) PB4 4 44 44 4 PA4 (ADC4)
(MOSI) PB5 DDRx: 5
7 5 65 5 4 3 5 25 51 0
PA5 (ADC5)
(MISO) PB6 PORTx: 6
7 6 66 5 4 3 6 26 61 0
PA6 (ADC6)
(SCK) PB7 PINx: 7
7 7 67 5 4 3 7 27 71 0
PA7 (ADC7)
DDRA
DDRB
PINB
PORTB
RESET PORTA AREF
PINA
Px7 Px6 Px5 Px4 Px3 Px2 Px1 Px0
VCC AGND
GND AVCC
XTAL2 PC7 (TOSC2)
XTAL1 PC6 (TOSC1)
(RXD) PD0 PC5 (TDI)
DDRx
(TXD) PD1 PC4 (TDO) 0 1
PORTx
(INT0) PD2 PC3 (TMS)
0 high impedance Out 0
(INT1) PD3 PC2 (TCK)
(OC1B) PD4 PC1 1 (SDA)pull-up Out 1
(OC1A) PD5 PC0 (SCL)
(ICP) PD6 PD7 (OC2)
• Each port has three I/O registers associated with it. They are designed as
– PINx (Port Input x),
– DDRx (Data Direction Register x) and
– PORTx. Where x represents ports A, B, C and D.
• Each of the I/O registers 8-bits wide. And each port has a maximum of 8-pins.
Therefore, each bit of the I/O registers affect one of the pins. For example the content
of bit 0 of DDRB represent the direction of the PB0 pin.
• DDRx role in outputting data
– Each of the ports A-D in the Atmega32 can be used for input or output.
– DDRx I/O register used solely for the purpose of making a given port an input or
output port.
– For example to make a port an output, we write 1s to the DDRx register.
• Example: Write a program that makes all the pins of PORTA one.
.INCLUDE “M32DEF.INC”
LDI R16,0xFF ;R16 = 11111111 (binary)
OUT DDRA,R20 ;DDRA = R16
L1: LDI R16, 0x55
OUT PORTA,R20 ;PORTA = R16
CALL DELAY
LDI R16, 0xAA
OUT PORTA, R16 ;PORTA = R16
CALL DELAY
RJMP L1
DDRx.0 = 0
PORTx.= 1
• Example 2
• The following code will toggle all 8 bits of Port B forever with some time delay
between “on” and “off” states:
LDI R16,0xFF ;R16 = 0xFF = 0b11111111
OUT DDRB,R16 ;make Port B an output port (1111 1111)
L1: LDI R16,0x55 ;R16 = 0x55 = 0b01010101
OUT PORTB,R16 ;put 0x55 on port B pins
LDI R16,0xAA ;R16 = 0xAA = 0b10101010
OUT PORTB,R16 ;put 0xAA on port B pins
RJMP L1
DDRC: 1 1 1 1 1 1 1 1
PORTC: 0 0 0 0 0 1 1 0
ATmega32 0
5 1
.INCLUDE “M32DEF.INC” 8 6
PORTC
LDI R20,0x06 ;R20 = 00000110 (binary) 2
4
OUT PORTC,R20 ;PORTC = R20
LDI R20,0xFF ;R20 = 11111111 (binary) 3
DDRx
OUT DDRC,R20 ;DDRC = R20 0 1
PORTx
L1: RJMP L1
0 high impedance Out 0
1 pull-up Out 1
DDR: 1 1 1 1 1 1 1 1
PORTC: 0 1 0 0 1 1 1 1
ATmega32 0
5 1
.INCLUDE “M32DEF.INC” 8 6
PORTC
LDI R20,0x4F ;R20 = 01001111 (binary) 2
4
OUT PORTC,R20 ;PORTC = R20
LDI R20,0xFF ;R20 = 11111111 (binary) 3
DDRx
0 1
L1: RJMP L1 PORTx
1 pull-up Out 1
• The following code gets the data present at the pins of port C and sends it to port B indefinitely, after
adding the value 5 to it:
.INCLUDE "M32DEF.INC"
LDI R16,0x00 ;R16 = 00000000 (binary)
OUT DDRC,R16 ;make Port C an input port
LDI R16,0xFF ;R16 = 11111111 (binary)
OUT DDRB,R16 ;make Port B an output port(1 for Out)
L2: IN R16,PINC ;read data from Port C and put in R16
LDI R17,5
ADD R16,R17 ;add 5 to it
OUT PORTB,R16 ;send it to Port B
RJMP L2 ;continue forever
• Write a program that continuously sends out to Port C the alternating values of 0x55
and 0xAA.
.INCLUDE "M32DEF.INC"
LDI R16,0xFF ;R16 = 11111111 (binary)
OUT DDRC,R16 ;make Port C an output port
L1: LDI R16,0x55 ;R16 = 0x55
OUT PORTC,R16 ;put 0x55 on Port C pins
LDI R16,0xAA ;R16 = 0xAA
OUT PORTC,R16 ;put 0xAA on Port C pins
RJMP L1
DDRx
0 1
PORTx
1 pull-up Out 1
REDING Assignment