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);
}
}
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
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Fundamentals and Applications of Single Chip Microcomputers (Edited by Zhang Liguang and Chen Zhongxiao)
- Single chip microcomputer control technology (Li Shuping, Wang Yan, Zhu Yu, Zhang Xiaoyun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- F28335 uses external SRAM for program simulation
- Looking for experts in Helmholtz coil magnetic field
- Do you know about the programmer and emulator of msp430?
- How to use the VGA_SYNC_N signal in the VGA of DE1-SOC?
- ATA-1000 Series Broadband Amplifier & ATA-2000 Series High Voltage Amplifier Series Comparison
- CCS Tips: Remove the prompt when burning DSP/BIOS
- SEED-DEC6416 is available for purchase, second-hand is also acceptable
- Calculation of stack usage in C2000 DSP
- IRLR024 Output Current
- Bidirectional thyristor circuit wiring problem