標(biāo)題: 單片機(jī)呼吸燈程序 [打印本頁]

作者: xiongda    時(shí)間: 2015-5-26 19:18
標(biāo)題: 單片機(jī)呼吸燈程序
#include<reg52.h>
sbit LED=P0^0;
void delay(unsigned int ms)
{
        unsigned int i=0,j=0;
        for(i=ms;i>0;i--)
                for(j=20;j>0;j--);
}
void main()
{
        unsigned int t = 50;
        unsigned int d = 1;
        while(t =t+ d)
        {
                LED = t%2;
                delay(t*(LED==1)+(100-t)*(LED==0));
                if(t>=95||t<=5)
{
                     d =-d;
}
        }
}

這個(gè)可以實(shí)現(xiàn)一個(gè)呼吸燈

哥-深圳:
首先,把 delay(t*(LED==1)+(100-t)*(LED==0));看做是:
(1)當(dāng)LED亮的時(shí)候(LED==1)等于1,(LED==0)等于0,此時(shí)
delay(t*(LED==1)+(100-t)*(LED==0))相當(dāng)于delay(t);
(2)當(dāng)LED滅的時(shí)候(LED==1)等于0,(LED==0)等于1,此時(shí)
delay(t*(LED==1)+(100-t)*(LED==0))相當(dāng)于delay((100-t));




void main()
{
        unsigned int t = 50;
        unsigned int d = 1;
        while(t =t+ d)
        {
                LED = t%2;
                if(LED==1)        
                {
                   delay(t);
                }         
                else
                {
                    delay(100-t);
                }
                if(t>=95||t<=5)
                {
                     d =-d;
                }
        }
}



作者: wujunbo27    時(shí)間: 2015-5-27 01:51





歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1