Lecture-5 (Microprocessors and Microcontrollers)
Lecture-5 (Microprocessors and Microcontrollers)
Microcontrollers
(EE-231)
Main Objectives
The arithmetic and Logic Instructions and
Programs
Addition, Subtraction, Multiplication, Division
Rotation and Other Logical operations
Checksum
BCD-ASCII conversions
ADD Instruction
ADD Instruction
Example
ADDC Instruction
Example
DA Instruction
DA Instruction
After an ADD or ADDC instruction
If the lower nibble (4 bits) is greater than 9, or if AC=1, add 0110 (6) to
the lower 4 bits
If the upper nibble is greater than 9, or if CY=1, add 0110 (6) to the
upper 4 bits
Example
DA Instruction
Example
Subtraction
In many microprocessor there are two different instructions
for subtraction: SUB and SUBB (subtract with borrow)
In the 8051 we have only SUBB
The 8051 uses adder circuitry to perform the subtraction
SUBB A, source ;A = A source CY
To make SUB out of SUBB, we have to make CY=0 (borrow in
this case) prior to the execution of the instruction
Subtraction
SUBB when CY = 1
This instruction is used for multi-byte numbers and will take care of the
borrow of the lower operand
Example
Multiplication
Division
Division
Example
Overflow
In 8-bit signed number operations, OV is set to 1 if either occurs:
There is a carry from D6 to D7, but no carry out of D7 (CY=0)
There is a carry from D7 out (CY=1), but no carry from D6 to D7
Example
Overflow
Example
Example
Note
Logical Instructions
Logical AND
ANL destination,source ; dest = dest AND source
ANL is often used to mask (set to 0) certain bits of an operand Logical OR
Logical OR
ORL destination,source ; dest = dest OR source
ORL instruction can be used to set certain bits of an operand to 1
Logical XOR
XRL destination,source ; dest = dest XOR source
XRL instruction can be used to toggle certain bits of an operand
It is also used for complementing the bits. E.g. XRL A,A clears A
Note:
The destination is normally the accumulator
The source operand can be a register, indirect-memory, or immediate
Logical Instructions
Example
Compare Instructions
CJNE destination , source , Label
The actions of comparing and jumping are combined into a single
instruction called CJNE (compare and jump if not equal)
The CJNE instruction compares two operands, and jumps if they are not
equal
The destination operand can be in the accumulator or in one of the Rn
registers
The source operand can be in a register, in memory, or immediate
The operands themselves remain unchanged
It changes the CY flag to indicate if the destination operand is larger or
smaller
Compare Instructions
START:
CJNE R5 , #80 , NOT_EQUAL ; check R5 for 80
MOV P1 , R5 ;R5 = 80 [Perform the tasks if found equal]
NOT_EQUAL:
JNC GREATER ;jump if R5 > 80
MOV P2 , R5 ;R5 < 80 [Perform the tasks if found lesser]
GREATER:
MOV P3 , R5; R5 > 80 [Perform the tasks if found greater]
Compare Instructions
Example
Rotate Instruction
Rotate Right
RR A ; rotate right A
In rotate right, Bit D0 exits from the LSB and enters into MSB, D7
E.g., 0001 1011 after rotation becomes 1000 1101
Rotate Left
RL A ; rotate left A
In rotate left, Bit D7 exits from the MSB and enters into LSB, D0
E.g., 1110 0100 becomes 1100 1001
Data Serialization
Data Serialization
Example
SWAP instruction
Devices like printers, LCDs etc. receive data in the form of ASCII.
Systems like computer, servers and embedded systems (like
microcontrollers) have a built-in clock that keeps track of time it is called
(RTC). RTCs are present in almost any electronic device which needs to
keep accurate time.
The RTC provides the time of day (hour, minute, second) and the date
(year , month , day) continuously, regardless of whether the power is on or
off
However this data is provided in packed BCD
To be displayed on an LCD or printed by the printer, it must be in ACSII
format
1.
2.
1.
2.
3.
To ensure the integrity of the ROM contents, every system must perform
the checksum calculation
The process of checksum will detect any corruption of the contents of
ROM
The checksum process uses what is called a checksum byte
The checksum byte is an extra byte that is tagged to the end of series of
bytes of data.
To calculate the checksum byte of a series of bytes of data
Add the bytes together and drop the carries
Take the 2s complement of the total sum, and it becomes the last byte of
the series
To perform the checksum operation,
add all the bytes, including the checksum byte
The result must be zero
If it is not zero, one or more bytes of data have been changed