Professional Documents
Culture Documents
Chapter 5
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Objectives
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Some ideas
• 100 – 34 = ?
• 99 – 34 = ?
• 100 – 34 = (99-34) + 1
• 34 – 19 = ?
• 34 +100 -19 – 100 = 34 + (99-19)+1 -100
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
• 100000000 – 00101101 = ?
• =011111111 – 00101101 + 1 =A
• 010110110 – 00101101 =
• 010110110 + A – 100000000 =
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
ADD instructions
ADD Rd,Rr ;Rd = Rd + Rr ( Direct or immediate are not supported)
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
ADD instructions
ADD Rd,Rr ;Rd = Rd + Rr ( Direct or immediate are not supported)
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
ADC instructions
ADC Rd,Rr ; Rd = Rd + Rr + C ; it means add two registers along with C flag
16+7 = 23
17
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
SUB instruction
SUB Rd,Rr ;Rd = Rd - Rr ( immediate are not supported)
SUBI Rd,K ; Rd = Rd – K
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
SUB: Example
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
SBC instruction
SBC Rd,Rr ;Rd = Rd – Rr-C ( immediate are not supported)
SBCI Rd,Rr ;Rd = Rd – K-C
27 62 (H)
- 12 96 (H)
-----------
14 CC (H)
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Multiplication
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Division
There is no division instruction available in AVR. However, we can divided numbers by
repeated subtraction as shown in the example below:
.DEF NUM,R20
.DEF DEN,R21
.DEF QOT,R22
LDI NUM, 45
LDI DEN,10
CLR QOT
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Logic Instructions
AND Rd,Rr ;Rd = Rd AND Rr
ANDI Rd,K
OR Rd,Rr ;Rd = Rd OR Rr
EOR Rd,Rr ;Rd = Rd XOR Rr ( immediate are not supported)
COM Rd ;Rd = 1’ Complement of Rd (11111111 – Rd)
NEG Rd ;Rd = 2’ Complement of Rd (100000000 – Rd)
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
00 0 00 0 00 0
01 0 01 1 01 1
10 0 10 1 10 1
11 1 11 1 11 0
AND OR XOR
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
00 0
01 0
10 0
11 1
AND
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
00 0
01 1
10 1
11 1
OR
SBR, CBR FOR GPRs BUT CAN ONLY SET OR CLEAR ONE BIT AT A TIME
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Setting and Clearing bits
AND Rd,Rr ;Rd = Rd AND Rr
OR Rd,Rr ;Rd = Rd OR Rr
EOR Rd,Rr ;Rd = Rd XOR Rr ( immediate are not supported)
COM Rd ;Rd = 1’ Complement of Rd (11111111 – Rd)
NEG Rd ;Rd = 2’ Complement of Rd (100000000 – Rd)
AND OR
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
EX-OR Function
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
QUIZ:
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Branch and CP Instructions
CP Rd, Rr ;Rd – Rr (only flags are set)
AFTER CP, YOU USE THE BRANCH INSTRUCTION
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
ROR instruction
ROR Rd ;Rd (only flags are set)
In ROR, as bits are rotated from left to right, the carry flag enters the MSB
and the LSB exits to the carry flag. In other words, in ROR the C is moved to
the MSB, and the LSB is moved to the C.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
ROL instruction
ROL Rd ;Rd (only flags are set)
ROL. In ROL, as bits are shifted from right to left, the carry flag enters the LSB and
the MSB exits to the carry flag. In other words, in ROL the C is moved to the LSB,
and the MSB is moved to the C.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Parity error detection system – SERIAL Communication Systems
Data is sent with even parity or odd parity – you add one extra bit as
a parity bit to the data packet
Even parity means the number of ones in the data sent out is EVEN
Tx------------------------------------( ) ---------------------------------------Rx
10011010
Send data channel will add noise
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
For even parity scheme:
Data is stored at $100 in RAM
Algorithm for generating parity bit:
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
1. WRITE A PROGRAM FOR THE TRANSMITTER MODULE IN A SERIAL
COMMUNICATIONS SYSTEM TO GENERATE PARITY FOR A 7-BIT
DATA VALUE STORED IN RAM LOCATION $100. CONSIDER ODD
PARITY SCHEME.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Counting 1s in a byte
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Data Serialization
1010000011
Microcontroller
Rx
Serial device
Tx
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
• 41H = 0100 0001
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Data Serialization
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
LSL Instruction
LSL Rd ;logical shift left
In the next code you can see what happens to 00100110 after running 3 LSL
instructions.
CLC ;make C = 0 (carry is 0 )
LDI R20 , 0x26 ;R20 = 0010 0110(38) c = 0
LSL R20 ;R20 = 0100 1100(74) C = 0
LSL R20 ;R20 = 1001 1000(148) C = 0
LSL R20 ;R20 = 0011 0000(98) C = 1 as C=1 and content of R20
;is not multiplied by 2
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
0000 0100 = 4 LSR R20
0000 1000 = 8
0001 0000 = 16 1000 0000 = 128
0010 0000 = 32 0100 0000 = 64
0100 0000 = 64 0010 0000 = 32
1000 0000 = 128 0001 0000 = 16
0000 0000 = 0, C = 1 0000 1000 = 8
0000 0100 = 4
LSL R20
EX2: R20 / 9
EX1: R20 X 9
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
R20 = 0000 1000 R20 = 1000 0001
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
LSR Instruction
LSR Rd ;Rd (only flags are set)
this instruction divides content of the register by 2 and carry flag contains
the remainder of division.
In the next code you can see what happens to 0010 0110 after running 3 LSL
instructions.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
ASR Instruction
ASR Rd ;Rd (only flags are set)
In the next code you can see what happens to 0010 0110 after running 5 ASL
instructions.
LDI R20 , 0D60 ;R20 = 1101 0000(-48) c = 0
LSL R20 ;R20 = 1110 1000(-24) C = 0
LSL R20 ;R20 = 1111 0100(-12) C = 0
LSL R20 ;R20 = 1111 1010(-6) C = 0
LSL R20 ;R20 = 1111 1101(-3) C = 0
LSL R20 ;R20 = 1111 1110(-1) C = 1
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
R20 = 1011 1000 , R20 = -0100 1000 = -72
1101 1100 0
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
LSR Limitation
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
SWAP Instruction
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Swapping Nibbles in a Byte
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
BCD, Packed BCD and ASCII conversion.
•BCD
•ASCII Codes – 7 bit code : 128 codes
BCD Codes
Packed BCD
BCD1 BCD0
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
ASCII CODE: 7 bit code = 128 combinations
A = $41
a = $61
“ “ = $20
Enter key = $0D = 13
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
59
0000
0001 0101 1001 – BCD NUMBER
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
PACKING OF BCD NUMBERS – SAVING MEMORY
OR R20,R21 ; R20 = 95
MOV R22,R20
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
UNPACKING OF BCD NUMBERS – FOR DISPLAY
ORI R20,$30
OUT PORTC,R20
ORI R21,$30
OUT PORTC,R21
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Packed BCD to ASCII conversion
To convert packed BCD to ASCII:
• you must first convert it to unpacked BCD.
• Then the unpacked BCD is tagged with 011 0000
(30H).
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Unpacking Packed BCD
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights