广东工业大学单片机课程设计何教授.docx
《广东工业大学单片机课程设计何教授.docx》由会员分享,可在线阅读,更多相关《广东工业大学单片机课程设计何教授.docx(14页珍藏版)》请在冰豆网上搜索。
![广东工业大学单片机课程设计何教授.docx](https://file1.bdocx.com/fileroot1/2022-12/16/d23a5243-393a-4fc0-9b90-d6ce48c664b2/d23a5243-393a-4fc0-9b90-d6ce48c664b21.gif)
广东工业大学单片机课程设计何教授
课程设计
课程名称单片机原理
题目名称单片机原理课程设计
学生学院___自动化学院_________
专业班级________
学号
学生姓名___________
指导教师何X敏、李X芳
2010年09月10日
(1)把两条输入线同一端口的一根输入线接到k0接口。
当k0=1,显示78FA;当k0=0,显示12BE:
程序如下:
ORG0000H
HJMPMAIN
ORG1000H
DB0EEH,8EH,0FEH,0E0H
DB9EH,3EH,0DAH,60H
MAIN:
MOV52H,#00H
MOV53H,#00H
MOV51H,#00H
MOV50H,#00H
MOVR5,#53H
KEYSUB:
MOVA,#03H
MOVDPTR,#7F00H
MOVX@DPTR,A
BEGIN:
MOVDPTR,#07F03H
MOVA,@DPTR
MOVR3,#0FEH
JBACC.1,DISPLAY1
JMPDISPLAY2
DISPLAY1:
MOVR2,#00H
JMPA1
DISPLAY2:
MOVR2,#04H
A1:
MOVA,R3
MOVDPTR,#7F01H
MOVX@DPTR,A
MOVDPTR,#1000H
MOVA,R2
MOVCA,@A+DPTR
MOVDPTR,#7F02H
MOVX@DPTR,A
CALLDLIMS
INCR2
MOVA,R3
RLA
MOVR3,A
JNBACC.4,BEGIN
JMPA1
DLIMS:
MOVR7,#05H
DL0:
MOVR6,#0FFH
DL1:
DJNZR6,DL1
DJNZR7,DL0
RET
END
5
4
7
9
0
E
B
A
(2)编程实现键盘并能左移显示:
程序如下:
ORG0000H
HJMPMAIN
ORG1000H
MAIN:
MOV52H,#00H
MOV53H,#00H
MOV51H,#00H
MOV50H,#00H
MOVR5,#53H
KEYSUB:
MOVA,#03H
MOVDPTR,#7F00H
MOVX@DPTR,A
BEGIN:
ACALLDIS
ACALLCLEAR
ACALLCCSCAN
JNZINK1
AJMPBEGIN
INK1:
ACALLDIS
ACALLDLIMS
ACALLDLIMS
ACALLCLEAR
ACALLCCSCAN
JNZINK2
AJMPBEGIN
INK2:
MOVR2,#0FEH
MOVR4,#00H
COLUM:
MOVDPTR,#7F01H
MOVA,R2
MOVX@DPTR,A
INCDPTR
INCDPTR
MOVXA,@DPTR
JBACC.3,LONE
MOVA,#00H
AJMPKCODE
LONE:
JB.ACC.2,NEXT
MOVA,#04H
KCODE:
ADDA,R4
ACALLPUTBUF
PUSHACC
KON:
ACALLDIS
ACALLCLEAR
ACALLCCSCAN
JNZKON
POPACC
NEXT:
INCR4
MOVA,R2
JNBACC.4,KEER
RLA
MOVR2,A
AJMPCOLUM
KERR:
AJMPBEGIN
CCSCAN:
MOVDPTR,#7F01H
MOVA,#00H
MOV@DPTR,A
INCDPTR
INCDPTR
MOVXA,@DPTR
CPLA
ANLA,#0CH
RET
CLEAR:
MOVDPTR,#7F02H
MOVA,#00H
MOVX@DPTR,A
RET
DIS:
PUSHACC
PUSH00H
PUSH03H
MOVR0,#05H
MOVR3,#0F7H
MOVA,R3
AGAIN:
MOVDPTR,#7F01H
MOVX@DPTR,A
MOVA,@R0
MOVDPTR,#DDEG
MOVCA,@A+DPTR
MOVDPTR,#7F02H
MOVX@DPTR,A
ACALLDLIMS
INCR0
MOVA,R3
JNBACC.0,OUT
RRA
MOVR3,A
AJMPAGAIN
OUT:
POP03H
POP00H
POPACC
RET
DDEG:
DB0EE,3EH,9EH,7AH
DB0E6H,0E0H,66H,0B6H
DLIMS:
MOVR7,#01H
DL0:
MOVR6,#0FFH
DL1:
DJNZR6,DL1
DJNZR7,DL0
RET
PUTBUF:
PUSH00H
MOVR7,A
MOVA,51H
MOV50H,A
MOVA,52H
MOV51H,A
MOVA,53H
MOV52H,A
MOVA,R7
MOVR3,A
POP00H
RET
END
(3)程序实现单片机售票机,以下要求:
a)当k0=1时,键入地址、价钱、SET,设置某个地址的票价,该机至少可连续设置三个不同地址的价钱:
上海864元、北京756元、广州819元。
b)当k0=0时,键入一个地址,购买的张数,按SET得到总价,再键入另一个地址、购买的张数,按SET即得总价,可计得至少三个地址的合总价。
程序如下:
#include
#include
#defineucharunsignedchar
#definepconXBYTE[0X7F00]
#definepaXBYTE[0X7F01]
#definepbXBYTE[0X7F02]
#definepcXBYTE[0X7F03]
#defineled0DBYTE[0x50]
#defineled1DBYTE[0x51]
#defineled2DBYTE[0x52]
#defineled3DBYTE[0x53]
#definekeynumDBYTE[0x54]
ucharbdatada;
sbitpc2=da^2;
sbitpc3=da^3;
sbitk0=da^1;
sbitp26=P2^6;
ucharcodetable[]={0XFC,0X60,0XBA,0XF2,0X66,0XB6,0XBE,0XE0,0XFE,0XE6};
ucharcodectrl[4]={0XFE,0XFD,0XFB,0XF7};
voidInit()
{
p26=1;
pa=0x00;
pcon=0x03;
}
VoidDelay(intsec)
{
inti,j;
for(i=0;i{
for(j=0;j<10;j++){;}
}
}
voidDisplay()
{
pa=ctrl[0];
pb=led0;
Delay(10);
pa=ctrl[1];
pb=led1;
Delay(10);
pa=ctrl[2];
pb=led2;
Delay(10);
pa=ctrl[3];
pb=led3;
Delay(10);
}
voidmain()
{
inttemp;
intstatus=0;
intsadd=0;
intcount[3]={0,0,0};
intprice[3]{0,0,0};
intnumbuff[4]={0,0,0,0};
unsignedinttotalprice=0;
Init();
led0=table[0];
led1=table[0];
led2=table[0];
led3=table[0];
while
(1)
{//1
pa=0x00;
da=pc;
if(pc2==0||pc3==0)
{//2
Delay(30);
///////////键盘扫描//////////
pa=ctrl[0];
da=pc;
if(pc2==0)keynum=7;
elseif(pc3==0)keynum=8;
else
{//3
pa=ctrl[1];
da=pc;
if(pc2==0)keynum=6;
elseif(pc3==0)keynum=1;
else
{//4
pa=ctrl[2];
da=pc;
if(pc2==0)keynum=5;
elseif(pc3==0)keynum=2;
else
{//5
pa=ctrl[3];
da=pc;
if(pc2==0)keynum=4;
elseif(pc3==0)keynum=3;
}//5
}//4
}//3
///////////以上键盘扫描////////////
while
(1)
{//3
pa=0x00;
da=pc;
if(pc3==0||pc2==0)
{//4
led0=table[numbuff[0]];
led1=table[numbuff[1]];
led2=table[numbuff[2]];
led3=table[numbuff[3]];
Display();
}//4
elsebreak;
}//3
///////////////数据处理///////////////////
da=pc;
if(k0==1)
{//3
/////////////address///////////////
if(status==0)
{//4
if(keynum>=3&&keynum<=5)
{//5
sadd=keynum;
status=1;
numbuff[3]=numbuff[2]=numbuff[1]=numbuff[0]=0;
}//5
}//4
////////////////address///////////////
///////////////price///////////////////
elseif(status==1)
{//4
switch(keynum)
{//5
case7:
{//6
if(numbuff[3]==9)numbuff[3]=0;
elsenumbuff[3]++;break;
}//6
case8:
{
if(numbuff[2]==9)numbuff[2]=0;
elsenumbuff[2]++;
break;
}
case1:
{
if(numbuff[1]==9)numbuff[1]=0;
elsenumbuff[1]++;
break;
}
case2:
{
if(numbuff[0]==9)numbuff[0]=0;
elsenumbuff[0]++;
break;
}
case6:
//set
{//6
if(sadd>=3&&sadd<=5)
{//7
price[sadd-3]=numbuff[0]*1000+numbuff[1]*100+numbuff[2]*10+numbuff[3];
sadd=0;
numbuff[3]=numbuff[2]=numbuff[1]=numbuff[0]=0;
status=0;
}//7
break;
}//6
}//5
}//4
////////////////////以上price////////////////////
}//3
elseif(k0==0)
{//3
/////////////address///////////////
if(status==0)
{//4
if(keynum>=3&&keynum<=5)
{//5
sadd=keynum;
status=2;
numbuff[3]=numbuff[2]=numbuff[1]=numbuff[0]=0;
}//5
}//4
////////////////address///////////////
elseif(status==2)//count
{//4
switch(keynum)
{//5
case7:
{//6
if(numbuff[3]==9)numbuff[3]=0;
elsenumbuff[3]++;
break;
}//6
case8:
{
if(numbuff[2]==9)numbuff[2]=0;
elsenumbuff[2]++;
break;
}
case1:
{
if(numbuff[1]==9)numbuff[1]=0;
elsenumbuff[1]++;
break;
}
case2:
{
if(numbuff[0]==9)numbuff[0]=0;
elsenumbuff[0]++;
break;
}
case6:
//set
{//6
if(sadd>=3&&sadd<=5)
{//7
count[sadd-3]=numbuff[0]*1000+numbuff[1]*100+numbuff[2]*10+numbuff[3];
totalprice+=count[sadd-3]*price[sadd-3];
numbuff[0]=totalprice/1000;
temp=totalprice-numbuff[0]*1000;
numbuff[1]=temp/100;
temp=temp-numbuff[1]*100;
numbuff[2]=temp/10;
temp=temp-numbuff[2]*10;
numbuff[3]=temp;
sadd=0;
status=0;
}//7
break;
}//6
}//5
/////////////////////switch///////
}//4
/////elseifstatus=2//////
}//3
/////k0==0////////
///////////////数据处理/////////////////
}//2
led0=table[numbuff[0]];
led1=table[numbuff[1]];
led2=table[numbuff[2]];
led3=table[numbuff[3]];
Display();
Delay(10);
}//1
}//0