单片机、设计数码管显示-电子时钟

MOV DPTR, #DISPTABLE DISPL1: MOV A, @R0 PUSH ACC MOV P2, R4 ANL A, #80H JZ DISPL11

MOV A, DISP_COUNT SUBB A, #32 JNC DISPL11 MOV P2, #0 DISPL11: POP ACC ANL A, #7FH

MOVC A, @A+DPTR MOV P0, A MOV R6, #00H MOV R7, #01H LCALL DELAY

MOV P0, #0FFH INC R0 MOV A, R4 RL A

MOV R4, A

DJNZ R5, DISPL1 POP ACC MOV R7, A POP ACC MOV R6, A POP ACC MOV R5, A POP ACC MOV R4, A POP PSW POP DPL POP DPH POP ACC RET

DISPTABLE:DB 0C0H,0F9H,0A4H,0B0H, 99H, 92H, 82H,0F8H, 80H, 90H, 88H, 83H,0C6H,0A1H, 86H, 8EH,0BFH, 89H,0C7H, 8CH,0FFH

; \\;

;-------------------------------------- 延迟功能。

;延迟时间=(R6R7)(ms)

12

;-------------------------------------- DELAY: PUSH ACC MOV A, R4 PUSH ACC MOV A, R5 PUSH ACC MOV A, R7 JZ DELAY01 INC R6

DELAY01: MOV R4, #02H MOV R5, #0F4H DELAY02: DJNZ R5, $

DJNZ R4, DELAY02 DJNZ R7, DELAY01 DJNZ R6, DELAY01 POP ACC MOV R5, A POP ACC MOV R4, A POP ACC RET ;

;-------------------------------------- ;用T0的中断功能

;-------------------------------------- T0_TIME: MOV TL0, #0B0H MOV TH0, #3CH PUSH PSW PUSH ACC

INC T0_COUNT MOV A, T0_COUNT SUBB A, #20 JC T0_OUT

MOV T0_COUNT, #0 INC SECO_REG MOV A, SECO_REG SUBB A, #60 JC T0_OUT

MOV SECO_REG, #0 INC MINU_REG MOV A, MINU_REG SUBB A, #60 JC T0_OUT

MOV MINU_REG, #0

13

INC HOUR_REG MOV A, HOUR_REG SUBB A, #24 JC T0_OUT

MOV HOUR_REG, #0 T0_OUT: POP ACC POP PSW RETI ; END

2.7.2. C语言程序编译

#include

#define uchar unsigned char #define uint unsigned int #define ulong unsigned long sbit BUZZ=P3^7; sbit KEY1=P1^0; sbit KEY2=P1^1;

uchar hour_reg,minute_reg,second_reg; void delay(uint); void display(uchar*); void time2str(uchar*); void time_set(void);

void main(void) {

uchar dispram[8];

TMOD=0x11; 选择工作方式1

IE=0x82; EA=1,CPU开放中断 ET0=1,使能定时器T0溢出中断 TR0=1; 开定时计数器 while(1) {

time2str(dispram); display(dispram);

if(KEY1==0) time_set(); } }

void time2str(uchar*ch) {

ch[0]=hour_reg/10; 显示小时的高位 ch[1]=hour_reg; 显示小时的低位

14

ch[2]=minute_reg/10; 显示分钟的高位 ch[3]=minute_reg; 显示分钟的低位 ch[4]=second_reg/10; 显示秒的高位 ch[5]=second_reg; 显示秒的低位 }

void time_set(void) {

uchar ch[6]; uchar i,c;

TR0=0; 关定时计数器 second_reg=0; 秒钟清零 time2str(ch); do

{ display(ch);} while(KEY1==0); c=2; while(c) {

time2str(ch);

if(c==2) {ch[0]|=0x80;ch[1]|=0x80;} 最高位补1 else { ch[2]|=0x80;ch[3]|=0x80;} display(ch); if(KEY1==0) { c--;

do{ display(ch); }

while(KEY1==0); }

if(KEY2==0) {

if(c==2) hour_reg=(hour_reg+1)$; 恢复显示小时 else minute_reg=(minute_reg+1)`; 恢复显示分钟 for(i=0;i<50;i++) display(ch); } }

TR0=1; 开定时计数器 }

void delay_ms(unsigned int dt) {

register unsigned char bt ,ct; for(;dt;dt--) for(ct=2;ct;ct--)

15

联系客服:779662525#qq.com(#替换为@)