Module 4 Final
Module 4 Final
MODULE - 4 2
❑ All 8051 microcontrollers have 4 I/O ports each comprising 8
bits which can be configured as inputs or outputs.
MODULE - 4 3
PORT - 0
❑ Port 0 occupies a total of 8 pins (pins 32-39). It can be used for input
or output.
❑ To use the pins of port 0 as both input and output ports, each pin
must be connected externally to a 10K ohm pull-up resistor. This is
due to the fact that P0 is an open drain, unlike P1, P2, and P3.
MODULE - 4 5
PORT - 1
❑ Port 1 occupies a total of 8 pins (pins 1 through 8). ‰
Port 1 can be used
as input or output
❑ In contrast to port 0, this port does not need any pull-up resistors
since it already has pull-up resistors internally
❑ P3 does not need any pull-up resistors, the same as P1 and P2 did
not.
MODULE - 4 9
Pin's Current limitations
❑ When configured as outputs (logic zero (0)), single port pins can
receive a current of 10mA.
❑ If all 8 bits of a port are active, a total current must be limited to
15mA (port P0: 26mA).
❑ If all ports (32 bits) are active, total maximum current must be
limited to 71mA.
❑ When these pins are configured as inputs (logic 1), built-in pull-up
resistors provide very weak current, but strong enough to activate up
to 4 TTL inputs of LS series.
MODULE - 4 10
LOGICAL STRUCTURE OF 8051 I/O PORTS
MODULE - 4 12
Writing “1″ to the port (output)
MODULE - 4 13
Reading “1″ to the port (input)
MODULE - 4 14
Reading “0″ to the port (input)
MODULE - 4 15
Reading a port (port-pins) versus reading a latch
MODULE - 4 15
Reading a port (port-pins) versus reading a latch
"read-modify-write" instructions
MODULE - 4 17
Reading a port (port-pins) versus reading a latch
❑ Reading a Pin: Examples of a few instructions that read port pin are
MOV A, P0 ; Move port-0 pin values to A
MOV A, P1; Move port-1 pin values to A
BIT ADDRESS 87H 86H 85H 84H 83H 82H 81H 80H
PORT 1 BITS P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0
BIT ADDRESS 97H 96H 95H 94H 93H 92H 91H 90H
PORT 2 BITS P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0
BIT ADDRESS A7H A6H A5H A4H A3H A2H A1H A0H
PORT 3 BITS P3.7 P3.6 P3.5 P3.4 P3.3 P3.2 P3.1 P3.0
BIT ADDRESS B7H B6H B5H B4H B3H B2H B1H B0H
MODULE - 4 19
20
MODULE - 4 21
The following code will continuously send out to port 1 the alternating
value 55H and AAH.
MODULE - 4 22
MODULE - 4 23
MODULE - 4 24
MODULE - 4 25
MODULE - 4 26
MODULE - 4 27
MODULE - 4 28
TIMERS
❑ The 8051 has two timers/counters, they can be used either as
➢ Timers to generate a time delay
➢ To generate a waveform with specific frequency
➢ To generate baud rate signal for serial communication
➢ Event counters to count events happening outside the microcontroller
MODULE - 4 32
TIMER registers
❑ Accessed as low byte and high byte
➢ The low byte register is called TL0/TL1 and
➢ The high byte register is called TH0/TH1
MODULE - 4 33
❑ Steps to calculate values to be loaded into the TL and TH registers
for finding the TH, TL registers’ values
1. Divide the desired time delay by 1.085us (if operating frequency is 11.0592 MHz)
3. Convert the result of Step2 to hex value, where yyxx is the initial hex value to
MODULE - 4 34
❑ Example: 500us time delay
Step2: P=65536-461=65075
MODULE - 4 35
TMOD REGISTER
❑ Both timers 0 and 1 use the same register, called TMOD (timer
mode), to set the various timer operation modes
MODULE - 4 36
TMOD REGISTER
Prescaler:
Divides down the clock
signals used for the
timer, giving reduced
overflow rates
MODULE - 4 37
TMOD REGISTER
MODULE - 4 40
❑ To generate a time delay
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
2. Load registers TL and TH with initial count value
3. Start the timer
4. Keep monitoring the timer flag (TF) with the JNB TFx, target instruction to see
if it is raised and Get out of the loop when TF becomes high
5. Stop the timer
6. Clear the TF flag for the next round
7. Go back to Step 2 to load TH and TL again
MODULE - 4 41
Timer 0 Mode 0 (13-bit timer)
❑ This is one of the rarities being kept only for the purpose of compatibility with
the previous versions of microcontrollers.
❑ This mode configures timer 0 as a 13-bit timer which consists of all 8-bits of TH0
and the lower 5-bits of TL0.
❑ How does it operate? Each coming pulse causes the lower register bits to change
their states. After receiving 32 pulses, this register is loaded and automatically
cleared, while the higher byte (TH0) is incremented by 1.
❑ This process is repeated until registers count up 8192 pulses. After that, both
registers are cleared and counting starts from 0
MODULE - 4 42
Timer 0 Mode 0 (13-bit timer)
MODULE - 4 43
Timer 0 Mode 1 (16-bit timer)
MODULE - 4 45
Steps to program Timer 0 Mode 1 (16-bit timer)
MODULE - 4 48
Timer 0 Mode 1 (16-bit timer)
MOV TMOD,#10H ;timer 1, mode 1
AGAIN: MOV TL1,#00 ;Timer value = DC00H
MOV TH1,#0DCH
SETB TR1 ;start
BACK: JNB TF1,BACK
CLR TR1 ;stop
CLR TF1 ;clear timer flag 1
CPL P2.3
SJMP AGAIN ;reload timer since mode 1 is not
;auto-reload
MODULE - 4 49
Timer 0 Mode-2 (Auto-reload Timer)
❑ When a timer is in mode 2, THx holds the "reload value" and TLx is the timer
itself.
❑ Thus, TLx starts counting up. When TLx reaches 255 and is subsequently
incremented, instead of resetting to 0 (as in the case of modes 0 and 1), it will
be reset to the value stored in THx.
❑ For example, let’s say TH0 holds the value FDh and TL0 holds the value FEh.
After every two cycles TL0 reset to the value stored in TH0.
❑ The auto-reload mode is very commonly used for establishing a baud rate.
MODULE - 4 50
Timer 0 Mode-2 (Auto-reload Timer)
❑ In fact, only the TL0 register operates as a timer, while another TH0 register
stores the value from which the counting starts.
❑ When the TL0 register is loaded, instead of being cleared the contents of TH0
will be reloaded to it.
❑ In order to register each 55th pulse, the best solution is to write the number 200
to the TH0 register and configure the timer to operate in mode 2.
MODULE - 4 51
Timer 0 Mode-2 (Auto-reload Timer)
MODULE - 4 52
Timer 0 Mode-2 (Auto-reload Timer)
MODULE - 4 53
Timer 0 Mode-2 (Auto-reload Timer)
MODULE - 4 54
Timer 0 Mode-3 (Split Timer)
❑ Mode 3 configures timer 0 so that registers TL0 and TH0 operate as
separate 8-bit timers.
❑ In other words, the 16-bit timer consisting of two registers TH0 and
TL0 is split into two independent 8-bit timers.
❑ This mode is provided for applications requiring an additional 8-bit
timer or counter.
❑ The TL0 timer turns into timer 0, while the TH0 timer turns into
timer 1.
MODULE - 4 55
Timer 0 Mode-3 (Split Timer)
MODULE - 4 56
Timer 0 Mode-3 (Split Timer)
❑ While Timer 0 is in split mode, you may not start or stop the real
timer 1 since the bits that do that are now linked to TH0.
❑ The only real use by using split timer mode is if you need to have
two separate timers and, additionally, a baud rate generator.
❑ In such case you can use the real Timer 1 as a baud rate generator
and use TH0/TL0 as two separate 8-bit timers.
MODULE - 4 57
Timer 0 Mode-3 (Split Timer)
MODULE - 4 58
TMOD REGISTER
MODULE - 4 39
❑ EXAMPLE-1: In the following program, we create a square wave of 50% duty
cycle (with equal portions high and low) on the P1.5bit. Timer 0 is used to
generate the time delay. Analyze the program. Assume XTAL = 11.0592 MHz
• Since XTAL = 11.0592 MHz, the counter counts up every 1.085 us.
• This means that out of many 1.085 us intervals we must make a 5 ms pulse.
• To get that, we divide one by the other. We need 5 ms / 1.085 us = 4608 clocks.
• To Achieve that we need to load into TL and TH the value 65536 – 4608 =
EE00H.
Solution:
(b) 1 / 2 of it for the high and low portion of the pulse is 250 us.
(c) 250 us / 1.085 us = 230 and 65536 – 230 = 65306 which in hex FF1AH.
MODULE - 4 64
MOV TMOD,#10H ;Timer 1, 16-bitmode
AGAIN: MOV TL1,#1AH ;TL1=1A, low byte of timer
MOV TH1,#0FFH ;TH1=FF, the high byte
CPL P1.7 ;Complement P1.5
SETB TR1 ;Start timer 1
BACK: JNB TF1,BACK ;until timer rolls over
CLR TR1 ;Stop the timer 1
CLR TF1 ;Clear timer 1 flag
SJMP AGAIN ;Reload timer
MODULE - 4 65
❑ EXAPMLE-5: Generate a square wave with TON of 3ms and T0FF of 10ms on all
pins of port 0. Assume an XTAL of 22MHz.
Since square wave TON and TOFF values are equal. So 2s/2 = 1 second.
MODULE - 4 70
❑ Timers can also be used as counters counting events happening
outside the 8051
➢ When it is used as a counter, it is a pulse outside of the 8051 that increments the
TH, TL registers
➢ TMOD and TH, TL registers are the same as for the timer discussed previously
MODULE - 4 71
❑ The C/T bit in the TMOD registers decides the source of the clock
for the timer
➢ When C/T = 1, the timer is used as a counter and gets its pulses from outside
the 8051
➢ The counter counts up as pulses are fed from pins 14 and 15, these pins are
called T0 (timer 0 input) and T1 (timer 1 input)
MODULE - 4 72
MODULE - 4 73
❑ EXAPMLE-1: Assuming that clock pulses are fed into pin T1, write a program for
counter 1 in mode 2 to count the pulses and display the state of the TL1 count
on P2, which connects to 8 LEDs.
MODULE - 4 74
❑ EXAPMLE-2: Write an 8051 assembly language program to implement a
counter for counting pulses of an input signals. Assume the crystal frequency as
22 MHz. Configure TIMER 1 to generate a clock pulse for every one seconds at
P3.5 and TIMER 0 as a counter which receives input pulses at P3.4 from P3.5
Display final count values in port P1 (TL0) & P2(TH0).
ORG 000H
REPEAT: MOV TMOD, #15H
SETB P3.4
MOV TL0, #00
MOV TH0, #00
SETB TR0
MODULE - 4 75
MOV R0,#28
AGAIN: MOV TL1,#00
MOV TH1, #00
SETB TR1
BACK: JNB TF1, BACK
CLR TF1
CLR TR1
DJNZ R0, AGAIN
MODULE - 4 76
MOV A, TL0
MOV P1,A
MOV A, TH0
MOV P2,A
SJMP REPEAT
END
MODULE - 4 77
1
❑ Computers transfer data in two ways:
➢ Serial
▪ To transfer to a device located many meters away,
the serial method is used
▪ The data is sent one bit at a time
2
➢ Parallel
▪ Often 8 or more lines (wire conductors) are used to
transfer data to a device that is only a few feet
away
3
❑ Serial data communication uses two methods
4
❑ Data transmission types:
5
❑ Protocol is a set of rules agreed by both the sender and
receiver on
➢ How the data is packed
➢ How many bits constitute a character
➢ When the data begins and end
❑ The start bit is always a 0 (low) & the stop bit(s) is 1 (high)
❑ The baud rate and bps are the same, and we use the terms
inter changeably
9
❑ The data transfer rate of given computer system depends
on communication ports incorporated into that system
❑ The standard was set long before the advent of the TTL
logic family, its input and output voltage levels are not TTL
compatible
10
❑ Current terminology classifies data communication equipment
as
11
12
13
❑ DTR (data terminal ready)
➢ When terminal is turned on, it sends out signal DTR to
indicate that it is ready for communication
❑ RI (ring indicator)
➢ An output from the modem and an input to a PC indicates that the
telephone is ringing
➢ It goes on and off in synchronous with the ringing sound
15
❑ The simplest connection between a PC and microcontroller
requires a minimum of three pins, TxD, RxD, and ground
16
❑ In data transmission, serial communication is the process of
sending data one bit at a time, sequentially, over
a communication channel or computer bus.
20
21
22
❑ SBUF is an 8-bit register used solely for serial
communication
23
❑ SCON is an 8-bit the special function register (bit-
addressable).
❑ This register contain not only the mode selection bits but also
the 9th data bit for transmit and receive (TB8 and RB8) and
the serial port interrupt bits (TI and RI).
24
25
REN (receive enable):
When it is high, it allows 8051 to receive data on RxD pin, If
low, the receiver is disable
TI (transmit interrupt):
When 8051 finishes the transfer of 8-bit character It raises
TI flag to indicate that it is ready to transfer another byte
RI (receive interrupt):
When 8051 receives data serially via RxD, it raises the RI flag
bit to indicate that a byte has been received and should be
picked up before it is lost
26
❑ In mode 0, serial data are transmitted and received through
the RXD pin, while the TXD pin output clocks. The baud rate
is fixed at 1/12 the oscillator frequency. On transmit, the
least significant bit (LSB bit) is sent/received first.
27
❑ In mode 1, 10 bits are transmitted through the TXD pin
or received through the RXD pin in the following manner: a
START bit (always 0), 8 data bits (LSB first) and a STOP
bit (always 1). The START bit is only used to initiate data
receive, while the STOP bit is automatically written to the
RB8 bit of the SCON register.
28
❑ In mode 2, 11 bits are transmitted through the TXD pin or
received through the RXD pin: a START bit (always 0), 8 data bits
(LSB first), a programmable 9th data bit and a STOP bit (1).
❑ On transmit, the 9th data bit is actually the TB8 bit of the SCON
register. This bit usually has a function of parity bit. On receive,
the 9th data bit goes into the RB8 bit of the same register
(SCON).
❑ Mode 3 is the same as Mode 2 in all respects except the baud rate.
The baud rate in Mode 3 is variable.
29
❑ Step to program 8051 to transfer character bytes serially
1. TMOD register is loaded with the value 20H, indicating the use
of timer 1 in mode 2 (8-bit auto-reload) to set baud rate
2. The TH1 is loaded with one of the values to set baud rate for
serial data transfer
6. The TI flag bit is monitored with the use of instruction JNB TI, xx
to see if the character has been transferred completely
31
32
33
34
35
36
37
38
39
Assume a switch is connected to pin P1.7. Write a program to monitor its status
and send two messages to serial port continuously as follows:
SW=0 send “NO”
SW=1 send “YES”
Assume XTAL = 11.0592 MHz, 9600 baud, 8-bit data, and 1 stop bit.
40
41
42
42
43
44
45
46
47
❑ A single microcontroller can serve several devices by two
ways: (i) Interrupt (ii). Polling
48
❑ Polling can monitor the status of several devices and serve
each of them as certain conditions are met
49
❑ For every interrupt, there must be an interrupt service
routine (ISR), or interrupt handler
50
❑ Upon activation of an interrupt, the microcontroller goes
through the following steps:
1. It finishes the instruction it is executing and saves the address of the
next instruction (PC) on the stack
2. It also saves the current status of all the interrupts internally (i.e: not
on the stack)
51
4. The microcontroller gets the address of the ISR from the interrupt
vector table and jumps to it
➢ It starts to execute the interrupt service subroutine until it reaches
the last instruction of the subroutine which is RETI (return from
interrupt)
52
53
❑ The interrupts must be enabled by software in order for the
microcontroller to respond to them,
➢ There is a register called IE (interrupt enable) that is responsible
for enabling (unmasking) and disabling (masking) the interrupts
56
57
58
❑ The 8051 has two external hardware interrupts
➢ Pin 12 (P3.2) and pin 13 (P3.3) of the 8051, designated as INT0
and INT1, are used as external hardware interrupts
➢ The interrupt vector table locations 0003H and 0013H are set
aside for INT0 and INT1
➢ There are two activation levels for the external hardware
interrupts
✓ Level trigged
✓ Edge trigged
59
60
❑ In the level-triggered mode, INT0 and INT1 pins are
normally high
➢ If a low-level signal is applied to them, it triggers the interrupt
➢ Then the microcontroller stops whatever it is doing and jumps to
the interrupt vector table to service that interrupt
➢ The low-level signal at the INT pin must be removed before the
execution of the last instruction of the ISR, RETI; otherwise,
another interrupt will be generated
62
ORG 0000H
LJMP main
64
❑ Pins P3.2 and P3.3 are used for normal I/O unless the
INT0 and INT1 bits in the IE register are enabled
➢ After the hardware interrupts in the IE register are
enabled, the controller keeps sampling the INTn pin
for a low-level signal once each machine cycle
68
69
70
71
72
73
❑ When the 8051 is powered up, the priorities are assigned
according to the following
➢ In reality, the priority scheme is nothing but an internal polling
sequence in which the 8051 polls the interrupts in the sequence
listed and responds accordingly
74
❑ Discuss what happens if interrupts INT0, TF0, and INT1
are activated at the same time. Assume priority levels were
set by the power-up reset and the external hardware
interrupts are edge-triggered.
Solution:
➢ If these three interrupts are activated at the same time, they
are latched and kept internally.
➢ Then the 8051 checks all five interrupts according to the
sequence listed above Table.
➢ If any is activated, it services it in sequence.
➢ Therefore, when the above three interrupts are activated, IE0
(external interrupt 0) is serviced first, then timer 0 (TF0), and
finally IE1 (external interrupt 1)
75
❑ We can alter the sequence of interrupt priority by assigning
a higher priority to any one of the interrupts by
programming a register called IP (interrupt priority)
76
77
❑ a) Program the IP register to assign the highest priority to
INT1(external interrupt 1), then