单片机:K1 K2控制流水灯0 1定时开关 K3 K4控制流水灯5 6定时开关 同时两个定时

#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit led1=P1^0;
sbit led2=P1^1;
sbit led3=P1^6;
sbit led4=P1^7;
sbit key1=P3^2;
sbit key2=P3^3;
sbit key3=P3^4;
sbit key4=P3^5;
uchar num0,num1;
void init()
{
TMOD=0x11;
TH0=0X2C;
TL0=0X00;
TH1=0X3C;
TL1=0X00;
EA=1; //开总中断
ET0=1; //开定时器0中断
ET1=1;
TR1=0;
TR0=0; //启动定时器0
}  
void keyscan()
{ if(key1==0) //如果按键1
   {
     TR0=1;
     
   }
  if(key2==0)                 // ???
   {
     TR0=0;
     
   }
  if(key3==0)                   // ???
   {
      TR1=1;
      
   }
  if (key4==0)                    //        ???
   {
     TR1=0;
     
   }
}
void main()
{ 
init();
while(1)
{
if(num0==20) 
{
led1=~led1; //让发光管状态取反
led2=~led2; //让发光管状态取反
num0=0;
}
else
{if(num1==20) 
{
led3=~led3; //让发光管状态取反
led4=~led4; //让发光管状态取反
num1=0;
}
}
}
}
void T0_time()interrupt 1
{
TR0=0;
TH0=0X2C;
TL0=0X00;
TR0=1;
num0++; 
}
void T1_time()interrupt 3
{
TR1=0;
TH1=0X3C;
TL1=0X00;
TR1=1;
num1++; 
}

 

上一篇:2022.3.2#NOI2015 荷马史诗


下一篇:单片机按键使用程序 (51单片机)