C51 模仿PWM,可调编码
时间:01-21 09:31 阅读:1658次
*温馨提示:点击图片可以放大观看高清大图
简介:本文给大家分享了C51 模仿PWM,可调编码。
#include
sbit LED_0 = P1^0;
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() 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;
}
}