找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 4569|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

51智能8路搶答器仿真及源程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主


所有資料下載:
51智能8路搶答器.zip (109.62 KB, 下載次數(shù): 2671)

源程序:
  1. /****************************************************************
  2. ******以單片機(jī)為核心,設(shè)計(jì)一個(gè)8位競(jìng)賽搶*******
  3. ******答器:同時(shí)供8名選手或8個(gè)代表隊(duì)*************
  4. ******比賽,分別用8個(gè)按鈕S0~S7表示。*************
  5. ******設(shè)置一個(gè)系統(tǒng)清除和搶答控制開關(guān)************
  6. ******S,開關(guān)由主持人控制。搶答器具有***********
  7. ******鎖存與顯示功能。即選手按按鈕,************
  8. ******鎖存相應(yīng)的編號(hào),并在優(yōu)先搶答選************
  9. ******手的編號(hào)一直保持到主持人將系統(tǒng)************
  10. ******清除為止。
  11. ******搶答器具有定時(shí)搶答功能,且一次************
  12. ******搶答的時(shí)間由主持人設(shè)定(如30秒)**********
  13. ******。
  14. ******當(dāng)主持人啟動(dòng)“開始”鍵后,定時(shí)************
  15. ******器進(jìn)行減計(jì)時(shí),同時(shí)揚(yáng)聲器發(fā)出短************
  16. ******暫的聲響,聲響持續(xù)的時(shí)間為0.5s左************
  17. ******右。
  18. ******參賽選手在設(shè)定的時(shí)間內(nèi)進(jìn)行搶答************
  19. ******,搶答有效,定時(shí)器停止工作,顯************
  20. ******示器上顯示選手的編號(hào)和搶答的時(shí)間*********
  21. ******,并保持到主持人將系統(tǒng)清除為止。*********
  22. ******如果定時(shí)時(shí)間已到,無人搶答,本次*********
  23. ******搶答無效,系統(tǒng)報(bào)警并禁止搶答,定*********
  24. ******時(shí)顯示器上顯示00。*********************************
  25. ******************************************************************/

  26. #include<reg52.h>

  27. #define uchar unsigned char
  28. #define uint unsigned int

  29. uchar code Tab0[]=                        //用于74HC595掃描的數(shù)組
  30. {
  31.         0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80
  32. };
  33. unsigned char code LED_DATE [] =                //共陽數(shù)碼管的編碼
  34. {
  35.         0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90
  36. };

  37. sbit Set_Rest        =P3^0;                        //主持人設(shè)置時(shí)間和復(fù)位按鍵
  38. sbit Start_Jia        =P3^1;                        //主持人開始和調(diào)整時(shí)間按鍵

  39. sbit S1                =P2^0;                        // 1號(hào)選手按鍵
  40. sbit S2                =P2^1;                        // 2號(hào)選手按鍵
  41. sbit S3                =P2^2;                        // 3號(hào)選手按鍵
  42. sbit S4                =P2^3;                        // 4號(hào)選手按鍵
  43. sbit S5                =P2^4;                        // 5號(hào)選手按鍵
  44. sbit S6                =P2^5;                        // 6號(hào)選手按鍵
  45. sbit S7                =P2^6;                        // 7號(hào)選手按鍵
  46. sbit S8                =P2^7;                        // 8號(hào)選手按鍵
  47. sbit BUZZ        =P3^2;


  48. sbit DS                =P1^5;                        //74HC595  I/O
  49. sbit SHCP        =P1^6;
  50. sbit STCP        =P1^7;

  51. uint Time_Alarm=500;                //搶答時(shí)間,默認(rèn)定時(shí)500ms
  52. uchar Player=0;                                //選手號(hào)碼
  53. bit K1_bit=0;                                        //K1標(biāo)志位
  54. bit Start_bit=0;                                //開始標(biāo)志位
  55. bit Set_bit=0;                                //設(shè)置標(biāo)志位
  56. uchar Set0=0;                                //用于設(shè)置時(shí)間時(shí)顯示閃爍控制
  57. bit Set1=0;                                //用于設(shè)置時(shí)間時(shí)顯示閃爍控制

  58. void Delay_ms(uint xms);        //延時(shí)
  59. void HC595(uchar Value);                //74HC595驅(qū)動(dòng)函數(shù)
  60. void Display();                                //顯示函數(shù)
  61. void Init();
  62. void KEY1();
  63. void KEY2();                                        //開始/加時(shí)間鍵函數(shù)
  64. void Player_KEY();                        //選手搶答按鍵


  65. void main()
  66. {
  67.         Init();
  68.         while(1)
  69.                 {
  70.                         KEY1();
  71.                         KEY2();
  72.                         Player_KEY();
  73.                         Display();
  74.                 }
  75. }

  76. void Player_KEY()                        //選手搶答按鍵
  77. {
  78.         if(Start_bit==1)
  79.                 {
  80.                         if(S1==0)
  81.                                 {
  82.                                         Delay_ms(10);
  83.                                         if(S1==0)
  84.                                                 {
  85.                                                         Player=1;
  86.                                                         Start_bit=0;
  87.                                                         TR0=0;
  88.                                                         while(!S1);
  89.                                                 }
  90.                                 }

  91.                         if(S2==0)
  92.                                 {
  93.                                         Delay_ms(10);
  94.                                         if(S2==0)
  95.                                                 {
  96.                                                         Player=2;
  97.                                                         Start_bit=0;
  98.                                                         TR0=0;
  99.                                                         while(!S2);
  100.                                                 }
  101.                                 }

  102.                         if(S3==0)
  103.                                 {
  104.                                         Delay_ms(10);
  105.                                         if(S3==0)
  106.                                                 {
  107.                                                         Player=3;
  108.                                                         Start_bit=0;
  109.                                                         TR0=0;
  110.                                                         while(!S3);
  111.                                                 }
  112.                                 }

  113.                         if(S4==0)
  114.                                 {
  115.                                         Delay_ms(10);
  116.                                         if(S4==0)
  117.                                                 {
  118.                                                         Player=4;
  119.                                                         Start_bit=0;
  120.                                                         TR0=0;
  121.                                                         while(!S4);
  122.                                                 }
  123.                                 }

  124.                         if(S5==0)
  125.                                 {
  126.                                         Delay_ms(10);
  127.                                         if(S5==0)
  128.                                                 {
  129.                                                         Player=5;
  130.                                                         Start_bit=0;
  131.                                                         TR0=0;
  132.                                                         while(!S5);
  133.                                                 }
  134.                                 }

  135.                         if(S6==0)
  136.                                 {
  137.                                         Delay_ms(10);
  138.                                         if(S6==0)
  139.                                                 {
  140.                                                         Player=6;
  141.                                                         Start_bit=0;
  142.                                                         TR0=0;
  143.                                                         while(!S6);
  144.                                                 }
  145.                                 }

  146.                         if(S7==0)
  147.                                 {
  148.                                         Delay_ms(10);
  149.                                         if(S7==0)
  150.                                                 {
  151.                                                         Player=7;
  152.                                                         Start_bit=0;
  153.                                                         TR0=0;
  154.                                                         while(!S7);
  155.                                                 }
  156.                                 }

  157.                         if(S8==0)
  158.                                 {
  159.                                         Delay_ms(10);
  160.                                         if(S8==0)
  161.                                                 {
  162.                                                         Player=8;
  163.                                                         Start_bit=0;
  164.                                                         TR0=0;
  165.                                                         while(!S8);
  166.                                                 }
  167.                                 }

  168.                        
  169.                 }
  170. }

  171. void KEY2()                                        //開始/加時(shí)間鍵函數(shù)
  172. {
  173.         if(Start_Jia==0)
  174.                 {
  175.                         Delay_ms(10);
  176.                          if(Start_Jia==0)
  177.                                  {
  178.                                         if(Set_bit==0)
  179.                                                 {
  180.                                                         if(Time_Alarm==0)                //判斷是否搶答結(jié)束
  181.                                                                 {
  182.                                                                         TR0=0;
  183.                                                                         Time_Alarm=0;
  184.                                                                 }
  185.                                                         else
  186.                                                                 {
  187.                                                                         TR0=~TR0;
  188.                                                                         Start_bit=~Start_bit;
  189.                                                                 }
  190.                                                 }
  191.                                         else
  192.                                                 {
  193.                                                         Time_Alarm=(Time_Alarm+250);
  194.                                                                 if(Time_Alarm>1500)
  195.                                                                         {
  196.                                                                                 Time_Alarm=500;
  197.                                                                         }
  198.                                                 }
  199.                                         while(!Start_Jia);
  200.                                  }
  201.                 }
  202. }


  203. void KEY1()
  204. {
  205.         if(Set_Rest==0)
  206.                 {
  207.                         Delay_ms(100);
  208.                         if(Set_Rest==0)
  209.                                 {
  210.                                         Delay_ms(500);                //判斷長(zhǎng)按還是短按
  211.                                         if(Set_Rest==0)
  212.                                                 {
  213.                                                         Time_Alarm=500;
  214.                                                         TR0=0;
  215.                                                         TR1=0;
  216.                                                         Set_bit=0;
  217.                                                         Player=0;
  218.                                                         Start_bit=0;
  219.                                                         BUZZ=1;
  220.                                                         while(!Set_Rest);
  221.                                                 }
  222.                                         else
  223.                                                 {
  224.                                                         if(Start_bit==0)        //判斷是否開始
  225.                                                                 {
  226.                                                                         Set_bit=~Set_bit;
  227.                                                                         TR1=~TR1;
  228.                                                                 }
  229.                                                 }
  230.                                 }
  231.                 }
  232. }

  233. void Time_0() interrupt 1
  234. {
  235.         TH0=0xfc;                //定時(shí)器0定時(shí)1毫秒
  236.         TL0=0x18;
  237.         Time_Alarm--;
  238.         if(Time_Alarm<=0)
  239.                 {
  240.                         TR0=0;
  241.                         Start_bit=0;
  242.                         BUZZ=0;
  243.                 }
  244. }

  245. void Time_1() interrupt 3
  246. {
  247.         TH1=0x3c;                //定時(shí)器1定時(shí)50ms
  248.         TL1=0xb0;
  249.         Set0++;
  250.         if(Set0==10)
  251.                 {
  252.                         Set0=0;
  253.                         Set1=~Set1;
  254.                 }
  255. }



  256. void Display()                                //顯示函數(shù)
  257. {
  258.         uchar i=0;
  259.         for(i=0;i<3;i++)        //選手編號(hào)數(shù)碼管前三位顯示0
  260.                 {
  261.                         P0=LED_DATE[0];                //送段數(shù)據(jù)
  262.                         HC595(Tab0[i]);                //送位數(shù)據(jù)
  263.                         Delay_ms(5);                //延時(shí)
  264.                         P0=0XFF;                                //消隱
  265.                 }
  266.         P0=LED_DATE[Player];                //數(shù)碼管第4位顯示選手編號(hào),送段數(shù)據(jù)
  267.         HC595(Tab0[3]);                                //送位數(shù)據(jù)
  268.         Delay_ms(5);                                //延時(shí)
  269.         P0=0XFF;                                                //消隱

  270.         if(Set_bit==0)
  271.                 {
  272.                         P0=LED_DATE[Time_Alarm/1000];        //顯示定時(shí)時(shí)間千位
  273.                         HC595(Tab0[4]);                                //送位數(shù)據(jù)
  274.                         Delay_ms(5);                                //延時(shí)
  275.                         P0=0XFF;                                                //消隱
  276.                         P0=LED_DATE[Time_Alarm%1000/100];        //顯示定時(shí)時(shí)間百位
  277.                         HC595(Tab0[5]);                                //送位數(shù)據(jù)
  278.                         Delay_ms(5);                                //延時(shí)
  279.                         P0=0XFF;                                                //消隱
  280.                         P0=LED_DATE[Time_Alarm%100/10];        //顯示定時(shí)時(shí)間十位
  281.                         HC595(Tab0[6]);                                //送位數(shù)據(jù)
  282.                         Delay_ms(5);                                //延時(shí)
  283.                         P0=0XFF;                                                //消隱
  284.                         P0=LED_DATE[Time_Alarm%10];        //顯示定時(shí)時(shí)間個(gè)位
  285.                         HC595(Tab0[7]);                                //送位數(shù)據(jù)
  286.                         Delay_ms(5);                                //延時(shí)
  287.                         P0=0XFF;                                                //消隱
  288.                 }
  289.         if(Set_bit==1)
  290.                 {
  291.                         if(Set1==1)
  292.                                 {
  293.                                         P0=LED_DATE[Time_Alarm/1000];        //顯示定時(shí)時(shí)間千位
  294.                                         HC595(Tab0[4]);                                //送位數(shù)據(jù)
  295.                                         Delay_ms(5);                                //延時(shí)
  296.                                         P0=0XFF;                                                //消隱
  297.                                         P0=LED_DATE[Time_Alarm%1000/100];        //顯示定時(shí)時(shí)間百位
  298.                                         HC595(Tab0[5]);                                //送位數(shù)據(jù)
  299.                                         Delay_ms(5);                                //延時(shí)
  300.                                         P0=0XFF;                                                //消隱
  301.                                         P0=LED_DATE[Time_Alarm%100/10];        //顯示定時(shí)時(shí)間十位
  302.                                         HC595(Tab0[6]);                                //送位數(shù)據(jù)
  303.                                         Delay_ms(5);                                //延時(shí)
  304.                                         P0=0XFF;                                                //消隱
  305.                                         P0=LED_DATE[Time_Alarm%10];        //顯示定時(shí)時(shí)間個(gè)位
  306.                                         HC595(Tab0[7]);                                //送位數(shù)據(jù)
  307.                                         Delay_ms(5);                                //延時(shí)
  308.                                         P0=0XFF;                                                //消隱
  309.                                 }
  310.                         else
  311.                                 {
  312.                                         P0=0xff;        //顯示定時(shí)時(shí)間千位
  313.                                         HC595(Tab0[4]);                                //送位數(shù)據(jù)
  314.                                         Delay_ms(5);                                //延時(shí)
  315.                                         P0=0XFF;                                                //消隱
  316.                                         P0=0xff;        //顯示定時(shí)時(shí)間百位
  317.                                         HC595(Tab0[5]);                                //送位數(shù)據(jù)
  318.                                         Delay_ms(5);                                //延時(shí)
  319.                                         P0=0XFF;                                                //消隱
  320.                                         P0=0xff;        //顯示定時(shí)時(shí)間十位
  321.                                         HC595(Tab0[6]);                                //送位數(shù)據(jù)
  322.                                         Delay_ms(5);                                //延時(shí)
  323.                                         P0=0XFF;                                                //消隱
  324.                                         P0=0xff;        //顯示定時(shí)時(shí)間個(gè)位
  325.                                         HC595(Tab0[7]);                                //送位數(shù)據(jù)
  326.                                         Delay_ms(5);                                //延時(shí)
  327.                                         P0=0XFF;                                                //消隱

  328.                                 }
  329.                 }
  330. }

  331. void Init()
  332. {
  333.         EA=1;
  334.         TMOD=0X11;
  335.         TH0=0xfc;                //定時(shí)器0定時(shí)1毫秒
  336.         TL0=0x18;
  337.         ET0=1;

  338.         TH1=0x3c;                //定時(shí)器1定時(shí)50ms
  339.         TL1=0xb0;
  340.         ET1=1;
  341.         TR1=0;
  342. }

  343. void HC595(uchar Value)
  344. {
  345.         uchar i;
  346.         for(i=0;i<8;i++)
  347.                 {
  348.                         DS=Value&0x80;                //取數(shù)據(jù)高位
  349.                         if(DS==0x80)
  350.                                 DS=1;
  351.                         else
  352.                                 DS=0;
  353.                         SHCP=0;                        //發(fā)生一個(gè)高脈沖,數(shù)據(jù)移入寄存器
  354.                         SHCP=1;
  355.                         Value<<=1;                //左移8次,取出數(shù)據(jù)的值
  356.                 }
  357.         STCP=0;
  358.         STCP=1;                        //數(shù)據(jù)移入鎖存器
  359. }

  360. void Delay_ms(uint xms)        //延時(shí)
  361. {
  362.         uint i,j;
  363.         for(i=xms;i>0;i--)
  364.                 for(j=110;j>0;j--);
  365. }
復(fù)制代碼


評(píng)分

參與人數(shù) 1黑幣 +60 收起 理由
admin + 60 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏2 分享淘帖 頂 踩

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:73710 發(fā)表于 2017-3-15 07:13 來自手機(jī) | 只看該作者
好資料,感謝樓主分享
回復(fù)

使用道具 舉報(bào)

板凳
ID:788044 發(fā)表于 2020-6-23 19:34 | 只看該作者
感謝分享
回復(fù)

使用道具 舉報(bào)

地板
ID:868184 發(fā)表于 2020-12-28 17:21 | 只看該作者
為什么蜂鳴器/揚(yáng)聲器不響?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表