0 - MPunit-6 & 7
0 - MPunit-6 & 7
0 - MPunit-6 & 7
8051
Contents:
Introduction
Block Diagram and Pin Description of the 8051
Registers
Memory mapping in 8051
Stack in the 8051
I/O Port Programming
Timer
Interrupt
Why do we need to learn
Microprocessors/controllers?
▪ The microprocessor is the core of computer systems.
Microprocessor Microcontroller
▪ CPU is stand-alone. RAM, • CPU, RAM, ROM, I/O and
ROM, I/O, timer are separate timer are all on a single chip
▪ designer can decide on the • fix amount of on-chip ROM,
amount of ROM, RAM and RAM, I/O ports
I/O ports. • for applications in which cost,
▪ expansive power and space are critical
▪ versatility • single-purpose
▪ general-purpose
Embedded System
▪ Embedded system means the processor is embedded into that
application.
▪ Example:
Printer, keyboard, video game player, LCD, Micro Wave Oven etc.
Three criteria in Choosing a
Microcontroller
1. Meeting the computing needs of the task “efficiently and cost
effectively”
• speed, the amount of ROM and RAM, the number of I/O ports
and timers, size, packaging, power consumption.
• easy to upgrade.
• cost per unit.
CPU
Bus Serial
4 I/O Ports
OSC Control Port
P0 P1 P2 P3 TxD RxD
Address/Data
8051 Hardware Features
▪ 8 bit CPU with registers A (accumulator) and B.
▪ Vcc(pin 40):
– Vcc provides supply voltage to the chip.
– The voltage source is +5V.
▪ GND(pin 20):ground.
C2
XTAL2
30pF
C1
XTAL1
30pF
GND
Pins of 8051
▪ RST(pin 9):reset
– It is an input pin and is active high(normally low).
▪ The high pulse must be high at least 2 machine cycles.
– It is a power-on reset.
▪ Upon applying a high pulse to RST, the
microcontroller will reset and all values in registers
will be lost.
▪ Reset values of some 8051 registers.
Power-On RESET Circuit
Vcc
10 uF 31
EA/VPP
30 pF X1
19
11.0592 MHz
8.2 K
X2
18
30 pF
9 RST
Pins of 8051
TB1
Read pin
Writing “1” to Output Pin P1.X
TB1
Read pin
Writing “0” to Output Pin P1.X
TB1
Read pin
Reading “High” at Input Pin
1 1 P1.X pin
Internal CPU bus D Q
P1.X
0 M1
Write to latch Clk Q
TB1
Read pin
3. Read pin=1 Read latch=0
Write to latch=1
Reading “Low” at Input Pin
TB1
Read pin
3. Read pin=1 Read latch=0
Write to latch=1
Other Pins
▪ P1, P2, and P3 have internal pull-up resisters.
– P1, P2, and P3 are not open drain.
Read latch
TB2
TB1
Read pin
Port 0 with Pull-Up Resistors
Vcc
10 K
P0.0
Port
DS5000 P0.1
P0.2
8751 P0.3
8951 P0.4 0
P0.5
P0.6
P0.7
Port 3 Alternate Functions
P3 Bit Function Pin
P3.0 RxD 10
P3.1 TxD 11
P3.2 INT0 12
P3.3 INT1 13
P3.4 T0 14
P3.5 T1 15
P3.6 WR 16
P3.7 RD 17
RESET Value of Some 8051 Registers:
▪ Accumulator (Acc or A)
– Primary register for byte operations such as math and logical
operations.
▪ B Register (B)
– Used along with the Accumulator for multiplication/division.
– These are 16-bit results, A is the low byte, B the high byte.
Program Status Word (PSW)
Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
CY AC F0 RS1 RS0 OV -- P
– Addresses and data are ‘pushed’ onto the stack and ‘popped’
off, last-in, first-out (LIFO).
4k 8k 32k
0000H 0000H 0000H
0FFFH
DS5000-32
1FFFH
8751
AT89C51 8752
7FFFH
AT89C52
from Atmel
Corporation from Dallas
Semiconductor
▪ RAM memory space allocation in the 8051
7FH
30H
2FH
Bit-Addressable RAM
20H
1FH
18H
Register Bank 3
17H
10H Register Bank 2
0FH
08H (Stack) Register Bank 1
07H
00H Register Bank 0
Stack in the 8051
▪ The register used to access the stack is called SP (stack pointer)
register.
▪ The stack pointer in the 8051 is only 8 bits wide, which means
that it can take value 00 to FFH. When 8051 powered up, the SP
register contains value 07.
8051 Addressing Modes
▪ Immediate: Operand is constant contained in instruction
Ex: ADD A,#31H (or) MOV R4,#40H
(or) MOV P1,#55H
We can use EQU directive to access immediate data
as shown below
NDAYS EQU 31
...
ADD A,#NDAYS
Examples:
MOV A, @R0
The above instruction moves the contents of RAM location whose
address is held by R0 into A
▪ Indexed addressing mode:
This addressing mode is widely used in accessing data elements of
look-up table entries located in the program ROM space of the 8051.
The 16-bit register DPTR and the register A are used to form the
address of the data element stored in the on-chip ROM.
rotate right RR A
▪ CJNE A, add, radd compare the contents of the A register with the
contents of the direct address; if they are not equal,
then jump to the relative address; set the carry flag
to 1 if a is less than the contents of the direct
address; other wise, set the carry flag to 0.
▪ CJNE A, #n, radd compare the contents of the A register with the
immediate number n ; if they are not equal,
then jump to the relative address; set the carry flag
to 1 if A is less than the number; other wise, set the
carry flag to 0.
▪ CJNE Rn, #n, radd compare the contents of register Rn with the
immediate number n; if they are not equal, then
jump to the relative address; set the carry flag to 1 if
Rn is less than the number; other wise, set the carry
flag to 0.
CJNE @Rp,#n, radd compare the contents of the address contained
in register Rp to the number n; if they are not
equal, then jump to the relative address. If the
contents of the address in Rp are less than the
number, set carry flag to 1 other wise, set it to 0.
In all the above instructions no flags are affected and they are
executed as they are.
CALLS AND SUBROUTINES
• Calls use Short or Long range Addressing
• ACALL sadd Call the subroutine located on the same page as the
address of the op-code immediately following the
ACALL instruction.
Push the address of the instruction immediately
after the call on the stack
▪ M1 : Mode bit 1.
▪ M0 : Mode bit 0.
TIMER / COUNTER PROGRAMMING
Timer programming
Mode1 Programming
Characteristics and operations of mode 1:
• It is a 16- bit timer; therefore , it allows values of 0000 to FFFFH to be loaded
into the timer’s registers TL and TH.
• After TH and TL are loaded with a 16-bit initial value, the timer must be
started . This is done by “SETB TR0”for timer 0 and “SETB TR1” for timer 1.
• After the timer is started, it starts to count up. It counts up until it reaches its
limit of FFFFH .When it rolls over from FFFFH to 0000, it sets high a flag bit
called TF (timer flag). This timer flag can be monitored. When this timer flag
is raised, one option would be to stop the timer with the instructions “CLR
TR0” for timer 0.
• After the timer reaches its limit and rolls over, in order to repeat the process the
registers TH and TL must be reloaded with the original value, and TF must be
reset to 0.
Steps to program in mode 1
• Load the TMOD value register indicating which timer (timer 0 or timer 1) is
to be used and which timer mode (0 or 1) is selected.
• Keep monitoring the timer flag (TF) with the “JNB TFx, target” instruction
to see if it is raised .Get out of the loop when TF becomes high.
Solution:
• After TH is loaded with the 8 bit- value , the 8051 gives a copy
of it to TL. Then the timer must be started. This is done by the
instructions “SETB TR0” for timer 0 and “SETB TR1” for
timer 1.This is just like mode1.
• After the timer is started, its starts to count by incrementing the
TL register. It counts up until it reaches its limit of FF H. When it
rolls over from FF h to 00, it sets high the TF (timer Flag). If we
are using timer 0 ,TF goes high; if we are timer 1 , TF is raised.
• When the TL register rolls from FFH to 0 and TF is set to 1,
TL is reloaded automatically with the original value kept by
the TH register .To repeat the process, we must simply clear
TF and let it go without any need bye the programmer to
reload the original value. This makes mode2 an auto-reload, in
contrast with mode1 in which the programmer has to reload
TH and TL.
Steps to program in mode2
• Load the TMOD value register indicating which timer (timer 0
or timer 1) is to be used , and the timer mode ( mode 2) is
selected.
• Load the TH registers with the initial count value.
• Start the timer.
• Keep monitoring the TF with the “JNB TFx, target”
instruction to see whether it is raised. Get out of the loop when
TF goes high.
• Clear the TF flag.
• Go back to step 4, since mode 2 is auto- reload.
Example:
Assume that XTAL= 11.0592MHz , find (a) Generate a square
wave with a time delay of 5ms on pin P1.0
Example:
Assuming that clock pulse are fed into pin T1, write a program for counter 1
in mode 2 to count the pulse and display the state of the TL1 count on P2.
Solution:
MOV TMOD,#0110000B ; counter 1,mode 2,C/T=1 external pulses
MOV TH1,#0 ; clear TH1
SETB P3.5 ; make T1 input
AGAIN: SETB TR1 ; start the counter
BACK: MOV A,TL1 ; get copy of count TL1
MOV P2,A ; display it on port 2
JNB TF1,BACK ; keep doing it if TF=0
CLR TR1 ; stop the counter 1
CLR TF1 ; make TF=0
SJMP AGAIN ; keep doing it.
SERIAL COMMUNICATION PROGRAMMING:
2. The TH1 is loaded with one of the values to set the baud rate for
serial data transfer (assuming XTAL=11.0592MHz).
3. The SCON register is loaded with the value 50H, indicating serial
mode 1, where an 8-bit data is framed with start and stop bits.
It is an 8 bit register.
• Program the mode (start bit, stop bit, data bits length).
• The stop bit is transferred. It is during the transfer of the stop bit
that the 8051 raises the TI flag (TI=1),indicating that the last
character was transmitted and it is ready to transfer the next
character.
• By monitoring the TI flag, we make sure that we are not
overloading the SBUF register. If we write another byte into
the SBUF register before TI is raised, the un-transmitted
portion of the previous byte will be lost. In other words when
the 8051 finishes transferring a byte, it raises the TI flag to
indicate it is ready for the next character.
• After SBUF is loaded with a new byte, the TI flag bit must be
forced to 0 by the ”CLR TI” instruction in order for this new
byte to be transferred.
Programming the 8051 to receive data serially:
1. The TMOD register is loaded with the value 20H, indicating the
use of timer 1 in mode 2(8-bit auto-reload) to set the baud rate.
2. TH1 is loaded with one of the values to set the baud rate
(assuming XTAL=11.0592MHz).
3. The SCON register is loaded with the value 50H, indicating the
serial mode 1, where 8-bit data is framed with start and stop bits.
7. When RI is raised, SUBF has the byte. Its contents are moved
into a safe place.
• It receives the start bit indicating that the next bit is the first
bit of the character byte it is about to receive.
• After the SBUF contents are copied into a safe place, the RI
flag bit must be forced to 0 by the “CLR RI” instruction in
order to allow the next received character byte to be placed in
SBUF . Failure to do this causes loss of the received character.
Interrupts :
Interrupt Programming
Interrupt Vs polling
Steps in executing an interrupt
1. It finishes the instruction it is executing and saves the address of
the next instruction (PC) on the stack .
Reset 0000 9
External hardware interrupt 0003 P3.2
0(INT 0)
Timer 0 interrupt (TF0) 000B
TF Interrupt
Example:-
Assume that pin 3.3(INT1) is connected to a pulse generator, write a program
in which the falling edge of the pulse will send a high to P1.3, which is
connected to an LED(or bezzer).In other words, the LED is turned on and off at
the same rate as the pulses are applied to the INT1 pin. This is an edge –
triggered version.
Solution:
ORG 0000H
LJMP MAIN
-------- ISR for hardware interrupt INT1 to turn on the LED
ORG 0013H ;INTI ISR
SETB P1.3 ;turn on the LED
MOV R3,#255
BACK: DJNZ R3,HERE ;keep the buzzer on for a while
CLR P1.3 ;turn off the buzzer
RETI
------ main program for initialization
ORG 30H
MAIN: SETB TCON.2 ;make INTI edge-trigger interrupt
MOV IE,#10000100B ;enable external INT1
HERE: SJMP HERE ;stay here until get interrupted.
END
Minimum pulse duration to detect edge-triggered interrupts
Programming the serial communication
interrupt:
Serial interrupt is invoked by TI or RI flags
Example:
Write a program in which the 8051 reads data from P1 and
writes it to P2 continuously while giving a copy of it to the serial
COM port to be transferred serially. Assume that
XTAL=11.0592.set the baud rate at 9600.
Solution:
ORG 0
LJMP MAIN
ORG 32H
LJMP SERIAL ;jump to serial interrupt ISR
ORG 30H
MAIN: MOV P1,#0FFH ;make P1 an input port
MOV TMOD,#20H ;timer 1 mode 2 (auto –reload)
MOV TH1,#0FDH ;9600 baud rate
MOV SCON,#50H ;8-bit, 1 stop, ren enabled
MOV IE,#10010000B ;enable serial interrupt
SETB TR1 ;start timer 1
BACK: MOV A, P1 ;read data from port 1
MOV SBUF,A ;give a copy to SBUF
MOV P2,A ;send it to P2
SJMP BACK ;stay in loop indefinitely
------serial port ISR
ORG 100H;
SERIAL: JB TI,TRANS ;jump if TI is high
MOV A,SBUF ;otherwise due to receive
CLR RI ;clear RI since CPU does not
RETI ;return from ISR
TRANS: CLR TI ;clear TI since CPU does not
RETI ;return from ISR
END
Interrupt priority in the 8051 upon reset