新闻中心

EEPW首页>嵌入式系统>设计应用> C51 模仿PWM,可调

C51 模仿PWM,可调

作者: 时间:2016-11-23 来源:网络 收藏
#include

sbitLED_0 = P1^0;

本文引用地址: //m.amcfsurvey.com/article/201611/320427.htm

sbit 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;//设置下降沿触发
EA=1;//开总中断
EX0=1;//开外部中断
}

void Int1_Init(void)
{
IT1=1;//设置下降沿触发
EA=1;//开总中断
EX1=1;//开外部中断
}

void main(void)
{
Time_Init();
Int0_Init();
Int1_Init();
LED_0 = 0;
LED_1 = 0;
while(1);

}

void INT_Time0()interrupt1 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;
}
}



关键词:C51模仿PWM可

评论


技术专区

关闭