/**********************51单片机学习例程************************
* 平台:Keil U4 + STC89C52
* 名称:Key1按下LED左循环,Key2按下LED变右循环
* 编写:Tony
* 日期:2022-2-10
* QQ : 674749794
* 晶体:12MHZ
* 变更记录:无
******************************************************************/
#include
#include
#define GPIO_LED P1
//外部中断的IO
sbit Key1=P3^2;
sbit Key2=P3^3;
void IntConfiguration();
void Delay(unsigned int n);
unsigned char KeyValue=0;
/*******************************************************************************
* 函 数 名 : main
* 函数功能 : 主函数
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void main(void)
{
GPIO_LED=0X01;
IntConfiguration();
while(1)
{
if(KeyValue)
GPIO_LED=_crol_(GPIO_LED,1);
else
GPIO_LED=_cror_(GPIO_LED,1);
Delay(2000);
}
}
/*******************************************************************************
* 函 数 名 : IntConfiguration()
* 函数功能 : 设置外部中断
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void IntConfiguration()
{
//设置INT0
IT0=1;//跳变沿出发方式(下降沿);IT0=0--->低电平触发中断
EX0=1;//打开INT0的中断允许。
//设置INT1
IT1=1;
EX1=1;
EA=1;//打开总中断
}
/*******************************************************************************
* 函 数 名 : Delay(unsigned int n)
* 函数功能 : 延时
* 输 入 : n
* 输 出 : 无
*******************************************************************************/
void Delay(unsigned int n) //延时50us误差 0us
{
unsigned char a,b;
for(;n>0;n--)
{
for(b=1;b>0;b--)
for(a=22;a>0;a--);
}
}
/*******************************************************************************
* 函 数 名 : Int0() interrupt 0
* 函数功能 : 外部中断0的中断函数
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void Int0() interrupt 0 //外部中断0的中断函数
{
Delay(1); //延时消抖
if(Key1==0)
KeyValue=1;
IE0=0; //中断请求标志位
}
/*******************************************************************************
* 函 数 名 : Int1() interrupt 2
* 函数功能 : 外部中断1的中断函数
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void Int1() interrupt 2//外部中断1的中断函数
{
Delay(1); //延时消抖
if(Key2==0)
KeyValue=0;
IE1=0;
}