标题:
用at89s52和七段数码管实现计时时钟
[打印本页]
作者:
daming
时间:
2014-12-30 23:43
标题:
用at89s52和七段数码管实现计时时钟
/*程序名称:at89s52实现时钟
程序功能:用at89s52和七段数码管实现计时时钟
作者 :hankjulianth
*/
#include"reg52.h"
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uint sec,min,hour;//秒,分,时计数
uint t1;//定义计数器
sbit sc=P3^7;//锁存信号
void _init_();//初始化函数
void timer0();//定时器T0中断函数
void showtime();//时间显示函数
void settime(uint,uint,uint)
void main()
{
t1=0;
settime(0,0,0);
_init_();
while(1)
{
if(t1==20)
{
sec++;
t1=0;
}
if(sec==60)
{
min+=1;
sec=0;
}
if(min==60)
{
hour+=1;
min=0;
}
if(hour==24)
{
hour=0;
}
showtime();
}
}
void _init_()
{
//启用且设置T0,定时50ms
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TMOD=0x01;
TR0=1;
}
void timer0() interrupt 1
{
_init_();
t1++;
}
void settime(uint i,uint j,uint k)//设置初始时间函数: k : j : i
{
sec=i;
min=j;
hour=k;
}
void showtime()
{
//秒钟显示
sc=1;
P0=table[sec/10];
P2=0x06;
sc=0;
sc=1;
P0=table[sec%10];
P2=0x07;
sc=0;
//分钟显示
sc=1;
P0=table[min/10];
P2=0x03;
sc=0;
sc=1;
P0=table[min%10];
P2=0x04;
sc=0;
//时钟显示
sc=1;
P0=table[hour/10];
P2=0x01;
sc=0;
sc=1;
P0=table[hour%10];
P2=0x0;
sc=0;
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1