用单片机控制6个共阳数码管秒表计时器分秒毫秒c代码怎么写

用单片机控制6个共阳数码管秒表计时器分秒毫秒c代码怎么写,第1张

显示部分自己写呀,与硬件关系很大

51单片机程序如下:

uint a=0;

uchar sec=0,min=0;

void main(void)

{ TMOD=0x01;

TH0=(65536-1000)/256;

TL0=(65536-1000)%256;

TR0=1;

ET0=1; EA=1;

while(1){display();}

}

中断程序:void timer0(void) interrupt 1

{

TH0=…… TL0=……

a++;

if(a==1000){sec++;}

if(sec==60){min++;}

}

个位 十位子百位分离方法:

bai=a/100%10;

shi=a/10%10;

ge=a%10;

STACK1 SEGMENT STACK

DW 200 DUP ()

STACK1 ENDS

DATA SEGMENT

SPACE DB 1000 DUP (' ')

PATTERN DB 6 DUP (' '),0C9H,26 DUP (0CDH),0BBH,6 DUP (' ')

DB 6 DUP (' '),0BAH,26 DUP (20H),0BAH,6 DUP (' ')

DB 6 DUP (' '),0C8H,26 DUP (0CDH),0BCH,6 DUP (' ')

DBUFFER DB 8 DUP (':'),12 DUP (' ')

DBUFFER1 DB 20 DUP (' ')

STR1 DB 0DH,0AH, 'PLEASE INPUT DATE(D) OR TIME(T) OR QUIT(Q): $'

DATA ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA,ES:DATA,SS:STACK1

START: MOV AX,0001H ;设置显示方式为4025彩色文本方式

INT 10H

MOV AX,DATA

MOV DS,AX

MOV ES,AX

MOV BP,OFFSET SPACE

MOV DX,0B00H

MOV CX,1000

MOV BX,0040H

MOV AX,1300H

INT 10H

MOV BP,OFFSET PATTERN ;显示矩形条

MOV DX,0B00H

MOV CX,120

MOV BX,004EH

MOV AX,1301H

INT 10H

LEA DX,STR1 ;显示提示信息

MOV AH,9

INT 21H

MOV AH,1 ;从键盘输入单个字符

INT 21H

CMP AL,44H ;AL='D'?

JNE A

CALL DATE ;显示系统日期

A: CMP AL,54H ;AL='T'?

JNE B

CALL TIME ;显示系统时间

B: CMP AL,51H ;AL='Q'?

JNE START

MOV AH,4CH ;返回dos状态

INT 21H

DATE PROC NEAR ;显示日期子程序

dis:MOV AH,2AH ;取日期

INT 21H

MOV SI,0

MOV AX,CX

MOV BX,100

DIV BL

MOV BL,AH

CALL BCDASC1 ;日期数值转换成相应的ASCII码字符

MOV AL,BL

CALL BCDASC1

INC SI

MOV AL,DH

CALL BCDASC1

INC SI

MOV AL,DL

CALL BCDASC1

MOV BP,OFFSET DBUFFER1

MOV DX,0C0DH

MOV CX,20

MOV BX,004EH

MOV AX,1301H

INT 10H

MOV AH,02H ;设置光标位置

MOV DX,0300H

MOV BH,0

INT 10H

MOV BX,0018H

REPEA: MOV CX,0FFFFH ;延时

REPEAT:LOOP REPEAT

DEC BX

JNZ REPEA

MOV AH,01H ;读键盘缓冲区字符到AL寄存器

INT 16H

JE dis

JMP START

MOV AX,4C00H

INT 21H

RET

DATE ENDP

TIME PROC NEAR ;显示时间子程序

DISPLAY1:MOV SI,0

MOV BX,100

DIV BL

MOV AH,2CH ;取时间

INT 21H

MOV AL,CH

CALL BCDASC ;将时间数值转换成ASCII码字符

INC SI

MOV AL,CL

CALL BCDASC

INC SI

MOV AL,DH

CALL BCDASC

MOV BP,OFFSET DBUFFER

MOV DX,0C0DH

MOV CX,20

MOV BX,004EH

MOV AX,1301H

INT 10H

MOV AH,02H

MOV DX,0300H

MOV BH,0

INT 10H

MOV BX,0018H

RE: MOV CX,0FFFFH

REA: LOOP REA

DEC BX

JNZ RE

MOV AH,01H

INT 16H

JE DISPLAY1

JMP START

MOV AX,4C00H

INT 21H

RET

TIME ENDP

BCDASC PROC NEAR ;时间数值转换成ASCII码字符子程序

PUSH BX

CBW

MOV BL,10

DIV BL

ADD AL,'0'

MOV DBUFFER[SI],AL

INC SI

ADD AH,'0'

MOV DBUFFER[SI],AH

INC SI

POP BX

RET

BCDASC ENDP

BCDASC1 PROC NEAR ;日期数值转换成ASCII码字符子程序

PUSH BX

CBW

MOV BL,10

DIV BL

ADD AL,'0'

MOV DBUFFER1[SI],AL

INC SI

ADD AH,'0'

MOV DBUFFER1[SI],AH

INC SI

POP BX

RET

BCDASC1 ENDP

CODE ENDS

END START

cursor equ 45H

attrib equ 2fh

code segment

assume cs:code,ds:code

start:

jmp go

oldcur dw

OLD1C DW 2 DUP()

NEWINT1C:

PUSHF

CALL DWORD PTR CS:OLD1C

push ax

PUSH BX

PUSH CX

PUSH DX

XOR BH,BH

MOV AH,3

INT 10H

MOV CS:OLDCUR,DX

MOV AH,2

xor bh,bh

MOV DX,CURSOR

INT 10H

MOV AH,2 ;读取系统时钟

INT 1AH

PUSH DX

PUSH CX

POP BX

PUSH BX

CALL SHOWBYTE

CALL SHOWCOLON

POP BX

XCHG BH,BL

CALL SHOWBYTE

CALL SHOWCOLON

POP BX

CALL SHOWBYTE

MOV DX,CS:OLDCUR

MOV AH,2

XOR BH,BH

INT 10H

POP DX

POP CX

POP BX

POP AX

IRET

SHOWBYTE PROC NEAR

PUSH BX

MOV CL,4

MOV AL,BH

SHR AL,CL

ADD AL,30H

CALL SHOW

CALL CURMOVE

POP BX

MOV AL,BH

AND AL,0FH

ADD AL,30H

CALL SHOW

CALL CURMOVE

RET

SHOWBYTE ENDP

SHOWCOLON PROC NEAR

MOV AL,':'

CALL SHOW

CALL CURMOVE

RET

SHOWCOLON ENDP

CURMOVE PROC NEAR

PUSH AX

PUSH BX

PUSH CX

PUSH DX

MOV AH,3

MOV BH,0

INT 10H

INC DL

MOV AH,2

INT 10H

POP DX

POP CX

POP BX

POP AX

RET

CURMOVE ENDP

SHOW PROC NEAR

PUSH AX

PUSH BX

PUSH CX

MOV AH,09H

MOV BX,attrib

MOV CX,1

INT 10H

POP CX

POP BX

POP AX

RET

SHOW ENDP

GO:

PUSH CS

POP DS

MOV AX,351CH ;取中断向量

INT 21H

MOV OLD1C,BX;保存原中断向量

MOV BX,ES

MOV OLD1C+2,BX

MOV DX,OFFSET NEWINT1C ;置新的中断向量

MOV AX,251CH

INT 21H

MOV DX,OFFSET GO

SUB DX,OFFSET START

MOV CL,4

SHR DX,CL

ADD DX,11H

MOV AX,3100H ;结束并驻留

INT 21H

CODE ENDS

END START

个人觉得比较好用的一款计时器叫Snaptimer,还有一个软件叫1Time,这两款都可以放在屏幕的一角,方便使用。我自己经常用。除此这外,还有

Screentimer(铺满整个屏幕)

小小秒表(功能稍多,但也是小软件)

分秒计时器(功能较多,可用于竞赛,可以快到时时读秒,但属于付费软件)

我以前做的一个6位数码管的时钟程序,你可以看看,其实6位足够了。

KEYVAL EQU 30H

KEYTM EQU 31H

KEYSCAN EQU 32H

DAT EQU 33H

SCANLED EQU 39H

CLK EQU 77H

SEC EQU 78H

MIN EQU 79H

HOUR EQU 7AH

PAUSE BIT 00H

DOT BIT 01H

ORG 0000H

LJMP MAIN

ORG 000BH

LJMP T0ISR ;50ms定时

ORG 001BH

LJMP T1ISR ;扫描显示

ORG 0030H

MAIN:

MOV SP,#5FH

MOV TMOD,#11H

MOV TH0,#03CH

MOV TL0,#0B0H

MOV TH1,#0ECH

MOV TL1,#078H

MOV KEYVAL,#0

MOV SCANLED,#0

MOV 33H,#10H

MOV 34H,#10H

MOV 35H,#10H

MOV 36H,#10H

MOV 37H,#10H

MOV 38H,#10H

MOV SEC,#0

MOV MIN,#0

MOV HOUR,#0

MOV CLK,#0

CLR PAUSE

SETB EA

SETB ET1

SETB TR1

LOOP:

LCALL KEYSEL

MOV A,KEYVAL

CJNE A,#0FFH,LOOP1

SJMP LOOP

LOOP1:

CJNE A,#10,LOOP2 ;“ON”启动

SETB TR0

SETB ET0

SETB PAUSE

SJMP LOOP

LOOP2:

CJNE A,#11,LOOP3 ;“=”清零

MOV SEC,#0

MOV MIN,#0

MOV HOUR,#0

LCALL DISCHG

SJMP LOOP

LOOP3:

CJNE A,#15,LOOP4 ;“+”暂停

CLR TR0

CLR ET0

CLR PAUSE

SJMP LOOP

LOOP4:

CJNE A,#14,LOOP5 ;“-”清显示暂停

MOV 33H,#10H

MOV 34H,#10H

MOV 35H,#10H

MOV 36H,#10H

MOV 37H,#10H

MOV 38H,#10H

CLR TR0

CLR ET0

CLR PAUSE

SJMP LOOP

LOOP5:

CJNE A,#10,LOOP6 ;数字键

LOOP6:

JC LOOP7

LJMP LOOP

LOOP7:

JNB PAUSE,LOOP8 ;暂停状态可以输入数字键

LJMP LOOP

LOOP8:

MOV 33H,34H

MOV 34H,35H

MOV 35H,36H

MOV 36H,37H

MOV 37H,38H

MOV 38H,KEYVAL

MOV A,33H

SWAP A

ORL A,34H

LCALL BCDH

MOV HOUR,A

MOV A,35H

SWAP A

ORL A,36H

LCALL BCDH

MOV MIN,A

MOV A,37H

SWAP A

ORL A,38H

LCALL BCDH

MOV SEC,A

LJMP LOOP

;------------------

;BCD转换为十六进制

BCDH:

MOV B,#10H

DIV AB

MOV R7,B

MOV B,#10

MUL AB

ADD A,R7

RET

;------------------

;十六进制转换为BCD

HBCD:

MOV B,#10

DIV AB

SWAP A

ORL A,B

RET

;------------------

KEYSEL:

MOV KEYVAL,#0

MOV KEYSCAN,#0EFH

LCALL GETKEY

MOV A,KEYTM

JZ KEYS1

MOV KEYVAL,A

SJMP KEYRTN

KEYS1:

MOV KEYSCAN,#0DFH

LCALL GETKEY

MOV A,KEYTM

JZ KEYS2

CLR C

ADD A,#4

MOV KEYVAL,A

SJMP KEYRTN

KEYS2:

MOV KEYSCAN,#0BFH

LCALL GETKEY

MOV A,KEYTM

JZ KEYS3

CLR C

ADD A,#8

MOV KEYVAL,A

SJMP KEYRTN

KEYS3:

MOV KEYSCAN,#7FH

LCALL GETKEY

MOV A,KEYTM

JZ KEYRTN

CLR C

ADD A,#12

MOV KEYVAL,A

KEYRTN:

LCALL CHGKEY

RET

;--------------------

GETKEY:

MOV KEYTM,#0

MOV A,KEYSCAN

MOV P3,A

NOP

MOV A,P3

ANL A,#0FH

XRL A,#0FH

JZ NOKEY

MOV R2,#10

LCALL DELAY

MOV A,P3

ANL A,#0FH

XRL A,#0FH

JZ NOKEY

MOV A,P3

ANL A,#0FH

MOV R7,A

SF:

MOV A,P3

ANL A,#0FH

XRL A,#0FH

JNZ SF

MOV A,R7

CJNE A,#0EH,NK1

MOV KEYTM,#1

SJMP NOKEY

NK1:

CJNE A,#0DH,NK2

MOV KEYTM,#2

SJMP NOKEY

NK2:

CJNE A,#0BH,NK3

MOV KEYTM,#3

SJMP NOKEY

NK3:

CJNE A,#07H,NOKEY

MOV KEYTM,#4

NOKEY: RET

;--------------------

DELAY:

MOV R3,#50

DELAY1:

MOV R4,#100

DJNZ R4,$

DJNZ R3,DELAY1

DJNZ R2,DELAY

RET

;--------------------

T0ISR:

PUSH ACC

CLR TR0

MOV TH0,#3CH

MOV TL0,#0B0H

SETB TR0

INC CLK

MOV A,CLK

CJNE A,#20,T0ISRE

MOV CLK,#0

INC SEC

MOV A,SEC

CJNE A,#60,T0ISRE

MOV SEC,#0

INC MIN

MOV A,MIN

CJNE A,#60,T0ISRE

MOV MIN,#0

INC HOUR

MOV A,HOUR

CJNE A,#24,T0ISRE

MOV SEC,#0

MOV MIN,#0

MOV HOUR,#0

T0ISRE:

LCALL DISCHG

POP ACC

RETI

;--------------------

DISCHG:

MOV A,HOUR

LCALL HBCD

PUSH ACC

ANL A,#0FH

MOV 34H,A

POP ACC

ANL A,#0F0H

SWAP A

MOV 33H,A

MOV A,MIN

LCALL HBCD

PUSH ACC

ANL A,#0FH

MOV 36H,A

POP ACC

ANL A,#0F0H

SWAP A

MOV 35H,A

MOV A,SEC

LCALL HBCD

PUSH ACC

ANL A,#0FH

MOV 38H,A

POP ACC

ANL A,#0F0H

SWAP A

MOV 37H,A

RET

;--------------------

T1ISR:

PUSH ACC

CLR TR1

MOV TH1,#0ECH

MOV TL1,#78H

SETB TR1

MOV DPTR,#LEDTAB

T100:

MOV R0,#DAT

MOV A,SCANLED

ADD A,R0

MOV R0,A

MOV A,SCANLED

JNZ T101

MOV P2,#01H

CLR DOT

SJMP T1DIS

T101:

DEC A

JNZ T102

MOV P2,#02H

SETB DOT

SJMP T1DIS

T102:

DEC A

JNZ T103

MOV P2,#04H

CLR DOT

SJMP T1DIS

T103:

DEC A

JNZ T104

MOV P2,#08H

SETB DOT

SJMP T1DIS

T104:

DEC A

JNZ T105

MOV P2,#10H

CLR DOT

SJMP T1DIS

T105:

MOV P2,#20H

CLR DOT

T1DIS:

MOV A,@R0

MOVC A,@A+DPTR

JNB DOT,T1DIS1

ORL A,#01H

T1DIS1:

CPL A

MOV P0,A

INC SCANLED

MOV A,SCANLED

CJNE A,#6,T1END

MOV SCANLED,#0

T1END:

POP ACC

RETI

;--------------------

CHGKEY:

MOV A,KEYVAL

JZ KV16

DEC A

JNZ KV01

MOV KEYVAL,#7

RET

KV01:

DEC A

JNZ KV02

MOV KEYVAL,#4

RET

KV02:

DEC A

JNZ KV03

MOV KEYVAL,#1

RET

KV03:

DEC A

JNZ KV04

MOV KEYVAL,#10

RET

KV04:

DEC A

JNZ KV05

MOV KEYVAL,#8

RET

KV05:

DEC A

JNZ KV06

MOV KEYVAL,#5

RET

KV06:

DEC A

JNZ KV07

MOV KEYVAL,#2

RET

KV07:

DEC A

JNZ KV08

MOV KEYVAL,#0

RET

KV08:

DEC A

JNZ KV09

MOV KEYVAL,#9

RET

KV09:

DEC A

JNZ KV10

MOV KEYVAL,#6

RET

KV10:

DEC A

JNZ KV11

MOV KEYVAL,#3

RET

KV11:

DEC A

JNZ KV12

MOV KEYVAL,#11

RET

KV12:

DEC A

JNZ KV13

MOV KEYVAL,#12

RET

KV13:

DEC A

JNZ KV14

MOV KEYVAL,#13

RET

KV14:

DEC A

JNZ KV15

MOV KEYVAL,#14

RET

KV15:

DEC A

JNZ KV16

MOV KEYVAL,#15

RET

KV16:

MOV KEYVAL,#0FFH

RET

;--------------------

LEDTAB: DB 0FCH ;"0" 00H

DB 60H ;"1" 01H

DB 0DAH ;"2" 02H

DB 0F2H ;"3" 03H

DB 66H ;"4" 04H

DB 0B6H ;"5" 05H

DB 0BEH ;"6" 06H

DB 0E0H ;"7" 07H

DB 0FEH ;"8" 08H

DB 0F6H ;"9" 09H

DB 0EEH ;"A" 0AH

DB 3EH ;"B" 0BH

DB 9CH ;"C" 0CH

DB 7AH ;"D" 0DH

DB 9EH ;"E" 0EH

DB 8EH ;"F" 0FH

DB 00H ;" " 10H

;--------------------

END

#include <stdioh>

#include <windowsh>

int main()

{

    int min, sec, sum;

    int i;

    

    sum = 0;

    printf("请输入分数和秒数: ");

    scanf("%d, %d", &min, &sec);

    getchar();

    sum = min60+sec;

    printf("你将等待%ds\n", sum);

    for(i=0; i<sum; i++)

    {

        printf("\r还剩%ds", sum-i);

        Sleep(1000);

    }

    return 0;

}

这个程序可以实现秒的计时,按键控制开始、暂停、清零功能,更多功能自己在看清程序的基础上进行改进。

#include <reg51H>

sbit P3_5 =P3^5;

unsigned char code dispcode[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,

                          0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0x00};

unsigned char second;

unsigned char keycnt;

unsigned int tcnt;

void main(void)

{

unsigned char i,j;

TMOD=0x02;

ET0=1;

EA=1;

second=0;

P1=dispcode[second/10];

P2=dispcode[second%10];

while(1)

{

if(P3_5==0)

{

for(i=20;i>0;i--)

for(j=248;j>0;j--);

if(P3_5==0)

{

keycnt++;

switch(keycnt)

{

case 1:

TH0=0x06;

TL0=0x06;

TR0=1;

break;

case 2:

TR0=0;

break;

case 3:

keycnt=0;

second=0;

P1=dispcode[second/10];

P2=dispcode[second%10];

break;

}

while(P3_5==0);

}

}

}

}

void t0(void) interrupt 1 using 0

{

tcnt++;

if(tcnt==4000)

{

tcnt=0;

second++;

if(second==100)

{

second=0;

}

P1=dispcode[second/10];

P2=dispcode[second%10];

}

}

以上就是关于用单片机控制6个共阳数码管秒表计时器分秒毫秒c代码怎么写全部的内容,包括:用单片机控制6个共阳数码管秒表计时器分秒毫秒c代码怎么写、用汇编语言编写一个时钟程序,能显示时分秒的,还要有秒表就是计时器的功能,、有好用的秒表计时器之类的软件不 就是比赛用的,需要计时间。 谢谢帮主等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10161724.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存