• No results found

MOV SBUF, #’A’

In document Micro - 51 Eb(User) (Page 36-39)

Upon execution of the above instruction the 8051 will begin transmitting the character via the serial port. Obviously transmission is not instantaneous--it takes a measurable amount of time to transmit. And since the 8051 does not have a serial output buffer we need to be sure that a character is completely transmitted before we try to transmit the next character.

The 8051 lets us know when it is done transmitting a character by setting the TI bit in SCON. When this bit is set we know that the last character has been transmitted and that we may send the next character, if any. Consider the following code segment:

CLR TI ;Be sure the bit is initially clear

MOV SBUF, #’A’ ;Send the letter ‘A’ to the serial port LOOP:JNB TI, LOOP ;Pause until the TI bit is set.

The above three instructions will successfully transmit a character and wait for the TI bit to be set before continuing. The last instruction says "Jump if the TI bit is not set to LOOP, means "the same address of the current instruction." Thus the 8051 will pause on the JNB instruction until the TI bit is set by the 8051 upon successful transmission of the character.

ii. Reading From the Serial Port

To read a byte from the serial port one just needs to read the value stored in the SBUF (99h) SFR after the 8051 has automatically set the RI flag in SCON.

For example, if your program wants to wait for a character to be received and subsequently read it into the Accumulator, the following code segment may be used:

LOOP: JNB RI,LOOP ;Wait for the 8051 to set the RI flag MOV A,SBUF ;Read the character from the serial port

The first line of the above code segment waits for the 8051 to set the RI flag; again, the 8051 sets the RI flag automatically when it receives a character via the serial port. So as long as the bit is not set the program repeats the "JNB" instruction continuously.

Once the RI bit is set upon character reception the above condition automatically fails and program flow falls through to the "MOV" instruction which reads the value.

1.4.18 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. There are two ways to do that: interrupts or polling. In the interrupt method, whenever any device needs its service, the device notifies the microcontroller by sending it an interrupt signal. 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. Although polling can monitor the status of several devices and serve each of them as certain conditions are met, it is not efficient as far as microcontroller is concerned. The advantage of interrupt is that each device get the attention of the microcontroller based on the priority assigned to it. In interrupt method the time taken by the microcontroller is less when compared to polling.

8051 supports total of 6 interrupt sources, meaning that it can recognize up to 6 different events that can interrupt regular program execution. Each of these interrupts can be individually enabled or disabled by configuring the register IE. Clearing the bit EA in the same register can disable also whole system of interrupts.

i. INTERRUPT SERVICE ROUTINE

For every interrupt, there must be an interrupt service routine (ISR). When an interrupt is invoked, the microcontroller runs the interrupt service routine. For every interrupt, there is a fixed location in memory that holds the address of its ISR.

Interrupt ROM location

(HEX)

Pin

Reset

External hardware interrupt 0 (INT 0) Timer 0 interrupt (TF 0)

External hardware interrupt 1 (INT 1) Timer 1 interrupt (TF 1)

Serial COM interrupt (R1 and T1)

0000 0003 000B 0013 001B 0023 9 P3.2 (12) P3.3 (13)

ii. IE (Interrupt Enable)

Fig 4.14:Interrupt Enable Register

Following table describes the bits of register IE. Same rule applies to all bits - logical state of 1 enables the appropriate interrupt. ET2 is not used in 8051. It is available in derivatives of 8051 versions where Timer 2 is available.

Bit Purpose

EA Enables/disables all interrupt sources ET2 Timer T2 interrupt

ES UART and SPI interrupts ET1 Timer T1 interrupt

EX1 External interrupt: pin INT1 ET0 Timer T0 interrupt

EX0 External interrupt: pin INT0

To enable an interrupt, bit D7 of the IE register (IE) must be set to high to allow the rest of register to take effect. If EA* is high, interrupts are enabled and will be responded to if their corresponding bits in IE are high. If EA* is low, no interrupt will be responded to, even if the associated bit in the IE register is high.

1.4.19 INTERRUPT FLAG SFR REGISTER BIT

The list of all interrupt flags are listed below: Four of the interrupt flags are held in the TCON register while the SCON has the RI and TI flags.

Interrupt Flag SFR Register Bit

External 0 External 1 Timer 0 Timer 1 Serial port Timer 2 Timer 2 IE0 IE1 TF0 TF1 T1 TF2 EXF2 TCON.1 TCON.3 TCON.5 TCON.7 SCON.1 T2SCON.7 (For 89C51) T2SCON.6 (For 89C51)

iv. INTERRUPT PRIORITY

When the 8051 is powered up, the priorities are assigned according to the table listed.

In document Micro - 51 Eb(User) (Page 36-39)

Related documents