当前位置:网站首页>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();
	}
}

原网站

版权声明
本文为[LCH Nan'an]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211407532433.html