首页 » 业余 » 单片机 » 正文

单片机4位数码管时钟钟表C语言源码

发布者:站点默认
2015/05/18 浏览数(2,038) 分类:单片机 单片机4位数码管时钟钟表C语言源码已关闭评论

效果:

89s52_4weishumaguan

清单:

AT89S52 * 1

74HC573 * 2

最小系统 * 1

4位数码管 * 1

杜邦线若干

说明:

P0的8个口接数码管,P1的0~2共3个口接位选、段选和秒指示灯

源码:

/**
 * 单片机4位数码管时钟钟表C语言源码
 */
#include <reg52.h>
unsigned int f = 0,delayCount = 0,delayLed = 0,sleepDelay = 3;
unsigned char code numTable[]={
	0x3f,
	0x06,0x5b,0x4f,0x66,0x6d,
	0x7d,0x07,0x7f,0x6f,0x77,
	0x7c,0x39,0x5e,0x79,0x71
};
unsigned char hour,minute,second,hourH,hourL,minH,minL;
sbit digit   = P1^0; // 位选,锁存器
sbit segment = P1^1; // 段选,锁存器
sbit led     = P1^2; // 秒指示灯
                     // P0接数码管的8根线
void init();
void delay(unsigned char n);
void showTime(unsigned char hour,minute);

void main(){
	init();
	while(1){
		if(delayLed <= 500){
			led = 0;
		}else if(delayLed > 500 && delayLed <= 1000){
			led = 1;
		}else{
			delayLed = 0;	
		}
		if(second > 59){
			second = 0;
			minute++;
		}
		if(minute > 59){
			minute = 0;
			hour++;
		}
		if(hour > 23){
			hour   = 0;
			minute = 0;
			second = 0;
		}
		//showTime(hour,minute);
		showTime(minute,second);
		//showTime(12,34);
	}
}
void init()
{
	TMOD= 0x01;
	TH0 = (65536 - 1000 ) / 256;
	TL0 = (65536 - 1000 ) % 256;
	EA  = 1; // 总中断
	ET0 = 1; // 定时器0的中断开启
	TR0 = 1; // 启动 timer0
	delayCount = 0;
	delayLed   = 0;
	hour   = 0;
	minute = 0;
	second = 0;
	led    = 1;
}
void showTime(unsigned char hour,minute)
{
	unsigned char hourH,hourL,minH,minL;
	hourH = hour / 10; // 时十位
	hourL = hour % 10; // 时个位
	minH  = minute  / 10; // 分十位
	minL  = minute  % 10; // 分个位

	digit = 1;
	P0 = 0x01; // 1 2 4 8
	digit = 0;
	segment = 1;
	P0 = ~numTable[hourH];
	segment = 0;
	delay(sleepDelay);
	
	digit = 1;
	P0 = 0x02; // 1 2 4 8
	digit = 0;
	segment = 1;
	P0 = ~numTable[hourL];
	segment = 0;
	delay(sleepDelay);

	digit = 1;
	P0 = 0x04; // 1 2 4 8
	digit = 0;
	segment = 1;
	P0 = ~numTable[minH];
	segment = 0;
	delay(sleepDelay);

	digit = 1;
	P0 = 0x08; // 1 2 4 8
	digit = 0;
	segment = 1;
	P0 = ~numTable[minL];
	segment = 0;
	delay(sleepDelay);
}
void delay(unsigned char n)
{
	delayCount = 0;
	while(1){
		if(delayCount >= n){
			delayCount = 0;
			break;
		}
	}
}
void te0() interrupt 1 // 1毫秒,1000次 == 1秒 == 1000毫秒
{
	TH0 = (65535 - 1000) / 256;
	TL0 = (65535 - 1000) % 256;
	f++; // 使 second 每秒累加
	if(f >= 1000) {
		second++;
		f = 0;
	}
	delayLed++;   // 秒指示灯间隔标记
	delayCount++; // 动态扫描时刷新间隔标记
}

// End

– 完 –

点击返回顶部
  1. 留言
  2. 联系方式