Design 2: 51 microcontroller external interrupt control

Table of contents

1. Design content

2. Interrupt related knowledge

1. 51 MCU interrupt source

2. Interrupt system special function register

3. Difference between interrupt function and function call

3. Schematic diagram of simulation

4. Programming

5. Simulation results

6. Thinking questions

The author has something to say


1. Design content

       This design uses 2 buttons. When no button is pressed, the bottom LED lights up; when KEY1 is pressed, external interrupt 0 requests an interrupt, and the LED is controlled to light up 3 times from bottom to top; when KEY2 Press, the external interrupt 1 requests an interrupt, and the control LED flashes 3 times. (It is required that the priority of external interrupt 1 is higher than that of external interrupt 0, that is, the action of the water lamp can be interrupted after pressing KEY2, and when the processing of external interrupt 1 is completed, the processing of external interrupt 0 will be resumed, and the interrupted one can be interrupted last time. LED starts to cycle).

2. Interrupt related knowledge

1. 51 MCU interrupt source

interrupt source

interrupt number

entry address

External interrupt 0

0

0003H

External interrupt 1

2

0013H

Timer T0

1

000BH

Timer T1

3

001BH

serial interrupt

4

0023H

2. Interrupt system special function register

     Interrupt enable register IE, timer/counter T0/T1 control register TCON, timer/counter T2 control register T2CON, serial port control register SCON

3. Difference between interrupt function and function call

      The interrupt function cannot have a return value, the interrupt function cannot pass parameters, and the interrupt function program should be short and concise

3. Schematic diagram of simulation

4. Programming

/*******************************************************************************
* 函 数 名  				: main
* 函数功能		      : 外部中断
* 创作者  					: 薄情书生
* 创作时间  				: 2022.9.27
*******************************************************************************/
main.c文件

#include "reg52.h"         
#include "led.h"
#include "exit.h"

/*************38译码器**************/
void SelectHC138(unsigned char channel)
{
   switch(channel)
	 {
		 case 0:
        P1 = (P1 & 0x1f)|0x00;break;    //发光二极管(LEDS0)
   }
}

/*************主函数******************/
void main()
{
  SelectHC138(0);
	Exint_Init();
	while(1)
	{
		P0 = 0x7f;
	}
}


delay.c文件

#include "delay.h"

/*********毫秒延时*********/
void delay_ms(u16 ms)
{
	u16 x,y;
	for(x=ms;x>0;x--)
		for(y=114;y>0;y--);
}


led.c文件

#include "led.h"
#include "reg52.h"
#include "delay.h"
#include "intrins.h"

/*************LED由下至上**************/
void Bottom_Up()
{
	u8 i,j;
	for(j=0;j<3;j++)
	{
		for(i=0,P0 = 0xff;i<8;i++)
		{
			P0 = P0<<1;	 
			delay_ms(200); 
		}
	}
	P0 = 0xff;
}

/*************LED由上至下**************/
void Up_Bottom()
{
	u8 i,j;
	for(j=0;j<3;j++)
	{
		for(i=0,P0 = 0xff;i<8;i++)
		{
			P0 = P0>>1;	 
			delay_ms(100); 
		}
	}
	P0 = 0xff;
}

/*************LED齐闪烁**************/
void LED_twinkle()
{
	u8 i,j;
	for(j=0;j<3;j++)
	{
		for(i=0,P0 = 0x00;i<3;i++)
		{
			P0 = ~P0;	 
			delay_ms(500); 
		}
	}
	P0 = 0xff;
}

/*************LED保持3s**************/
void Hold_3s()
{
	P0 = 0x00;	 
	delay_ms(3000); 
	P0 = 0xff;
}

/*************两边至中间**************/
void LED_sides()
{
	u8 i,j,ri,lf;
	for(j=0;j<3;j++)
  {
		for(i=0,ri=0x7f,lf=0xfe;i<4;i++)
		{
			P0 = ri&lf;
			ri = _cror_(ri,1);
			lf = _crol_(lf,1);
			delay_ms(1000);
		}
  }
	P0 = 0xff;
}

/*************中间至两边**************/
void LED_middle()
{
	u8 i,j,ri,lf;
	for(j=0;j<3;j++)
	{
		for(i=0,ri=0xef,lf=0xf7;i<4;i++)
		{
			P0 = ri&lf;
			ri = _crol_(ri,1);
			lf = _cror_(lf,1);
			delay_ms(1000);
		}
	}
	P0=0xff;
}


exit.c文件

#include "exit.h"
#include "reg52.h"
#include "led.h"

/*********外部中断0、1初始化***********/
void Exint_Init()
{
	IT0 = 0;
	IT1 = 0;
	PX0 = 0;
	PX1 = 1;
	EA = 1;
	EX0 = 1;
	EX1 = 1;
}

/*********外部中断0服务函数***********/
void Exint0_Service() interrupt 0
{
	Up_Bottom();
}

/*********外部中断1服务函数***********/
void Exint1_Service() interrupt 2
{
	LED_twinkle();
}

5. Simulation results

When no button is pressed, the bottom LED lights up.

 KEY1 is pressed, external interrupt 0 requests an interrupt, and the LED is controlled to light up 3 times from bottom to top.

 KEY2 is pressed, external interrupt 1 requests an interrupt, and the control LED flashes 3 times.

After KEY2 is pressed, the action of KEY1 water lamp can be interrupted. After external interrupt 1 is processed, the processing of external interrupt 0 will be resumed, and the cycle can start from the LED that was interrupted last time.

6. Thinking questions

Please modify the program, set the priorities of INT0 and INT1 to 0, press KEY2 first, and then press KEY1 during the blinking of the LED to see if it can interrupt the blinking of the LED? what is the reason? The reference program for thinking questions will be given in Design 3: 51 MCU Nixie Tube Display Control.

The author has something to say

This column is a practical tutorial of 51 single-chip microcomputer, which aims to promote learning through practice , and help friends quickly get started with 51 single-chip microcomputer. Please learn the corresponding theoretical knowledge of 51 single-chip microcomputer by yourself. The relevant theoretical knowledge involved in this design: external interrupt control of 51 single-chip microcomputer, the use of independent buttons, modular programming , please refer to the materials to learn by yourself.

This design source program and simulation source files Baidu network disk link: Link: https://pan.baidu.com/s/1TNwn74gbOoMzb3Wcqopvrg 
Extraction code: vga5 
 

Design 1: 51 single-chip microcomputer control running water lamp Thinking question reference source program and simulation source file Baidu network disk link: https://pan.baidu.com/s/1bdtvHcNLI8ydR_t94DWQXw 
Extraction code: gnv6 

Guess you like

Origin blog.csdn.net/weixin_53402301/article/details/130978988