/*#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define uchar unsigned char
sbit K1=P3^4;
sbit K2=P3^5;
sbit ledr=P1^0;
sbit ledg=P1^1;
sbit ledb=P1^2;
bit LEDDirection=0;//LED控制方向0:渐亮1:渐灭
char pwm=0;
char pwmr=0;
char scw=0;//中断记数
char tt=0;
char n;
void dealy(uint z);
void Timer0Init(void)
{
TMOD=0x01;
TH0=0xff;
TL0=0x47;
EX0=1;
IT0=0;
PX0=1;
ET0=1;
TR0=1;
EA=1;
}
void main()
{
Timer0Init();
while(1){ if(K1==0)
{
dealy (1);
if(K1==0)
{TR0=1;
ledr=0;
dealy(5);
TR0=0;
}
}
if(K2==0)
{
dealy (1);
if(K2==0)
{ while(1)
{
ledr=0; //亮
dealy(100-n*10);
ledr=1; //熄
dealy(n*10);
}
}
}
}
}
void Time0Isr(void) interrupt 1
{
// pwm=0;
TH0=0xff;
TL0=0x47;
scw++; }*/
#include<reg52.h>
#define uchar unsigned char
bit LEDDirection=0;
sbit P2_0=P1^0;
sbit key1=P3^4;
sbit key2=P3^5;
sbit key3=P3^6;
uchar zkb,i,t;// zkb指占空比
uchar pwm;
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void init() //初始化函数
{
TMOD=0X01;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
}
void keyscan() //键盘扫描
{
P3=0XFF;
if(key1==0)
{
delay(5);
if(key1==0)
{
while(!key1);
if(zkb<9)
{
zkb++;
}
}
}
if(key2==0)
{
delay(5);
if(key2==0)
{
while(!key2);
if(zkb>0)
{
zkb--;
}
}
}
if(key3==0)
{TR0=1;
delay(5);
if(key3==0)
{while(!key3);
if((zkb<=9)&&(0==LEDDirection))
{
zkb++;
if(zkb>9)
{
LEDDirection=1;
zkb=9;
}
}
if((zkb>=0 )&&(1==LEDDirection))
{
zkb--;
if(zkb<0 )
{
LEDDirection=0;
zkb=0 ;
//dealy(3000);
}
}
} //pwm=pwmr;
}
}
void main() //主函数
{
zkb=2;
init();
while(1)
{
keyscan();
}
}
void time0(void) interrupt 1 //中断函数
{
TH0=(65536-200)/256;
TL0=(65536-200)%256;
++i;
if(i>10)
{
i=0;
};
if(i<=zkb)
{
P2_0=1;
}
else P2_0=0;
}
/*void time0(void) interrupt 0 //中断函数
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
++i;
if(i>10)
{
i=0;
};
if(i<=zkb)
{
P2_0=1;
}
else P2_0=0;
}*/