Mca Note Mod3
Mca Note Mod3
8051 supports five vectored interrupt sources. Three of these are generated automatically
by internal operations. They are
(1) Timer 0 interrupt
(2) Timer 1 interrupt and
(3) Serial COM interrupt
Two interrupts are triggered by external signals provided by circuitry connected to the
corresponding interrupt pins and are
Table below lists the name, flag, vector address and pin number of all interrupts in 8051
Level-triggered interrupts
In the level triggered mode, the external interrupt pins (INT0 and INT1) are normally high
and the processor merely samples the input signal at every instruction cycle and if it sees low
(logic 0) it triggers the interrupt. The 8051 will hold the interrupt signal until the routine can be
vectored to and completed as signaled by the execution of RETI instruction.
The level- triggered interrupt method is desired in situations in which the input signal is not
freed by the hardware device requesting the interrupt until sometime after service the interrupt
Edge- triggered interrupts
An edge triggered interrupt is caused by a high to low transition in the input signal. As in
the case of level triggered interrupts, the signal is sampled once every instruction cycle. This
method of interrupt triggering is useful for devices that will not pull the signal back to logic
level one immediately upon being serviced. Here the input signal at INT pin must return to a
logic level one to cause another interrupt in the system
INTERRUPT PRIORITY
When more than one interrupts are enabled. User can program the interrupt priority levels by
setting or clearing the bits in the SFR called Interrupt Priority (IP) register. Each interrupt sources can
individually set to one of two priority levels. If the pin corresponding to an interrupt source is set, it will
have high priority. Similarly, if the bit is cleared the interrupt will be of low priority and subject to being
interrupted by any high priority interrupts. Fig shows the IP register and its bit assignments
Bit Symbol Function
TIMERS IN 8051
There are many microcontroller applications which require real time operations such as pulse
counting, frequency measurement, pulse width measurement, baud rate generation etc. All these tasks can
be done through software techniques. But software loops for these tasks will keep the processor encaged,
so that other important functions during this time will not be carried out. To relieve the processor of this
burden, two 16-bit up counters called Timer0(T0) and Timer1 (T1) are provided in 8051 microcontrollers.
Both of these counters can be configured as timer or counters independently.
A timer counts the internal clock pulses by incrementing the counter register (T0/T1) on every
machine cycle and provides a reference time delay or a clock. Since a machine cycle consists
of 12 oscillator periods, the counting rate is 1/12 of the oscillator frequency. ie at oscillator frequency of
12MHz, the clocking period will be equal to 1us.
A counter counts external pulses and increments the register in response to a transition from ‘1’
to ‘0’ at its corresponding external input pin (T0/T1). Thus, the counter output will be a count or number
representing the occurrence of such ‘1’ to ‘0’ transitions at the external input pin. For counting function
,8051 takes two machine cycles (24 oscillator periods) to detect a ‘1’ to ‘0’ transition at pin T 0 (P3.4) or
T1(P3.5)
When a timer or counter overflows from FFFF H to 0000 H, it sets a flag and generates an interrupt.
The 16-bit registers of timers are divided in to two 8-bit registers and are referred to as higher
byte (TH0/TH1) and the lower byte (TL0/TL1). The action of counter is controlled by the bit status in the
special function registers TMOD (Timer Mode control) and TCON (Timer/Counter Control) registers and
some program instructions.
TMOD register is a special function register used to control the mode of operation of both timers. This
register is an 8 bit register with two similar nibbles. The upper nibble (bits 4 through7) contains the
configuration bits for Timer1 and the lower nibble (bits 0 through 3) has the configuration bits for Timer0.
The detailed description on TMOD register is given below
Timer/Counter in 8051 are controlled by TR1/0 bits in TCON, GATE bits in TMOD and INT1/0
input pins of 8051. The C/T pin in the TMOD register selects between the external pinT0/1 and the internal
clock frequency divided by 12. This input signal is then gated by a combination of TR0/1, GATE and the
INT0/1 pin.
The counter counts the selected input signal if TR0/1 is high along with GATE being low or
INT0/1 pin being high. This is illustrated in fig above
TIMER/COUNTER MODES
Timers can be operated in four different modes determined by the mode bits M1 andM0 in the
TMOD register
MODE0:
In this mode timer operates as a 13 bit up-counter in a way, the lower 5bits are in the TL0/1 and
the upper 8bits in TH0/1 register. Here TL0/1 will count from 0 to 31. When it is incremented from 31, it
will reset to 0 and increment TH0/1. Thus, effectively only 13 bits (bits 0-4 of TL0/1 and bits 0-7 of TH0/1)
of the timer bytes are being used. The timer interrupt flag TF0/1 bit is set, when the counter rolls over from
FF H to 00h. This bit can then be used to interrupt the 8051. Fig.above illustrates the timer operation in
mode0
MODE1:
This mode is exactly similar to mode0, but the counter is16 bits with lower byte in TL0/1 and the upper
byte in TH0/1. All other functions are similar to mode 0
MODE2:
Mode2 is the auto reload mode. In this mode the counter work as an 8bit counter in TL0/1.Here
TH0/1is used to hold a value that is loaded in to TL0/1. When the counter rolls over from FF H to 00H the
interrupt flag TF0/1 is set and the counter TL0/1 is reloaded automatically with the value in TH0/1. This
mode is useful to divide the input clock frequency to the counter by a preset value
MODE3:
Timer mode3 is a split-timer mode. In this mode the timer0 and timer1 don’t operate independently,
if mode3 is chosen for timer0. Timer1in mode3 just hold its current count. The control bit TR1 and the
timer flag TF1 are then be used by timer 0. Timer0 in mode3 functions as two independent counters .TL0
as an 8bit counter that counts the internal clock/12 or external events as represented in fig. The roll over of
TL0 from FFH to 00h sets the TF0 flag. This flag can then be used to interrupt the processor if required.
TH0 is an independent 8bit counter that counts 1./12 the clock frequency and is gated by TR1. Roll over
of this counter sets theTF1 flag which can also be used to interrupt the processor.
Tick = (1/(Fosc/12)
= 12/Fosc
If the clock frequency (crystal frequency) is 12MHz
Tick = 12/12x10-6
= 1us
Now the Timer value for the required delay can be calculated using following steps
1.Calculate the tick value as 12/Fosc
2.Ddevide the desired time delay by tick value to get the count (n) required for the delay
3.Perform 65536-count(n) to get the timer value in decimal
4.Convert the result of step3 to Hex
5.Load the lower byte of the Hex value obtained in step 4 into lower byte of Timer (TL) and higher byte of
the Hex value into higher byte of timer (TH)
Here the time delay calculation using mode1(16-bit) can be done using the formula
T= (65536-NNNN)x Tick
Where NNNN is the decimal value obtained in step 3
Example: The value to be loaded into timer registers operating in mode1 to get a time delay of 5ms using
12Mhz crystal can be calculated as follows
Baud rate
Baud rate is a term to represent the rate of data transfer in serial communication. It is the number
of signal changes per second and is the reciprocal of the time to send one bit. Baud rate need not be equal
to the number of bits per second. This is because each data byte may consist few additional bits like start
and stop etc
In synchronous method a clock signal ( a pulse stream of logic low and high level at a fixed high
frequency) is transmitted as the control signal . The data bits are transmitted/received with reference to
the edges of the clock signal. All data transfer is thus synchronized to this clock. High speed of
communication is possible through this method. Synchronous serial data communication method is used
for block-oriented data transmission
In asynchronous communication no clock pulses are transmitted to synchronize the data transfer.
Here the data transfer takes place based on internal clock pulses. Thus, the interface lines between
communicating devices carry only data signals. The sender and receiver should be synchronized to the same
clock frequency. Asynchronous serial data communication method is used for character -oriented data
transmission
SBUF Register
It is an 8-bit register used solely for serial communication in 8051. For a byte of data to be
transferred via TxD line, it must be placed in SBUF register (Transmit). Similarly, SBUF
register(receive) holds the serially inputted data received by RxD line of 8051. All though the
name is SBUF for both transmit and receive register; they are two independent registers with the
same name. The receive buffer SBUF is read only and cannot be written into. The transmit buffer
SBUF is write only and cannot be read. All read instruction to SBUF will access the receive register
and all write instruction to SBUF will access the transmit register
The moment the byte is written in SBUF, it is framed with start and stop bits and
transferred serially through TxD line.
Similarly, when bits are received serially via RxD it is de-framed by eliminating stop and start
bits and a byte of data is received and placed in SBUF
MODE 0
In mode 0 it acts as an 8bit shift register clocking data in and out at a baud rate of
1/12 of the oscillator frequency. Here eight data bits are transmitter in LSB first fashion. In this mode of
operation pin RXD is used for transmission as well as reception of the data. Whereas pin TXD is used to
provide the shift clock. Therefore mode 0 doesn’t support full duplex operation
This mode is useful for situations in which the microcontroller is used to interface to a serial device
such as an EEPROM that has a serial eight-bit interface format
MODE1
In mode 1 eight data bits are transmitted in a ten-bit frame: one start bit, eight data bits and one
stop bit as shown in fig. Here bits are transmitted through TXD pin and received through RXD pin. The
baud rate in this mode is variable and is controlled by the overflow rate of timer 1 in the auto reload mode.
This mode is suitable for communication with most serial devices including personal computers.
MODE2
Mode 2 operation causes data bits to transmit in an 11 bit frame as a low start bit, 8 data bits, a 9th
bit and a stop bit as shown in fig . user can define 9th bit with TB8 bit of SCON register for transmission
and RB8 bit of the SCON register for reception. The 9th bit is typically used for inter processor
communications. In mode 2, the baud rate is defined by the bit SMOD in PCON and the oscillator
frequency. the baud rate is 1/64 of the oscillator frequency when SMOD bit is cleared and is 1/32 of the
oscillator frequency when SMOD bit is set
MODE3
As in the case of mode 2, mode 3 also causes data transmission or reception in 11-bit frame through
pin TXD /RXD. In fact mode 3 is the same as mode 2 in all respects except the baud rate. The baud rate in
mode 3 is variable and is controlled by the overflow rate of timer 1 in the auto reload mode
RS232 STANDARDS
To allow the compatibility among data communication equipment made by various manufactures,
an interfacing standard called RS232 was set by Electronics Industries Association (EIA). This standard is
used in PCs and numerous types of equipment. Since this standard is set long before the advent of TTL
logic family its input and output voltage levels are not TTL compatible. In RS232 a 1 (HIGH)is represented
by -3 Vto -25V, while a 0 bit is represented by +3V-+25V, making -3 V- +3V undefined. For this reason,
to connect any RS232 to a microcontroller system we must use a voltage converter to convert the TTL logic
level to RS232 voltage level and vice versa. MAX232 IC chips are commonly used IC chips for this
purpose.
RS232 Pins
The original RS232 cable DB-25 has 25 pins. But all these pins were not used in PC cables. due to
this reason IBM introduced the DB-9 version of the serial I/O standard, which uses only 9 pins as shown in
fig above. The pin description of DB-9 is given in table
MAX232
The MAX232 has two sets of line drivers for transferring and receiving data as shown in fig above.
The line drivers used for TxD are called T1 and T2(, while the line drivers used for RxD are called R1 and
R2.Both RxD and TxD driver have input and output pins designated as T1 IN, T2IN, T1OUT, T2OUT for TxD
driver and R1IN , R2IN, R1OUT, R2OUT for RXD ddriver In many applications only one of each is used.
MAX232 requires four capacitors ranging from 0.1 to 22 uf to to provide more stable voltage. The most
widely used value for these capacitors is 22uf
8051 CONNECTION TO RS232
In connection of 8051 to RS232 T1 and R1 are used together for TxD and RxD and second is left
un used as shown in fig (b). Here the T1IN (pin11) of Max232 is connected to the TxD pin of the
microcontroller, while T1OUT (p in14) is connected to RxD pin of the RS232 DB connector. The R1IN (pin13)
of Max232 is connected to the TxD pin of the RS232 DB connector., while R1 OUT (pin12) is connected to
RxD pin of the micro controller