基于单片机的彩灯小制作
3.软件设计
图3单片机控制LED调光电路流程图
4.程序设计
#includereg52.h>
sbit LED1=P1^0;
sbit LED=P1^1;
sbit LED2=P1^2;
unsigned char CYCLE;
unsigned char PWM_ON;
void delay (unsigned int cnt)
{
while(--cnt);
}
main()
{ bit flag;
TMOD=0x01;
TH0=(65536-100)/256;
TL0=(65536-100)%256;
IE=0x82;
TR0=1;
CYCLE=50;
while(!flag)
{ delay(20000);
PWM_ON++;
if(PWM_ON==CYCLE)
{ flag=1;
}
}
while(flag)
{ delay(20000);
PWM_ON--;
if(PWM_ON==0)
{ flag=0;
}
}
}
void time(void) interrupt 1
{ static unsigned char count;
TH0=(65536-100)/256;
TL0=(65536-100)%256;
if(count==PWM_ON)
{ LED1=1;
LED=1;
LED2=1;
}
count++;
if(count==CYCLE)
{ count=0;
if(PWM_ON!=0)
LED1=0;
LED=0;
LED2=0;
}}
*
参考文献:
[1].ULN2003datasheethttp://www.dzsc.com/datasheet/ULN2003_500970.html.
pwm相关文章:pwm原理
评论