[转]精确到1%秒的单片机计时器汇编程序

程序效果:利用单片机的定时/计数器设计一个计时器,
按key0后启动,要求精确显示到百分之一秒。
发挥部分:
1:定时结束后有提示音报警,并可重新定时
2:定时时间可设置,最多30s
3:定时过程中可暂停
4:按下key1暂停计时,并记下此刻数值
5:按下key2显示第一次按下key1的数值(按下第一次有效,其它均无效)
3:本程序版权所有:
*/
ORG 0000H
LJMP START //主程序必须避开地址000BH
ORG 000BH //定时器0的中段服务程序,起始地址为000BH
LJMP IT00
ORG 0030H
START: MOV TH0,#0EBH //装入初始值,定时时间为10ms
MOV TL0,#64H
MOV TMOD,#01H //工作方式1
MOV 40H,#00
MOV 41H,#00
MOV 42H,#00
MOV 43H,#00
MOV 33H,#00 //显示初值为0
MOV 32H,#00
MOV 31H,#00
MOV 30H,#00
MOV R1,#00
MOV R2,#00
SETB ET0 //打开定时0
SETB EA //开总中断
HERE: JNB P3.4,KEY0 //扫描是否有按键按下
-
-
JNB P3.5,KEY1

来源:电子产品世界

转载于:https://www.cnblogs.com/dianzichanpin/p/3559302.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
;name: ELECTRONIC CLOCK data segment mess1 db 'Press C or c to correct the time',0ah,0dh db 'Press R or r to SET the RING time',0ah,0dh db 'Press ESC button to exit',0ah,0dh,'$' tn db 'Please input the new time (hh:mm:ss):',0dh,0ah,'$' tM db 'Please input the RING time (hh:mm:SS):',0dh,0ah,'$' mess2 db 'Time is:',0ah,0dh,'$' t_buff db 40 ;在数据段开一段时间显示缓冲区 db ? db 40 dup (?) HOR1 DB 'S' MIN1 DB 'S' SEC1 DB 'S' hor db ? min db ? sec db ? fg db 0 data ends stack segment db 100 dup(?) stack ends code segment assume cs:code,ss:stack,ds:data ;确定各个逻辑段的类型 start: call clear ;调用清屏子程序 display: ;时间显示部分 mov ax,data mov ds,ax mov bx,offset t_buff ;送t_buff的偏移地址到BX mov ah,2ch ;调用DOS时间调用功能,功能号:2cH,小时,分钟,秒数分别保存在CH,CL,DH中 int 21h mov al,ch ;小时数设定 mov ah,0 call bctd ;调用进制换子程序 CALL ASC ;---------------------------------------------------------- mov al,':' ;显示分隔符号 mov [bx],al inc bx ;------------------------------------------------------- mov ah,2ch int 21h mov al,cl ;分钟数设定 mov ah,0 call bctd CALL ASC ;------------------------------------------------------------------------- mov al,':' ;显示分隔符号 mov [bx],al inc bx ;------------------------------------------------------------------------- mov ah,2ch ;秒设定 int 21h mov al,dh mov ah,0 call bctd CALL ASC ;---------------------------------------------------------------------- mov al,'$' ;将字符串的结束位送至显示缓冲区的最后一位 mov [bx],al ;------------------------------------------------------------------------------ push bx ;置光标位置 ,AH=2,BH=0,DH跟DL分别为行号与列号,并入栈保护BX mov ah,2 mov bh,0 mov dh,3 mov dl,8 int 10h pop bx lea dx,t_buff ;送t_buff偏移地址到DX,并调用DOS显示功能,功能号为9 mov ah,9 int 21h push bx ;置光标位置 mov ah,2 mov bh,0 mov dh,0 mov dl,0 int 10h pop bx lea dx,mess1 mov ah,9 int 21h push bx ;置光标位置 mov ah,2 mov bh,0 mov dh,3 mov dl,0 int 10h pop bx lea dx,mess2 mov ah,9 int 21h ;------------------------------------------------- mov ah,2ch ;调用DOS时间调用功能,功能号:2cH,小时,分钟,秒数分别保存在CH,CL,DH中 int 21h CMP CH,HOR1 JNZ nxt ;判断闹钟是否到时,如果到时调用07H输出响声 CMP CL,MIN1 JNZ nxt CMP DH,SEC1 JNZ nxt MOV DL,07H MOV AH,02H INT 21H ;----------------------------- nxt: call delay1 mov ah,1 ;调用键盘I/O中断功能号1,获取键值到AL int 16h cmp al,'c' ;是c键,到时间修改程序 je Cor cmp al,'C' ;是C键,到时间修改程序 je Cor CMP AL,'R' JE RING CMP AL,'r' JE RING cmp al,1bh jz quit ;是ESC键,退出程序 JMP DISPLAY quit:mov ah,4ch ;程序终止功能号 int 21h ret RING: call INPUT2 JMP START Cor: call correct ;调用时间修改子程序 ;------------------------------- bctd proc near ;二进制BCD码子程序 ;AX输入参数 ;AX输出参数,存放调整过的BCD码 mov dx,ax mov ax,0 mov cx,16 ;设循环次数 bctd1: clc ;清进位标志C rcl dx,1 ;通过进位的循环右移 adc al,al ;带进位加法 daa ;加法的十进制调整 xchg al,ah ;交换高、低八位 adc al,al daa xchg al,ah loop bctd1 ;循环次数保存在CX里 ret bctd endp ;------------------------------------------- clear proc near push ax ;入栈保护现场 push bx push cx push dx mov ax,0600h ;ah=06(滚动)al=00(全屏空白) mov bh,0fh ;设置背景颜色(2)和前景颜色(e) sub cx,cx mov dx,5f5fh int 10h pop dx ;出栈恢复现场 pop cx pop bx pop ax ret clear endp ;----------------------------------------- delay1 PROC ;精确延迟时间子程序 MOV DX,04ffh ;循环次数 up: XOR CX,CX a: NOP LOOP a DEC DX JNZ up RET delay1 ENDP ;---------------------------- correct proc ;时间修改子程序 call input ;调用键盘输入子程序输入数据 mov ch,hor mov cl,min mov dh,sec and dl,0h mov ah,2dh int 21h jmp start ret correct endp ;---------------------------------- input2 proc ;闹钟设置子程序 push ax ;入栈保护数据 push bx push cx push dx pushf mov dx,offset tm ;显示设置闹钟的格式提示 mov ah,09h int 21h mov dx,offset t_buff ;数据缓冲区的数据输入 mov ah,0ah int 21h and dx,0h lea bx,t_buff inc bx inc bx mov dh,[bx] sub dh,30h inc bx mov dl,[bx] sub dl,30h mov cl,10 mov al,dh mul cl add al,dl mov ch,al mov hor1,al inc bx inc bx mov dh,[bx] sub dh,30h inc bx mov dl,[bx] sub dl,30h mov cl,10 mov al,dh mul cl add al,dl mov cl,al mov min1,al inc bx inc bx mov dh,[bx] sub dh,30h inc bx mov dl,[bx] sub dl,30h mov cl,10 mov al,dh mul cl add al,dl mov dh,al mov sec1,al popf ;出栈恢复数据 pop dx pop cx pop bx pop ax ret input2 endp ;---------------------------------- input proc ;键盘输入子程序 push ax ;入栈保护数据 push bx push cx push dx pushf mov dx,offset tn ;显示修改时间的格式提示 mov ah,09h int 21h mov dx,offset t_buff ;数据缓冲区的数据输入 mov ah,0ah int 21h and dx,0h lea bx,t_buff inc bx inc bx mov dh,[bx] sub dh,30h inc bx mov dl,[bx] sub dl,30h mov cl,10 mov al,dh mul cl add al,dl mov ch,al mov hor,al inc bx inc bx mov dh,[bx] sub dh,30h inc bx mov dl,[bx] sub dl,30h mov cl,10 mov al,dh mul cl add al,dl mov cl,al mov min,al inc bx inc bx mov dh,[bx] sub dh,30h inc bx mov dl,[bx] sub dl,30h mov cl,10 mov al,dh mul cl add al,dl mov dh,al mov sec,al popf ;出栈恢复数据 pop dx pop cx pop bx pop ax ret input endp ;---------------------------- ASC PROC PUSH AX AND AL,0F0H ;选取AL高四位 MOV CL,4 ;设置右循环的次数 ROL AL,CL ;右循环 OR AL,30H ;加30H得到ACSII码 MOV [BX],AL ;将得到的结果送到T_BUFF缓冲区 INC BX ;BX自加1,指针指向下一个缓冲区的下一个地址 POP AX AND AL,0FH ;选取低四位 OR AL,30H MOV [BX],AL ;将换后的低四位值送入缓冲区的第二个地址 INC BX RET ASC ENDP ;---------------------------------- code ends end start
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值