C51联盟 —— 外部中断+定时器中断

版权声明: https://blog.csdn.net/JRK_CSDN/article/details/85226589
#include <reg52.h>
#include <intrins.h>

typedef unsigned char u8;
typedef unsigned int u16;

sbit SEG = P1^0;
sbit KEY = P3^2;
u8 LED_Buff=1;
/*****************************************************/
void Int0Init();   //开启外部中断INTR0
void Timer1Init();  //定时器中断 5毫秒@12.000MHz
/*****************************************************/
void main()
{ 
 SEG = 0;
 Int0Init();
 Timer1Init(); 
 while(1);  
}
/*****************************************************/
void FlowLED()	//流水灯
{
  static u8 buff=0;   
  static u8 shift=0x01; 
  P2 = ~shift;
  if(buff == 0)
  {
    shift <<= 1;
    if(shift == 0x80) 
      buff = 1;   
  }
  else
  {
    shift >>= 1;
    if(shift == 0x01) 
      buff = 0;  
  }
}
/*****************************************************/
void Int0Init()   //开启外部中断INTR0
{
 IT0=1;
 EX0=1;
 EA=1;
}
/*****************************************************/
void Int0() interrupt 0  //外部中断0的中断函数
{
 _nop_();  //延时消抖
 if(KEY==0)
 {
  LED_Buff = ~LED_Buff;
 }
}
/*****************************************************/
void Timer1Init()  //定时器 @ 1毫秒 @ 12.000MHz
{
 TMOD = 0X10;
 TL1 = 0x78;  //设置定时初值
 TH1 = 0xEC;  //设置定时初值
 EA=1;
 ET1=1;
 TF1 = 0;  //清除TF1标志
 TR1 = 1;  //定时器1开始计时
}
/*****************************************************/
void Timer1() interrupt 3	//定时器1中断
{
 static u8 count=0;
 TL1 = 0x78;  //设置定时初值
 TH1 = 0xEC;  //设置定时初值
 count++;
 if(count>200 && LED_Buff == 1)
 {
  count = 0;
  FlowLED();
 }
}

猜你喜欢

转载自blog.csdn.net/JRK_CSDN/article/details/85226589