Section 2 Microprocessors Course Dr. S.O.Fatemi By: Mahdi Hassanpour
Section 2 Microprocessors Course Dr. S.O.Fatemi By: Mahdi Hassanpour
Section 2 Microprocessors Course Dr. S.O.Fatemi By: Mahdi Hassanpour
Microprocessors course
Dr. S.O.Fatemi
1
Contents:
2
I/O Programming; Bit Manipulation
– The instruction XRL P1,#0FFH do EX-OR P1 and FFH ( That is, to toggle P1. )
3
Bit Manipulation
• Sometimes we need to access only 1 or 2 bits of the port instead of the entire 8 bits.
• This table shows how to name each pin for each I/O port.
• Example:
Solution:
SETB P1.2 ;make P1.2 an input
MOV A,#45H ;A=45H
AGAIN:JNB P1.2,AGAIN;get out when P.2=1
MOV P0,A ;issue A to P0
SETB P2.3 ;make P2.3 high
CLR P2.3 ;make P2.3 low for H-to-L
Note :
1. JNB: jump if no bit ( jump if P1.2 = 0 )
2. a H-to-L pulse by the sequence of instructions SETB and CLR.
4
Single-Bit Addressability of Ports
P0 P1 P2 P3 Port Bit
P0.0 P1.0 P2.0 P3.0 D0
P0.1 P1.1 P2.1 P3.1 D1
P0.2 P1.2 P2.2 P3.2 D2
P0.3 P1.3 P2.3 P3.3 D3
P0.4 P1.4 P2.4 P3.4 D4
P0.5 P1.5 P2.5 P3.5 D5
P0.6 P1.6 P2.6 P3.6 D6
P0.7 P1.7 P2.7 P3.7 D7
5
Time delay Generation and calculation
• Machine cycle
• The frequency of the crystal connected to the 8051 family ca vary from
4MHz to 30 MHz, depending on the chip rating and manufacturer. Very
often the 11.0592 MHz crystal oscillator is used to make the 8051-based
system compatible with the serial port of the IBM PC.
6
7
Example:
Find the time delay for the following subroutine, assuming a crystal
frequency of 11.0592 MHz
Solution:
250x(1+1+1+1+2)+2x1.085 us=1627.5 us
8
Timers /Counters Programming
• The 8051 has 2 timers/counters: timer/counter 0 and
timer/counter 1. They can be used as
1. The timer is used as a time delay generator.
– The clock source is the internal crystal frequency of the
8051.
2. An event counter.
– External input from input pin to count the number of
events on registers.
– These clock pulses cold represent the number of people
passing through an entrance, or the number of wheel
rotations, or any other event that can be converted to pulses.
9
Timer
P2 P1 to
Set LCD
Timer 0 TH0
TL0
10
Counter
12
Basic Registers of the Timer
13
Timer Registers
TH0 TL0
Timer 0
TH1 TL1
Timer 1
14
TMOD Register
(MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
15
Figure 9-3. TMOD Register
17
Gate
18
M1, M0
• M0 and M1 select the timer mode for timers 0 & 1.
(MSB) (LSB)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
21
TCON Register (2/2)
(MSB) (LSB)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
22
Equivalent Instructions for the Timer
Control Register
For timer 0
SETB TR0 = SETB TCON.4
CLR TR0 = CLR TCON.4
25
Steps of Mode 1 (2/3)
TR0=1 TR0=0
Start timer TH0 TL0
Stop timer
TF = 0 TF = 0 TF = 0 TF = 0 TF = 1
27
Mode 1 Programming
XTAL
oscillator 12 ÷
C/T = 0
TH TL TF
overflow
TF goes high
TR flag
when FFFF 0
28
Timer Delay Calculation for XTAL =
11.0592 MHz
29
Example 9-4 (1/3)
In the following program, we are creating a square wave of 50%
duty cycle (with equal portions high and low) on the P1.5 bit.
Timer 0 is used to generate the time delay.
Analyze the program.
Solution:
In mode 1, the program must reload the TH1, TL1 register every
timer if we want to have a continuous wave.
FFFFH – 7634H + 1 = 89CCH = 35276 clock count
Half period = 35276 × 1.085 s = 38.274 ms
Whole period = 2 × 38.274 ms = 76.548 ms
Frequency = 1/ 76.548 ms = 13.064 Hz.
Also notice that the high portion and low portion of the square
wave are equal.
In the above calculation, the overhead due to all the instructions in
the loop is not included. 34
Find Timer Values
35
Example 9-12 (1/2)
Solution:
Look at the following steps.
(a) The period of the square wave = 1 / 50 Hz = 20 ms.
(b) The high or low portion of the square wave = 10 ms.
(c) 10 ms / 1.085 s = 9216
65536 – 9216 = 56320 in decimal = DC00H in hex.
(d) TL1 = 00H and TH1 = DCH.
36
Example 9-12 (2/2)
MOV TH1,#0DCH
SETB TR1 ;start
BACK: JNB TF1,BACK
CLR TR1 ;stop
CPL P2.3
CLR TF1 ;clear timer flag 1
SJMP AGAIN ;reload timer since
;mode 1 is not
;auto-reload
37
Generate a Large Time Delay
38
Example 9-13
Examine the following program and find the time delay in seconds.
Exclude the overhead due to the instructions in the loop.
MOV TMOD,#10H
MOV R3,#200
AGAIN: MOV TL1,#08
MOV TH1,#01
SETB TR1
BACK: JNB TF1,BACK
CLR TR1
CLR TF1
DJNZ R3,AGAIN
Solution:
TH – TL = 0108H = 264 in decimal
65536 – 264 = 65272.
One of the timer delay = 65272 × 1.085 s = 70.820 ms
Total delay = 200 × 70.820 ms = 14.164024 seconds 39
Timer Mode 0
40
Timer Mode 2
• 8-bit timer.
– It allows only values of 00 to FFH to be loaded into TH0.
• Auto-reloading
• TL0 is incremented continuously when TR0=1.
• In the following example, we want to generate a
delay with 200 MCs on timer 0.
• See Examples 9-14 to 9-16
41
Steps of Mode 2 (1/2)
XTAL
oscillator 12 ÷
C/T = 0
overflow
TL1 TF1 flag
reload
TR1 TH1
TF goes high
when FF 0
44
Example 9-15
Find the frequency of a square wave generated on pin P1.0.
Solution:
MOV TMOD,#2H ;Timer 0,mode 2
MOV TH0,#0
AGAIN:MOV R5,#250 ;count 250 times
ACALL DELAY
CPL P1.0
SJMP AGAIN
DELAY:SETB TR0 ;start
BACK: JNB TF0,BACK
CLR TR0 ;stop
CLR TF0 ;clear TF
DJNZ R5,DELAY ;timer 2: auto-reload
RET
T = 2 (250 × 256 × 1.085 s) = 138.88 ms, and frequency = 72 Hz. 45
Example 9-16
Assuming that we are programming the timers for mode 2, find the
value (in hex) loaded into TH for each of the following cases.
(a) MOV TH1,#-200 (b) MOV TH0,#-60 (c) MOV TH1,#-3
(d) MOV TH1,#-12 (e) MOV TH0,#-48
Solution:
Some 8051 assemblers provide this way.
-200 = -C8H 2’s complement of –200 = 100H – C8H = 38 H
Decimal 2’s complement (TH value)
-200 = - C8H 38H
- 60 = - 3CH C4H
- 3 FDH
- 12 F4H
- 48 D0H 46
Example 9-17 (1/2)
Solution:
“MOV TH0,#-150” uses 150 clocks.
The DELAY subroutine = 150 × 1.085 s = 162 s.
The high portion of the pulse is twice tat of the low portion (66%
duty cycle).
The total period = high portion + low portion
= 325.5 s + 162.25 s = 488.25 s
Frequency = 2.048 kHz.
47
Example 9-17 (2/2)
MOV TMOD,#2H ;Timer 0,mode 2
MOV TH0,#-150 ;Count=150
AGAIN:SETB P1.3
high
ACALL DELAY
period
ACALL DELAY
CLR P1.3 low
ACALL DEALY period
SJMP AGAIN
49
Port 3 Pins Used For Timers 0 and 1
(MSB) (LSB)
GATE C/T=1 M1 M0 GATE C/T=1 M1 M0
Timer 1 Timer 0
50
Counter Mode 1
overflow
Timer 0 flag
external TH0 TL0 TF0
input
Pin 3.4
TF0 goes high
C/T = 1 TR0 when FFFF 0
52
Counter Mode 2
• 8-bit counter.
– It allows only values of 00 to FFH to be loaded into TH0.
• Auto-reloading
• TL0 is incremented if TR0=1 and external pulse
occurs.
• See Figure 9.6, 9.7 for logic view
• See Examples 9-18, 9-19
53
Example 9-18 (1/2)
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
TL 1 count on P2.
Solution:
MOV TMOD,#01100000B ;mode 2, counter 1
MOV TH1,#0
SETB P3.5 ;make T1 input port
AGAIN:SETB TR1 ;start
BACK: MOV A,TL1
MOV P2,A ;display in P2
JNB TF1,Back ;overflow
CLR TR1 ;stop
CLR TF1 ;make TF=0
SJMP AGAIN ;keep doing it 54
Example 9-18 (2/2)
We use timer 1 as an event counter where it counts up as clock
pulses are fed into pin3.5.
Notice in the above program the role of the instruction “SETB
P3.5”. Since ports are set up for output when the 8051 is powered
up , we must make P3.5 an input port by making it high.
8051
P2 is connected to 8 LEDs
P2 to
and input T1 to pulse.
LEDs
P3.5
T1
55
Example 9-19 (1/3)
Assume that a 1-Hz frequency pulse is connected to input pin 3.4.
Write a program to display counter 0 on an LCD. Set the initial
value of TH0 to -60.
Solution:
Note that on the first round, it starts from 0 and counts 256 events,
since on RESET, TL0=0. To solve this problem, load TH0 with
-60 at the beginning of the program.
8051
P1 to
LCD
P3.4
1 Hz clock T0
56
Example 9-19 (2/3)
60
Example for GATE=1
61
Timer/Counter 0
XTAL
oscillator 12 ÷
C/T = 0
C/T = 1
T0 Pin
Pin 3.4 TR0
Gate
INT0 Pin
Pin 3.2
62
Interrupts Programming
63
Steps in executing an interrupt:
64
Six interrupts in 8051
SJMP FIRST
ORG 30H
FIRST:
…
END
65
Enabling and disabling an interrupt:
Example:
Write a program using interrupts to simultaneously create 7
kHz and 500 Hz square waves on P1.7 and P1.6.
66
Solution:
ORG 0
LJMP MAIN
ORG 000BH
LJMP T0ISR 8051 143s
ORG 001BH 71s
LJMP T1ISR P1.7
ORG 0030H
MAIN: MOV TMOD,#12H
MOV TH0,#-71
SETB TR0
SETB TF1 2ms
MOV IE,#8AH 1ms
P1.6
MOV IE,#8AH
SJMP $
T0ISR: CLR P1.7
RETI
T1ISR: CLR TR1
MOV TH1,#HIGH(-1000)
MOV TL1,#LOW(-1000)
SETB TR1
CPL P1.6
RETI
END
67
External Interrupts:
Level-triggered (default)
INT0
)Pin 3.2( 0
0003
IT0
1 IE0
2 (TCON.3)
Edge-triggered
Level-triggered (default)
INT0
)Pin 3.3( 0 0013
IT1
1 IE1
2 (TCON.3)
Edge-triggered
68
Exercise
69
Serial Communication
70
Basics of serial communication
•Baud Rate
71
72
Start and stop bits
73
RxD and TxD pins in the 8051
SBUF register
74
MAX232
75
76
77