Chapter 3
Chapter 3
Chapter 3
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Topics
Introduction to jump and call instructions
Jump
Call
Stack
Calling a function
Program/code execution time
Generate time delay in software
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Jump and Call
CPU executes instructions one
after another.
1 void main ()
For example in the following C 2 {
program, CPU first executes the 3 a = b + c;
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Jump and Call (Continued)
But sometimes we need the CPU to execute, an
instruction other than the next instruction. For
example:
When we use a conditional instruction (if)
When we make a loop
When we call a function
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Jump and Call (Continued)
Example 1: Not executing the
next instruction, because of
condition. 1 void main ()
In the following example, the 2 {
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Jump and Call (Continued)
Example 2: In this example
the next instruction will not
be executed because of 1 void main ()
loop. 2 {
3
In the following example,
int a, c = 0;
4 for(a = 2; a < 4; a++)
follows: 6
7 }
a = c + 2;
Line 4 8
9
Line 5
Again, line 4
Again line 5
Line 6
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Jump and Call (Continued)
Example 3: Not executing
the next instruction, because
of calling a function. 1
Code
void func1 ();
In the following example, 2 void main ()
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Jump and Call (Continued)
In the assembly language, there are 2 groups of
instructions that make the CPU execute an
instruction other than the next instruction. The
instructions are:
Jump: used for making loops and checking
condition
Call: used for making function calls
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Jump
Jump changes the Program Counter (PC)
and causes the CPU to execute an instruction
other than the next instruction.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Jump
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.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Unconditional Jump in AVR
There are 3 unconditional jump
instructions in AVR: RJMP,
JMP, and IJMP Code
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
JMP
JMP PC = operand
1001 010X XXXX 110X XXXX XXXX XXXX XXXX
10 bit opcode (bits in red) and a 22 bit operand (x)
2^22 locations – the entire program ROM
Can jump anywhere in the program ROM
Example:
910014010000000C1100
0 0000
0 0000
0 0000
6 0110
Operand = 0000000000000000000110
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
JMP
Address Code
In JMP, the operand, PC: 0002
0001
0000
0007
contains the address of 0000 .ORG 0
value 0006
0006 LBL_NAME:
Machine code: 0006 ADD R16,R17
940C 0006
0006 0007 JMP LBL_NAME
opCode operand 0009
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
RJMP (Relative jump)
RJMP PC = PC + operand
2^12 – 4096 locations
1100 XXXX XXXX XXXX
Operand = 000000000110
PC = PC + 000000000110
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
IJMP (Indirect jump)
IJMP PC = Z register
1001 0100 0000 1001
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Conditional Jump in AVR
SREG: I T H S V N Z C
The conditional jump instructions in AVR are as follows:
Instruction Abbreviation of Comment
BREQ lbl Branch if Equal Jump to location lbl if Z = 1,
BRNE lbl Branch if Not Equal Jump if Z = 0, to location lbl
BRCS lbl Branch if Carry Set Jump to location lbl, if C = 1
BRLO lbl Branch if Lower
BRCC lbl Branch if Carry Cleared Jump to location lbl, if C = 0
BRSH lbl Branch if Same or Higher
BRMI lbl Branch if Minus Jump to location lbl, if N = 1
BRPL lbl Branch if Plus Jump if N = 0
BRGE lbl Branch if Greater or Equal Jump if S = 0
BRLTlbl Branch if Less Than Jump if S = 1
BRHS lbl Branch if Half Carry Set If H = 1 then jump to lbl
BRHC lbl Branch if Half Carry Cleared if H = 0 then jump to lbl
BRTS Branch if T flag Set If T = 1 then jump to lbl
BRTC Branch if T flag Cleared If T = 0 then jump to lbl
BRIS Branch if I flag set If I = 1 then jump to lbl
BRIC Branch if I flag cleared If I = 0 then jump to lbl
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Usages of Conditional jump
Check for a condition
Loop – repeating N times
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Conditions
When b is subtracted from a: (A-B)
The result is zero, when a is equal to b a
Carry will be set when a < b -b
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 1
Write a program to check if R20 is equal to R21.
If true then increment R22, else do nothing.
INC R22
NEXT: increment R22
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 2
Write a program to check if R26 < R24 then R22
increases, else do nothing.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 3
Write a program that if R26 >= R24 then R22
increases.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 4: IF and ELSE
R17 = 5;
R17 = 5
if (R20 > R21)
R22++;
else
No
R22--; if (R20 > R21)
R17 ++;
Yes
increment R22
LDI R17,5
SUB R21,R20
Decrement R22
BRCS IF_LABEL
DEC R22
JMP NEXT
IF_LABEL:
increment R17
INC R22
NEXT:
INC R17
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Loop
Write a program that executes the instruction
“ADD R30,R31” 9 times.
R16 = 9
Solution:
.ORG 00 ADD R30,R31
No
END
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Loop
Write a program that calculates the result of
9+8+7+…+1
R16 = 9
R17 = 0
Solution:
.ORG 00 R17 = R17 + R16
9+8+7+6+....+1 No
END
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Loop
Write a program that calculates the result of
20+19+18+17+…+1
R16 = 20
R17 = 0
Solution:
.ORG 00 R17 = R17 + R16
No
END
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Loop
for (init; condition; calculation) init
{
do something
Do something
} calculation
{ No
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Loop
Write a program that calculates 1+3+5+…+27
Solution:
R20 = 0
R16 = 1
LDI R20,0
LDI R16,1 R20 = R20 + R16
L1:ADD R20,R16
R16 = R16 + 2
INC R16
INC R16
LDI R17,27 ;R17 = 27
Yes
R16 <= 27
SUB R17,R16
BRCC L1 ;if R16 <= 27 jump L1 No
END
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Loop
Write a program that calculates 0+2+4+…+28
Solution:
R20 = 0
R16 = 1
LDI R20,0
LDI R16,0 R20 = R20 + R16
L1:ADD R20,R16
R16 = R16 + 2
INC R16
INC R16
LDI R17,28 ;R17 = 27
Yes
R16 <= 27
SUB R17,R16
BRCC L1 ;if R16 <= 27 jump L1 No
END
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Call Topics
Stack, Push and Pop
Calling a function
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Initializing the Stack Pointer
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Stack
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Stack Bottom
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Stack Pointer
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Stack
PUSH Rr POP Rd
[SP] = Rr
SP = SP + 1
SP = SP - 1
Rd = [SP]
SP
Stack
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Stack
Address Code
ORG 0
0000 LDI R16,HIGH(RAMEND)
0001 OUT SPH,R16
R20: $10
$00 R22: $30
$00
0002 LDI R16,LOW(RAMEND)
R21: $00
$20 R0: $00 0003 OUT SPL,R16
0004 LDI R20,0x10
0005 LDI R21, 0x20
Memory
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Calling a Function
40 PIN DIP
(XCK/T0) PB0 1 40 PA0 (ADC0)
(T1) PB1 2 39 PA1 (ADC1)
(INT2/AIN0) PB2 3 38 PA2 (ADC2)
(OC0/AIN1) PB3 4 MEGA32 37 PA3 (ADC3)
(SS) PB4 5 36 PA4 (ADC4)
(MOSI) PB5 6 35 PA5 (ADC5)
(MISO) PB6 7 34 PA6 (ADC6)
(SCK) PB7 8 33 PA7 (ADC7)
RESET 9 32 AREF RAM EEPROM Timers
VCC 10 31 AGND
GND 11 30 AVCC PROGRAM
XTAL2 12 29 PC7 (TOSC2) Flash ROM
XTAL1 13 28 PC6 (TOSC1)
Program Data
(RXD) PD0 14 27 PC5 (TDI)
Bus Bus
(TXD) PD1 15 26 PC4 (TDO) CPU
(INT0) PD2 16 25 PC3 (TMS)
(INT1) PD3 17 24 PC2 (TCK)
(OC1B) PD4 18 23 PC1 (SDA)
(OC1A) PD5 19 22 PC0 (SCL)
(ICP) PD6 20 21 PD7 (OC2)
Interrupt Other
OSC Ports
Unit Peripherals
I/O
PINS
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Software Delay
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Software Delay
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Time Delay
Time Delay = number of clock cycles x time period of the clock cycle
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Time delay
machine cycle
LDI R16, 19 1
LDI R20, 95 1
LDI R21, 5 1
ADD R16, R20 1
ADD R16, R21 1
5
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Time delay
machine cycle
LDI R16, 100
1
AGAIN: ADD R17,R16 *100
1
*100
DEC R16 1
BRNE AGAIN 1/2 *100
Branch penalty
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Time delay
machine cycle
LDI R16, 50
1
AGAIN: NOP 1 *50
NOP 1 *50
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Time delay
machine cycle
LDI R17, 20
1
L1: LDI R16, 50
1 *20
L2: NOP
1 *20 * 50
NOP 1 *20 * 50
DEC R16 1 *20 * 50
BRNE L2 1/2 *20 * 50
DEC R17 1 *20
BRNE L1 1/2 *20
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example with 2 loops
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example with 3 loops
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Address Code
0000 .ORG 0
0000 LDS R16, 15
0001 LDS R17, 5
0002 RJMP L1
0003 LDS R18, 4
0004 ADD R18, R17
0005 STS 0x90,R18
0006 L1: SUB R16,R17
0007 STS 0x90,R16
0008 RJMP LBL_NAME
0009
000A
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights