新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > C51 模仿PWM,可調(diào)

C51 模仿PWM,可調(diào)

作者: 時(shí)間:2016-11-23 來(lái)源:網(wǎng)絡(luò) 收藏
#include

sbit LED_0 = P1^0;

本文引用地址:http://butianyuan.cn/article/201611/320427.htm

sbit LED_1 = P1^1;

unsigned Pwm = 0; // 控制 LED_0 的有暗到亮 其中LED_1 作為對(duì)比

unsigned Count=0;

void Time_Init(void)
{
TMOD = 0x02; //定時(shí)器0 定時(shí),方式2
TH0 = 0x9c; //100us
TL0 = 0x9c;
EA = 1;
ET0 = 1;
TR0 = 1;
}

void Int0_Init(void)
{
IT0=1; //設(shè)置下降沿觸發(fā)
EA=1; //開(kāi)總中斷
EX0=1; //開(kāi)外部中斷
}

void Int1_Init(void)
{
IT1=1; //設(shè)置下降沿觸發(fā)
EA=1; //開(kāi)總中斷
EX1=1; //開(kāi)外部中斷
}

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; //此時(shí)占空比為Pwm/100
}
else
{
Pwm=99;
}
}

void Int1() interrupt 2
{
if(Pwm>=5)
{
Pwm=Pwm-5;
}
else
{
Pwm=0;
}
}



關(guān)鍵詞: C51模仿PWM可

評(píng)論


技術(shù)專(zhuān)區(qū)

關(guān)閉