找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1246|回复: 0
打印 上一主题 下一主题
收起左侧

八路抢答器的c程序

[复制链接]
跳转到指定楼层
楼主
ID:360655 发表于 2018-6-27 23:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<reg52.h>

#define uchar unsigned char
#define uint unsigned int

uchar code Tab0[]=                        //??74HC595?????
{
        0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80
};
unsigned char code LED_DATE [] =                //????????
{
        0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90
};

sbit Set_Rest        =P3^0;                        //????????????
sbit Start_Jia        =P3^1;                        //????????????

sbit S1                =P2^0;                        // 1?????
sbit S2                =P2^1;                        // 2?????
sbit S3                =P2^2;                        // 3?????
sbit S4                =P2^3;                        // 4?????
sbit S5                =P2^4;                        // 5?????
sbit S6                =P2^5;                        // 6?????
sbit S7                =P2^6;                        // 7?????
sbit S8                =P2^7;                        // 8?????
sbit BUZZ        =P3^2;


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

uint Time_Alarm=500;                //????,????500ms
uchar Player=0;                                //????
bit K1_bit=0;                                        //K1???
bit Start_bit=0;                                //?????
bit Set_bit=0;                                //?????
uchar Set0=0;                                //?????????????
bit Set1=0;                                //?????????????

void Delay_ms(uint xms);        //??
void HC595(uchar Value);                //74HC595????
void Display();                                //????
void Init();
void KEY1();
void KEY2();                                        //??/??????
void Player_KEY();                        //??????


void main()
{
        Init();
        while(1)
                {
                        KEY1();
                        KEY2();
                        Player_KEY();
                        Display();
                }
}

void Player_KEY()                        //??????
{
        if(Start_bit==1)
                {
                        if(S1==0)
                                {
                                        Delay_ms(10);
                                        if(S1==0)
                                                {
                                                        Player=1;
                                                        Start_bit=0;
                                                        TR0=0;
                                                        while(!S1);
                                                }
                                }

                        if(S2==0)
                                {
                                        Delay_ms(10);
                                        if(S2==0)
                                                {
                                                        Player=2;
                                                        Start_bit=0;
                                                        TR0=0;
                                                        while(!S2);
                                                }
                                }

                        if(S3==0)
                                {
                                        Delay_ms(10);
                                        if(S3==0)
                                                {
                                                        Player=3;
                                                        Start_bit=0;
                                                        TR0=0;
                                                        while(!S3);
                                                }
                                }

                        if(S4==0)
                                {
                                        Delay_ms(10);
                                        if(S4==0)
                                                {
                                                        Player=4;
                                                        Start_bit=0;
                                                        TR0=0;
                                                        while(!S4);
                                                }
                                }

                        if(S5==0)
                                {
                                        Delay_ms(10);
                                        if(S5==0)
                                                {
                                                        Player=5;
                                                        Start_bit=0;
                                                        TR0=0;
                                                        while(!S5);
                                                }
                                }

                        if(S6==0)
                                {
                                        Delay_ms(10);
                                        if(S6==0)
                                                {
                                                        Player=6;
                                                        Start_bit=0;
                                                        TR0=0;
                                                        while(!S6);
                                                }
                                }

                        if(S7==0)
                                {
                                        Delay_ms(10);
                                        if(S7==0)
                                                {
                                                        Player=7;
                                                        Start_bit=0;
                                                        TR0=0;
                                                        while(!S7);
                                                }
                                }

                        if(S8==0)
                                {
                                        Delay_ms(10);
                                        if(S8==0)
                                                {
                                                        Player=8;
                                                        Start_bit=0;
                                                        TR0=0;
                                                        while(!S8);
                                                }
                                }


                }
}

void KEY2()                                        //??/??????
{
        if(Start_Jia==0)
                {
                        Delay_ms(10);
                         if(Start_Jia==0)
                                 {
                                        if(Set_bit==0)
                                                {
                                                        if(Time_Alarm==0)                //????????
                                                                {
                                                                        TR0=0;
                                                                        Time_Alarm=0;
                                                                }
                                                        else
                                                                {
                                                                        TR0=~TR0;
                                                                        Start_bit=~Start_bit;
                                                                }
                                                }
                                        else
                                                {
                                                        Time_Alarm=(Time_Alarm+250);
                                                                if(Time_Alarm>1500)
                                                                        {
                                                                                Time_Alarm=500;
                                                                        }
                                                }
                                        while(!Start_Jia);
                                 }
                }
}


void KEY1()
{
        if(Set_Rest==0)
                {
                        Delay_ms(100);
                        if(Set_Rest==0)
                                {
                                        Delay_ms(500);                //????????
                                        if(Set_Rest==0)
                                                {
                                                        Time_Alarm=500;
                                                        TR0=0;
                                                        TR1=0;
                                                        Set_bit=0;
                                                        Player=0;
                                                        Start_bit=0;
                                                        BUZZ=1;
                                                        while(!Set_Rest);
                                                }
                                        else
                                                {
                                                        if(Start_bit==0)        //??????
                                                                {
                                                                        Set_bit=~Set_bit;
                                                                        TR1=~TR1;
                                                                }
                                                }
                                }
                }
}

void Time_0() interrupt 1
{
        TH0=0xfc;                //???0??1??
        TL0=0x18;
        Time_Alarm--;
        if(Time_Alarm<=0)
                {
                        TR0=0;
                        Start_bit=0;
                        BUZZ=0;
                }
}

void Time_1() interrupt 3
{
        TH1=0x3c;                //???1??50ms
        TL1=0xb0;
        Set0++;
        if(Set0==10)
                {
                        Set0=0;
                        Set1=~Set1;
                }
}



void Display()                                //????
{
        uchar i=0;
        for(i=0;i<3;i++)        //????????????0
                {
                        P0=LED_DATE[0];                //????
                        HC595(Tab0[i]);                //????
                        Delay_ms(5);                //??
                        P0=0XFF;                                //??
                }
        P0=LED_DATE[Player];                //????4???????,????
        HC595(Tab0[3]);                                //????
        Delay_ms(5);                                //??
        P0=0XFF;                                                //??

        if(Set_bit==0)
                {
                        P0=LED_DATE[Time_Alarm/1000];        //????????
                        HC595(Tab0[4]);                                //????
                        Delay_ms(5);                                //??
                        P0=0XFF;                                                //??
                        P0=LED_DATE[Time_Alarm%1000/100];        //????????
                        HC595(Tab0[5]);                                //????
                        Delay_ms(5);                                //??
                        P0=0XFF;                                                //??
                        P0=LED_DATE[Time_Alarm%100/10];        //????????
                        HC595(Tab0[6]);                                //????
                        Delay_ms(5);                                //??
                        P0=0XFF;                                                //??
                        P0=LED_DATE[Time_Alarm%10];        //????????
                        HC595(Tab0[7]);                                //????
                        Delay_ms(5);                                //??
                        P0=0XFF;                                                //??
                }
        if(Set_bit==1)
                {
                        if(Set1==1)
                                {
                                        P0=LED_DATE[Time_Alarm/1000];        //????????
                                        HC595(Tab0[4]);                                //????
                                        Delay_ms(5);                                //??
                                        P0=0XFF;                                                //??
                                        P0=LED_DATE[Time_Alarm%1000/100];        //????????
                                        HC595(Tab0[5]);                                //????
                                        Delay_ms(5);                                //??
                                        P0=0XFF;                                                //??
                                        P0=LED_DATE[Time_Alarm%100/10];        //????????
                                        HC595(Tab0[6]);                                //????
                                        Delay_ms(5);                                //??
                                        P0=0XFF;                                                //??
                                        P0=LED_DATE[Time_Alarm%10];        //????????
                                        HC595(Tab0[7]);                                //????
                                        Delay_ms(5);                                //??
                                        P0=0XFF;                                                //??
                                }
                        else
                                {
                                        P0=0xff;        //????????
                                        HC595(Tab0[4]);                                //????
                                        Delay_ms(5);                                //??
                                        P0=0XFF;                                                //??
                                        P0=0xff;        //????????
                                        HC595(Tab0[5]);                                //????
                                        Delay_ms(5);                                //??
                                        P0=0XFF;                                                //??
                                        P0=0xff;        //????????
                                        HC595(Tab0[6]);                                //????
                                        Delay_ms(5);                                //??
                                        P0=0XFF;                                                //??
                                        P0=0xff;        //????????
                                        HC595(Tab0[7]);                                //????
                                        Delay_ms(5);                                //??
                                        P0=0XFF;                                                //??

                                }
                }
}

void Init()
{
        EA=1;
        TMOD=0X11;
        TH0=0xfc;                //???0??1??
        TL0=0x18;
        ET0=1;

        TH1=0x3c;                //???1??50ms
        TL1=0xb0;
        ET1=1;
        TR1=0;
}

void HC595(uchar Value)
{
        uchar i;
        for(i=0;i<8;i++)
                {
                        DS=Value&0x80;                //?????
                        if(DS==0x80)
                                DS=1;
                        else
                                DS=0;
                        SHCP=0;                        //???????,???????
                        SHCP=1;
                        Value<<=1;                //??8?,??????
                }
        STCP=0;
        STCP=1;                        //???????
}

void Delay_ms(uint xms)        //??
{
        uint i,j;
        for(i=xms;i>0;i--)
                for(j=110;j>0;j--);
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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