按键和定时器中断综合应用-秒表计时器
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
& INT1按键中断INT0#
和T0中断的综合应用
‐简易秒表计时器
范例1:汇编源代码
范例2:C51源代码
P7 EQU 0F8H ORG 0100H
#60H P6 EQU 0E8H
D1 EQU 0FEH //数码管个位EQU 0FDH MAIN: MOV SP ,#60H //设置堆栈
MOV P2,#0FFH //关P2口LED MOV TMOD,#00H D2 //十位D3 EQU 0FBH // 百位
VARX DATA 30H //计数变量DATA 31H MOV TH0,#4BH
MOV TL0,#0FDH //设置T0MOV R7,#20MOV VARX,#0 //置计数变量0DIG1 //BCD 个位DIG2 DATA 32H //BCD 十位DIG3 DATA 33H //BCD ,SETB ET0SETB EX0SETB IT0//百位ORG 0000H LJMP MAIN SETB EX1SETB IT1
SETB EA //允许相关中断
ORG 0003H
LJMP KY1INT //KY1中断ORG 000BH
LJMP T0INT //T0中断ORG 0013H
KY2INT //KY2LJMP 中断
CONV:
MOV A,VARX //读计数变量MOV A,DIG2 //读十位BCD MOV B,#100DIV AB DIG3A MOVC A,@A+DPTR MOV P7,A P6#D2//MOV DIG3,A MOV A,B MOV B,#10MOV P6,#D2 查表送显示ACALL DELAY //扫描延时A,DIG3//DIV AB
MOV DIG2,A DIG1B MOV 读百位BCD MOVC A,@A+DPTR MOV P7,A
MOV DIG1,B //转换为3位BCD DISP: MOV A,DIG1 //读个位BCD
MOV DPTR,#SEGTBL MOV P6,#D3 //查表送显示ACALL DELAY
CONV //MOVC A,@A+DPTR MOV P7,A
P6,#D1//LJMP 返回读取VARX
MOV 查表送显示
ACALL DELAY //扫描延时
•//软件延时程序段•R1#100T0INT: DJNZ R7,BACK
R7#20DELAY: MOV R1,#100•DL1: MOV R2,#10R2DL2
MOV R7,#20CPL P2.0INC VARX •DL2: DJNZ R2,DL2•DJNZ R1,DL1BACK: RETI
//•RET
KY1INT:
共阳型数码管字符//0‐9的的段码表
ORG 0200H
0C0H 0F9H 0A4H 0B0H
JNB TR0,RUNT0 //若未启动TR0,则启动计数
SEGTBL: DB 0C0H,0F9H,0A4H,0B0H DB 99H,92H,82H,0F8H,80H,90H
CLR TR0 //否则停止计数RETI
RUNT0: SETB TR0
RETI
KY2INT: MOV VARX,#0 //将VARX 清零
RETI
范例2:C51的程序实现
•#include "stc15.h"
•#include intrins.h
"intrins h"
•typedef unsigned char u8;
•u8 varx;;
•#define D1 0xFE
•#define D2 0xFD
•#define D3 0xFB
•u8 dig1,dig2,dig3;
•u8 count;
8t
•void dly20ms();
•const u8 code Seg8[10] =
{0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};•u8 *ptr;
•void main()
•{
•P2 = 0xFF;•varx= 0;
•ptr= Seg8;•count = 20;•TMOD = 0x00;
TH0 19453/256;•=
•TL0 = 19453%256;•ET0 = 1;;
•EX0 = 1;
•IT0 = 1;;
•EX1 = 1;
•IT1 = 1;
•EA = 1;
•while(1)
•{
•dig3 = varx/100;•dig2 = (varx%100)/10;•dig1 = (varx%100)%10;•P7 = *(ptr+dig1);
•P6 = D1;
•dly20ms();
•P7 = *(ptr+dig2);
•P6 = D2;
•dly20ms();
•=*(ptr+dig3);
P7 (ptr dig3);
•P6 = D3;
•dly20ms();
•}
•
•}
•void ky1int() interrupt 0•{
•if(TR0 == 0 ) TR0 = 1;•else TR0 =0;
•}
•void ky2int() interrupt 2•0;}
{ varx=
•void T0int() interrupt 1•{
()
•if(count‐‐!=0) return;•else
•{
•P20 ^= 1;
•count = 20;•varx++;
•}
•}