Mikroc Serial Interrupt In 8051

Universal Asynchronous Receiver/Transmitter or UART for short represents the hardware - integrated circuit, used for the serial communication. Function serialinit is the same as before but note that the serial port interrupts are enabled (EA = 1 and ES = 1). Function sendserial sends a null-terminated string to the serial output port. Similarly, function sendlchar sends a single character to the serial port. Serial data is read in via the serial port interrupt service routine (serial). Interrupt Control. The standard 8051 and AT89C2051 provide six interrupt sources: Each interrupt is assigned a fixed location in memory and an interrupt causes the CPU tojump tothat location, where it executes the interrupt service routine. Table 1.5 gives the interrupt sources and the start of their service routines in memory.

One of the microcontroller features making it so powerful is an integrated UART, better known as a serial port. It is a full-duplex port, thus being able to transmit and receive data simultaneously and at different baud rates. Without it, serial data send and receive would be an enormously complicated part of the program in which the pin state is constantly changed and checked at regular intervals. When using UART, all the programmer has to do is to simply select serial port mode and baud rate.

When it's done, serial data transmit is nothing but writing to the SBUF register, while data receive represents reading the same register. The microcontroller takes care of not making any error during data transmission. Serial port must be configured prior to being used. In other words, it is necessary to determine how many bits is contained in one serial “word”, baud rate and synchronization clock source.

The whole process is in control of the bits of the SCON register (Serial Control).Serial Port Control (SCON) Register. SM0 - Serial port mode bit 0 is used for serial port mode selection. SM1 - Serial port mode bit 1. SM2 - Serial port mode 2 bit, also known as multiprocessor communication enable bit.

When set, it enables multiprocessor communication in mode 2 and 3, and eventually mode 1. It should be cleared in mode 0. REN - Reception Enable bit enables serial reception when set.

When cleared, serial reception is disabled. TB8 - Transmitter bit 8. Since all registers are 8-bit wide, this bit solves the problem of transmiting the 9th bit in modes 2 and 3. It is set to transmit a logic 1 in the 9th bit.

RB8 - Receiver bit 8 or the 9th bit received in modes 2 and 3. Cleared by hardware if 9th bit received is a logic 0. Set by hardware if 9th bit received is a logic 1. TI - Transmit Interrupt flag is automatically set at the moment the last bit of one byte is sent. It's a signal to the processor that the line is available for a new byte transmite. It must be cleared from within the software.

RI - Receive Interrupt flag is automatically set upon one byte receive. It signals that byte is received and should be read quickly prior to being replaced by a new data. This bit is also cleared from within the software.As seen, serial port mode is selected by combining the SM0 and SM2 bits:SM0SM1MODEDESCRIPTIONBAUD RATE0008-bit Shift Register1/12 the quartz frequency0118-bit UARTDetermined by the timer 11029-bit UART1/32 the quartz frequency (1/64 the quartz frequency)1139-bit UARTDetermined by the timer 1. TRANSMIT - Data transmit is initiated by writing data to the SBUF register.

In fact, this process starts after any instruction being performed upon this register. When all 8 bits have been sent, the TI bit of the SCON register is automatically set. RECEIVE - Data receive through the RXD pin starts upon the two following conditions are met: bit REN=1 and RI=0 (both of them are stored in the SCON register). When all 8 bits have been received, the RI bit of the SCON register is automatically set indicating that one byte receive is complete. Since there are no START and STOP bits or any other bit except data sent from the SBUF register in the pulse sequence, this mode is mainly used when the distance between devices is short, noise is minimized and operating speed is of importance.

A typical example is I/O port expansion by adding a cheap IC (shift registers 74HC595, 74HC597 and similar).Mode 1. TRANSMIT - Data transmit is initiated by writing data to the SBUF register. End of data transmission is indicated by setting the TI bit of the SCON register. RECEIVE - The START bit (logic zero (0)) on the RXD pin initiates data receive.

The following two conditions must be met: bit REN=1 and bit RI=0. Both of them are stored in the SCON register. The RI bit is automatically set upon data reception is complete. Mode 3Mode 3 is the same as Mode 2 in all respects except the baud rate. The baud rate in Mode 3 is variable. The parity bit is the P bit of the PSW register. The simplest way to check correctness of the received byte is to add a parity bit to it.

8051

Simply, before initiating data transmit, the byte to transmit is stored in the accumulator and the P bit goes into the TB8 bit in order to be “a part of the message”. The procedure is opposite on receive, received byte is stored in the accumulator and the P bit is compared with the RB8 bit. If they are the same- everything is OK! Baud RateBaud Rate is a number of sent/received bits per second. In case the UART is used, baud rate depends on: selected mode, oscillator frequency and in some cases on the state of the SMOD bit of the SCON register.

All the necessary formulas are specified in the table:BAUD RATEBITSMODMode 0Fosc. / 12Mode 11 Fosc. 16 12 (256-TH1)BitSMODMode 2Fosc. / 641 0Mode 31 Fosc.

16 12 (256-TH1)Timer 1 as a clock generatorTimer 1 is usually used as a clock generator as it enables various baud rates to be easily set. The whole procedure is simple and is as follows:. First, enable Timer 1 overflow interrupt. Configure Timer T1 to operate in auto-reload mode. Depending on needs, select one of the standard values from the table and write it to the TH1 register. That's all.BAUD RATEFOSC. (MHZ)BIT SMOD4.040 h30 h00 h0300A0 h98 h80 h75 h52 h0600D0 hCC hC0 hBB hA9 h01200E8 hE6 hE0 hDE hD5 h02400F4 hF3 hF0 hEF hEA h04800F3 hEF hEF h14800FA hF8 hF5 h09600FD hFC h09600F5 h119200FD hFC h138400FE h176800FF h1Multiprocessor CommunicationAs you may know, additional 9th data bit is a part of message in mode 2 and 3.

Mikroc

It can be used for checking data via parity bit. Another useful application of this bit is in communication between two or more microcontrollers, i.e. Multiprocessor communication. This feature is enabled by setting the SM2 bit of the SCON register. As a result, after receiving the STOP bit, indicating end of the message, the serial port interrupt will be generated only if the bit RB8 = 1 (the 9th bit). This is how it looks like in practice: Suppose there are several microcontrollers sharing the same interface. Each of them has its own address.

An address byte differs from a data byte because it has the 9th bit set (1), while this bit is cleared (0) in a data byte. When the microcontroller A (master) wants to transmit a block of data to one of several slaves, it first sends out an address byte which identifies the target slave. An address byte will generate an interrupt in all slaves so that they can examine the received byte and check whether it matches their address. Of course, only one of them will match the address and immediately clear the SM2 bit of the SCON register and prepare to receive the data byte to come.

Interrupts In 8051 Microcontroller Ppt

Interrupt

Uart

Other slaves not being addressed leave their SM2 bit set ignoring the coming data bytes.