51单片机实现呼吸灯效果

180it 2022-03-22 AM 1031℃ 0条
#include<reg52.h>
#define uchar unsigned char
#define uint  unsigned int

sbit PWM=P2^0; //LED灯
bit DIR;
uint count,value,timer1;

void Timer1Init()
{
          TMOD|=0x01;
    TH1=0xff;
          TL1=0xff;
          EA=1;
          ET1=1;
          TR1=1;
}
void main()
{
          Timer1Init();
    while(1)
                {
                    if(count>100)
                                {
                                    count=0;
                                          if(DIR==1)
                                                {
                                                   value++;
                                                }
                                                if(DIR==0)
                                                {
                                                   value--;
                                                }
                                }
                                if(value==1000)
                                {
                                    DIR=0;
                                }
                                if(value==0)
                                {
                                    DIR=1;
                                }
                                if(timer1>1000)
                                {
                                    timer1=0;
                                }
                                if(timer1<value)
                                {
                                    PWM=1;
                                }
                                else
                                {
                                    PWM=0;
                                }
                }
}
void Time1(void)  interrupt 3
{
    TH1=0xff;
          TL1=0xff;
          timer1++;
          count++;
}

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

51单片机实现呼吸灯效果