- ;简单的时钟程序
- BUFF_KEY EQU 21H ;键盘键
- LED1 EQU 22H ;数码管显示缓存
- LED2 EQU 23H
- LED3 EQU 24H
- LED4 EQU 25H
- LED5 EQU 26H
- LED6 EQU 27H
- LED_LP EQU 28H ;T0键盘显示扫描缓存
- HOR EQU 30H ;小时值缓存
- MIN EQU 31H ;分钟值缓存
- SEC EQU 32H ;秒值缓存
- F_BEEP BIT 01H
- LED_PORT EQU P1
- DIG_PORT EQU P0
- SW1 BIT P3.0
- SW2 BIT P3.1
- SW3 BIT P3.2
- SW4 BIT P3.3
- SW5 BIT P3.4
- SW6 BIT P3.5
- BEEP BIT P1.6
- org 000h
- JMP main
- ORG 00BH
- JMP T0_INT
- ORG 100H
- MAIN: MOV SP ,#40H
- MOV PSW,#00H
- MOV R0,#7FH ;初始化RAM区,
- CLR_RAM:MOV A,#00H
- MOV @R0,A
- DJNZ R0,CLR_RAM
- MOV P1,0FFH ;INIT
- MOV P2,0FFH
- MOV P0,0FFH
- MOV P3,0FFH
- MOV HOR,#12
- MOV TMOD,#21h
- MOV TH0,#240 ;T0=4MS
- MOV TL0,#96
- SETB TR0
- SETB ET0
- SETB EA
- WAIT_KEY: ;等候按键!
- JNB SW1,SW1_OK
- JNB SW2,SW2_OK
- JNB SW3,SW3_OK
- JNB SW4,SW4_OK
- JNB SW5,SW5_OKA
- JNB SW6,SW6_OKA
- JMP WAIT_KEY
- SW5_OKA: JMP SW5_OK
- SW6_OKA: JMP SW6_OK
- SW1_OK: MOV A,HOR
- CJNE A,#24,SW1_OVER
- MOV HOR,#00H
- SETB F_BEEP
- CLR BEEP
- LCALL DEL
- SETB BEEP
- CLR F_BEEP
- JMP WAIT_KEY
- SW1_OVER:INC HOR ;时加1
- SETB F_BEEP
- CLR BEEP
- LCALL DEL
- SETB BEEP
- CLR F_BEEP
- JMP WAIT_KEY
- SW2_OK: MOV A,HOR
- CJNE A,#00,SW2_OVER
- MOV HOR,#23
- SETB F_BEEP
- CLR BEEP
- LCALL DEL
- SETB BEEP
- CLR F_BEEP
- JMP WAIT_KEY
- SW2_OVER:DEC HOR ;时减1
- SETB F_BEEP
- CLR BEEP
- LCALL DEL
- SETB BEEP
- JMP WAIT_KEY
- SW3_OK: MOV A,MIN
- CJNE A,#59,SW3_OVER
- MOV MIN,#00H
- SETB F_BEEP
- CLR BEEP
- LCALL DEL
- SETB BEEP
- CLR F_BEEP
- JMP WAIT_KEY
- SW3_OVER:INC MIN ;分加1
- SETB F_BEEP
- CLR BEEP
- LCALL DEL
- SETB BEEP
- CLR F_BEEP
- JMP WAIT_KEY
- SW4_OK: MOV A,MIN
- CJNE A,#00,SW4_OVER
- MOV MIN,#59
- SETB F_BEEP
- CLR BEEP
- LCALL DEL
- SETB BEEP
- CLR F_BEEP
- JMP WAIT_KEY
- SW4_OVER:DEC MIN ;分减1
- SETB F_BEEP
- CLR BEEP
- LCALL DEL
- SETB BEEP
- CLR F_BEEP
- JMP WAIT_KEY
- SW5_OK: MOV A,SEC
- CJNE A,#59,SW5_OVER
- MOV SEC,#00H
- SETB F_BEEP
- CLR BEEP
- LCALL DEL
- SETB BEEP
- CLR F_BEEP
- JMP WAIT_KEY
- SW5_OVER:INC SEC ;秒加1
- SETB F_BEEP
- CLR BEEP
- LCALL DEL
- SETB BEEP
- CLR F_BEEP
- JMP WAIT_KEY
- SW6_OK: MOV A,SEC
- CJNE A,#00,SW6_OVER
- MOV HOR,#59
- SETB F_BEEP
- CLR BEEP
- LCALL DEL
- SETB BEEP
- CLR F_BEEP
- JMP WAIT_KEY
- SW6_OVER:DEC SEC ;时减1
- SETB F_BEEP
- CLR BEEP
- LCALL DEL
- SETB BEEP
- CLR F_BEEP
- JMP WAIT_KEY
- DEL: MOV R5,#03 ;为演示方便,人为插入的延时
- DEL01: MOV R6,#200
- DEL02: MOV R7,#200
- DJNZ R7,$
- DJNZ R6,DEL02
- DJNZ R5,DEL01
- RET
- BIN_BCD: MOV B,#10
- DIV AB
- RET
- ;------------- T0 INTRRUPT 数码管扫描定时中断---------------------------
- T0_INT: PUSH ACC
- PUSH PSW
- MOV TH0,#240 ;T0=4MS
- MOV TL0,#96
- INC LED_LP
- MOV A,LED_LP
- CJNE A,#250,CONT_SCAN ;250*4MS=1S
- MOV LED_LP,#00H
- INC SEC
- MOV A,SEC
- CJNE A,#60,CONT_SCAN
- MOV SEC,#00H
- INC MIN
- MOV A,MIN
- CJNE A,#60,CONT_SCAN
- MOV MIN,#00H
- INC HOR
- MOV A,HOR
- CJNE A,#24,CONT_SCAN
- MOV HOR,#00H
- clr beep
- clr p1.0
- lcall del
- setb beep
- setb p1.0
- CONT_SCAN:MOV A,SEC
- LCALL BIN_BCD
- MOV LED6,B
- MOV LED5,A
- MOV A,MIN
- LCALL BIN_BCD
- MOV LED4,B
- MOV LED3,A
- MOV A,HOR
- LCALL BIN_BCD
- MOV LED2,B
- MOV LED1,A
- MOV A,LED_LP
- MOV B,#06
- DIV AB
- MOV A,B
- RL A
- MOV DPTR,#TAB_SCAN
- JMP @A+DPTR
- TAB_SCAN: AJMP SCAN_L1
- AJMP SCAN_L2
- AJMP SCAN_L3
- AJMP SCAN_L4
- AJMP SCAN_L5
- AJMP SCAN_L6
- SCAN_L1: MOV DIG_PORT,#11111110B
- MOV A,LED1
- JMP END_SCAN
- SCAN_L2: MOV DIG_PORT,#11111101B
- MOV A,LED2
- JMP END_SCAN
- SCAN_L3: MOV DIG_PORT,#11110111B
- MOV A,LED3
- JMP END_SCAN
- SCAN_L4: MOV DIG_PORT,#11101111B
- MOV A,LED4
- JMP END_SCAN
- SCAN_L5: MOV DIG_PORT,#10111111B
- MOV A,LED5
- JMP END_SCAN
- SCAN_L6: MOV DIG_PORT,#01111111B
- MOV A,LED6
- END_SCAN: MOV DPTR,#TAB_LED
- MOVC A,@A+DPTR
- MOV LED_PORT,A
- JNB F_BEEP,EXIT_T0
- CLR BEEP
- EXIT_T0: POP PSW
- POP ACC
- RETI
- TAB_LED: DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H ;共阳字码表
- END
复制代码 |