unit 4- Microcontroller part 2
unit 4- Microcontroller part 2
unit 4- Microcontroller part 2
General-purpose microprocessor:
Microcontroller:
• A smaller computer
• On-chip RAM, ROM, I/O ports...
• Example:Motorola’s 6811, Intel’s 8051, Zilog’s Z8 and PIC 16X
1
Microprocessor vs. Microcontroller:
2
ALU - Arithmetic Logical Unit: This unit is used for the arithmetic calculations.
A-Accumulator: This register is used for arithmetic operations. This is also bit addressable and
8 bit register.
B-Register: This register is used in only two instructions MUL AB and DIV AB. This is also bit
addressable and 8 bit register.
PC-Program Counter:
Points to the address of next instruction to be executed from ROM
It is 16 bit register means the 8051 can access program address from 0000H to FFFFH. A
total of 64KB of code.
Initially PC has 0000H
PC is incremented after each instruction.
ROM in 8051
4KB on chip ROM is available.
Max ROM space is 64 KB because 16 bit address line is available in 8051.
Starting address for ROM is 0000H (because PC which points the ROM is 16 bit wide).
Flag Bits and PSW Register:
→ Used to indicate the Arithmetic condition of Accumulator (ACC).
3
→ Flag register in 8051 is called as program status word (PSW). This special function register
PSW is also bit addressable and 8 bit wide means each bit can be set or reset independently.
Pin Diagram:
Vcc(pin 40):
– Vcc provides supply voltage to the chip.
6
– The voltage source is +5V.
GND(pin 20):ground
XTAL1 and XTAL2(pins 19,18)
Using a quartz crystal oscillator we can observe the frequency on the XTAL2 pin.
/EA(pin 31):external access
– There is no on-chip ROM in 8031 and 8032 .
– The /EA pin is connected to GND to indicate the code is stored externally.
– /PSEN & ALE are used for external ROM.
– For 8051, /EA pin is connected to Vcc.
– “/” means active low.
/PSEN(pin 29):program store enable
– This is an output pin and is connected to the OE pin of the ROM.
ALE(pin 30):address latch enable
– It is an output pin and is active high.
– 8051 port 0 provides both address and data.
– The ALE pin is used for de-multiplexing the address and data by connecting to
the G pin of the 74LS373 latch.
I/O port pins
– The four ports P0, P1, P2, and P3.
– Each port uses 8 pins.
– All I/O pins are bi-directional.
7
A Register (Accumulator)
A register is a general-purpose register used for storing intermediate results obtained during
operation. Prior to executing an instruction upon any number or operand it is necessary to
store it in the accumulator first. All results obtained from arithmetical operations performed
by the ALU are stored in the accumulator. Data to be moved from one register to another
must go through the accumulator. In other words, the A register is the most commonly used
register and it is impossible to imagine a microcontroller without it. More than half
instructions used by the 8051 microcontroller use somehow the accumulator.
B Register
Multiplication and division can be performed only upon numbers stored in the A and B
registers. All other instructions in the program can use this register as a spare accumulator
(A).
During the process of writing a program, each register is called by its name so that their exact
addresses are not of importance for the user. During compilation, their names will be
automatically replaced by appropriate addresses.
R Registers (R0-R7)
This is a common name for 8 general-purpose registers (R0, R1, R2 ...R7). Even though they are
not true SFRs, they deserve to be discussed here because of their purpose. They occupy 4 banks
within RAM. Similar to the accumulator, they are used for temporary storing variables and
intermediate results during operation. Which one of these banks is to be active depends on two
bits of the PSW Register. Active bank is a bank the registers of which are currently used.
Program Status Word (PSW) Register
8
PSW register is one of the most important SFRs. It contains several status bits that reflect the
current state of the CPU. Besides, this register contains Carry bit, Auxiliary Carry, two
register bank select bits, Overflow flag, parity bit and user-definable status flag.
P - Parity bit. If a number stored in the accumulator is even then this bit will be
automatically set (1), otherwise it will be cleared (0). It is mainly used during data transmit
and receive via serial communication.
- Bit 1. This bit is intended to be used in the future versions of microcontrollers.
OV Overflow occurs when the result of an arithmetical operation is larger than 255 and
cannot be stored in one register. Overflow condition causes the OV bit to be set (1).
Otherwise, it will be cleared (0).
RS0, RS1 - Register bank select bits. These two bits are used to select one of four register
banks of RAM. By setting and clearing these bits, registers R0-R7 are stored in one of four
banks of RAM.
9
Stack Pointer (SP) Register
A value stored in the Stack Pointer points to the first free stack address and permits stack
availability. Stack pushes increment the value in the Stack Pointer by 1. Likewise, stack pops
decrement its value by 1. Upon any reset and power-on, the value 7 is stored in the Stack
Pointer, which means that the space of RAM reserved for the stack starts at this location. If
another value is written to this register, the entire Stack is moved to the new memory
location.
There are five interrupt sources for the 8051, which means that they can recognize 5 different
events that can interrupt regular program execution. Each interrupt can be enabled or disabled by
setting bits of the IE register.
10
IP Register (Interrupt Priority)
The IP register bits specify the priority level of each interrupt (high or low priority).
Serial port:
One of the microcontroller features making it so powerful is an integrated UART, better known
as a serial port. It is a full-duplex port, thus being able to transmit and receive data
simultaneously and at different baud rates.
Serial port must be configured prior to being used. In other words, it is necessary to determine
how many bits is contained in one serial “word”, baud rate and synchronization clock source.
The whole process is in control of the bits of the SCON register (Serial Control).
Serial Port Control (SCON) Register
SM0 - Serial port mode bit 0 is used for serial port mode selection.
SM1 - Serial port mode bit 1.
11
SM2 - Serial port mode 2 bit, also known as multiprocessor communication enable bit.
When set, it enables multiprocessor communication in mode 2 and 3, and eventually
mode 1. It should be cleared in mode 0.
REN - Reception Enable bit enables serial reception when set. When cleared, serial
reception is disabled.
TB8 - Transmitter bit 8. Since all registers are 8-bit wide, this bit solves the problem of
transmiting the 9th bit in modes 2 and 3. It is set to transmit a logic 1 in the 9th bit.
RB8 - Receiver bit 8 or the 9th bit received in modes 2 and 3. Cleared by hardware if 9th
bit received is a logic 0. Set by hardware if 9th bit received is a logic 1.
TI - Transmit Interrupt flag is automatically set at the moment the last bit of one byte is
sent. It's a signal to the processor that the line is available for a new byte transmite. It
must be cleared from within the software.
RI - Receive Interrupt flag is automatically set upon one byte receive. It signals that byte
is received and should be read quickly prior to being replaced by a new data. This bit is
also cleared from within the software.
As seen, serial port mode is selected by combining the SM0 and SM2 bits:
TMOD is dedicated solely to the two timers and can be considered to be two duplicate 4-bit registers and
each of which controls the action of one of the timers.
If neither external memory nor serial communication system are used then 4 ports with in
total of 32 input/output pins are available for connection to peripheral environment. Each bit
within these ports affects the state and performance of appropriate pin of the microcontroller.
Thus, bit logic state is reflected on appropriate pin as a voltage (0 or 5 V) and vice versa,
voltage on a pin reflects the state of appropriate port bit.
As mentioned, port bit state affects performance of port pins, i.e. whether they will be
configured as inputs or outputs. If a bit is cleared (0), the appropriate pin will be configured
as an output, while if it is set (1), the appropriate pin will be configured as an input. Upon
reset and power-on, all port bits are set (1), which means that all appropriate pins will be
configured as inputs.
I/O ports are directly connected to the microcontroller pins. Accordingly, logic state of these
registers can be checked by voltmeter and vice versa, voltage on the pins can be checked by
inspecting their bits!
13
I/O Pins Ports and Circuits
Port 0
Port 0 pins may serve as inputs, outputs, or, when used together, as a bi-directional low order address and
data bus for external memory. For example, when a pin is to be used as an input, a I must be written to the
corresponding port 0 latch by the program, thus turning both of the output transistors off, which in turn
causes the pin to "float" in a high impedance state, and the pin is essentially connected to the input buffer
When used as an output, the pin latches that are programmed to a 0 will turn on the lower FET, grounding
the pin. All latches that are programmed to a I still float; thus, external pull up resistors will be needed to
supply a logic high when using port 0 as an output.
When port 0 is used as an address bus to external memory, internal control signals switch the address
lines to the gates of the Field Effect Transistories (FETs). A logic I on an address bit will turn the upper
FET on and the lower FET off to provide a logic high at the pin. When the address bit is a zero, the lower
FET is on and the upper FET off to provide a logic low at the pin. After the address has been formed and
latched into external circuits by the Address Latch Enable (ALE) pulse, the bus is turned around to
become a data bus. Port 0 now reads data from the external memory and must be configured as an input,
so a logic I is automatically written by internal control logic to all port 0 latches.
14
Port 1
Port 1 pins have no dual functions. Therefore, the output latch is connected directly to the gate of the
lower FET. which has an FET circuit labeled Internal FET Pullup as an active pullup load.
Used as an input. a I is written to the latch. turning the lower FET off; the pin and the input to the pin
buffer are pulled high by the FET load. An external circuit can overcome the high impedance pullup and
drive the pin low to input a 0 or leave the input high for a 1 .
If used as an output. the latches containing a 1 can drive the input of an external circuit high through the
pullup. If a 0 is written to the latch. the lower FET is on. the pullup is off. and the pin can drive the input
of the external circuit low.
To aid in speeding up switching times when the pin is used as an output. the internal FET pull up has
another FET in parallel with it. The second FET is turned on for two oscillator time periods during a low-
to-high transition on the pin. as shown in Figure 7. This arrangement provides a low impedance path to
the positive voltage supply to help reduce rise times in charging any parasitic capacitances in the external
circuitry.
Port 2
Port 2 may be used as an input/output port similar in operation to port 1. The alternate use of port 2 is to
supply a high-order address byte in conjunction with the port 0 low-order byte to address external
memory.
Port 2 pins are momentarily changed by the address control signals when supplying the high byte of a 16-
bit address. Port 2 latches remain stable when external memory is addressed, as they do not have to be
turned around (set to 1) for data input as is the case for port 0 .
Port 3
Port 3 is an input/output port similar to port 1 . The input and output functions can be programmed under
the control of the P3 latches or under the control of various other special function registers. The port 3
alternate uses are shown in the following table:
Unlike ports 0 and 2. which can have external addressing functions and change all eight port bits when in
alternate use. each pin of port 3 may be individually programmed to be used either as 110 or as one of the
alternate functions.
15
ADDRESSING MODES OF 8051
MOVX A,@DPTR
MOVX @DPTR,A
Both commands utilize DPTR. In these instructions, DPTR must first be loaded with the
address of external memory. Once DPTR holds the correct external memory address, the first
command will move the contents of that external memory address into the Accumulator. The
second command will do the opposite: it will allow to write the value of the Accumulator to
the external memory address pointed to by DPTR.
17
INSTRUCTION SET OF 8051 MICROCONTROLLER
DATA TRANSFER INSTRUCTIONS:
Data transfer instructions move the content of one register to another. The register the content of
which is moved remains unchanged. If they have the suffix “X” (MOVX), the data is exchanged
with external memory.
Data Transfer Instructions
Mnemonic Description Byte Cycle
MOV A,Rn Moves the register to the accumulator 1 1
MOV A,direct Moves the direct byte to the accumulator 2 2
MOV A,@Ri Moves the indirect RAM to the accumulator 1 2
MOV A,#data Moves the immediate data to the accumulator 2 2
MOV Rn,A Moves the accumulator to the register 1 2
MOV Rn,direct Moves the direct byte to the register 2 4
MOV Rn,#data Moves the immediate data to the register 2 2
MOV direct,A Moves the accumulator to the direct byte 2 3
MOV direct,Rn Moves the register to the direct byte 2 3
MOV direct,direct Moves the direct byte to the direct byte 3 4
MOV direct,@Ri Moves the indirect RAM to the direct byte 2 4
MOV direct,#data Moves the immediate data to the direct byte 3 3
MOV @Ri,A Moves the accumulator to the indirect RAM 1 3
MOV @Ri,direct Moves the direct byte to the indirect RAM 2 5
MOV @Ri,#data Moves the immediate data to the indirect RAM 2 3
MOV DPTR,#data Moves a 16-bit data to the data pointer 3 3
MOVC Moves the code byte relative to the DPTR to the accumulator
1 3
A,@A+DPTR (address=A+DPTR)
Moves the code byte relative to the PC to the accumulator
MOVC A,@A+PC 1 3
(address=A+PC)
MOVX A,@Ri Moves the external RAM (8-bit address) to the accumulator 1 3-10
MOVX A,@DPTR Moves the external RAM (16-bit address) to the accumulator 1 3-10
MOVX @Ri,A Moves the accumulator to the external RAM (8-bit address) 1 4-11
MOVX @DPTR,A Moves the accumulator to the external RAM (16-bit address) 1 4-11
PUSH direct Pushes the direct byte onto the stack 2 4
POP direct Pops the direct byte from the stack 2 3
XCH A,Rn Exchanges the register with the accumulator 1 2
XCH A,direct Exchanges the direct byte with the accumulator 2 3
XCH A,@Ri Exchanges the indirect RAM with the accumulator 1 3
XCHD A,@Ri Exchanges the low-order nibble indirect RAM with the accumulator 1 3
18
ARITHMETIC INSTRUCTIONS
Arithmetic instructions perform several basic operations such as addition, subtraction,
division, multiplication etc. After execution, the result is stored in the first operand. For
example:
ADD A,R1 - The result of addition (A+R1) will be stored in the accumulator.
Mnemonic Description
ADD A,Rn Adds the register to the accumulator
ADD A,direct Adds the direct byte to the accumulator
ADD A,@Ri Adds the indirect RAM to the accumulator
ADD A,#data Adds the immediate data to the accumulator
ADDC A,Rn Adds the register to the accumulator with a carry flag
ADDC A,direct Adds the direct byte to the accumulator with a carry flag
ADDC A,@Ri Adds the indirect RAM to the accumulator with a carry flag
ADDC A,#data Adds the immediate data to the accumulator with a carry flag
SUBB A,Rn Subtracts the register from the accumulator with a borrow
SUBB A,direct Subtracts the direct byte from the accumulator with a borrow
SUBB A,@Ri Subtracts the indirect RAM from the accumulator with a borrow
SUBB A,#data Subtracts the immediate data from the accumulator with a borrow
INC A Increments the accumulator by 1
INC Rn Increments the register by 1
INC direct Increments the direct byte by 1
INC @Ri Increments the indirect RAM by 1
DEC A Decrements the accumulator by 1
DEC Rn Decrements the register by 1
DEC direct Decrements the direct byte by 1
DEC @Ri Decrements the indirect RAM by 1
INC DPTR Increments the Data Pointer by 1
MUL AB Multiplies A and B
DIV AB Divides A by B
DA A Decimal adjustment of the accumulator according to BCD code
LOGIC INSTRUCTIONS:
Logic instructions perform logic operations upon corresponding bits of two registers. After
execution, the result is stored in the first operand.
Mnemonic Description
ANL A,Rn AND register to accumulator
ANL A,direct AND direct byte to accumulator
ANL A,@Ri AND indirect RAM to accumulator
ANL A,#data AND immediate data to accumulator
19
ANL direct,A AND accumulator to direct byte
ANL direct,#data AND immediae data to direct register
ORL A,Rn OR register to accumulator
ORL A,direct OR direct byte to accumulator
ORL A,@Ri OR indirect RAM to accumulator
ORL direct,A OR accumulator to direct byte
ORL direct,#data OR immediate data to direct byte
XRL A,Rn Exclusive OR register to accumulator
XRL A,direct Exclusive OR direct byte to accumulator
XRL A,@Ri Exclusive OR indirect RAM to accumulator
XRL A,#data Exclusive OR immediate data to accumulator
XRL direct,A Exclusive OR accumulator to direct byte
XRL direct,#data Exclusive OR immediate data to direct byte
CLR A Clears the accumulator
CPL A Complements the accumulator (1=0, 0=1)
SWAP A Swaps nibbles within the accumulator
RL A Rotates bits in the accumulator left
RLC A Rotates bits in the accumulator left through carry
RR A Rotates bits in the accumulator right
RRC A Rotates bits in the accumulator right through carry
20
RL A - Rotates the accumulator one bit left
Description: Eight bits in the accumulator are rotated one bit left, so that the bit 7 is rotated
into the bit 0 position.
Syntax: RL A;
Byte: 1 (instruction code);
STATUS register flags: No flags affected;
EXAMPLE:
Before execution: A= C2h (11000010 Bin.)
After execution: A=85h (10000101 Bin.)
RLC A - Rotates the accumulator one bit left through the carry flag
Description: All eight bits in the accumulator and carry flag are rotated one bit left. After
this operation, the bit 7 is rotated into the carry flag position and the carry flag is rotated into
the bit 0 position.
Syntax: RLC A;
Byte: 1 (instruction code);
STATUS register flags: C;
EXAMPLE:
Before execution: A= C2h (11000010 Bin.) C=0
After execution: A= 85h (10000100 Bin.) C=1
RRC A - Rotates the accumulator one bit right through the carry flag
Description: All eight bits in the accumulator and carry flag are rotated one bit right. After
this operation, the carry flag is rotated into the bit 7 position and the bit 0 is rotated into the
carry flag position.
21
Syntax: RRC A;
Byte: 1 (instruction code);
STATUS register flags: C;
EXAMPLE:
Before execution: A= C2h (11000010 Bin.) C=0
After execution: A= 61h (01100001 Bin.) C=0
22
BRANCH INSTRUCTIONS OR CONTROL TRANSFER INSTRUCTIONS:
LJMP:
LJMP (long jump) causes the program to branch to a destination address defined by the 16-bit
operand in the jump instruction. Because a 16-bit address is used the instruction can cause a
jump to any location within the 64KByte program space (216 = 64K). Some example
instructions are:
SJMP:
SJMP (short jump) uses a singe byte address. This address is a signed 8-bit number and allows
the program to branch to a distance –128 bytes back from the current PC address or +127 bytes
forward from the current PC address. The address mode used with this form of jumping (or
branching) is referred to as relative addressing, introduced earlier, as the jump is calculated
relative to the current PC address.
AJMP:
This is a special 8051 jump instruction, which allows a jump with a 2KByte address boundary (a
2K page)
There is also a generic JMP instruction supported by many 8051 assemblers. The assembler will
decide which type of jump instruction to use, LJMP, SJMP or AJMP, so as to choose the most
efficient instruction.
23
LCALL:
This instruction is used to call a subroutine at a specified address. The address is 16 bits long so
the call can be made to any location within the 64KByte memory space. When a LCALL
instruction is executed the current PC content is automatically pushed onto the stack of the PC.
When the program returns from the subroutine the PC contents is returned from the stack so that
the program can resume operation from the point where the LCALL was made The return from
subroutine is achieved using the RET instruction, which simply pops the PC back from the stack.
ACALL:
The ACALL instruction is logically similar to the LCALL but has a limited address range similar
to the AJMP instruction. CALL is a generic call instruction supported by many 8051 assemblers.
The assembler will decide which type of call instruction, LCALL or ACALL, to use so as to
choose the most efficient instruction.
Mnemonic Description
ACALL addr11 Absolute subroutine call
LCALL addr16 Long subroutine call
RET Returns from subroutine
RETI Returns from interrupt subroutine
AJMP addr11 Absolute jump
LJMP addr16 Long jump
Short jump (from –128 to +127 locations relative to the following
SJMP rel
instruction)
JC rel Jump if carry flag is set. Short jump.
JNC rel Jump if carry flag is not set. Short jump.
JB bit,rel Jump if direct bit is set. Short jump.
JBC bit,rel Jump if direct bit is set and clears bit. Short jump.
JMP @A+DPTR Jump indirect relative to the DPTR
JZ rel Jump if the accumulator is zero. Short jump.
JNZ rel Jump if the accumulator is not zero. Short jump.
Compares direct byte to the accumulator and jumps if not equal. Short
CJNE A,direct,rel
jump.
Compares immediate data to the accumulator and jumps if not equal. Short
CJNE A,#data,rel
jump.
CJNE Rn,#data,rel Compares immediate data to the register and jumps if not equal. Short jump.
CJNE Compares immediate data to indirect register and jumps if not equal. Short
@Ri,#data,rel jump.
DJNZ Rn,rel Decrements register and jumps if not 0. Short jump.
DJNZ Rx,rel Decrements direct byte and jump if not 0. Short jump.
NOP No operation
24
Assembly language programming
Here some simple programs of 8051 are given to understand the operation of different instructions
and to understand the logic behind particular program. First the statement of the program that
describes what should be done is given. Then the solution is given which describes the logic how
it will be done and last the code is given with necessary comments.
Statement 3: - treat r6-r7 and r4-r5 as two 16 bit registers. Perform subtraction between them.
Store the result in 20h (lower byte) and 21h (higher byte).
Solution: - first we shall clear the carry. Then subtract the lower bytes afterward then subtract
higher bytes.
CLR C ; clear carry
MOV A, R4 ; get first lower byte
SUBB A, R6 ; subtract it with other
MOV 20H, A ; store the result
MOV A, R5 ; get the first higher byte
25
SUBB A, R7 ; subtract from other
MOV 21H, A ; store the higher byte
Statement 4: - divide the content of r0 by r1. Store the result in r2 (answer) and r3 (reminder).
Then restore the original content of r0.
Solution:-after getting answer to restore original content we have to multiply answer with divider
and then add reminder in that.
MOV A, R0 ; get the content of R0 and R1
MOV B, R1 ; in register A and B
DIV AB ; divide A by B
MOV R2, A ; store result in R2
MOV R3, B ; and reminder in R3
MOV B, R1 ; again get content of R1 in B
MUL AB ; multiply it by answer
ADD A, R3 ; add reminder in new answer
MOV R0, A ; finally restore the content of R0
Statement 5: - transfer the block of data from 20h to 30h to external location 1020h to 1030h.
Solution: - here we have to transfer 10 data bytes from internal to external RAM. So first, we
need one counter. Then we need two pointers one for source second for destination.
MOV R7, #0AH ; initialize counter by 10d
MOV R0, #20H ; get initial source location
MOV DPTR, #1020H ; get initial destination location
NXT: MOV A, @R0 ; get first content in ACC
MOVX @DPTR, A ; move it to external location
INC R0 ; increment source location
INC DPTR ; increase destination location
DJNZ R7, NXT ; decrease R7. if zero then over otherwise move next
Statement 6: - find out how many equal bytes between two memory blocks 10h to 20h and 20h to
30h.
Solution: - here we shall compare each byte one by one from both blocks. Increase the count
every time when equal bytes are found
MOV R7, #0AH ; initialize counter by 10d
MOV R0, #10H ; get initial location of block1
MOV R1, #20H ; get initial location of block2
MOV R6, #00H ; equal byte counter. Starts from zero
NXT: MOV A, @R0 ; get content of block 1 in acc
26
MOV B, A ; move it to B
MOV A, @R1 ; get content of block 2 in acc
CJNE A, B, NOMATCH ; compare both if equal
INC R6 ; increment the counter
NOMATCH: INC R0 ; otherwise go for second number
INC R1
DJNZ R7, NXT ; decrease r7. if zero then over otherwise move next
Statement 7: - given block of 100h to 200h. Find out how many bytes from this block are greater
then the number in r2 and less then number in r3. Store the count in r4.
Solution: - in this program, we shall take each byte one by one from given block. Now here two
limits are given higher limit in r3 and lower limit in r2. So we check first higher limit and then
lower limit if the byte is in between these limits then count will be incremented.
MOV DPTR, #0100H ; get initial location
MOV R7, #0FFH ; counter
MOV R4, #00H ; number counter
MOV 20H, R2 ; get the upper and lower limits in
MOV 21H, R3 ; 20h and 21h
NXT: MOVX A, @DPTR ; get the content in acc
CJNE A, 21H, LOWER ; check the upper limit first
SJMP OUT ; if number is larger
LOWER: JNC OUT ; jump out
CJNE A, 20H, LIMIT ; check lower limit
SJMP OUT ; if number is lower
LIMIT: JC OUT ; jump out
INC R4 ; if number within limit increment count
OUT: INC DPTR ; get next location
DJNZ R7, NXT ; repeat until block completes
27
8 Bit Subtraction (Immediate Addressing)
8 Bit Multiplication
8 Bit Division
28
To write an ALP to perform logical and bit manipulation operations using 8051
microcontroller.
PROGRAM TABLE
29