#include <reg51.h>
unsigned char PWM_COUNT; //计数
unsigned int HUXI_COUNT; //占空比更新时间
unsigned int PWM_VALUE[8]; //每个灯的亮度
void timer0_init()
{
TMOD=0x02; //模式设置,00010000,定时器0,工作于模式2(M1=1,M0=0)
TH0=0x47; //定时器溢出值设置,每隔200us发起一次中断。
TL0=0X47;
TR0=1; //定时器0开始计时
ET0=1; //开定时器0中断
EA=1; //开总中断
PWM_COUNT =0;
}
void time0() interrupt 1
{
int i,b,c;
PWM_COUNT++;
HUXI_COUNT++;
for(i=0;i<=7;i++) if(PWM_COUNT==PWM_VALUE[i]) P2&=~(1<<i);
if(PWM_COUNT==10) P2=0xff,PWM_COUNT=0;
if(HUXI_COUNT==600)
{
b=PWM_VALUE[0];
PWM_VALUE[0]=PWM_VALUE[7];
HUXI_COUNT=0;
for(i=1;i<=7;i++){
c=PWM_VALUE[i];
PWM_VALUE[i]=b;
b=c;
}
}
}
void main()
{
int i;
i=0;
timer0_init();
PWM_COUNT=0;
HUXI_COUNT=0;
PWM_VALUE[0]=9;
PWM_VALUE[1]=7;
PWM_VALUE[2]=4;
PWM_VALUE[3]=1;
while(1);
}
流星灯
最新推荐文章于 2021-05-17 22:14:19 发布