赞
踩
请按照如图所示的设计一个数字钟,要是显示时钟的时分秒三位,秒钟从0记到60,分钟加一,分钟从0记到60,时钟加一。
设置六个按钮,分别是对分钟+1、-1,时钟+1、-1,数码管显示时钟和关闭显示。
代码如下:
#include <reg51.h> sbit key1 = P1^0; sbit key2 = P1^1; sbit key3 = P1^2; sbit key4 = P1^3; sbit key5 = P1^4; sbit key6 = P1^5; bit flag1s = 0; unsigned char hour = 0; unsigned char minute = 0; unsigned char second = 0; unsigned char data k1state; unsigned char data k2state; void FreshTime(); void TimeInit(); void InitLedBuff(); void keyscan(); unsigned char code LedChar[] = { //共阴数码管显示字符转换表 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40,0x00}; unsigned char LedBuff[8] = { //数码管显示缓冲区 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; void TimeInit(){ //定时器初始化 TMOD = 0X11; //设置定时器为模式1 16位计数器 TL0 = 0xB0; TH0 = 0x3C; //定时初值 50ms TL1 = 0x18; TH1 = 0xFC; //定时初值 1ms TR0 = 1; //定时器0开始计时 ET0 = 1; //使能定时器0 TR1 = 1; //定时器1开始计时 ET1 = 1; //使能定时器1 PT0 = 1; //提高计时准确性,提高定时器0中断优先级 EA = 1; //打开总中断 } void InitLedBuff(){ //初始化时间到显示缓冲区 LedBuff[0] = hour/10; LedBuff[1] = hour%10; LedBuff[2] = minute/10; LedBuff[3] = minute%10; LedBuff[4] = second/10; LedBuff[5] = second%10; } void Ledfresh() { //数码管显示刷新 static unsigned char i = 0; switch(i) { case 0 : P2 = ~(0x01<<i);P0 = LedChar[LedBuff[0]];i++;break; case 1 : P2 = ~(0x01<<i);P0 = LedChar[LedBuff[1]];i++;break; case 2 : P2 = ~(0x01<<i);P0 = 0x40;i++;break;//时分间隔线 case 3 : P2 = ~(0x01<<i);P0 = LedChar[LedBuff[2]];i++;break; case 4 : P2 = ~(0x01<<i);P0 = LedChar[LedBuff[3]];i++;break; case 5 : P2 = ~(0x01<<i);P0 = 0x40;i++;break;//分秒间隔线 case 6 : P2 = ~(0x01<<i);P0 = LedChar[LedBuff[4]];i++;break; case 7 : P2 = ~(0x01<<i);P0 = LedChar[LedBuff[5]];i=0;break; } } void FreshTime(){ //刷新时间到显示缓冲区 second++; if(second == 60)//进位 { second = 0; minute++; if(minute==60)//进位 { minute=0;//上次分钟会显示到60,改进后我分钟的显示移动到清零之后,这样就不会出现分钟显示到60的情况 hour++; LedBuff[2] = 0; LedBuff[3] = 0; if(hour == 24) hour = 0; LedBuff[0] = hour/10; LedBuff[1] = hour%10; }else{ LedBuff[2] = minute/10; LedBuff[3] = minute%10; } } LedBuff[4] = second/10; LedBuff[5] = second%10; } void main(){ TimeInit(); InitLedBuff(); while(1)//显示内容处理 { keyscan(); } } void Time0() interrupt 1 { // 50ms static unsigned char n = 0; TL0 = 0xB0; TH0 = 0x3C; n++; if(n==20)//1s标志位 { n=0; FreshTime(); } } void Time1() interrupt 3{ // 1ms TL1 = 0x18; // TH1 = 0xFC; //定时初值 1ms Ledfresh(); } void _nop(){ int i ; while(i++ < 1000); } void keyscan(){ if(!key1){ _nop(); if(!key1){ if(minute == 60){ minute = 0; hour++; LedBuff[2] =0; LedBuff[3] = 0; if(hour == 24) hour = 0; LedBuff[0] = hour/10; LedBuff[1] = hour%10; }else{ minute++; LedBuff[2] = minute/10; LedBuff[3] = minute%10; } } while(!key1); } if(!key2){ _nop(); if(!key2){ if(minute != 0){ minute--; }else{ minute = 60; hour--; LedBuff[0] = hour/10; LedBuff[1] = hour%10; } LedBuff[2] = minute/10; LedBuff[3] = minute%10; } while(!key2); } if(!key3){ _nop(); if(!key3){ hour++; if(hour == 25){ hour = 0; } LedBuff[0] = hour/10; LedBuff[1] = hour%10; } while(!key3); } if(!key4){ _nop(); if(!key4){ hour--; if(hour == -1){ hour = 0; } LedBuff[0] = hour/10; LedBuff[1] = hour%10; } while(!key4); } if(!key5){ _nop(); if(!key5){ P2 = 0xff; TR0 = 0; //定时器0开始计时 TR1 = 0; //定时器1开始计时 ET1 = 0; //使能定时器1 ET0 = 0; //使能定时器0 PT0 = 0; //提高计时准确性,提高定时器0中断优先级 EA = 0; } while(!key5); } if(!key6){ _nop(); if(!key6){ TimeInit(); } while(!key6); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。