Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 10730|回復: 4
打印 上一主題 下一主題

Ten bar LED + Arduino 模仿霹靂遊俠李麥克

[複製鏈接]
1#
發表於 2008-1-8 23:26:26 | 顯示全部樓層
  1. #include "reg54.h"
  2. #define X_LED P2
  3. int acount,bcount;
  4. void delay(int count)
  5. {
  6. while(count--);
  7. }
  8. unsigned char XSwitch(unsigned char DataIn,int Time,int act,int bct,int max)
  9. {
  10. unsigned char temp_DI=DataIn;
  11. while(Time--)
  12. {
  13.   X_LED=DataIn^0xff;
  14.   while(bct<max)
  15.   {
  16.    act--;
  17.    bct++;
  18.    if(act>0)
  19.    {
  20.     if(temp_DI<0x80)
  21.      DataIn<<=1;
  22.     else
  23.      DataIn>>=1;
  24.     delay(act);
  25.     X_LED=DataIn^0xff;
  26.    }
  27.    if(bct<max)
  28.    {
  29.     if(temp_DI<0x80)
  30.      DataIn>>=1;
  31.     else
  32.      DataIn<<=1;
  33.     delay(bct);
  34.     X_LED=DataIn^0xff;
  35.    }
  36.   }
  37.   if(bct==max)
  38.   {
  39.    act=max;
  40.    bct=0;
  41.    if(temp_DI<0x80)
  42.     DataIn<<=1;
  43.    else
  44.     DataIn>>=1;
  45.   }
  46. }
  47. return(DataIn);
  48. }
  49. void main(void)
  50. {
  51. P1=0xff;
  52. acount=P1^0xff;
  53. X_LED=P1^0xff;
  54. delay(9000);
  55. delay(9000);
  56. bcount=0;
  57. while(1)
  58. {
  59.   P1=0xff;
  60.   acount=P1^0xff;
  61.   XSwitch(XSwitch(0x01,7,acount,bcount,acount),7,acount,bcount,acount);
  62. }
  63. }
複製代碼
這是我之前寫的漸變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, 下載次數: 1610)

[ 本帖最後由 omegaCD 於 2008-1-8 23:28 編輯 ]
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-6-12 07:41 , Processed in 0.185901 second(s), 10 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表