• 8051 provides 4 interrupt sources
  • 2 external interrupts
  • 2 timer interrupts
  • They are controlled via two SFRs, IE and IP
  • Each interrupt source can be individually enabled or disabled by setting or clearing a bit in IE (Interrupt Enable). IE also exists a global disable bit, which can be cleared to       disable all interrupts at once
  • Each interrupt source can also be individually set to one of two priority levels by setting or clearing a bit in IP (Interrupt Priority)
  • A low-priority interrupt can be interrupted by high-priority interrupt, but not by another low-priority one
  • A high-priority interrupt can‟t be interrupted by any other interrupt source
  • If interrupt requests of the same priority level are received simultaneously, an internal polling sequence determines which request is serviced, so within each priority lever there is a second priority structure
  • This internal priority structure is determined by the polling sequence, shown in the following table

 

External Interrupt

·        External interrupts ~INT0 and ~INT1 have two ways of activation

·        Level-activated

·        Transition-activated

·        This depends on bits IT0 and IT1 in TCON

·        The flags that actually generate these interrupts are bits IE0 and IE1 in TCON

·        On-chip hardware clears that flag that generated an external interrupt when the service routine is vectored to, but only if the interrupt was transition-activated

·        When the interrupt is level-activated, then the external requesting source is controlling the request flag, not the on-chip hardware

Handling Interrupt

  • When interrupt occurs (or correctly, when the flag for an enabled interrupt is found to be set (1)), the interrupt system generates an LCALL to the appropriate location in Program Memory, unless some other conditions block the interrupt
  • Several conditions can block an interrupt
  • An interrupt of equal or higher priority level is already in progress
  • The current (polling) cycle is not the final cycle in the execution of the instruction in progress
  • The instruction in progress is RETI or any write to IE or IP registers
  • If an interrupt flag is active but not being responded to for one of the above conditions, must be still active when the blocking condition is removed, or the denied interrupt will not be serviced
  • Next step is saving the registers on stack. The hardware-generated LCALL causes only the contents of the Program Counter to be pushed onto the stack, and reloads the PC with the beginning address of the service routine
  • In some cases it also clears the flag that generated the interrupt, and in other cases it doesn't. It clears an external interrupt flag (IE0 or IE1) only if it was transitionavtivated.
  • Having only PC be automatically saved gives programmer more freedom to decide how much time to spend saving other registers. Programmer must also be more careful with proper selection, which register to save.
  • The service routine for each interrupt begins at a fixed location. The interrupt locations are spaced at 8-byte interval, beginning at 0003H for External Interrupt 0, 000BH for Timer 0, 0013H for External Interrupt 1 and 001BH for Timer 1.

 

I/O Ports

  • The 8051 contains four I/O ports
  • All four ports are bidirectional
  • Each port has SFR (Special Function Registers P0 through P3) which works like a latch, an output driver and an input buffer
  • Both output driver and input buffer of Port 0 and output driver of Port 2 are used for accessing external memory
  • Accessing external memory works like this
  • Port 0 outputs the low byte of external memory address (which is timemultiplexed with the byte being written or read)
  • Port 2 outputs the high byte (only needed when the address is 16 bits wide)
  • Port 3 pins are multifunctional
  • The alternate functions are activated with the 1 written in the corresponding bit in the port SFR

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.

Timer SFR

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

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 compatability with its predecesor, 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 from 31, 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.

Again, there is very little reason to use this mode and it is only mentioned so you wont be surprised if you ever end up analyzing archaeic code which has been passed down through the generations (a generation in a programming shop is often on the order of about 3 or 4 months).

SFR Name Description SFR Address

TH0 Timer 0 High Byte 8Ch TL0 Timer 0 Low Byte 8Ah TH1 Timer 1 High Byte 8Dh TL1 Timer 1 Low Byte 8Bh TCON Timer Control 88h TMOD Timer Mode 89h

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 13-bit 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.

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.

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.

USING TIMERS AS EVENT COUNTERS 

We've discussed how a timer can be used for the obvious purpose of keeping track of

time. However, the 8051 also allows us to use the timers to count events. How can this be useful? Let's say you had a sensor placed across a road that would send a pulse every time a car passed over it. This could be used to determine the volume of traffic on the road. We could attach this sensor to one of the 8051's I/O lines and constantly monitor it, detecting when it pulsed high and then incrementing our counter when it went back to a low state. This is not terribly difficult, but requires some code. Let's say we hooked the sensor to P1.0; the code to count cars passing would look something like this: JNB P1.0,$ ;If a car hasn't raised the signal, keep waiting JB P1.0,$ ;The line is high which means the car is on the sensor right now INC COUNTER ;The car has passed completely, so we count it

Serial Communication

Some of the external I/0 devices receive only the serial data. Normally serial communication is used in the Multi Processor environment.8051 has two pins for serial communication.

  1. SID- Serial Input data. (2)SOD-Serial Output data.