基于單片機(jī)的彩燈小制作
3.軟件設(shè)計(jì)
圖3 單片機(jī)控制LED調(diào)光電路流程圖
4.程序設(shè)計(jì)
#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;
}}
*
參考文獻(xiàn):
[1].ULN2003datasheethttp://www.dzsc.com/datasheet/ULN2003_500970.html.
pwm相關(guān)文章:pwm原理
評(píng)論