#include <reg52.h>
sbit SG_PWM=P0^0;
sbit Key_1=P2^4;
sbit Key_2=P2^3;
sbit Key_3=P2^2;
unsigned char count=0;
unsigned char PWM_count=1;
void delay(unsigned char i)
{
unsigned char j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void Timer_Init()
{
TMOD=0X01;
TH0=0Xfe;
TL0=0X33;
ET0=1;
TR0=1;
EA=1;
}
void Timer() interrupt 1
{
TR0=0;
TH0=0Xfe;
TL0=0X33;
if(count<=PWM_count)
{
SG_PWM=1;
}
else
{
SG_PWM=0;
}
count++;
if(count>=40)
{
count=0;
}
TR0=1;
}
void main()
{
Timer_Init();
while(1)
{
if(Key_1==1)
{
delay(10);
EA=0;
if(Key_1==1)
{
PWM_count=2;
count=0;
EA=1;
}
while(!Key_1);
}
if(Key_2==1)
{
delay(10);
EA=0;
if(Key_2==1)
{
PWM_count=4;
count=0;
EA=1;
}
while(!Key_2);
}
if(Key_3==1)
{
delay(10);
EA=0;
if(Key_3==1)
{
PWM_count=3;
count=0;
EA=1;
}
while(!Key_3);
}
}
}
|