1.实验任务
用AT89S51单片机的P0.0/AD0-P0.7/AD7端口接数码管的a-h端,8位数码管的S1-S8通过74LS138译码器的Y0-Y7来控制选通每个数码管的位选端。AT89S51单片机的P1.0-P1.2控制74LS138的A,B,C端子。在8位数码管上从右向左循环显示“12345678”。能够比较平滑地看到拉幕的效果。
2.电路原理图
图4.21.1
3.系统板上硬件连线
(1).把“单片机系统”区域中的P0.0/AD0-P0.7/AD7用8芯排线连接到“动态数码显示”区域中的a-h端口上;
(2).把“三八译码模块”区域中的Y0-Y7用8芯排线连接到“动态数码显示”区域中的S1-S8端口上;
(3).把“单片机系统”区域中的P1.0-P1.2端口用3根导线连接到“三八译码模块”区域中的A、B、C“端口上;
4.程序设计方法
(1).动态数码显示技术;如何进行动态扫描,由于一次只能让一个数码管显示,因此,要显示8位的数据,必须经过让数码管一个一个轮流显示才可以,同时每个数码管显示的时间大约在1ms到4ms之间,所以为了保证正确显示,我必须每隔1ms,就得刷新一个数码管。而这刷新时间我们采用单片机的定时/计数器T0来控制,每定时1ms对数码管刷新一次,T0采用方式2。
(2).在进行数码显示的时候,要对显示单元开辟8个显示缓冲区,每个显示缓冲区装有显示的不同数据即可。
5.程序框图
主程序框图
中断服务程序
6.汇编源程序
DISPBUF EQU 30H
DISPCNT EQU 38H
DISPBIT EQU 39H
T1CNTA EQU 3AH
T1CNTB EQU 3BH
CNT EQU 3CH
ORG 00H
LJMP START
ORG 0BH
LJMP INT_T0
START: MOV DISPCNT,#8
MOV A,#10
MOV R1,#DISPBUF
LP: MOV @R1,A
INC R1
DJNZ DISPCNT,LP
MOV DISPBIT,#00H
MOV T1CNTA,#00H
MOV T1CNTB,#00H
MOV CNT,#00H
MOV TMOD,#01H
MOV TH0,#(65536-1000) / 256
MOV TL0,#(65536-1000) MOD 256
SETB TR0
SETB ET0
SETB EA
SJMP $
INT_T0:
MOV TH0,#(65536-1000) / 256
MOV TL0,#(65536-1000) MOD 256
MOV A,DISPBIT
ADD A,#DISPBUF
MOV R0,A
MOV A,@R0
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
MOV A,P1
ANL A,#0F8H
ADD A,DISPBIT
MOV P1,A
INC DISPBIT
MOV A,DISPBIT
CJNE A,#08H,NEXT
MOV DISPBIT,#00H
NEXT: INC T1CNTA
MOV A,T1CNTA
CJNE A,#50,LL1
MOV T1CNTA,#00H
INC T1CNTB
MOV A,T1CNTB
CJNE A,#8,LL1
MOV T1CNTB,#00H
INC CNT
MOV A,CNT
CJNE A,#9,LLX
MOV CNT,#00H
MOV A,CNT
LLX: CJNE A,#01H,NEX1
MOV 30H,#8
LL1: LJMP DONE
NEX1: CJNE A,#02H,NEX2
MOV 31H,#8
MOV 30H,#8
LJMP DONE
NEX2: CJNE A,#03H,NEX3
MOV 32H,#8
MOV 31H,#8
MOV 30H,#8
LJMP DONE
NEX3: CJNE A,#04H,NEX4
MOV 33H,#8
MOV 32H,#8
MOV 31H,#8
MOV 30H,#8
LJMP DONE
NEX4: CJNE A,#05H,NEX5
MOV 34H,#8
MOV 33H,#8
MOV 32H,#8
MOV 31H,#8
MOV 30H,#8
LJMP DONE
NEX5: CJNE A,#06H,NEX6
MOV 35H,#8
MOV 34H,#8
MOV 33H,#8
MOV 32H,#8
MOV 31H,#8
MOV 30H,#8
LJMP DONE
NEX6: CJNE A,#07H,NEX7
MOV 36H,#8
MOV 35H,#8
MOV 34H,#8
MOV 33H,#8
MOV 32H,#8
MOV 31H,#8
MOV 30H,#8
LJMP DONE
NEX7: CJNE A,#08H,NEX8
MOV 37H,#8
MOV 36H,#8
MOV 35H,#8
MOV 34H,#8
MOV 33H,#8
MOV 32H,#8
MOV 31H,#8
MOV 30H,#8
LJMP DONE
NEX8: CJNE A,#00H,DONE
MOV 37H,#10
MOV 36H,#10
MOV 35H,#10
MOV 34H,#10
MOV 33H,#10
MOV 32H,#10
MOV 31H,#10
MOV 30H,#10
LL: LJMP DONE
DONE: RETI
TABLE: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH,00H
END
7.C语言源程序
#include
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00};
unsigned char dispbitcode[]={0xf8,0xf9,0xfa,0xfb,
0xfc,0xfd,0xfe,0xff};
unsigned char dispbuf[8]={16,16,16,16,16,16,16,16};
unsigned char dispbitcnt;
unsigned int t02scnt;
unsigned char t5mscnt;
unsigned char u;
unsigned char i;
void main(void)
{
TMOD=0x02;
TH0=0x06;
TL0=0x06;
TR0=1;
ET0=1;
EA=1;
while(1);
}
void t0(void) interrupt 1 using 0
{
t5mscnt++;
if(t5mscnt==4)
{
t5mscnt=0;
P0=dispcode[dispbuf[dispbitcnt]];
P1=dispbitcode[dispbitcnt];
dispbitcnt++;
if(dispbitcnt==8)
{
dispbitcnt=0;
}
}
t02scnt++;
if(t02scnt==1600)
{
t02scnt=0;
u++;
if(u==9)
{
u=0;
}
for(i=0;i<8;i++)
{
dispbuf[i]=16;
}
for(i=0;iO:P>
{
dispbuf[i]=8;
}
}
}
上一篇:基于89C52的电子钟设计
下一篇:基于51的电子琴设计
推荐阅读最新更新时间:2024-03-16 15:51