Microcontroller experiment: How to test the external interrupt system to turn on the 1357 and 2468 lights alternately

Since the PPT in class is not easy to upload, please see my Notion for detailed notes:

        https://www.notion.so/7259b6dac1ff4e878fd1cb12ea360b92

#include "reg51.h"
#include<intrins.h>  //可以使用_nop_函数的头文件
#define uchar unsigned char

const tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f} ; //正向流水灯

uchar flag;

void delay(int x){	 //延时程序
   unsigned i,j,k;
   while(x--){
		for(i=0;i<0x01;i++)
    	for(j=0;j<0xff;j++)
      	for(k=0;k<0xff;k++);
   }
}

//延时1s:
void Delay1000ms()		//@11.0592MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 43;
	j = 6;
	k = 203;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void int1()interrupt 0 //外部中断0的中断服务函数
{
    flag=0x01;
}

void int2()interrupt 2 //外部中断1的中断服务函数
{
    flag=0x02;
}


void main(){

	uchar i;
	EA=1;
	        //总中断允许
	EX0=1;
	        //允许外部中断0中断
	EX1=1;
	        //允许外部中断1中断
	IT0=1;
	        //选择外部中断0为跳沿触发方式
	IT1=1;
	        //选择外部中断1为跳沿触发方式
	P2=0xff;
	
	while(1)
	{
	  switch(flag){
	    case 1:
			for(i=0;i<8;i++){
	     	if(flag==0x02) break;
	       	P2=tab[i];
	       	delay(1);
	       	}
	     	if(flag==0x02) break;
	     	P2=0x00;
	     	delay(1);
	      	break;
		case 2:
			
		//功能:要让1357灯亮,2468灯灭
		  	
			P2 = 0xaa;
			Delay1000ms();
			P2 = 0x55;
			Delay1000ms();

			break;			

		//test:  success!
		//拓展:所有灯全亮,过1s,全灭
		/*
			P2=0x00;
			delay(1);
			P2=0xff;
			delay(1);
			break;
		*/
			
	    }
	}
  }

Guess you like

Origin blog.csdn.net/CaptainDrake/article/details/123695835