基于单片机计数器及出租车计费器Word下载.docx
《基于单片机计数器及出租车计费器Word下载.docx》由会员分享,可在线阅读,更多相关《基于单片机计数器及出租车计费器Word下载.docx(34页珍藏版)》请在冰豆网上搜索。
TMOD=0X60;
TH1=0;
TL1=0x00;
TR1=1;
//TF1=0;
}
voidmain()
P2=0xff;
P0=0X00;
init_timer();
while
(1)
{
num=TL1;
display();
delay
(1);
}
/*----------------------------------------------------
显示函数
-------------------------------------------------------*/
voiddisplay()
unsignedcharx,y,z;
x=num%10;
y=(num%100)/10;
z=num/100;
P2=0x02;
P0=num_table[z];
delay
(1);
P2=0x01;
P0=num_table[y];
P2=0x00;
P0=num_table[x];
delay
(1);
voiddelay(unsignedintaa)
unsignedinti,j;
for(i=0;
i<
aa;
i++)
for(j=0;
j<
110;
j++);
汇编:
ORG0000H
LJMPMAIN
ORG0030H
MAIN:
MOVTMOD,#60H;
初始化
SETBTR1
MOVA,TL1
LCALLBCD
LCALLDISPLAY
LJMPMAIN
BCD:
MOVB,#64H
DIVAB
MOV72H,A
XCHA,B
MOVB,#0AH
MOV71H,A
MOV70H,B
RET
DISPLAY:
MOVR1,#70H;
显示模块
MOVR5,#00H
LOOP1:
MOVA,R5
MOVP2,A
MOVDPTR,#TAB
MOVA,@R1
MOVCA,@A+DPTR
MOVP0,A
LCALLDELAY
INCR1
INCR5
CJNER5,#03H,LOOP1
DELAY:
MOVR6,#4
D1:
MOVR7,#248
DJNZR7,$
DJNZR6,D1
TAB:
DB03FH,006H,05BH,04FH,66H,6DH,7DH,007H,7FH,6FH
END
Protues仿真结果
2.0~50000计数
利用T1定时器的工作方式1进行计数,当TL1=0时表示没有有效脉冲的输入,当TL1=1时,通过间接的函数对计数进加1,同时利用动态显示的功能在数码管上显示出来。
软件设计:
C语言
#include<
reg52.h>
unsignedintcodenum_table[10]={
unsignedcharcodebit_table[6]={
0x00,0x01,0x02,0x03,0x04,0x05};
unsignedintnum=100;
unsignedinti,j,k,x,y,z;
voidBCD();
unsignedchari,j;
/*--------------------------------------------------
系统初始化
----------------------------------------------------*/
voidinit()
TMOD=0X50;
TH1=0X00;
TL1=0X00;
//x=0;
y=0,z=0;
//display();
while
(1)
if(TL1!
=0X00)
BCD();
voidBCD()
TL1=0;
num++;
{
if(num==50000)
{
num=0;
}
i=num/100000;
j=num/10000%10;
k=num/1000%10;
x=num/100%10;
y=num/10%10;
z=num%10;
P2=0x05;
P0=num_table[i];
P2=0x04;
P0=num_table[j];
P2=0x03;
P0=num_table[k];
init();
MOV70H,#00H
MOV71H,#00H
MOV72H,#00H
MOV73H,#00H
MOV74H,#00H
MOV75H,#00H
MOVTMOD,#50H
MOVTL1,#00H
JZMAIN
MOVR1,#70H
CJNER5,#06H,LOOP1
MOVTL1,#00H
MOVR1,#70H
MOVR0,#75H
LOOP2:
MOVA,@R1
INCA
CJNEA,#0AH,LOOP4
MOV@R1,#00H
CJNER1,#76H,LOOP2
LJMPLOOP3
LOOP4:
MOV@R1,A
LOOP3:
RET
DB3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH
PROTUES仿真结果:
3.出租车计费器
利用T1定时器的工作方式1,当TL1=0时表示没有有效脉冲的输入,当TL1=1时通过间接的函数对计数进加1,TL1清零;
起步价为5元(2km以内),2km后,0.8元/0.5km;
unsignedcharcodetable1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
unsignedcharcodetable2[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsignedintw,x,y,z,i,j,k,km,money;
voidinit();
voiddisplay1();
voiddisplay2();
/*---------------------------------------------------
主函数
-----------------------------------------------------*/
unsignedcharm,n;
for(m=0;
m<
m++)
for(n=0;
n<
n++);
初始化函数
------------------------------------------------------*/
km=0;
money=50;
//display1();
//display2();
while(TL1==0X00)//动态扫描
display1();
display2();
}
km++;
}
/*------------------------------------------------------
出租车的计费系统进行设置
--------------------------------------------------------*/
if(km<
=20)//2KM以内起步价5元
money=50;
delay
(1);
else//2km后0.8元/0.5km且每500米计费刷新一次
if((km%10)%5==0)
money=money+8;
if((money/100)>
9)
{
money=50;
km=0;
}
else;
voiddisplay1()//显示里程
w=km/1000;
x=km/100%10;
y=km/10%10;
z=km%10;
P2=0x07;
P0=table2[w];
P2=0x06;
P0=table2[x];
P0=table1[y];
//这一位后有小数点
P0=table2[z];
voiddisplay2()//显示费用
i=money/100;
j=money/10%10;
k=money%10;
P0=table2[i];
P0=table1[j];
P0=table2[k];
SJMPMAIN
MOVTMOD,#50H
LOOP:
MOVR3,#00H
JZLOOP
LCALLMOMEY
INCR3
MOVA,R3
ZZZ:
CJNEA,#0AH,LOOP1
INCR4
MOVR3,#00H
MOV72H,R4
LOOP1:
;
SJMPBCD
MONEY:
CC:
CJNER3,#14H,LOOP2
L2:
MOVA,70H
XRLA,#00H
JZLOOP5
XRLA,#05H
LOOP5:
ADDA,#08H
MOVR5,A
JBCY,L1
JNBCY,L2
L1:
MOVP2,#00H
MOVDPTR,#TAB2
ACALLDELAY
MOVP2,#01H
MOVDPTR,#TAB1
MOVA,71H
MOVP2,#02H
MOVA,72H
MOVP2,#03H
MOVA,73H
MOVP2,#04H
MOVA,74H
MOVP2,#05H
MOVA,75H
MOVR4,#10
MOVR5,#100
LOOP3:
DJNZR5,LOOP3
DJNZR4,LOOP2
TAB1:
DB0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef
TAB2:
DB0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
END
仿真结果PROTUES
三.硬件实现
1.0~255计数器
程序:
unsignedcharcodetable[10]={
unsignedcharnum,flag;
sbitdula=P2^6;
sbitwela=P2^7;
//sbitD=P3^5;
sbitD1=P1^0;
/*---------------------------------------------------------------------
-----------------------------------------------------------------------*/
voidinit_timer()
TMOD=0X61;
TH1=0x00;
TH0=-50000/256;
TL0=-50000%256;
ET0=1;
TR0=1;
EA=1;
//P2=0xff;
//P0=0X00;
{
unsignedintbai,shi,ge;
bai=num/100;
shi=num%100/10;
ge=num%10;
wela=1;
P0=0xfe;
wela=0;
P0=0x00;
dula=1;
P0=table[bai];
dula=0;
P0=0xff;
P0=0xfd;
P0=table[shi];
P0=0xfb;
P0=table[ge];
voidQUF()interrupt1
if(flag==0)
D1=0;
flag=1;
else
D1=1;
flag=0;
}
硬件实现图:
2.0~50000计数器
unsignedintcodetable[10]={
unsignedintnum=0,flag;
TMOD=0X51;
TH0=-5000/256;
TL0=-5000%256;
x=0;
display();
P0=table[i];
P0=table[j];