找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6824|回复: 0
收起左侧

单片机控制的炫彩流水灯

[复制链接]
ID:76686 发表于 2015-4-11 22:11 | 显示全部楼层 |阅读模式
     这个试验未使用到电烙铁、万用板、焊锡丝等,是利用一块单片机最小系统板,然后用一块1cm的泡沫板作的基板布置好元件,用杜邦线连接好电路。下面是试验过程图片及源程序。
视频如下:









源代码如下:
  1. #include <AT89X52.h>
  2. unsigned char RunMode;
  3. //*******************************************************
  4. void Delay1ms(unsigned int count)
  5. {
  6. unsigned int i,j;
  7. for(i=0;i<count;i++)
  8. for(j=0;j<120;j++);
  9. }
  10. unsigned char code LEDDisplayCode[] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, //0~7
  11.                               0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};
  12. void Display(unsigned char Value)
  13. {
  14. P3 = LEDDisplayCode[Value];
  15. }
  16. void LEDFlash(unsigned char Count)
  17. {
  18. unsigned char i;
  19. bit Flag;
  20. for(i = 0; i<Count;i++)
  21. {
  22.   Flag = !Flag;
  23.   if(Flag)
  24.    Display(RunMode);
  25.   else
  26.    Display(0x10);
  27.   Delay1ms(100);
  28. }
  29. Display(RunMode);
  30. }
  31. unsigned char GetKey(void)
  32. {
  33. unsigned char KeyTemp,CheckValue,Key = 0x00;
  34. CheckValue = P2&0x32;
  35. if(CheckValue==0x32)
  36.   return 0x00;

  37. Delay1ms(10);
  38. KeyTemp = P2&0x32;
  39. if(KeyTemp==CheckValue)
  40.   return 0x00;
  41. if(!(CheckValue&0x02))
  42.   Key|=0x01;
  43. if(!(CheckValue&0x10))
  44.   Key|=0x02;
  45. if(!(CheckValue&0x20))
  46.   Key|=0x04;
  47. return Key;
  48. }
  49. unsigned int TimerCount,SystemSpeed,SystemSpeedIndex;
  50. void InitialTimer2(void)
  51. {
  52. T2CON  = 0x00;   //16 Bit Auto-Reload Mode
  53.   TH2 = RCAP2H = 0xFC;   //重装值,初始值 TL2 = RCAP2L = 0x18;
  54. ET2=1;     //定时器 2 中断允许
  55. TR2 = 1;    //定时器 2 启动
  56. EA=1;
  57. }
  58. unsigned int code SpeedCode[]={   1,   2,   3,   5,   8,  10,  14,  17,  20,  30,
  59.             40,  50,  60,  70,  80,  90, 100, 120, 140, 160,
  60.         180, 200, 300, 400, 500, 600, 700, 800, 900,1000};//30
  61. void SetSpeed(unsigned char Speed)
  62. {
  63. SystemSpeed =SpeedCode[Speed];
  64. }
  65. void LEDShow(unsigned int LEDStatus)
  66. {
  67. P1 = ~(LEDStatus&0x00FF);
  68. P0 = ~((LEDStatus>>8)&0x00FF);
  69. }
  70. void InitialCPU(void)
  71. {
  72. RunMode = 0x00;
  73. TimerCount = 0;
  74. SystemSpeedIndex = 10;
  75. P1 = 0x00;
  76. P0 = 0x00;
  77. P2 = 0xFF;
  78. P3 = 0x00;
  79. Delay1ms(500);
  80. P1 = 0xFF;
  81. P0 = 0xFF;
  82. P2 = 0xFF;
  83. P3 = 0xFF;
  84. SetSpeed(SystemSpeedIndex);
  85. Display(RunMode);
  86. }
  87. //Mode 0
  88. unsigned int LEDIndex = 0;
  89. bit LEDDirection = 1,LEDFlag = 1;
  90. void Mode_0(void)
  91. {
  92. LEDShow(0x0001<<LEDIndex);
  93. LEDIndex = (LEDIndex+1)%16;

  94. }
  95. //Mode 1
  96. void Mode_1(void)
  97. {
  98. LEDShow(0x8000>>LEDIndex);
  99. LEDIndex = (LEDIndex+1)%16;
  100. }
  101. //Mode 2
  102. void Mode_2(void)
  103. {
  104. if(LEDDirection)
  105.   LEDShow(0x0001<<LEDIndex);
  106. else
  107.   LEDShow(0x8000>>LEDIndex);
  108. if(LEDIndex==15)
  109.   LEDDirection = !LEDDirection;
  110.    LEDIndex = (LEDIndex+1)%16;
  111. }
  112. //Mode 3
  113. void Mode_3(void)
  114. {
  115. if(LEDDirection)
  116.   LEDShow(~(0x0001<<LEDIndex));
  117. else
  118.   LEDShow(~(0x8000>>LEDIndex));
  119. if(LEDIndex==15)
  120.   LEDDirection = !LEDDirection;
  121.    LEDIndex = (LEDIndex+1)%16;
  122. }
  123. //Mode 4
  124. void Mode_4(void)
  125. {
  126. if(LEDDirection)
  127. {
  128.   if(LEDFlag)
  129.    LEDShow(0xFFFE<<LEDIndex);
  130.      else
  131.    LEDShow(~(0x7FFF>>LEDIndex));
  132. }
  133. else
  134. {
  135.   if(LEDFlag)
  136.    LEDShow(0x7FFF>>LEDIndex);
  137.   else
  138.    LEDShow(~(0xFFFE<<LEDIndex));
  139. }
  140. if(LEDIndex==15)
  141. {
  142.   LEDDirection = !LEDDirection;
  143.   if(LEDDirection) LEDFlag = !LEDFlag;
  144. }
  145.     LEDIndex = (LEDIndex+1)%16;
  146. }
  147. //Mode 5
  148. void Mode_5(void)
  149. {
  150. if(LEDDirection)
  151.   LEDShow(0x000F<<LEDIndex);
  152. else
  153.   LEDShow(0xF000>>LEDIndex);
  154. if(LEDIndex==15)
  155.   LEDDirection = !LEDDirection;
  156.     LEDIndex = (LEDIndex+1)%16;
  157. }
  158. //Mode 6
  159. void Mode_6(void)
  160. {
  161. if(LEDDirection)
  162.   LEDShow(~(0x000F<<LEDIndex));
  163. else
  164.   LEDShow(~(0xF000>>LEDIndex));
  165. if(LEDIndex==15)
  166.   LEDDirection = !LEDDirection;
  167.     LEDIndex = (LEDIndex+1)%16;
  168. }
  169. //Mode 7
  170. void Mode_7(void)
  171. {
  172. if(LEDDirection)
  173.   LEDShow(0x003F<<LEDIndex);
  174. else
  175.   LEDShow(0xFC00>>LEDIndex);
  176. if(LEDIndex==9)
  177.   LEDDirection = !LEDDirection;
  178.     LEDIndex = (LEDIndex+1)%10;
  179. }
  180. //Mode 8
  181. void Mode_8(void)
  182. {
  183. LEDShow(++LEDIndex);
  184. }
  185. void TimerEventRun(void)
  186. {
  187. if(RunMode==0x00)
  188. {
  189.   Mode_0();
  190. }
  191. else if(RunMode ==0x01)
  192. {
  193.   Mode_1();
  194. }
  195. else if(RunMode ==0x02)
  196. {
  197.   Mode_2();
  198. }
  199. else if(RunMode ==0x03)
  200. {
  201.   Mode_3();
  202. }
  203. else if(RunMode ==0x04)
  204. {
  205.   Mode_4();
  206. }
  207. else if(RunMode ==0x05)
  208. {
  209.   Mode_5();
  210. }
  211. else if(RunMode ==0x06)
  212. {
  213.   Mode_6();
  214. }
  215. else if(RunMode ==0x07)
  216. {
  217.   Mode_7();
  218. }
  219. else if(RunMode ==0x08)
  220. {
  221.   Mode_8();
  222. }
  223. }
  224. void Timer2(void) interrupt 5 using 3
  225. {
  226. TF2 = 0;  //中断标志清除( Timer2 必须软件清标志!)
  227. if(++TimerCount>=SystemSpeed)
  228. {
  229.   TimerCount = 0;
  230.   TimerEventRun();
  231.     }
  232. }
  233. unsigned char MusicIndex = 0;
  234. void KeyDispose(unsigned char Key)
  235. {
  236. if(Key&0x01)
  237. {
  238.   LEDDirection = 1;
  239.   LEDIndex = 0;
  240.   LEDFlag = 1;
  241.   RunMode = (RunMode+1)%9;
  242.   Display(RunMode);
  243. }
  244. if(Key&0x02)
  245. {
  246.   if(SystemSpeedIndex>0)
  247.   {
  248.    --SystemSpeedIndex;
  249.    SetSpeed(SystemSpeedIndex);
  250.   }
  251.   else
  252.   {
  253.    LEDFlash(6);
  254.   }
  255. }
  256. if(Key&0x04)
  257. {
  258.   if(SystemSpeedIndex<28)
  259.   {
  260.    ++SystemSpeedIndex;
  261.    SetSpeed(SystemSpeedIndex);
  262.   }
  263.   else
  264.   {
  265.    LEDFlash(6);
  266.   }
  267. }
  268. }
  269. //***********************************************************************************
  270. main()
  271. {
  272. unsigned char Key;
  273. InitialCPU();
  274. InitialTimer2();
  275. while(1)
  276. {
  277.   Key = GetKey();
  278.   if(Key!=0x00)
  279.   {
  280.    KeyDispose(Key);
  281.   }
  282. }
  283. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表