#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
#define CYCLE 10 //pwn周期
sbit PWN0=P1^4;
sbit PWN1=P1^5;
sbit key1=P3^0; //jia
sbit key2=P3^1; //jian
sbit key3=P3^2; //tingzhi
sbit key4=P3^3; //zhengfanzhuan
char PWN_ON=0;
char count=0; //定时器计数
int num,flag;//正反转标志位
sbit beep=P2^3;
void delayms(int x)
{
int i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void keyscan()
{
if(key1==0)
{
delayms(5);
if(key1==0)
{
while(!key1);
if(num<=3)
num++;
if(num==2)
{
beep=0;
P0=0x00;
delayms(10);
beep=1;
P0=0xff;
}
if(num==3)
{
beep=0;
P0=0x00;
}
}
}
if(key2==0)
{
delayms(5);
if(key2==0)
{
if(num>0)
num--;
while(!key2);
}
}
if(key3==0)
{
delayms(5);
if(key3==0)
{
while(!key3);
num=!num;
}
}
if(key4==0)
{
delayms(5);
if(key4==0)
{
while(!key4);
flag++;
if(flag==2)
flag=0;
}
}
switch(num)
{
case 0: PWN_ON=0;break;
case 1: PWN_ON=4;break;
case 2: PWN_ON=7;break;
case 3: PWN_ON=10;break;
default:
break;
}
}
void init() //初始化
{
PWN0=1;
PWN1=1;
TMOD=0X01;
TH1=(65535-45872)/256;
TL1=(65535-45872)%256;
EA=1;
ET1=1;
TR1=1;
}
void main()
{
init();
while(1)
{
keyscan();
}
}
void T1_time()interrupt 3
{
TH1=(65535-45872)/256;
TL1=(65535-45872)%256;
count++;
if(count>CYCLE)
count=0;
if(count<PWN_ON)//进行正反转
{
if(flag==1)
{
PWN0=0;
PWN1=1;
}
else
{
PWN0=1;
PWN1=0;
}
}
else
{
PWN0=1;
PWN1=1;
}
}
|