51单片机数字钟实验.docx
《51单片机数字钟实验.docx》由会员分享,可在线阅读,更多相关《51单片机数字钟实验.docx(10页珍藏版)》请在冰豆网上搜索。
51单片机数字钟实验
51单片机数字钟实验(原理图及程序)
1.实验任务
(1. 开机时,显示12:
00:
00的时间开始计时;
(2. P0.0/AD0控制“秒”的调整,每按一次加1秒;
(3. P0.1/AD1控制“分”的调整,每按一次加1分;
(4. P0.2/AD2控制“时”的调整,每按一次加1个小时;
2.电路原理图
图4.20.1
3.系统板上硬件连线
(1. 把“单片机系统”区域中的P1.0-P1.7端口用8芯排线连接到“动态数码显示”区域中的A-H端口上;
(2. 把“单片机系统:
区域中的P3.0-P3.7端口用8芯排线连接到“动态数码显示”区域中的S1-S8端口上;
(3. 把“单片机系统”区域中的P0.0/AD0、P0.1/AD1、P0.2/AD2端口分别用导线连接到“独立式键盘”区域中的SP3、SP2、SP1端口上;
4.相关基本知识
(1. 动态数码显示的方法
(2. 独立式按键识别过程
(3. “时”,“分”,“秒”数据送出显示处理方法
5.程序框图
6.汇编源程序
SECONDEQU30H
MINITEEQU31H
HOUREQU32H
HOURKBITP0.0
MINITEK BITP0.1
SECONDK BITP0.2
DISPBUF EQU40H
DISPBIT EQU48H
T2SCNTA EQU49H
T2SCNTB EQU4AH
TEMPEQU4BH
ORG00H
LJMPSTART
ORG0BH
LJMPINT_T0
START:
MOVSECOND,#00H
MOVMINITE,#00H
MOVHOUR,#12
MOVDISPBIT,#00H
MOVT2SCNTA,#00H
MOVT2SCNTB,#00H
MOVTEMP,#0FEH
LCALLDISP
MOVTMOD,#01H
MOVTH0,#(65536-2000)/256
MOVTL0,#(65536-2000)MOD256
SETBTR0
SETBET0
SETBEA
WT:
JBSECONDK,NK1
LCALLDELY10MS
JBSECONDK,NK1
INCSECOND
MOVA,SECOND
CJNEA,#60,NS60
MOVSECOND,#00H
NS60:
LCALLDISP
JNBSECONDK,$
NK1:
JBMINITEK,NK2
LCALLDELY10MS
JBMINITEK,NK2
INCMINITE
MOVA,MINITE
CJNEA,#60,NM60
MOVMINITE,#00H
NM60:
LCALLDISP
JNBMINITEK,$
NK2:
JBHOURK,NK3
LCALLDELY10MS
JBHOURK,NK3
INCHOUR
MOVA,HOUR
CJNEA,#24,NH24
MOVHOUR,#00H
NH24:
LCALLDISP
JNBHOURK,$
NK3:
LJMPWT
DELY10MS:
MOVR6,#10
D1:
MOVR7,#248
DJNZR7,$
DJNZR6,D1
RET
DISP:
MOVA,#DISPBUF
ADDA,#8
DECA
MOVR1,A
MOVA,HOUR
MOVB,#10
DIVAB
MOV@R1,A
DECR1
MOVA,B
MOV@R1,A
DECR1
MOVA,#10
MOV@R1,A
DECR1
MOVA,MINITE
MOVB,#10
DIVAB
MOV@R1,A
DECR1
MOVA,B
MOV@R1,A
DECR1
MOVA,#10
MOV@R1,A
DECR1
MOVA,SECOND
MOVB,#10
DIVAB
MOV@R1,A
DECR1
MOVA,B
MOV@R1,A
DECR1
RET
INT_T0:
MOVTH0,#(65536-2000)/256
MOVTL0,#(65536-2000)MOD256
MOVA,#DISPBUF
ADDA,DISPBIT
MOVR0,A
MOVA,@R0
MOVDPTR,#TABLE
MOVCA,@A+DPTR
MOVP1,A
MOVA,DISPBIT
MOVDPTR,#TAB
MOVCA,@A+DPTR
MOVP3,A
INCDISPBIT
MOVA,DISPBIT
CJNEA,#08H,KNA
MOVDISPBIT,#00H
KNA:
INCT2SCNTA
MOVA,T2SCNTA
CJNEA,#100,DONE
MOVT2SCNTA,#00H
INCT2SCNTB
MOVA,T2SCNTB
CJNEA,#05H,DONE
MOVT2SCNTB,#00H
INCSECOND
MOVA,SECOND
CJNEA,#60,NEXT
MOVSECOND,#00H
INCMINITE
MOVA,MINITE
CJNEA,#60,NEXT
MOVMINITE,#00H
INCHOUR
MOVA,HOUR
CJNEA,#24,NEXT
MOVHOUR,#00H
NEXT:
LCALLDISP
DONE:
RETI
TABLE:
DB3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH,40H
TAB:
DB0FEH,0FDH,0FBH,0F7H,0EFH,0DFH,0BFH,07FH
END
7.C语言源程序
#include
unsignedcharcodedispcode[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00};
unsignedchardispbitcode[]={0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f};
unsignedchardispbuf[8]={0,0,16,0,0,16,0,0};
unsignedchardispbitcnt;
unsignedcharsecond;
unsignedcharminite;
unsignedcharhour;
unsignedinttcnt;
unsignedcharmstcnt;
unsignedchari,j;
voidmain(void)
{
TMOD=0x02;
TH0=0x06;
TL0=0x06;
TR0=1;
ET0=1;
EA=1;
while
(1)
{
if(P0_0==0)
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(P0_0==0)
{
second++;
if(second==60)
{
second=0;
}
dispbuf[0]=second%10;
dispbuf[1]=second/10;
while(P0_0==0);
}
}
if(P0_1==0)
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(P0_1==0)
{
minite++;
if(minite==60)
{
minite=0;
}
dispbuf[3]=minite%10;
dispbuf[4]=minite/10;
while(P0_1==0);
}
}
if(P0_2==0)
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(P0_2==0)
{
hour++;
if(hour==24)
{
hour=0;
}
dispbuf[6]=hour%10;
dispbuf[7]=hour/10;
while(P0_2==0);
}
}
}
}
voidt0(void)interrupt1using0
{
mstcnt++;
if(mstcnt==8)
{
mstcnt=0;
P1=dispcode[dispbuf[dispbitcnt]];
P3=dispbitcode[dispbitcnt];
dispbitcnt++;
if(dispbitcnt==8)
{
dispbitcnt=0;
}
}
tcnt++;
if(tcnt==4000)
{
tcnt=0;
second++;
if(second==60)
{
second=0;
minite++;
if(minite==60)
{
minite=0;
hour++;
if(hour==24)
{
hour=0;
}
}
}
dispbuf[0]=second%10;
dispbuf[1]=second/10;
dispbuf[3]=minite%10;
dispbuf[4]=minite/10;
dispbuf[6]=hour%10;
dispbuf[7]=hour/10;
}
}