望采纳
#include <reg52.h>
unsigned char count;
sbit pwm =P3^0 ;
sbit jia =P3^2;
sbit jan =P3^3;
unsigned char jd;
void delay(unsigned char i)
{
unsigned char j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void Time0_Init()
{
TMOD = 0x01;
IE = 0x82;
TH0 = 0xfe;
TL0 = 0x33;
TR0=1;
}
void Time0_Int() interrupt 1
{
TH0 = 0xfe;
TL0 = 0x33;
if(count<jd)
pwm=1;
else
pwm=0;
count=(count+1);
count=count%40;
}
void keyscan()
{
if(jia==0)
{
delay(10);
if(jia==0)
{
jd++;
count=0;
if(jd==6)
jd=5;
while(jia==0);
}
}
if(jan==0)
{
delay(10);
if(jan==0)
{
jd--;
count=0;
if(jd==0)
jd=1;
while(jan==0);
}
}
}
void main()
{
jd=1;
count=0;
Time0_Init();
while(1)
{
keyscan();
}
} |