您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > msp430单片机实现猜数字游戏
Msp430猜数字游戏设计设计任务:1.先以一组四位数字为原版;2.游戏参与者依次输入所猜测的四位数字,单片机根据所说入的四位数字,进行提示xAyb,x为数字位置都正确的个数,y为数字正确位置不正确的个数;3.设置按键显示正确结果。设计方案:根据第一个实验设计进行改变,在其基础上添加比较compare()子函数,运行时调用次子函数进行比较。之后显示结果。设计目的:体验单片机的趣味性。程序代码:#includemsp430x14x.h#defineucharunsignedchar#defineuintunsignedintunsignedcharyuanshu[4]={9,4,7,3};//设置原数组//unsignedcharleddata[27]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x8f,0xbf,0x89,0xb9,0xff,0x86,0xaf,0xcf,0xc7,0x91,0x8c};unsignedchartime[8]={0,10,0,11,0,0,0,0};//初始数码管unsignedchargetkey(void);unsignedintqiushu();longcompare();voidqingping(void);voidfenjie();voiddelay(unsignedintasdf){for(uintasd=0;asdasdf;asd++);};/*************************主函数************************/voidmain(void){ucharflag=0,i=9;WDTCTL=WDTPW+WDTHOLD;//关闭看门狗P5DIR|=BIT7;P5OUT&=~BIT7;P4DIR=0xff;P2DIR=0xff;P1DIR=0x0F;P1OUT=0xff;while(1){longp,q,r;i=getkey();if(i10){time[4]=time[5];time[5]=time[6];time[6]=time[7];time[7]=i;}else{switch(i){case12:time[4]=yuanshu[0];//显示原数time[5]=yuanshu[1];time[6]=yuanshu[2];time[7]=yuanshu[3];break;case13:time[0]=17;p=qiushu();qingping();flag=13;break;case14:time[0]=18;p=qiushu();qingping();flag=14;break;case15:time[0]=19;p=qiushu();qingping();flag=15;break;case10:time[0]=5;//显示sillypigtime[1]=1;time[2]=24;time[3]=24;time[4]=25;time[5]=26;time[6]=1;time[7]=9;break;case11:{q=compare();//调用compare函数p=q/10%10;r=q%10;time[0]=p;time[2]=r;break;if((r9999)||(r0)){qingping();time[5]=21;time[6]=21;time[7]=22;}elsefenjie(r);}break;}}for(ucharj=0;j=7;j++)//显示{P4OUT=0x80j;P2OUT=leddata[time[j]];delay(200);}}}//endofmainlongcompare()//添加比较compare()子函数{uchari,j,a=0,b=0,x,s;for(i=4;i=7;i++){if(yuanshu[i-4]==time[i])a++;}for(i=0;i=4;i++){for(j=4;j=7;j++)if(yuanshu[i]==time[j])b++;}s=b-a;x=a*10+s;return(x);}unsignedintqiushu()//转化成相应的十进制整数{unsignedinty;y=time[2]*100000+time[3]*10000+time[4]*1000+time[5]*100+time[6]*10+time[7];returny;}voidqingping(void)//清除屏幕{unsignedinta;for(a=7;a1;a--)time[a]=0;}voidfenjie(longr)//提取十进制整数各位数值,并显示{time[7]=r%10;time[6]=r/10%10;time[5]=r/100%10;time[4]=r/1000%10;time[3]=r/10000%10;time[2]=r/100000%10;}longfactorial(longa){longx,i;x=a;for(i=1;ia;i++){x=x*(a-i);}return(x);}uchargetkey(void)//获取键值{ucharz,x=16;P1OUT=0x00;if((!(P1IN&BIT6))||(!(P1IN&BIT7))||(!(P1IN&BIT5))||(!(P1IN&BIT4))){delay(10000);if((!(P1IN&BIT6))||(!(P1IN&BIT7))||(!(P1IN&BIT5))||(!(P1IN&BIT4))){for(z=0;z=1;z++){P1OUT=~BIT0;if(!(P1IN&BIT7)){x=3;break;}P1OUT=~BIT0;if(!(P1IN&BIT6)){x=7;break;}P1OUT=~BIT0;if(!(P1IN&BIT5)){x=11;break;}P1OUT=~BIT0;if(!(P1IN&BIT4)){x=15;break;}//line1P1OUT=~BIT1;if(!(P1IN&BIT7)){x=2;break;}P1OUT=~BIT1;if(!(P1IN&BIT6)){x=6;break;}P1OUT=~BIT1;if(!(P1IN&BIT5)){x=10;break;}if(!(P1IN&BIT4)){x=14;break;}//line3P1OUT=~BIT3;if(!(P1IN&BIT6)){x=4;break;}P1OUT=~BIT3;if(!(P1IN&BIT7)){x=0;break;}P1OUT=~BIT3;if(!(P1IN&BIT5)){x=8;break;}P1OUT=~BIT3;if(!(P1IN&BIT4)){x=12;break;}//line2P1OUT=~BIT2;if(!(P1IN&BIT6)){x=5;break;}P1OUT=~BIT2;if(!(P1IN&BIT7)){x=1;break;}P1OUT=~BIT2;if(!(P1IN&BIT5)){x=9;break;}P1OUT=~BIT2;if(!(P1IN&BIT4)){x=13;break;}}while((!(P1IN&BIT6))||(!(P1IN&BIT7))||(!(P1IN&BIT5))||(!(P1IN&BIT4)))for(ucharj=0;j=7;j++){P4OUT=0x80j;P2OUT=leddata[time[j]];delay(500);}delay(20000);while((!(P1IN&BIT6))||(!(P1IN&BIT7))||(!(P1IN&BIT5))||(!(P1IN&BIT4)))for(ucharj=0;j=7;j++){P4OUT=0x80j;P2OUT=leddata[time[j]];delay(500);}delay(20000);}}return(x);}
本文标题:msp430单片机实现猜数字游戏
链接地址:https://www.777doc.com/doc-6198124 .html