⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 外部中断的使用.c

📁 单片机的实用程序代码
💻 C
字号:
/********************************************************************
* 文件名  : 外部中断的使用.c
* 描述    :  外部中断,是单片机最基本也是最重要的功能。
			 外部中断0端口P3.2按键,数码管加一。
			 外部中断1端口P3.3按键,数码管减一。
* 创建人  : 东流,2009年4月9日
* 版本号  : 2.0
***********************************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint  unsigned int

sbit KEY1 = P3^2;
sbit KEY2 = P3^3;

uchar Count = 0;
uchar code table[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

/********************************************************************
* 名称 : Delay()
* 功能 : 延时,延时时间为 10ms * del
* 输入 : del
* 输出 : 无
***********************************************************************/
void Delay(uint del)
{
	uint i,j;
	for(i=0; i<del; i++)
	for(j=0; j<1827; j++)    
	;
}

/********************************************************************
* 名称 : Outside_Init()
* 功能 : 外部中断0,1 的初始化
* 输入 : 无
* 输出 : 无
***********************************************************************/
void Outside_Init(void)
{
	EX0 = 1;  //开外部中断0
	IT0 = 1;  //负边沿触发

	EX1 = 1;  //开外部中断1
	IT1 = 1;  //负边沿触发

	EA = 1;	  //开总中断
}

/********************************************************************
* 名称 : Outside_Int1()
* 功能 : 外部中断0 的中断处理
* 输入 : 无
* 输出 : 无
***********************************************************************/
void Outside_Int1(void) interrupt 0	using 1
{
	Delay(2);
	if(KEY1 == 0)
	{
		Count++;
	}
	Delay(30);	
}
/********************************************************************
* 名称 : Outside_Int2()
* 功能 : 外部中断1 的中断处理
* 输入 : 无
* 输出 : 无
***********************************************************************/
void Outside_Int2(void) interrupt 2	using 1
{
	Delay(2);
	if(KEY2 == 0)
	{
		Count--;
	}
	Delay(30);
}
/********************************************************************
* 名称 : Main()
* 功能 : 外部中断试验主程序
* 输入 : 无
* 输出 : 无
***********************************************************************/
void Main(void)
{
	Outside_Init();
	while(1)
	{
		P0 = table[Count % 10];
		Delay(2);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -