Vous êtes sur la page 1sur 20

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS

UNIT V INTERFACING MICROCONTROLLER


1.

8051 timers:

The 8051 comes equipped with two timers, both of which may be controlled, set, read, and
configured individually. The 8051 timers have three general functions: 1) Keeping time and/or
calculating the amount of time between events, 2) Counting the events themselves, or 3)
Generating baud rates for the serial port. one of the primary uses of timers is to measure time. We
will discuss this use of timers first and will subsequently discuss the use of timers to count
events. When a timer is used to measure time it is also called an "interval timer" since it is
measuring the time of the interval between two events.

8051 timer

TIMER 0

TIMER 1

MODE 0

MODE 0

MODE 1

MODE 1

MODE 2

MODE 2

MODE 3

KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS


TIME SFR:
TMOD REGISTER:
8051 has two timers which each function essentially the same way. One timer is TIMER0 and the
other is TIMER1. The two timers share two SFRs (TMOD and TCON) which control the timers,
and each timer also has two SFRs dedicated solely to itself (TH0/TL0 and TH1/TL1).

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

The TMOD byte is not bit addressable.


00- MODE 0
01- MODE 1
10- MODE 2
11- MODE 3

KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS


TCON REGISTER:

TF 1-Timer 1 overflow flag


TF0TR1Timer 1 Run control bit
TR0IE1Interrupt 1
IE0IT1- Timer 1 interrupts
IT08051 TIMER/COUNTER;

KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS

TIMER 0:

MODE 0:
13-bit Time Mode (mode 0)
Timer mode "0" is a 13-bit timer. This is a relic that was kept around in the 8051 to maintain
compatibility with its predecessor, the 8048. Generally the 13-bit timer mode is not used in new
development. When the timer is in 13-bit mode, TLx will count from 0 to 31. When TLx is
incremented. It will "reset" to 0 and increment THx. Thus, effectively, only 13 bits of the two
timer bytes are being used: bits 0-4 of TLx and bits 0-7 of THx. This also means, in essence, the
timer can only contain 8192 values. If you set a 13-bit timer to 0, it will overflow back to zero
8192 machine cycles later.

KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS

16-bit Time Mode (mode 1)


Timer mode "1" is a 16-bit timer. This is a very commonly used mode. It functions just like 13bit mode except that all 16 bits are used. TLx is incremented from 0 to 255. When TLx is
incremented from 255, it resets to 0 and causes THx to be incremented by 1. Since this is a full
16-bit timer, the timer may contain up to 65536 distinct values. If you set a 16-bit timer to 0, it
will overflow back to 0 after 65,536 machine cycles.

KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS

8-bit Time Mode (mode 2)


Timer mode "2" is an 8-bit auto-reload mode. What is that, you may ask? Simple. 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.

KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS


8.5 Split Timer Mode (mode 3)
Timer mode "3" is a split-timer mode. When Timer 0 is placed in mode 3, it essentially becomes
two separate 8-bit timers. That is to say, Timer 0 is TL0 and Timer 1 is TH0. Both timers count
from 0 to 255 and overflow back to 0. All the bits that are related to Timer 1 will now be tied to
TH0.
While Timer 0 is in split mode, the real Timer 1 (i.e. TH1 and TL1) can be put into modes 0, 1 or
2 normally--however, you may not start or stop the real timer 1 since the bits that do that are now
linked to TH0. The real timer 1, in this case, will be incremented every machine cycle no matter
what.

2.

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.

KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS


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

KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS


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:

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

KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS


Example 1:

Example 2:
;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:
KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS


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.

3.

8051 SERIAL PORT:

Parallel Vs Serial:

KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS


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 ICs made by many manufacturers for serial communications.
UART (universal asynchronous Receiver transmitter)
USART (universal synchronous-asynchronous Receiver transmitter)
Simplex Vs Duplex:

Asynchronous Start & Stop Bit


KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS


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)

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

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:


KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS


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

Registers related to Serial Communication


1. SBUF Register
2. SCON Register
3. PCON Register
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 w hen it is received by 8051 RxD line.
Sample Program:

SCON Register:

KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS

8051
Serial Port Mode 0
The Serial Port in Mode-0 has the following features:
1. Serial data enter 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
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
8051 Serial Port Mode 2
The Serial Port in Mode-2 has the following features:
1. Serial data enters through RXD
KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS


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

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.
8051 Serial Port Program:

KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS

KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS


Program (Transfer):

KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS


Program (Receive):

Write a program for the 8051 to transfer letter A serially at 4800 baud rate, continuously.

KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

EC 6504 MICROPROCESSORS AND MICROCONTROLLERS

Program the 8051 to receive bytes of data serially, and put them in P1. Set the baud rate at
4800.

KARTHIK. S

SSM INSTITUTE OF ENGINEERING AND TECHNOLOGY

Vous aimerez peut-être aussi