基于單片機的彩燈設計
3.軟件設計
圖3 單片機控制LED調(diào)光電路流程圖
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;
}}
*
評論