单片机60秒计时器的程序,用AT89C51实现

如题所述

//1:用AT89C51单片机的定时/计数器T0产生一秒的
//定时时间,作为秒计数时间,当一秒产生时,秒
//计数加1,秒计数到60时,自动从0开始。单片机
//晶振频率为12MHZ
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
0x6f};
void display(uchar fen,uchar miao);
uchar a,fen,miao,shu;
void delay(uint z);
void init();
void main()
{
init();
while(1)
{
display(fen,miao);
}

}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
a++;
if(a==20)
{
a=0;
shu++;
fen=shu/10;
miao=shu%10;
if(shu==60)
shu=0;
}

}

void init()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void display(uchar fen,uchar miao)
{
P1=0xFe;
P2=table[fen];
delay(5);//延时5毫秒

P1=0xFf;
P2=table[miao];
delay(5);//延时5毫秒
}

void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-06-04
汇编源程序

Second EQU 30H
ORG 0
START: MOV Second,#00H
NEXT: MOV A,Second
MOV B,#10
DIV AB
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
MOV A,B
MOVC A,@A+DPTR
MOV P2,A
LCALL DELY1S
INC Second
MOV A,Second
CJNE A,#60,NEXT
LJMP START
DELY1S: MOV R5,#100
D2: MOV R6,#20
D1: MOV R7,#248
DJNZ R7,$
DJNZ R6,D1
DJNZ R5,D2
RET
TABLE: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH
END

C语言源程序
#include <AT89X51.H>
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char Second;

void delay1s(void)
{
unsigned char i,j,k;
for(k=100;k>0;k--)
for(i=20;i>0;i--)
for(j=248;j>0;j--);
}

void main(void)
{
Second=0;
P0=table[Second/10];
P2=table[Second%10];
while(1)
{
delay1s();
Second++;
if(Second==60)
{
Second=0;
}
P0=table[Second/10];
P2=table[Second%10];
}
}
第2个回答  2009-06-06
Second EQU 30H
ORG 0
START: MOV Second,#00H
NEXT: MOV A,Second
MOV B,#10
DIV AB
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
MOV A,B
MOVC A,@A+DPTR
MOV P2,A
LCALL DELY1S
INC Second
MOV A,Second
CJNE A,#60,NEXT
LJMP START
DELY1S: MOV R5,#100
D2: MOV R6,#20
D1: MOV R7,#248
DJNZ R7,$
DJNZ R6,D1
DJNZ R5,D2
RET
TABLE: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH
END
第3个回答  2009-06-05
用定时器做的...试一下
#include <reg51.h>
unsigned int i=0;
void main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{}
}
void timer() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
if(i==1200) i=0;
i+=1;
}