#include sbit LED_0 = P1^0;
本文引用地址:http://m.butianyuan.cn/article/201611/320427.htmsbit LED_1 = P1^1;
unsigned Pwm = 0; // 控制 LED_0 的有暗到亮 其中LED_1 作為對比
unsigned Count=0;
void Time_Init(void)
{
TMOD = 0x02; //定時器0 定時,方式2
TH0 = 0x9c; //100us
TL0 = 0x9c;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void Int0_Init(void)
{
IT0=1; //設(shè)置下降沿觸發(fā)
EA=1; //開總中斷
EX0=1; //開外部中斷
}
void Int1_Init(void)
{
IT1=1; //設(shè)置下降沿觸發(fā)
EA=1; //開總中斷
EX1=1; //開外部中斷
}
void main(void)
{
Time_Init();
Int0_Init();
Int1_Init();
LED_0 = 0;
LED_1 = 0;
while(1);
}
void INT_Time0() interrupt 1 using 1
{
Count++;
if(Count==100)
{
Count=0;
}
if(Count<=Pwm)
{
LED_0 = 0;
}
else
{
LED_0 = 1;
}
}
void Int0() interrupt 0
{
if(Pwm<=94)
{
Pwm=Pwm+5; //此時占空比為Pwm/100
}
else
{
Pwm=99;
}
}
void Int1() interrupt 2
{
if(Pwm>=5)
{
Pwm=Pwm-5;
}
else
{
Pwm=0;
}
}
評論