Unit I MC1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 108

8051

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

The TMOD byte is not bit addressable.

00- MODE 0
01- MODE 1
10- MODE 2
11- MODE 3 8
TCON Register

TF 1-Timer 1 overflow flag


TF0-
TR1- Timer 1 Run control bit
TR0-
IE1- Interrupt 1
IE0-
IT1- Timer 1 interrupt
IT0- 9
8051 Timer/Counter
OSC ÷12
C /T = 0 TLx THx TFx
(8 Bit) (8 Bit) (1 Bit)
C /T =1

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

1111 1111 1111 1111


0000 0000 0000 0000
INT 0 PIN 216 = 65536*1us= 0.65536s
11
TIMER 0 – Mode 0
13 Bit Timer / Counter

OSC ÷12
C /T = 0 TL0 TH0 INTERRUPT
C /T =1 TF0
T 0PIN (5 Bit) (8 Bit)
TR0

Gate

INT 0 PIN

Maximum Count = 1FFFh (0001111111111111)


12
TIMER 0 – Mode 1
16 Bit Timer / Counter

OSC ÷12
C /T = 0 TL0 TH0 INTERRUPT
C /T =1 TF0
T 0PIN (8 Bit) (8 Bit)
TR0

Gate

INT 0 PIN

Maximum Count = FFFFh (1111111111111111)


13
TIMER 0 – Mode 2
8 Bit Timer / Counter with AUTORELOAD

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)

Maximum Count = FFh (11111111)


14
• Mode 2 is an 8 bit mode. The initial value is loaded into the
higher byte.

• 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.

• Mode 2 is commonly used for setting baud rates for serial


communication.
TIMER 0 – Mode 3
Two - 8 Bit Timer / Counter

OSC ÷12
C /T = 0 TL0 INTERRUPT
C /T =1 TF0
T 0PIN
(8 Bit)
TR0

Gate

INT 0 PIN

OSC ÷12 TH0 INTERRUPT


TF1
(8 Bit)

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

Maximum Count = 1FFFh (0001111111111111)


20
TIMER 1 – Mode 1
16 Bit Timer / Counter

OSC ÷12
C /T = 0 TL1 TH1 INTERRUPT
C /T =1 TF1
T1PIN (8 Bit) (8 Bit)
TR1

Gate

INT1 PIN

Maximum Count = FFFFh (1111111111111111)


21
TIMER 1 – Mode 2
8 Bit Timer / Counter with AUTORELOAD

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)

Maximum Count = FFh (11111111)


22
Timer modes
TCON Register (1/2)
• Timer control register: TMOD
– Upper nibble for timer/counter, lower nibble for
interrupts
• TR (run control bit)
– TR0 for Timer/counter 0; TR1 for Timer/counter 1.
– TR is set by programmer to turn timer/counter on/off.
• TR=0: off (stop) TR=1: on (start)

(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

SETB TF0 = SETB TCON.5


CLR TF0 = CLR TCON.5
For timer 1
SETB TR1 = SETB TCON.6
CLR TR1 = CLR TCON.6

SETB TF1 = SETB TCON.7


CLR TF1 = CLR TCON.7

TCON: Timer/Counter Control Register

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0


Programs in 8051 TIMERS
Timer Mode 1
• In following, we all use timer 0 as an example.

• 16-bit timer (TH0 and TL0)

• TH0-TL0 is incremented continuously when TR0 is set to 1. And


the 8051 stops to increment TH0-TL0 when TR0 is cleared.

• The timer works with the internal system clock. In other words,
the timer counts up each machine cycle.

• When the timer (TH0-TL0) reaches its maximum of FFFFH, it


rolls over to 0000, and TF0 is raised.

• Programmer should check TF0 and stop the timer 0.


Steps of Mode 1 (1/3)

1. Choose mode 1 timer 0


– MOV TMOD,#01H
2. Set the original value to TH0 and TL0.
– MOV TH0,#FFH
– MOV TL0,#FCH
3. You had better to clear the flag to monitor: TF0=0.
– CLR TF0
4. Start the timer.
– SETB TR0
Steps of Mode 1 (2/3)
5.The 8051 starts to count up by incrementing the
TH0-TL0.
– TH0-TL0=
FFFCH,FFFDH,FFFEH,FFFFH,0000H
TR0=1 TR0=0
Start timer TH0 TL0
Stop timer

FFFC FFFD FFFE FFFF 0000

TF = 0 TF = 0 TF = 0 TF = 0 TF = 1

TF Monitor TF until TF=1


Steps of Mode 1 (3/3)
6. When TH0-TL0 rolls over from FFFFH to
0000, the 8051 set TF0=1.
TH0-TL0= FFFEH, FFFFH, 0000H (Now TF0=1)
7. Keep monitoring the timer flag (TF) to see if it
is raised.
AGAIN: JNB TF0, AGAIN
8. Clear TR0 to stop the process.
CLR TR0
9. Clear the TF flag for the next round.
CLR TF0
Mode 1 Programming

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

;each loop is a half clock


MOV TMOD,#01 ;Timer 0,mode 1(16-bit)
HERE: MOV TL0,#0F2H ;Timer value = FFF2H
MOV TH0,#0FFH
CPL P1.5
ACALL DELAY P1.5
SJMP HERE
50% 50%

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

FFF2 FFF3 FFF4 FFFF 0000

TF0 = 0 TF0 = 0 TF0 = 0 TF0 = 0 TF0 = 1


Example 1 (3/3)
Solution:
In the above program notice the following steps.
1. TMOD = 0000 0001 is loaded.
2. FFF2H is loaded into TH0 – TL0.
3. P1.5 is toggled for the high and low portions of the pulse.
4. The DELAY subroutine using the timer is called.
5. In the DELAY subroutine, timer 0 is started by the “SETB TR0”
instruction.
6. Timer 0 counts up with the passing of each clock, which is provided by the
crystal oscillator.
As the timer counts up, it goes through the states of FFF3, FFF4, FFF5, FFF6,
FFF7, FFF8, FFF9, FFFA, FFFB, FFFC, FFFFD, FFFE, FFFFH. One more
clock rolls it to 0, raising the timer flag (TF0 = 1). At that point, the JNB
instruction falls through.
7. Timer 0 is stopped by the instruction “CLR TR0”. The DELAY subroutine
ends, and the process is repeated.

Notice that to repeat the process, we must reload the TL and TH


registers, and start the timer again (in the main program).
Example 2 (1/2)
• This program generates a square wave on pin P1.5 Using timer 1
• Find the frequency.(dont include the overhead of instruction delay)
• XTAL = 11.0592 MHz

MOV TMOD,#10H ;timer 1, mode 1


AGAIN:MOV TL1,#34H ;timer value=3476H
MOV TH1,#76H
SETB TR1 ;start
BACK: JNB TF1,BACK
CLR TR1 ;stop
CPL P1.5 ;next half clock
CLR TF1 ;clear timer flag 1
SJMP AGAIN ;reload timer1
Example 2 (2/2)

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

• Assume that XTAL = 11.0592 MHz .


• And we know desired delay
• how to find the values for the TH,TL ?
1. Divide the delay by 1.085 s and get n.
2. Perform 65536 –n
3. Convert the result of Step 2 to hex (yyxx )
4. Set TH = yy and TL = xx.
Example 3 (1/2)
• Assuming XTAL = 11.0592 MHz,
• write a program to generate a square wave of 50 Hz
frequency on pin P2.3.

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)

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
CPL P2.3
CLR TF1 ;clear timer flag 1
SJMP AGAIN ;reload timer since
;mode 1 is not
;auto-reload
8051
Serial
Port
42
43
Basics of Serial Communication
• Serial data communication uses two methods
– Synchronous method transfers a block of data at a time

– Asynchronous method transfers a single byte at a time

• There are special IC’s made by many manufacturers


for serial communications.
– UART (universal asynchronous Receiver transmitter)

– USART (universal synchronous-asynchronous Receiver-


transmitter)
44
45
46
Asynchronous – Start & Stop Bit
• Asynchronous serial data communication is
widely used for character-oriented transmissions

• The start bit is always a 0 (low) and the stop


bit(s) is 1 (high)

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).

• Another widely used terminology for bps is baud


rate.
– It is modem terminology and is defined as the
number of signal changes 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:

Mode 0 :Synchronous Serial Communication


Mode 1 :8-Bit UART with Timer Data Rate
Mode 2 :9-Bit UART with Set Data Rate
Mode 3 :9-Bit UART with Timer Data Rate
50
Registers related to Serial Communication

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

SM0 SM1 SM2 REN TB8 RB8 TI RI

Set when a Cha-


ractor received
Set to Enable
Serial Data
reception Set when Stop bit Txed

9th Data Bit


Enable Multiprocessor 9th Data Bit
Transmitted
Communication Mode Received in Mode 2,3
in Mode 2,3

54
8051 Serial Port – Mode 0
The Serial Port in Mode-0 has the following
features:

1. Serial data enters and exits through RXD

2. TXD outputs the clock

3. 8 bits are transmitted / received

4. The baud rate is fixed at (1/12) of the oscillator frequency

55
8051 Serial Port – Mode 1
The Serial Port in Mode-1 has the following
features:

1. Serial data enters through RXD


2. Serial data exits through TXD
3. On receive, the stop bit goes into RB8 in SCON
4. 10 bits are transmitted / received
1. Start bit (0)
2. Data bits (8)
3. Stop Bit (1)
5. Baud rate is determined by the Timer 1 over flow rate.

56
8051 Serial Port – Mode 2
The Serial Port in Mode-2 has the following
features:

1. Serial data enters through RXD


2. Serial data exits through TXD
3. 9th data bit (TB8) can be assign value 0 or 1
4. On receive, the 9th data bit goes into RB8 in SCON
5. 11 bits are transmitted / received
1.Start bit (0)
2.Data bits (9)
3.Stop Bit (1)
6. Baud rate is programmable
57
8051 Serial Port – Mode 3
The Serial Port in Mode-3 has the following
features:

1. Serial data enters through RXD


2. Serial data exits through TXD
3. 9th data bit (TB8) can be assign value 0 or 1
4. On receive, the 9th data bit goes into RB8 in SCON
5. 11 bits are transmitted / received
1.Start bit (0)
2.Data bits (9)
3.Stop Bit (1)
6. Baud rate is determined by Timer 1 overflow rate.
58
Programs in 8051 serial port

TIMER 1 MODE 2 {AUTO RELOAD}


Write a program for the 8051 to transfer letter ‘A’
serially at 4800 baud rate, continuously.
MOV TMOD, #20H ; Timer 1, mode 2
MOV TH1,#-06 TH1 is loaded to set the baud rate.

MOV SCON, #50H


SETB TR1 ; Run Timer 1
L2 : MOV SBUF, # ’A’
Loop: JNB TI, Loop ; Monitor RI
MOV A, SBUF
CLR TI
SJMP L2
Program the 8051 to receive bytes of data serially, and
put them in P1. Set the baud rate at 4800.

MOV TMOD, #20H ; Timer 1, mode 2


MOV TH1,#-06 TH1 is loaded to set the baud rate.

MOV SCON, #50H


SETB TR1 ; Run Timer 1
Loop: JNB RI, Loop ; Monitor RI
MOV A, SBUF
CLR RI
SJMP Loop
8051
Interrupts
65
INTERRUPTS

• An interrupt is an external or internal event


that interrupts the microcontroller to inform it
that a device needs its service

• A single microcontroller can serve several


devices by two ways:
1. Interrupt
2. Polling
66
Interrupt
– Upon receiving an interrupt signal, the
microcontroller interrupts whatever it is doing
and serves the device.
– The program which is associated with the
interrupt is called the interrupt service routine
(ISR) .

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.

• Jumps to a fixed location in memory depend on type


of interrupt

• Starts to execute the interrupt service routine until


RETI (return from interrupt)

• Upon executing the RETI the microcontroller returns


to the place where it was interrupted. Get pop PC
from stack
Interrupt Sources
• Original 8051 has 6 sources of interrupts
– Reset (RST)
– Timer 0 overflow (TF0)
– Timer 1 overflow (TF1)
– External Interrupt 0 (INT0)
– External Interrupt 1 (INT1)
– Serial Port events (RI+TI)
{Reception/Transmission of Serial Character}
8051 Interrupt Vectors

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

– SCON - RI and TI interrupt flags for RS232 {SERIAL


COMMUNICATION}

– 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.

MOV IE,#08h • ES : Enable Serial port interrupt.


or
SETB ET1
• ET1 : Enable Timer 1 control bit.
• EX1 : Enable External 1 interrupt.
• ET0 : Enable Timer 0 control bit.
• EX0 : Enable External 0 interrupt.
74
Interrupt Priority

75
Interrupt Priority (IP) Register

Reserved PS PT1 PX1 PT0 PX0

Serial Port
Timer 1 Pin INT 0 Pin

INT 1 Pin Timer 0 Pin

Priority bit=1 assigns high priority


Priority bit=0 assigns low priority
76
77
KEYBOARD
INTERFACING

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

• It has a permanent magnet rotor called the shaft


which is surrounded by a stator. Commonly used
stepper motors have 4 stator windings

• Such motors are called as four-phase or unipolar


stepper motor.
86
87
88
Full step

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

Shunt voltage diod

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

You might also like