ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Mikroc Serial Interrupt In 8051
    카테고리 없음 2020. 3. 6. 11:20
    1. Interrupt Priority In 8051

    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.

    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.

    Interrupt Priority In 8051

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

    Mikroc Serial Interrupt In 8051

    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.

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

    MICROCONTROLLER IMPLEMENTATIONSThe final stage of a digital control system design is the implementation of the controller algorithm (set of difference equations) on a digital computer. In this section, we shall explore the implementation of digital controller algorithms on PIC microcontrollers. The PIC 16F877 microcontroller will be used in the examples since this microcontroller has a built-in A/D converter and a reasonable amount of program memory and data memory. There are many other microcontrollers in the PIC family with built-in A/D converters, and in general any of these can be used since the operation of microcontrollers in the PIC family with similar features is identical.Microcontrollers have traditionally been programmed using the assembly language of the target hardware. Assembly language has several important disadvantages and is currently less popular than it used to be. One important disadvantage is that the code generated using the assembly language can only run on the specific target hardware.

    For example, the assembly program developed for a PIC microcontroller cannot be used, say, on an Intel 8051 micro- controller. Assembly language programs also tend to be more difficult to develop, test and maintain.In this section, the Hi-Tech PICC language as described in Chapter 4 is used in the imple- mentation of the algorithms. As described in Section 1.6 there are several methods that can be used to implement the controller algorithm.

    One of the most common, which has the advantage of accurate implementation, is the use of a timer interrupt to generate the required loop delay (or the sampling interval). In this method the software consists of two parts: the main pro- gram and the interrupt service routine. As shown in Figure 10.15(a) Figure 10.15, in the main program various variables, as well as the A/D converter and the timer interrupt mechanism, are initialized.

    The timer is set to interrupt at an interval equivalent to the sampling interval of the required digital controller. The main program then enters a loop waiting for the timer interrupts to occur. Whenever a timer interrupt occurs the program jumps the interrupt serviceroutine (ISR) as shown in Figure 10.15(b), and it is within this routine that the actual controller algorithm is implemented. The error signal is obtained by calculating the difference between the reference values and measured values.

    The algorithm is then implemented and the output sample for the current sampling time is obtained. A preprocessing step is then performed by updating the variables for the next sample. On return from the ISR, the program waits in the main program until the next sampling interval, and the above process repeats. 10.5.1 Implementing Second-Order ModulesIn Section 10.2 we saw how a second-order module can be realized using adders, multipliers and delay elements. The second-order module is shown in Figure 10.16. The difference equations describing such a module are (10.19) and (10.20).

    If we letThe implementation of the second-order module is shown as a flow-chart in Figure 10.17. This figure does not show the initialization of the variables T1, T2, r1, r2, the A/D initialization,the I/O port initialization, etc.; it only shows the interrupt service routine.

    A PIC microcon- troller program for the implementation of a second-order module is given in the following example. Example 10.6The circuit diagram of a digital control system is shown in Figure 10.18. A PIC16F877 micro- controller is to be used as the digital controller in this system. Assume that the set-point input s is to be hard-coded to the program, and the output y is analog and connected to A/D channel AN0 (bit 0 of port A) of the microcontroller. The microcontroller is assumed to operate with a crystal frequency of 4 MHz as shown in the figure. The output (port B) of the microcontroller is interfaced to a D/A converter which acts as a zero-order-hold and generates an analog output to drive the plant.SolutionThe controller hardware is based on a PIC16F877 microcontroller.

    The microcontroller is operated from a 4 MHz crystal, connected to OSC1 and OSC2 inputs. With a 4 MHz crystal, the basic clock rate of the microcontroller is 1µs (the crystal frequency is divided by 4 to obtain the basic timing rate). The analog output of the plant ( y) is connected to A/D converter channel AN0 of the microcontroller. Similarly, port B digital outputs of the microcontroller are connected to an AD7302 type D/A converter.

    The operation of this D/A converter is very simple. Digital data is applied to eight inputs D0–D7, while the write control input, WR, is at logic high.

    The analog data appears at the output after the WR input is lowered to logic low. The WR input of the D/A converter is controlled from port pin RC0 of the microcontroller. The D/A converter is set to operate with a full-scale reference voltage of +5 V. The resolution of the converter is 8 bits, i.e. There are 28 = 256 quantization levels.

    With a full-scale reference voltage of +5 V,the resolution is 5000/256 = 19.53 mV, i.e. The digital bit pattern ‘00000001’ corresponds to 19.53 mV, the bit pattern ‘00000010’ corresponds to 2 × 19.53 = 39.06 mV and so on.The first program attempt is shown in Figure 10.19. Comments are used to describe operation of various parts of the program. The program consists of the main program and the functions: Initialize AD, Initialize Timer, Read AD Input, and ISR.Main program. The coefficients of the controller are defined at the beginning of the main program. Also, the A/D converter and the timer initialization functions are called here.

    The main program then enables global interrupts and enters an endless loop waiting for timer interrupts to occur.ISR. This is the interrupt service routine.

    The program jumps to this function every 10 ms. The function reads a sample, and calculates the error term e k. The output value y k is then calculated and sent to the D/A converter.

    In the final part of the ISR, the variables are updated for the next sample, and the timer interrupt is re-enabled.Initialize Timer. This function initializes the timer TMR0 so that timer interrupts can be generated at 10 ms intervals.

    As described in Chapter 3, the timing interval depends on the clock frequency, the pre-scaler value, and the data loaded into the TMR0 register. It can be shown that the timing interval is given by:Initialize AD. This function initializes the A/D converter so that analog data can be received from channel AN0 of the microcontroller.

    As described in Chapter 3, the A/D converter is initialized by first selecting the reference voltage and the output data format using the ADCON1 register, and then selecting the A/D converter clock using the ADCON0 register. Thus, channel AN0 is configured by loading 0x8E into the ADCON1 register, and 0x41 into the ADCON0 register. Thus, the Initialize AD function is programmed as follows:Read AD Input. This function starts the A/D converter and reads a sample at the end of the conversion. The conversion is started by setting the GO/DONE bit of ADCON0. The program should then wait until the conversion is complete, which is indicated by the GO/DONE bit becoming zero.

    The high 2 bits of the converted data are in ADRESH, and the low 8 bits are in register ADRESL. The 10-bit converted data is extracted and stored in variable y k.Implementing First-Order ModulesIn Section 10.2 we saw how a first-order module can be realized using adders, multipliers and delay elements. The first-order module is shown in Figure 10.21. The difference equations describing a first-order module were found to beThe implementation of the first-order module is similar to the second-order module and an example is given below.Example 10.7The circuit diagram of a digital control system is shown in Figure 10.18.

    A PIC16F877 micro- controller is to be used as the digital controller in this system. Assume that the set-point input s is to be hard-coded to the program, and the output y is analog and connected to A/D channel AN0 (bit 0 of port A) of the microcontroller. The microcontroller is assumed to operate with a crystal frequency of 4 MHz as shown in the figure. The output (port B) of the microcontroller is interfaced to a D/A converter which acts as a zero-order-hold and generates an analog output to drive the plant.Assume that the digital controller to be implemented is in the form of a first-order module, and write a program in C to implement this controller. The controller parameters are assumed to be:SolutionThe implementation of a first-order module is very similar to a second-order module. The program to implement a first-order controller is shown in Figure 10.22.

    The operation of the program is very similar to the second-order program and is not described here. Implementing Higher-Order ModulesHigher-order controllers can be implemented by cascading first-order and second-order mod- ules. For example, a fourth-order controller can be implemented by cascading two second-order modules, as shown in Figure 10.23.

Designed by Tistory.