51 MCU Registers-3.4 MCU Serial Port Interrupt

Publisher:EnchantedMelodyLatest update time:2016-03-29 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Now let's see how to operate the serial communication interrupt. Let's review the timer interrupt mentioned earlier. When the TH0 and TL0 counts overflow, the hardware automatically sets the TF0 overflow flag to 1, notifying the CPU to request an interrupt. Serial communication is very similar. Assuming that the CPU is sending serial data, when a frame is sent, the hardware automatically sets TI to 1, notifying the CPU that a frame of data has been sent and waiting for the CPU's instructions, that is, the serial interrupt service program. In the same way, for the serial data reception interrupt, when a frame is received, the hardware automatically sets RI to 1, notifying the CPU that a frame of data has been received, and whether there are other instructions. As mentioned earlier, for interrupts, whether it is a serial data transmission interrupt or a serial reception interrupt, there are two switch controls, a main switch EA, and a sub-switch ES (for serial interrupts). Therefore, before using the serial interrupt, both switches must be turned on EA=1, ES=1. Below we will give an example for serial transmission and reception interrupts to illustrate.

Example 3-3-4: Study the UART sending interrupt. The microcontroller controls the LED0 to turn on and off. Every time the serial port sends a frame of data, it enters the interrupt service program and turns on and off another LED in the interrupt program.

According to the interrupt analysis steps mentioned above,

(1) What is the CPU busy with?

(2) What urgent matter has occurred?

(3) How to notify the CPU

(4) How does the CPU handle it?

The reference code is as follows:

#include "reg52.h"

typedef unsigned char uchar;

sbit LED0=P1^3;

sbit LED1=P1^5;

void delayUs(uchar t);

void delayMs(uchar t);

void main(void)

{

        

        SM0=0;

        SM1=1; //Uart working mode

        TMOD=0X20;

        TH1=TL1=253;//Baud=9600        

        TR1=1; //Timer starts running

        EA=1;

        ES=1;

        while(1)

        {

                LED0=~LED0;

       

            SBUF=0x38;

                delayMs(255);

                

        }

}

void uart_txd_interrupt(void) interrupt 4

{

        TI=0;

        LED1=~LED1;

        delayMs(255);

        delayMs(255);

}

void delayUs(uchar t)

{

        while(--t);

}

void delayMs(uchar t)

{

        while(--t)

        {

                 delayUs(245);

                delayUs(245);

        }

}

Example 3-3-4-1 Study the UART sending interrupt. The microcontroller controls the LED0 to turn on and off. Every time the serial port receives a frame of data, it enters the interrupt service program and turns on and off another LED in the interrupt program.

For the same reason, it is easy to write code, as shown below

#include "reg52.h"

typedef unsigned char uchar;

sbit LED0=P1^6;

sbit LED1=P1^7;

void delayUs(uchar t);

void delayMs(uchar t);

void main(void)

{

        uchar Uart_RXD_data;

        SM0=0;

        SM1=1;

        TMOD=0X20;

        TH1=TL1=253;

        TR1=1;

        REN=1;

        EA=1;

        ES=1;        

        while(1)

        {

                LED0=~LED0;

                Uart_RXD_data=SBUF;

                delayMs(245);

                delayMs(245);

        }

}

void uart_rxd_interrupt(void) interrupt 4

{

        RI=0;

        LED1=~LED1;

}

void delayUs(uchar t)

{

        while(--t);

}

void delayMs(uchar t)

{

        while(--t)

        {

                delayUs(245);

                delayUs(245);

                

        }

}

Reference address:51 MCU Registers-3.4 MCU Serial Port Interrupt

Previous article:51 MCU C language-4.1 data types
Next article:51 MCU Registers-3.3 MCU Serial Communication Sending and Receiving

Recommended ReadingLatest update time:2024-11-16 13:56

Microcontroller Learning Part 4: MCS-51 Microcontroller Pin Description
51 series microcontroller 89C51/89S51 both adopt 40-pin package dual-row direct DIP structure. In their 40-pin configuration, there are two positive power supply and ground lines, two clock lines of external quartz oscillator, 4 groups of 8 bits, a total of 32 I/O ports, and the P3 port line is multiplexed with the sec
[Microcontroller]
Microcontroller Learning Part 4: MCS-51 Microcontroller Pin Description
Design of timer program based on 51 single chip microcomputer
This timer design based on 51 single-chip microcomputer uses common cathode digital tube display. If you want to change to common anode digital tube, you only need to change the array in the program to the array of common anode digital tube. The 38 decoder LSA, LSB, LSC are connected to P2^2, P2^3, P2^4 of the single-
[Microcontroller]
Design of timer program based on 51 single chip microcomputer
51 microcontroller control stepper motor hardware connection part
1. Summary: This case explains the hardware connection part of the stepper motor controlled by the 51 microcontroller. Later, we will explain the microcontroller program, S-curve acceleration and deceleration method, host computer and other related content. 2. Functional schematic diagram: 2.1, 51 microcontroller: ①
[Microcontroller]
51 microcontroller control stepper motor hardware connection part
51 MCU read and write 2G SD card program
SD card is a new generation memory device based on semiconductor flash memory. SD was successfully developed in August 1999 and weighs only 2 grams. However, it has high memory capacity, fast data transfer rate, great mobility and good security. SD card is also easy to reformat and has a wide range of applications. Fo
[Microcontroller]
Briefly describe the structure and principle of 8051 microcontroller
The basic structure of the microcontroller includes six parts: central processing unit (CPU), memory, timer/counter, input and output interface, interrupt control system and clock circuit. 1. Basic composition Central processing unit (CPU) Including the arithmetic unit and the controller, it is the core of the mic
[Microcontroller]
Briefly describe the structure and principle of 8051 microcontroller
Use of 51 single chip microcomputer Keil C51 (C language)
Purpose: Initially master the operation and use of Keil (C51 language) and SUN ES59PA experiment instrument, and be able to input and run simple programs. Laboratory equipment: A set of SUN ES59PA experimental instrument, a computer with an RS232 serial port and Keil C51 installed. Experimental principle and en
[Microcontroller]
Use of 51 single chip microcomputer Keil C51 (C language)
51 single chip ultrasonic ranging program code
51 single chip ultrasonic ranging program code /Ultrasonic module ME007 display program //Crystal oscillator = 8M //MCU=STC10F04XE //P0.0-P0.6 common anode digital tube pin //Trig = P1^0 //Echo = P3^2 #include reg52.h //Include a 52 standard kernel header file #define uchar unsigned char //define for eas
[Microcontroller]
High-speed control scheme for large-screen LED display using MCS51 single-chip microcomputer
introduction The basic working principle of LED display screen is dynamic scanning. The display control process is to first read the font data from the data storage, then write the data to the LED dot matrix through the serial port or parallel port of the microcontroller, and then scan it. Compared with th
[Microcontroller]
High-speed control scheme for large-screen LED display using MCS51 single-chip microcomputer
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号