这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » STC51单片机实例之14外部中断的使用

共4条 1/1 1 跳转至

STC51单片机实例之14外部中断的使用

高工
2014-05-17 01:26:18     打赏
/*****************************外部中断0的电平触发**************************
* 描述    :  外部中断,是单片机最基本也是最重要的功能。
			 P3.2单片机外部中断0的引脚和外部中断0。
			 每当按下S19端口的时候,数码管的值会加1.

***********************************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint  unsigned int

sbit KEY1 = P3^2;

uchar Count = 100;
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 = 0;  //电平触发 
	EA = 1;	  //开总中断
}

/********************************************************************
* 名称 : Outside_Int1()
* 功能 : 外部中断0 的中断处理
* 输入 : 无
* 输出 : 无
***********************************************************************/
void Outside_Int1(void) interrupt 0	using 1
{
	Delay(2);
	if(KEY1 == 0)
	{
		Count++;
	}
	Delay(30);	
}

/********************************************************************
* 名称 : Main()
* 功能 : 外部中断试验主程序
* 输入 : 无
* 输出 : 无
***********************************************************************/
void Main(void)
{
	Outside_Init();
	P2 = 7;
	while(1)
	{
		P0 = table[Count % 10];
		Delay(2);
	}
}

 

/****************************外部中断0的负边沿触发*******************************
* 描述    :  外部中断,是单片机最基本也是最重要的功能。
			 P3.2单片机外部中断0的引脚和外部中断0。
			 每当按下S19端口的时候,数码管的值会加1.

***********************************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint  unsigned int

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

uchar Count = 100;
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;  //负边沿触发
	EA = 1;	  //开总中断
}

/********************************************************************
* 名称 : Outside_Int1()
* 功能 : 外部中断0 的中断处理
* 输入 : 无
* 输出 : 无
***********************************************************************/
void Outside_Int1(void) interrupt 0	using 1
{
	Delay(2);
	if(KEY1 == 0)
	{
		Count++;
	}
	Delay(30);	
}

/********************************************************************
* 名称 : Main()
* 功能 : 外部中断试验主程序
* 输入 : 无
* 输出 : 无
***********************************************************************/
void Main(void)
{
	Outside_Init();
	P2 = 7;
	while(1)
	{
		P0 = table[Count % 10];
		Delay(2);
	}
}

 外部中断1的电平触发

/********************************************************************
* 描述    :  外部中断,是单片机最基本也是最重要的功能。
			 P3.3单片机外部中断1的引脚。
			 每当按下S18端口的时候,数码管的值会减1.

***********************************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint  unsigned int

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

uchar Count = 100;
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)
{

	EX1 = 1;  //开外部中断1
	IT1 = 0;  //电平触发
	EA = 1;	  //开总中断
}

/********************************************************************
* 名称 : 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();
	P2 = 7;
	while(1)
	{
		P0 = table[Count % 10];
		Delay(2);
	}
}

 

外部中断0的负边沿触发
/********************************************************************
* 描述    :  外部中断,是单片机最基本也是最重要的功能。
			 P3.3单片机外部中断1的引脚。
			 每当按下S20端口的时候,数码管的值会减1.

***********************************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint  unsigned int

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

uchar Count = 100;
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)
{

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


/********************************************************************
* 名称 : 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();
	P2 = 7;
	while(1)
	{
		P0 = table[Count % 10];
		Delay(2);
	}
}

 


/********************************************************************
* 描述    :  这个代码中,我们用外部中断0来作为一个信号捕捉的端口。
             当在P3.2端口采集到下降沿的时候,会在数码管上加1.
			 (P1.0连接到P3.2)在程序代码中,我们给了P1.0端口,进行 一个赋值,让它电平交替变化。
			 每变化一次,数码管的值就会加1。

***********************************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint  unsigned int 

unsigned long i = 0;
sbit OUT = P1^0;
uchar table[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar code LED_W[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
/********************************************************************
* 名称 : Delay_1ms()
* 功能 : 延时子程序,延时时间为 1ms * x
* 输入 : x (延时一毫秒的个数)
* 输出 : 无
***********************************************************************/
void Delay(uint i)
{
	uint x,j;
	for(j=0;j<i;j++)
	for(x=0;x<=148;x++);	
}
										 
void Outside_Int1(void) interrupt 0	using 1
{
	i++;
}

/********************************************************************
* 名称 : Main()
* 功能 : 主函数
* 输入 : 无
* 输出 : 无
***********************************************************************/
void main()
{
	unsigned long j;
	unsigned long tabPL[8]={0};
	EX0 = 1;  //开外部中断0
	IT0 = 1;  //负边沿触发
	EA = 1;	  //开总中断
	while(1)
	{
		tabPL[0] = i % 10;
		tabPL[1] = (i /10)%10;
		tabPL[2] = (i /100)%10;
		tabPL[3] = (i /1000)%10;
		tabPL[4] = (i /10000)%10;
		tabPL[5] = (i /100000)%10;
		tabPL[6] = (i /1000000)%10;
		tabPL[7] = (i /10000000)%10;
		for(j=0;j<8;j++)
		{
			P0 = table[tabPL[j]];	  //取 i 的个位
			P2 = 7-j;
			Delay(1);
		}
		Delay(2);
		OUT = ~OUT;	
	}
}

 


高工
2014-05-18 11:02:39     打赏
2楼
中断就是要快速响应啊,进入中断之后,正在响应中断的时候就不再响应新中断了,所以可以不担心毛刺

高工
2014-05-21 09:44:09     打赏
3楼
i是作为局部变量的啊,内存使用完毕就被释放掉了

高工
2014-08-12 10:54:03     打赏
4楼
稍后发布AVR有关资料

共4条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]