標(biāo)題: 按鍵控制彩燈亮燈方式簡易小程序+電路圖 [打印本頁]

作者: 森小胖    時(shí)間: 2017-3-27 01:21
標(biāo)題: 按鍵控制彩燈亮燈方式簡易小程序+電路圖
按鍵控制彩燈的仿真電路圖:




單片機(jī)源程序:
  1. /**********************劉金森 1440302103 電子信息工程*********************/
  2. #include<reg51.h>                      //51單片機(jī)頭文件
  3. #include<intrins.h>                       //包含crol(循環(huán)左移)函數(shù)所在的頭文件
  4. #define uint unsigned int      
  5. void delay(int z);                   //聲明子函數(shù)
  6. unsigned char temp;                 //定義一個(gè)變量,用來給P1口賦值
  7. int i,j;
  8. sbit Key0=P0^0;
  9. sbit Key1=P0^1;
  10. sbit Key2=P0^2;                           //定義三個(gè)按鍵
  11. /**************按鍵控制函數(shù)************/
  12. uint key()
  13. {
  14.      uint temp;
  15.          Key0=0;
  16.          Key1=0;
  17.          Key2=0;                             
  18.      if(Key0==1)
  19.              {
  20.                 delay(500);                    //延時(shí)500毫秒,去按鍵時(shí)的抖動(dòng)
  21.                          if(Key0==1)
  22.                         {
  23.             temp=0xfc;                    //1111 1100B 只有兩盞燈亮
  24.                     P1=temp;
  25.                         while(Key0)
  26.                         {
  27.                     delay(500);
  28.                     temp=_crol_(temp,1);         //將temp循環(huán)左移一位后再賦給temp
  29.                 P1=temp;                                     //將移位后的值賦給P1口,從低位到高位逐個(gè)點(diǎn)亮發(fā)光二極管
  30.                         }
  31.                         }
  32.                                         
  33.               }
  34.              else if(Key1==1)
  35.              {
  36.                 delay(500);                  //延時(shí)500毫秒,去按鍵時(shí)的抖動(dòng)
  37.                          if(Key1==1)
  38.                         {
  39.             temp=0x10;                   //0001 0000B 只有一盞燈不亮
  40.                     P1=temp;
  41.                         while(Key1)
  42.                         {
  43.                     delay(500);
  44.                     temp=_cror_(temp,1);        //將temp循環(huán)左移一位后再賦給temp
  45.                 P1=temp;                                     //將移位后的值賦給P1口,從低位到高位逐個(gè)點(diǎn)亮發(fā)光二極管
  46.                         }
  47.                         }
  48.                                         
  49.               }
  50.                    else if(Key2==1)
  51.              {
  52.                 delay(500);                  //延時(shí)500毫秒,去按鍵時(shí)的抖動(dòng)
  53.                          if(Key2==1)
  54.                         {
  55.             temp=0xfd;                  //1111 1101B 只有一盞燈亮
  56.                     P1=temp;
  57.                         while(Key2)
  58.                         {
  59.                     delay(500);
  60.                     temp=_cror_(temp,1);         //將temp循環(huán)左移一位后再賦給temp
  61.                 P1=temp;                                     //將移位后的值賦給P1口,從低位到高位逐個(gè)點(diǎn)亮發(fā)光二極管
  62.                         }
  63.                         }
  64.                                         
  65.               }
  66.                
  67.                
  68. return temp;
  69. }
  70. void delay(int z)           //延時(shí)z毫秒
  71. {
  72.       unsigned int x,y;
  73.           for(x=z;x>0;x--)
  74.                   for(y=110;y>0;y--);
  75. }
  76. /*****************主函數(shù)*************/
  77. void main()
  78. {


  79.    …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

下載:
test1.zip (73.8 KB, 下載次數(shù): 63)

作者: admin    時(shí)間: 2017-4-2 01:28
好資料,初學(xué)者有用,51黑有你更精彩
作者: kimicoo    時(shí)間: 2017-4-2 12:10
非常好,給力!
作者: kimicoo    時(shí)間: 2017-4-2 12:11
贊一個(gè)
作者: hanhongyi    時(shí)間: 2018-11-2 11:17
很好  很實(shí)用  好東西
作者: cyril2002    時(shí)間: 2018-11-2 13:44
硬件仿真是個(gè)好方向!
作者: 快樂眼淚    時(shí)間: 2018-11-2 18:57
我這菜鳥表示感謝!
作者: 931143848    時(shí)間: 2018-11-11 12:03
不錯(cuò)啊
作者: 956907357    時(shí)間: 2018-12-28 09:54
不錯(cuò)的文章
作者: 964624514    時(shí)間: 2019-4-9 12:42
不錯(cuò)不錯(cuò)不錯(cuò)




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