仿真版本:Proteus8.6及以上版本
程序編譯:Keil5 C51
編程語(yǔ)言:C語(yǔ)言
設(shè)計(jì)編號(hào):【Z02】
1.? 簡(jiǎn)介
本文提出了一種基于51單片機(jī)的推箱子設(shè)計(jì)。本系統(tǒng)由AT89C51單片機(jī)為控制核心,按鍵處理包括移動(dòng)小人和移動(dòng)箱子,通過(guò)移動(dòng)上下左右鍵來(lái)控制小人的移動(dòng),通過(guò)小人推動(dòng)箱子,把箱子推到指定的目的地為過(guò)關(guān),箱子只能推不能拉。
2.? 主要功能
a.支持按鍵實(shí)現(xiàn)開(kāi)始/選關(guān)、上下左右控制!
3.? 源程序(Keil5)
本設(shè)計(jì)采用keil5 C51軟件(內(nèi)附安裝包)實(shí)現(xiàn)程序設(shè)計(jì)。如需用keil4打開(kāi)程序,可自行通過(guò).c和.h文件新建工程即可。
void dischar(uc row,uc col,uc cha) //寫(xiě)ASCII碼字符,row字符行(=8圖形行),col字符列(=8圖形列),chaASCII碼
{ //顯示ASCII碼時(shí),16行x30列
ui address;
uc dat1,dat2;
address=row*30+col+0x1000; //計(jì)算文本顯示區(qū)地址
dat1=address;
dat2=address>>8;
lcdcmd2(dat1,dat2,0x24);//地址指針定位
lcdcmd1(cha,0xc4); //寫(xiě)字符,地址指針不變
}
//數(shù)字和漢字代碼寫(xiě)入自定義字符存儲(chǔ)區(qū)
void setcgram(uc count) //count是要寫(xiě)的漢字個(gè)數(shù)
{//定義自定義字符在GCRAM中存儲(chǔ)區(qū)地址,自定義字符首地址0x80(固定值),一個(gè)字符8x8=8字節(jié),對(duì)應(yīng)字節(jié)地址0x0400
ui i;
lcdcmd2(0x03,0x00,0x22); //定義自定義字符(在GCRAM中)存儲(chǔ)區(qū)字節(jié)地址高5位為0x03,
lcdcmd2(0x00,0x1c,0x24); //0x03<<11+0x0400=0x1c00(字節(jié)地址)
for(i=0;i<16*8*12/8;i++)
lcdcmd1(shucode[i],0xc0); //寫(xiě)數(shù)字(每個(gè)16*8)*12(0-9,:)到GCRAM
for(i=0;i<16*16*count/8;i++)
lcdcmd1(hancode[i],0xc0); //寫(xiě)漢字(每個(gè)16*16)*count到GCRAM
}
//寫(xiě)漢字代碼,row行,col列,sit漢字在自定義字符中的位置,每個(gè)漢字相當(dāng)于4個(gè)字符(2行2列)
void dishan(uc row,uc col,uc sit)// sit位置用han[]描述
{
dischar(row,col,sit);//每個(gè)漢字按左上,左下,右上,右下存放
dischar(row+1,col,sit+1);
dischar(row,col+1,sit+2);
dischar(row+1,col+1,sit+3);
}
//寫(xiě)數(shù)字代碼,row行,col列,sit數(shù)字在自定義字符中的位置,每個(gè)數(shù)字相當(dāng)于2個(gè)字符(2行1列)
void disshu(uc row,uc col,uc sit)// sit位置用shu[]描述
{
dischar(row,col,sit);//每個(gè)漢字按左上,左下,右上,右下存放
dischar(row+1,col,sit+1);
}
void bushu() //顯示步數(shù)
{
uc b1=0,b2=0,b3=0;
if(bu>999) {b1=9;b2=9;b3=9;}
else {b1=bu/100;b2=bu/10;b3=bu%10;}
disshu(10,23,shu[b1]);disshu(10,24,shu[b2]);disshu(10,25,shu[b3]);
dishan(10,26,han[6]);
}
void huamap() //畫(huà)關(guān)卡地圖
{
uc i,j;
for(i=0;i<8;i++)
for(j=0;j<10;j++)
{
dishan(2*i,2*j,han[map[guan-1][i][j]]);
rammap[i][j]=map[guan-1][i][j];
if(rammap[i][j]==0x07) rammap[i][j]=0x00;
}
dishan(4,22,han[4]);
disshu(4,24,shu[0]);
disshu(4,25,shu[guan]);
dishan(4,26,han[5]);
bushu();
dishan(2*renh,2*renl,han[1]);
}
void disxg() //顯示選關(guān)提示
{
dishan(0,22,han[8]);
dishan(0,24,han[9]);
dishan(0,26,han[5]);
disshu(0,28,shu[10]);
}
void shengli() //過(guò)關(guān)判斷
{
uc i,j,s1=0,s2=0;
for(i=0;i<8;i++)
for(j=0;j<10;j++)
{
if(map[guan-1][i][j]==0x07) //檢視目標(biāo)
{
s1++; //目標(biāo)計(jì)數(shù)
if(rammap[i][j]==0x02) s2++; //目標(biāo)上箱子計(jì)數(shù)
}
}
if(s1==s2) //目標(biāo)計(jì)數(shù)和目標(biāo)上箱子計(jì)數(shù)相同,則過(guò)關(guān)
{
dishan(2,20,han[10]); //顯示過(guò)關(guān)提示
dishan(2,22,han[11]);
dishan(2,24,han[12]);
dishan(2,26,han[5]);
disshu(2,28,shu[11]);
startf=0; //停止游戲
disxg(); //顯示選關(guān)提示
}
}
void delay() //延時(shí)
{
ui x;
for(x=0;x<1000;x++);
}
uc key() //檢測(cè)按鍵
{
uc x,k=0;
P3=0xff;
x=P3;
if(x==0xff) //沒(méi)按鍵跳出
goto over;
else
{
delay(); //有按鍵,延時(shí)
P3=0xff;
x=P3;
if(x==0xff) //再測(cè)
goto over;
else
{
switch(x) //獲取鍵值
{
case 0xfe:k=1;break;
case 0xfd:k=2;break;
case 0xfb:k=3;break;
case 0xf7:k=4;break;
case 0xef:k=5;break;
case 0xdf:k=6;break;
case 0xbf:k=7;break;
case 0x7f:k=8;break;
default:break;
}
wait: //等待按鍵釋放
P3=0xff;
x=P3;
if(x==0xff) goto over;
else goto wait;
}
}
over:
return k;
}
4.? 仿真proteus8.6
? 本設(shè)計(jì)采用proteus8.6軟件(內(nèi)附安裝包)實(shí)現(xiàn)仿真設(shè)計(jì),如下圖所示。proteus軟件一般存在高低版本兼容問(wèn)題,該仿真是通過(guò)proteus8.6導(dǎo)出的,所以請(qǐng)盡量使用proteus8.6打開(kāi)文件,低于proteus8.6的版本不能夠正常使用,高于proteus8.6的版本除了proteus8.8/8.9理論上可以正常使用。
仿真界面:
5.? 設(shè)計(jì)報(bào)告
設(shè)計(jì)報(bào)告主要包括硬件電路、軟件設(shè)計(jì)、仿真調(diào)試等等,僅供參考。
6.? 資料清單
設(shè)計(jì)資料主要包括C程序+仿真+設(shè)計(jì)報(bào)告+使用說(shuō)明+元件清單+操作視頻等。
7.? 資料獲取鏈接
百度網(wǎng)盤(pán)下載鏈接:【Z02】百度網(wǎng)盤(pán)下載鏈接
更多單片機(jī)仿真設(shè)計(jì)合集:單片機(jī)仿真設(shè)計(jì)合集