当前位置: 代码迷 >> 单片机 >> 大一翻新项目之单片机实现猜数字小游戏
  详细解决方案

大一翻新项目之单片机实现猜数字小游戏

热度:98   发布时间:2016-04-28 15:28:30.0
大一创新项目之单片机实现猜数字小游戏
//组长:WWR//组员:JHL,HM,WJL,DYN//指导教室:WQ#include <reg52.h>#include <intrins.h>#include<stdlib.h>#include <reg52.h>#include "74HC595.H"#include cishu 3 //才数字次数的控制unsigned char  i;unsigned int  m,n;#define  num  sizeof(tab)unsigned char code aa[]= {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};unsigned char code tab[]={   //取模方式 阴码 列扫描 逆向    0x00,0x3E,0x41,0x49,0x3A,0x00,0x00,0x00,/*"G",0*/    0x00,0x7E,0x11,0x11,0x7E,0x00,0x00,0x00,/*"A",1*/    0x00,0x7F,0x06,0x06,0x7F,0x00,0x00,0x00,/*"M",2*/    0x00,0x7F,0x49,0x49,0x41,0x00,0x00,0x00,/*"E",3*/    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",4*/    0x00,0x7F,0x41,0x41,0x7F,0x00,0x00,0x00,/*"O",5*/    0x00,0x0F,0x70,0x70,0x0F,0x00,0x00,0x00,/*"V",6*/    0x00,0x7F,0x49,0x49,0x41,0x00,0x00,0x00,/*"E",7*/    0x00,0x7F,0x19,0x29,0x46,0x00,0x00,0x00,/*"R",8*/    /*0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",9    0x00,0x01,0x7F,0x01,0x01,0x00,0x00,0x00,/*"T",10    0x00,0x7F,0x08,0x08,0x7F,0x00,0x00,0x00,/*"H",11    0x00,0x7E,0x11,0x11,0x7E,0x00,0x00,0x00,/*"A",12    0x00,0x7F,0x06,0x18,0x7F,0x00,0x00,0x00,/*"N",13    0x00,0x7F,0x08,0x14,0x63,0x00,0x00,0x00,/*"K",14    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",15    0x03,0x04,0x78,0x04,0x03,0x00,0x00,0x00,/*"Y",16    0x00,0x7F,0x41,0x41,0x7F,0x00,0x00,0x00,/*"O",17    0x00,0x3F,0x40,0x40,0x3F,0x00,0x00,0x00,/*"U",18*/};#define uchar unsigned char#define uint  unsigned int//p0    数码管         编码//p1    键盘           编码//p2    数码管开关     编码sbit P20=P2^0;sbit P21=P2^1;sbit P22=P2^2;sbit P23=P2^3;sbit P24=P2^4;sbit P25=P2^5;sbit P26=P2^6;sbit P27=P2^7;sbit fmq=P3^6;sbit P00=P0^0;sbit P01=P0^1;uchar  key;unsigned char code table[]= {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xBF,0x9c};/*共阳数码管显示字型码数组 0123456789 abcdef - o                    P0显示*/unsigned char code w8[8]= {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//数码管的开关unsigned char code go[8]= {0x90,0x9c,0x90,0x9c,0x90,0x9c,0x90,0x9c};unsigned char code null[8]= {0xbf,0xbf,0xbf,0xbf,0xbf,0xbf,0xbf,0xbf};unsigned char code t1[8]= {0xf9,0xf9,0xf9,0xf9,0xf9,0xf9,0xf9,0xf9};unsigned char code t2[8]= {0xa4,0xa4,0xa4,0xa4,0xa4,0xa4,0xa4,0xa4};unsigned char code t3[8]= {0xb0,0xb0,0xb0,0xb0,0xb0,0xb0,0xb0,0xb0};unsigned char  ts[8];unsigned char code duo[8]= {0xbf,0xbf,0xbf,0xbf,0xbf,0xbf,0xbf,0xbf};unsigned char code dou[8]= {0xf9,0xf9,0xf9,0xf9,0xbf,0xbf,0xbf,0xa1};unsigned char code sin[8]= {0xc0,0xc0,0xc0,0xc0,0xbf,0xbf,0xbf,0x92};unsigned char code key_code[]= {0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77};unsigned char code good[8]= {0x42,0xa3,0xa3,0x21,0x42,0xa3,0xa3,0x21} ;void delayshort(unsigned int k);int print(unsigned char a[],unsigned char b[]);int input();uchar  keyscan();void beep();void delay500us();void delay1ms(uint i);int panduan(int o,int g);void output(uint a,uint b);int suijishu();/*--------------------------*///显示函数void Display(void){    Ser_IN((aa[i])); //列扫描数据    Ser_IN(tab[i + n]);//查表取出行扫描数据    Par_OUT();//输出显示    i++;    if(i == 8) i = 0;//循环扫描    m++;    if(m == 500)    {        m = 0;    //滚动速度控制        n++;    }    if(n == num-7) n = 0;//循环显示}/*--------------------------*///定时器初始化void T0_init(void){    TMOD = 0xf8;    TH0  = 0xcc;//5000us    TL0  = 0x00;    IE   = 0x82;    TR0  = 1;}/*--------------------------*///定时器中断服务void T0_intservice(void) interrupt 1 using 0{    TH0 = 0xf8;    TL0 = 0xcc;    Display();}int main (void){    uint x,y,w,s,c1=0;    print(w8,t1);    print(w8,t2);    print(w8,t3);    print(w8,go) ; //显示123go    do    {        print(w8,dou) ; //1111 for double        print(w8,sin);        w=input();    }    while((w!=1111&&w!=0000));    if(w==1111)//两人玩    {        x=input();        // print(w8,null); //显示‘-’        while(1)        {            y=input();c1++;if(c1>cishu) {  P2=0x00;  print(w8,duo);  break;}            if(panduan(x,y)==1)  break;        }    }    else  //一人玩    {        s=suijishu();        while(1)        {            y=input();c1++;if(c1>cishu) {  P2=0x00;  print(w8,duo);  break;}            if(panduan(s,y)==1)  break;        }    }    T0_init();    while(1);}void delayshort(unsigned int k){    unsigned int data i,j;    for(i=0; i<k; i++)    {        for(j=0; j<121; j++)        {            ;        }    }}int print(unsigned char a[],unsigned char b[]){    uint j=300;    while(j--)    {        unsigned int i;        for(i=0; i<8; i++)        {            P2=a[i] ;            P0=b[i];            delayshort(1) ;        }    }}int input(){    uchar i=3,j,tt,c;    uint m,nu[4];    for(j=0; j<4; j++)    {        P27=1;        P0 = 0xbf;    //数码管显示"-"        P2=w8[j];        P1 = 0xff;        while(1)        {            P1=0xf0;            if((P1&0xf0)!=0xf0)            {                keyscan();                if(key<=9&&key>=0)                {                    P0=table[key];                    nu[j]=key;                    for(tt=0; tt<200; tt++)                    {                        beep();                    }                    fmq=1;                    delay1ms(200);                    break;                }            }        }        delay1ms(1);    }    m=100;    while(m--)    {        P2=w8[0];        P0=table[nu[0]];        delay1ms(1);        P2=w8[1];        P0=table[nu[1]];        delay1ms(1);        P2=w8[2];        P0=table[nu[2]];        delay1ms(1);        P2=w8[3];        P0=table[nu[3]];        delay1ms(1);    }    c=4;    while(c--)    {        for(tt=0; tt<200; tt++)        {            beep();        }        fmq=1;        delay1ms(200);    }    return(1000*nu[0]+100*nu[1]+10*nu[2]+nu[3]);}/**********************************************************键盘扫描子函数**********************************************************/uchar  keyscan(){    uchar  scan1,scan2,keycode,j;    P1=0xf0;    scan1=P1;    if((scan1&0xf0)!=0xf0)           //判键是否按下    {        delay1ms(20);                   //延时20ms        scan1=P1;        if((scan1&0xf0)!=0xf0)         //二次判键是否按下        {            P1=0x0f;  //线反转法的核心            scan2=P1;            keycode=scan1|scan2;         //组合成键编码            for(j=0; j<=15; j++)            {                if(keycode== key_code[j])  //查表得键值                {                    key=j;                    return(key);                }            }        }    }    else        P1=0xff; //P1口写1 输入状态    return (16);//"-"}void beep()//产生1KHZ频率声音的函数{    fmq=0;    delay500us();    fmq=1;    delay500us();}void delay500us(){    unsigned char j;    for(j=0; j<57; j++) //500us基准延时程序    {        ;    }}void delay1ms(uint i){    uchar j;    while(i--)    {        for(j=0; j<115; j++) //1ms基准延时程序        {            ;        }    }}int panduan(int o,int g){    int i,j,m[4],n[4],a=0,b=0;    for(i=3; i>=0; i--)    {        m[i]=o%10;        o=o/10;        n[i]=g%10;        g=g/10;    }    for(i=0; i<4; i++)    {        if(m[i]==n[i])  a++;        for(j=0; j<4; j++)        {            if(m[i]==n[j]&&i!=j) b++;        }    }    if(a==4)    {        print(w8,good);        return 1;    }    else    {        ts[0]=table[a];        ts[1] = 0x88;        ts[2]=table[b];        ts[3] = 0x80;        ts[4]=table[a];        ts[5] = 0x88;        ts[6]=table[b];        ts[7] = 0x80;        print(w8,ts);        print(w8,ts);        return 0;    }}int suijishu(){    int g,s,b,q;    q=rand()%10;    while(q==b)    {        b=rand()%10;    }    while(s==b||s==q)    {        s=rand()%10;    }    while(g==s||g==q||g==b)    {        g=rand()%10;    }    return  (1000*q+100*b+10*s+g);}


 

  相关解决方案