当前位置:网站首页>Mcu-08 interrupt system and external interrupt application
Mcu-08 interrupt system and external interrupt application
2022-06-24 18:11:00 【LCH Nan'an】
reference :【 New reminder 】【 Blue Bridge Cup MCU 07】 Thoroughly understand 51 MCU interrupt system - - 21ic Electronic technology development forum
Interrupt definition :
You're chasing a TV show 《 The Brave Archer and His Mate 》, When I was fascinated , The phone rings , You pause the TV show , Answer the phone , In the process of answering the phone , The doorbell rang again , You put the phone down for a while , Go and open the door . If you are chasing a TV series Execute main program , So the phone is Interrupt source , When the phone rings, it's just Interrupt request , Pause TV is Site protection , Just answer the phone Interrupt response , The doorbell rang Higher level interrupt request , Go and open the door , That's it Break nesting . Come back after opening the door and talk on the phone , That is Interrupt return , After answering the phone, pause the TV play and open it Scene recovery .
There are two main ways of interaction between kernel and peripherals : polling and interrupt . Polling seems fair , But the actual efficiency is very low , And can't respond to emergencies in a timely manner ; The interrupt system makes the kernel have the ability to deal with emergencies .
Interrupts have a feature , You don't know when the interruption will happen . therefore , Each interrupt needs to have a Interrupt entry address , Has become a Interrupt vector . such , No matter when the interruption occurs , It has a definite starting point for program execution . After interrupt response , The program executed , We call it Interrupt service function , That is, this function is dedicated to the interrupt service .
Interrupt related register :
Interrupt related registers are 4 individual , Each register is OK Bit addressing Of , This brings convenience to the programming . among 2 One is the control register :IE register And IP register :

in addition 2 An interrupt request flag :TCON register And SCON register :

In general , There are two interrupt handling functions , One is Interrupt initialization function , The second is Interrupt service function . The initialization function is an ordinary function , The interrupt service function has Special format requirement :
<1> The interrupt function has no return value , Nor can it take parameters .
<2> The function name should be followed by a keyword interrupt, It shows that this is an interrupt service function .
<3> In keywords interrupt Keep up with Interrupt number , It indicates that this interrupt service function serves that interrupt .
Interrupt service function Format by :
void Function name () interrupt Interrupt number
{ ---- The body of the function ---- }
We're going to use a timer 0 For interval timing , Interrupt program architecture we C Language can write like this :

subject :

Circuit diagram :

Blue Bridge Cup MCU simulation schematic diagram : Blue Bridge Cup MCU simulation schematic diagram .pdsprj- Handout document resources -CSDN library
Code :
#include<reg52.h>
// Decoder definition
sbit HC138_C =P2^7;
sbit HC138_B =P2^6;
sbit HC138_A =P2^5;
//LED Definition
sbit L1 =P0^0;
sbit L8 =P0^7;
// Only control is used LED(Y4) The latch of
void SwitchHC138(unsigned char channel)
{
switch(channel)
{
case 4:
HC138_C= 1;
HC138_B= 0;
HC138_A= 0;
break;
case 5:
HC138_C= 1;
HC138_B= 0;
HC138_A= 1;
break;
}
}
void DelayL(unsigned int t)
{
while(t--);
while(t--);
}
//L1 Flicker function
void Working()
{
SwitchHC138(4);
L1 = 0; // Lighten up
DelayL(60000);
DelayL(60000);
L1 =1; // Extinguish
DelayL(60000);
DelayL(60000);
}
// ==========================
// Interrupt initialization
void Init_INT0()
{
IT0 = 1;
EX0=1;
EA=1;
}
// Interrupt service function ( Method 1 : Press the key L8 Lighten up , meanwhile L1 It will also light up )
//void Service1_INT0() interrupt 0
//{
// L8 = 0;
// DelayL(60000);
// DelayL(60000);
// L8 = 1;
// DelayL(60000);
// DelayL(60000);
//
//}
// ==========================
// Interrupt service function ( Method 2 : Use the flag quantity to light up L8, This method is based on Working After the function is executed, execute Light function )
unsigned char flag =0;
void Service2_INT0() interrupt 0
{
flag = 1;
}
void Light()
{
if(flag == 1)
{
L8 = 0;
DelayL(60000);
DelayL(60000);
L8 = 1;
DelayL(60000);
DelayL(60000);
}
flag =0;
}
// ==========================
void Init()
{
SwitchHC138(5);
P0 = 0x00;
DelayL(100);
SwitchHC138(4);
P0 = 0xff;
}
void main()
{
Init();
Init_INT0();
while(1)
{
Working();
Light();
}
}边栏推荐
- Tencent cloud layer 7 load balancing log analysis and monitoring
- System Verilog - randomize
- Recommend 14 commonly used test development tools
- Easynvr fails to use onvif to detect the device. What is the reason why "no data" is displayed?
- Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
- Install MySQL using Yum for Linux
- 电子元器件行业B2B电商市场模式、交易能力数字化趋势分析
- Fragment usage
- Specification for self test requirements of program developers
- 你知道CMDB吗?
猜你喜欢

C language - structure II

How can an enterprise successfully complete cloud migration?

How does the chief information security officer discuss network security with the enterprise board of directors

Three indicators to help you measure the effectiveness of digital transformation

(Video + graphics) introduction to machine learning series - Chapter 11 support vector machines

Two micro service interviews where small companies suffer losses

He "painted" what a smart city should look like with his oars

Five skills of selecting embedded programming language

How to decompile APK files

Specification for self test requirements of program developers
随机推荐
How to create simple shapes in illustrator 2022
Common GCC__ attribute__
On N handshakes and M waves of TCP
How to start cloud native application development
Selection (032) - what is the output of the following code?
13 ways to reduce the cost of cloud computing
EasyPlayer流媒体播放器播放HLS视频,起播速度慢的技术优化
腾讯云荣获“可信云技术最佳实践-虚拟化”
Bigdecimalavoiddoubleconstructorrule: do not directly use the double variable as a parameter to construct BigDecimal
Specification for self test requirements of program developers
High quality defect analysis: let yourself write fewer bugs
Millions of dollars worth of NFT were stolen in the attack, and Google issued an emergency warning to 3.2 billion users worldwide | February 21 global network security hotspot
Go collaboration and pipeline to realize asynchronous batch consumption scheduling task
Continue to help enterprises' digital transformation -tce has obtained the certification of the first batch of digital trusted service platforms in China
Setting the Arduino environment for tinyml experiments
国家出手了!对知网启动网络安全审查
Explanation of MySQL indexing principle
Redpacketframe and openmode packages
Uncover the secrets of Tencent R & D! 30% of the demand will be responded within 1 day!
[NLP] 3 papers on how Stanford team builds a better chat AI