| 
 | 
- #include "reg54.h"
 
 - #define X_LED P2
 
 - int acount,bcount;
 
 - void delay(int count)
 
 - {
 
 - while(count--);
 
 - }
 
 - unsigned char XSwitch(unsigned char DataIn,int Time,int act,int bct,int max)
 
 - {
 
 - unsigned char temp_DI=DataIn;
 
 - while(Time--)
 
 - {
 
 -   X_LED=DataIn^0xff;
 
 -   while(bct<max)
 
 -   {
 
 -    act--;
 
 -    bct++;
 
 -    if(act>0)
 
 -    {
 
 -     if(temp_DI<0x80)
 
 -      DataIn<<=1;
 
 -     else
 
 -      DataIn>>=1;
 
 -     delay(act);
 
 -     X_LED=DataIn^0xff;
 
 -    }
 
 -    if(bct<max)
 
 -    {
 
 -     if(temp_DI<0x80)
 
 -      DataIn>>=1;
 
 -     else
 
 -      DataIn<<=1;
 
 -     delay(bct);
 
 -     X_LED=DataIn^0xff;
 
 -    }
 
 -   }
 
 -   if(bct==max)
 
 -   {
 
 -    act=max;
 
 -    bct=0;
 
 -    if(temp_DI<0x80)
 
 -     DataIn<<=1;
 
 -    else
 
 -     DataIn>>=1;
 
 -   }
 
 - }
 
 - return(DataIn);
 
 - }
 
 - void main(void)
 
 - {
 
 - P1=0xff;
 
 - acount=P1^0xff;
 
 - X_LED=P1^0xff;
 
 - delay(9000);
 
 - delay(9000);
 
 - bcount=0;
 
 - while(1)
 
 - {
 
 -   P1=0xff;
 
 -   acount=P1^0xff;
 
 -   XSwitch(XSwitch(0x01,7,acount,bcount,acount),7,acount,bcount,acount);
 
 - }
 
 - }
 
  複製代碼 這是我之前寫的漸變LED霹靂燈程式(8051使用),各位可以拿去玩玩:P 
我是用keil C下去編譯的,如果是別的compiler或MCU,PORT定義(reg54.h)可能要自己改掉 
 
裏面我有用P1輸入的8位元資料去做速度控制,輸出是P2(定義在第2行),請自行改成需要的PORT。 
連接圖如下:(註:參考用,LED是共陽及接法,如LED電流要限制的話請自行加上排阻,這裡省略是因為白光LED可以直接吃5V的電壓) 
 
 
影片檔:
 
PiziLight_2.rar
(1.28 MB, 下載次數: 4189)
 
 
[ 本帖最後由 omegaCD 於 2008-1-8 23:28 編輯 ] |   
 
 
 
 |