include了挺多不需要的头文件=-= ,,
贴出源码
- #include
- #include
- //#include
- #include
- //#include
- //#include
- #define DATAP P0
- #define KeyPort P3
- #define clrp sendbyte(0x01,0)
- #define MAXTRY 5
- ***it RS = P2^2;
- ***it RW = P2^3;
- ***it E = P2^4;
- //LCD相关
- void newp(); //初始化
- void viewstring(char x,char y,char *bb);
- void viewchar(char x,char y,char bb);
- bit busyp(); //判忙 忙为1
- void sendbyte(char bb,bit dorc);
- //主程序相关
- void newgame();
- void DelayMs(unsigned char t);
- void newnumber(); //新随机数
- bit badnum();
- void getAB(); //解析答案函数
- //键盘相关
- unsigned char KeyPro(void);
- unsigned char KeyScan(void);
- unsigned char now=0,num[4];//目前画面0首页1用户输入2显示信息3认输中
- unsigned char ipsp=0,input[4];
- unsigned char wrongtimes,key,ipA,ipB,ipAB[5];
- void main ()
- {
- unsigned char keynum,n;
- TMOD=0x02;
- TL0=0;
- TH0=0;
- TR0=1;
-
- newp();
- newgame();
- head:
- while(1)
- {
- if (now==0)
- {
- keynum=Keypro() ;
- if(keynum!=0xff)
- {
- now=1;
- clrp;
- newnumber();
- }
- }
- if (now==1)
- {
- keynum=Keypro();
- if(keynum!=0xff)
- {
- if(keynum<10)//数字
- {
- if(ipsp<4)//未满
- {
- input[ipsp]=keynum;
- ipsp++;
- viewchar(ipsp-1,0,keynum+48);
- }
- }
- if(keynum==11)//退格
- {
- viewstring(ipsp-1,0,"");
- ipsp--;
- }
- if(keynum==12)//确认
- {
- if (ipsp==4)
- {
-
- if (badnum())//不合法
- {
- viewstring(0,0,"NumbersShouldBe");
- viewstring(3,1,"Different!");
- now=2;//显示消息中
- ipsp=0;
- goto head;
- }
- getAB(); //这是一个判断正确个数的函数 A的个数放到ipA中 B放到ipB中
- if (ipA==4) //正确
- {
- viewstring(0,0,"RIGHT!");
- for (n=0;n<4;n++) viewchar(6+n,1,num[n]+'0');
- now=3;
- }
-
- else
- {
- if(wrongtimes
- {
-
- clrp;
- viewstring(0,0,"WRONG:");
- if(wrongtimes==MAXTRY-1)viewstring(6,0,"DieSOON!");
- viewstring(6,1,ipAB);
- wrongtimes++;
- ipsp=0;
- now=2;//显示信息中
-
- }
- else
- {
- clrp;
- viewstring(0,0,"YouLostT T");
- for (n=0;n<4;n++) viewchar(6+n,1,num[n]+'0');
- now=3;//输.
- }
- }
- //错误
- }
- }
- if(keynum==13)//认输
- {
- viewstring(0,0,"HAHAHAHAHAHAHA!!");
- for (n=0;n<4;n++) viewchar(6+n,1,num[n]+'0');
- now=3;//输.
-
- }
- }
- }
- if(now==2) //显示信息中 等待按任意键继续游戏
- {
- keynum=Keypro() ;
- if(keynum!=0xff)
- {now=1;clrp;}
- }
- if(now==3) //认输或者输了或者赢了总之就是准备开始新游戏
- {
- keynum=Keypro() ;
- if(keynum!=0xff)
- {
- clrp;
- newgame();
- }
- }
- }
- }
- void newgame()
- {
- viewstring(0,0," Guess Number!");
- ipsp=0;
- wrongtimes=0;
- now=0;
- }
- bit badnum()//1为坏数字 0为好数字
- {
- char n,i;
- for (n=0;n<4;n++)
- {
- for (i=0;i
- {
- if(input[n]==input[i]) return 1;
- }
- }
- return 0;
- }
- void getAB()
- {
- char n,m;
- ipA=0;
- ipB=0;
- for (n=0;n<4;n++)
- {
- for (m=0;m<4;m++)
- {
- if(input[n]==num[m])
- {
- if (n==m)
- {ipA++;}
- else
- {ipB++;}
- }
- }
- }
- ipAB[0]=ipA+'0';
- ipAB[1]='A';
- ipAB[2]=ipB+'0';
- ipAB[3]='B';
- ipAB[4]=0;
- }
- char getrand() //取0-9的随机数
- {
- srand(TL0);
- return (rand() % 10);
- }
- void newnumber()
- {
- unsigned char n,i;
- bit h;//一个标记
-
- for (n=0;n<4;n++)
- {
- h=1;
- while(h)
- {
- h=0;
- num[n]=getrand();
- for (i=0;i
- {
- if(num[n]==num[i]) h=1;
- }
- }
-
- }
-
- }
- void DelayUs2x(unsigned char t)
- {
- while(--t);
- }
- void DelayMs(unsigned char t)
- {
-
- while(t--)
- {
- //大致延时1mS
- DelayUs2x(245);
- DelayUs2x(245);
- }
- }
- unsigned char KeyScan(void) //键盘扫描函数,使用行列逐级扫描法
- {
- unsigned char Val;
- KeyPort=0xf0;//高四位置高,低四位拉低
- if(KeyPort!=0xf0)//表示有按键按下
- {
- DelayMs(10); //去抖
- if(KeyPort!=0xf0)
- { //表示有按键按下
- KeyPort=0xfe; //检测第一行
- if(KeyPort!=0xfe)
- {
- Val=KeyPort&0xf0;
- Val+=0x0e;
- while(KeyPort!=0xfe);
- DelayMs(10); //去抖
- while(KeyPort!=0xfe);
- return Val;
- }
- KeyPort=0xfd; //检测第二行
- if(KeyPort!=0xfd)
- {
- Val=KeyPort&0xf0;
- Val+=0x0d;
- while(KeyPort!=0xfd);
- DelayMs(10); //去抖
- while(KeyPort!=0xfd);
- return Val;
- }
- KeyPort=0xfb; //检测第三行
- if(KeyPort!=0xfb)
- {
- Val=KeyPort&0xf0;
- Val+=0x0b;
- while(KeyPort!=0xfb);
- DelayMs(10); //去抖
- while(KeyPort!=0xfb);
- return Val;
- }
- KeyPort=0xf7; //检测第四行
- if(KeyPort!=0xf7)
- {
- Val=KeyPort&0xf0;
- Val+=0x07;
- while(KeyPort!=0xf7);
- DelayMs(10); //去抖
- while(KeyPort!=0xf7);
- return Val;
- }
- }
- }
- return 0xff;
- }
- unsigned char KeyPro(void) //键盘
- {
- switch(KeyScan())
- {
- case 0xee:return 1;break;//0 按下相应的键显示相对应的码值
- case 0xde:return 2;break;//1
- case 0xbe:return 3;break;//2
- case 0x7e:return 11;break;//3
- case 0xed:return 4;break;//4
- case 0xdd:return 5;break;//5
- case 0xbd:return 6;break;//6
- case 0x7d:return 12;break;//7
- case 0xeb:return 7;break;//8
- case 0xdb:return 8;break;//9
- case 0xbb:return 9;break;//a
- case 0x7b:return 0xff;break;//b
- case 0xe7:return 0;break;//c
- case 0xd7:return 0;break;//d
- case 0xb7:return 0xff;break;//e
- case 0x77:return 13;break;//f
- default:return 0xff;break;
- }
- }
- void newp()
- {
-
- sendbyte(0x38,0);
- sendbyte(0x0F,0);
- sendbyte(0x06,0);
- clrp;
- }
- void viewstring(char x,char y,char *bb)
- {
- if (y == 0)
- {
- sendbyte(0x80 + x,0); //表示第一行
- }
- else
- {
- sendbyte(0xC0 + x,0); //表示第二行
- }
- while (*bb)
- {
- sendbyte(*bb,1);
- bb++;
- }
- }
- void viewchar(char x,char y,char bb)
- {
- if (y == 0)
- {
- sendbyte(0x80 + x,0); //表示第一行
- }
- else
- {
- sendbyte(0xC0 + x,0); //表示第二行
- }
- sendbyte(bb,1);
- }
- bit busyp()
- {
- DATAP=0xFF;
- E=0;
- RS=0;
- RW=1; //READ
- E=1;
- _nop_();
- return (DATAP & 0x80);
- }
- void sendbyte(char bb,bit dorc) //bb发送的东东 dorc=data or command=1 or 0
- {
- while(busyp());
- E=0;
- RS=dorc;
- RW=0;
- DATAP=bb;
- E=1;
-
- _nop_();
- E=0;
- _nop_();
- }
复制代码
|