Professional Documents
Culture Documents
Unit I MC1
Unit I MC1
Unit I MC1
TIMERS
1
8051 Timer Modes
8051 TIMERS
Timer 0 Timer 1
Mode 0 Mode 0
Mode 1 Mode 1
Mode 2 Mode 2
Mode 3
6
TMOD Register
GATE:
When set, timer/counter x is enabled, if INTx pin is high and TRx
is set.
When cleared, timer/counter x is enabled, if TRx bit set.
C/T*:
When set(1), counter operation (input from Tx input pin).
When clear(0), timer operation (input from internal clock).
7
TMOD Register
00- MODE 0
01- MODE 1
10- MODE 2
11- MODE 3 8
TCON Register
T PIN
INTERRUPT
TR
Gate
INT PIN
10
TIMER 0
OSC
÷12
12 M
C /T = 0
TL0 TH0 TF0
C /T =1
T 0PIN
INTERRUPT
TR0
0000 0000 0000 0000
0000 0000 0000 0001
Gate 0000 0000 0000 0010
OSC ÷12
C /T = 0 TL0 TH0 INTERRUPT
C /T =1 TF0
T 0PIN (5 Bit) (8 Bit)
TR0
Gate
INT 0 PIN
OSC ÷12
C /T = 0 TL0 TH0 INTERRUPT
C /T =1 TF0
T 0PIN (8 Bit) (8 Bit)
TR0
Gate
INT 0 PIN
OSC ÷12
C /T = 0 TL0 TH0 INTERRUPT
C /T =1 TF0
T 0PIN (8 Bit) (8 Bit)
TR0
Gate Reload
INT 0 PIN
TH0
(8 Bit)
• A copy of the same is passed to the lower byte. The Timer can
update from 00H to FFH. The Timer rolls over from FFH to
initial value automatically.
OSC ÷12
C /T = 0 TL0 INTERRUPT
C /T =1 TF0
T 0PIN
(8 Bit)
TR0
Gate
INT 0 PIN
TR1
17
• In mode 3, also known as split mode, the Timer breaks into two 8-
bit Timers that can count from 00H up to FFH.
• The initial values are loaded into the higher byte and lower byte
of the Timer.
• In this case, the start and flag bits associated with the other Timer
are now associated with high byte Timer. So one cannot start or
stop the other Timer.
• The other Timer can be used in modes 0, 1 or 2 and is updated
automatically for every machine cycle.
• For example, if Timer0 is used in split mode, TH0 and TL0 will
become separate Timers.
• The start and flag bits associated with Timer1 will now be
associated with the TH0. Timer 1 cannot be stopped or started,
but will be updated for every machine cycle.
• Split mode is useful when two Timers are required in addition to a
baud rate generator.,
TIMER 1
OSC ÷12
C /T = 0
TL1 TH1 TF1
C /T =1
T1PIN
INTERRUPT
TR1
Gate
INT1 PIN
19
TIMER 1 – Mode 0
13 Bit Timer / Counter
OSC ÷12
C /T = 0 TL1 TH1 INTERRUPT
C /T =1 TF1
T1PIN (5 Bit) (8 Bit)
TR1
Gate
INT1 PIN
OSC ÷12
C /T = 0 TL1 TH1 INTERRUPT
C /T =1 TF1
T1PIN (8 Bit) (8 Bit)
TR1
Gate
INT1 PIN
OSC ÷12
C /T = 0 TL1 TH1 INTERRUPT
C /T =1 TF1
T1PIN (8 Bit) (8 Bit)
TR1
Gate Reload
INT1 PIN
TH1
(8 Bit)
(MSB) (LSB)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
TCON Register (2/2)
• TF (timer flag, control flag)
– TF0 for timer/counter 0; TF1 for timer/counter 1.
– TF is like a carry. Originally, TF=0. When TH-TL roll
over to 0000 from FFFFH, the TF is set to 1.
• TF=0 : not reach
• TF=1: reach
• If we enable interrupt, TF=1 will trigger ISR.
(MSB) (LSB)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
Equivalent Instructions for the Timer Control
Register
For timer 0
SETB TR0 = SETB TCON.4
CLR TR0 = CLR TCON.4
• The timer works with the internal system clock. In other words,
the timer counts up each machine cycle.
TF = 0 TF = 0 TF = 0 TF = 0 TF = 1
XTAL
oscillator ÷ 12
C/T = 0
Timer
overflow
flag
TH TL TF
TR
TF goes high when FFFF 0
Timer Delay Calculation for XTAL = 11.0592 MHz
(a) in hex
• (FFFF – YYXX + 1) × 1.085 s
• where YYXX are TH, TL initial values respectively.
• Notice that values YYXX are in hex.
(b) in decimal
• Convert YYXX values of the TH, TL register to
decimal to get a NNNNN decimal number
• then (65536 – NNNNN) × 1.085 s
Example 1 (1/3)
• square wave of 50% duty on P1.5
• Timer 0 is used
whole clock
Example 1 (2/3)
;generate delay using timer 0
DELAY:
SETB TR0 ;start the timer 0
AGAIN:JNB TF0,AGAIN
CLR TR0 ;stop timer 0
CLR TF0 ;clear timer 0 flag
RET
Solution:
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.
Note
Mode 1 is not auto reload then the program must reload
the TH1, TL1 register every timer overflow if we want to
have a continuous wave.
Find Timer Values
Solution:
1. The period of the square wave = 1 / 50 Hz = 20 ms.
2. The high or low portion of the square wave = 10 ms.
3. 10 ms / 1.085 s = 9216
4. 65536 – 9216 = 56320 in decimal = DC00H in hex.
5. TL1 = 00H and TH1 = DCH.
Example 3 (2/2)
47
Asynchronous – Start & Stop Bit
48
Data Transfer Rate
• The rate of data transfer in serial data
communication is stated in bps (bits per second).
49
8051 Serial Port
• Synchronous and Asynchronous
• SCON Register is used to Control
• Data Transfer through TXd & RXd pins
• Some time - Clock through TXd Pin
• Four Modes of Operation:
1. SBUF Register
2. SCON Register
3. PCON Register
51
SBUF Register
• SBUF is an 8-bit register used solely for serial communication.
• For a byte data to be transferred via the TxD line, it must be
placed in the SBUF register.
• SBUF holds the byte of data when it is received by 8051 RxD
line.
52
SBUF Register
• Sample Program:
53
SCON Register
54
8051 Serial Port – Mode 0
The Serial Port in Mode-0 has the following
features:
55
8051 Serial Port – Mode 1
The Serial Port in Mode-1 has the following
features:
56
8051 Serial Port – Mode 2
The Serial Port in Mode-2 has the following
features:
67
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.
2. It also saves the current status of all the interrupts internally (i.e:
not on the stack).
3. It jumps to a fixed location in memory, called the interrupt vector
table, that holds the address of the ISR.
4. The microcontroller gets the address of the ISR from the
interrupt vector table and jumps to it.
5. It starts to execute the interrupt service subroutine until it
reaches the last instruction of the subroutine which is RETI
(return from interrupt).
6. Upon executing the RETI instruction, the microcontroller returns
to the place where it was interrupted.
68
Steps in executing an interrupt
• Finish current instruction and saves the PC on stack.
71
8051 Interrupt related Registers
• The various registers associated with the use of
interrupts are:
– TCON - Edge and Type bits for External Interrupts 0/1
– IE - interrupt Enable
– IP - Interrupts priority
72
Enabling and Disabling an Interrupt
• The register called IE (interrupt enable) that is
responsible for enabling (unmasking) and
disabling (masking) the interrupts.
73
Interrupt Enable (IE) Register
--
• EA : Global enable/disable.
• --- : Reserved for additional interrupt hardware.
75
Interrupt Priority (IP) Register
Serial Port
Timer 1 Pin INT 0 Pin
78
KEYBOARD INTERFACING
• Keyboards are organized in a matrix of rows
and columns
The CPU accesses both rows and columns
through ports .
• ƒTherefore, with two 8-bit ports, an 8 x 8
matrix of keys can be connected to a
microprocessor
When a key is pressed, a row and a
column make a contact
79
• Otherwise, there is no connection
between rows and columns
•‰A 4x4 matrix connected to two ports
The rows are connected to an
output port and the columns are
connected to an input port
80
4x4 matrix
81
82
Connection with keyboard matrix
Final Circuit
Stepper Motor
Interfacing
85
Stepper Motor Interfacing
• Stepper motor is used in applications such as;
dot matrix printer, robotics etc
89
Step angle:
• Step angle is defined as the minimum degree of
rotation with a single step.
• No of steps per revolution = 360° / step angle
• Steps per second = (rpm x steps per revolution) /
60
• Example: step angle = 2°
• No of steps per revolution = 180
90
A switch is connected to pin P2.7. Write an ALP to
monitor the status of the SW.
If SW = 0, motor moves clockwise and
If SW = 1, motor moves anticlockwise
SETB P2.7
MOV A, #66H
MOV P1,A
TURN: JNB P2.7, CW
RL A
ACALL DELAY
MOV P1,A DELAY: MOV R1,#20
SJMP TURN L2: MOV R2,#50
CW: RR A L1:DJNZ R2,L2
DJNZ R2,L1
ACALL DELAY
RET
MOV P1,A
SJMP TURN 91
LCD Interfacing
{before discussed in Unit 3 LCD
interfacing using 8086}
92
93
Pin Connections of LCD:
94
95
A/D Interfacing
{before discussed in Unit 3 A/D
interfacing using 8086}
96
Interfacing ADC to 8051
ADC0804 is an 8 bit successive approximation analogue to digital
converter from National semiconductors. The features of ADC0804
are differential analogue voltage inputs, 0-5V input voltage range, no zero
adjustment, built in clock generator, reference voltage can be externally
adjusted to convert smaller analogue voltage span to 8 bit resolution etc.
97
ADC Interfacing:
98
D/A Interfacing
{before discussed in Unit 3 D/A
interfacing using 8086}
99
8051 Connection to DAC808
100
program to send data to the DAC to
generate a stair-step ramp
101
SENSOR
INTERFACING
take temperature sensor for example
102
8051 WITH TEMPERATURE SENSOR
103
potentiometer
104
EXTERNAL
MEMORY
INTERFACING
105
Access to External Memory
• Port 0 acts as a multiplexed address/data bus. Sending the
low byte of the program counter (PCL) as an address.
• Port 2 sends the program counter high byte (PCH) directly to
the external memory.
• The signal ALE operates as in the 8051 to allow an external
latch to store the PCL byte while the multiplexed bus is made
ready to receive the code byte from the external memory.
• Port 0 then switches function and becomes the data bus
receiving the byte from memory.
106
107
Presented by C.GOKUL,AP/EEE , Velalar College of Engg & Tech, Erode