方法一:利用定时器溢出查询实现
#include <reg51.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
//共阴数码管段选表0-9
uchar code tabel[]= {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,0x40};
uchar hour,minute,second;
void delay();
void display();
void timerInit();
void main(void)
{
uchar i;
timerInit();
i = 0;
hour = 12;
minute = 34;
second = 56;
while(1){
if( 1 == TF0 ){
TF0 = 0;
TH0 = 0x4B;
TL0 = 0xFC;
i++;
if( 20 == i ){
second++;
i = 0;
}
if( 60 == second ){
second = 0;
minute++;
}
if( 60 == minute ){
minute = 0;
hour++;
}
if( 24 == hour ){
hour = 0;
}
}
display();
}
}
void timerInit()
{
TMOD = 0x01;
TH0 = 0x4B;
TL0 = 0xFC;
TR0 = 1;
}
void delay()
{
uchar i;
for( i = 0; i < 111; i++ ) ;
}
void display()
{
P0 = tabel[ hour / 10 ];
P1 = 0x80;
delay();
P0 = tabel[ hour % 10 ];
P1 = 0x40;
delay();
P0 = tabel[ 10 ];
P1 = 0x20;
delay();
P0 = tabel[ minute / 10 ];
P1 = 0x10;
delay();
P0 = tabel[ minute % 10 ];
P1 = 0x08;
delay();
P0 = tabel[ 10 ];
P1 = 0x04;
delay();
P0 = tabel[ second / 10 ];
P1 = 0x02;
delay();
P0 = tabel[ second % 10 ];
P1 = 0x01;
delay();
}
方法二:利用定时器中断实现
#include <reg51.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
//共阴数码管段选表0~9和-
uchar code tabel[]= {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,0x40};
uchar code wei[]= {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
uchar hour,minute,second,i;
void delay();
void display();
void timerInit();
void main(void)
{
timerInit();
hour = 12;
minute = 34;
second = 56;
while(1){
display();
}
}
void timerInit()
{
TMOD = 0x01;
TH0 = 0x4B;
TL0 = 0xFC;
TR0 = 1;
EA = 1;
ET0 = 1;
}
void delay()
{
uchar i;
for( i = 0; i < 111; i++ ) ;
}
void display()
{
P0 = tabel[ hour / 10 ];
P1 = wei[ 7 ];
delay();
P0 = tabel[ hour % 10 ];
P1 = wei[ 6 ];
delay();
P0 = tabel[ 10 ];
P1 = wei[ 5 ];
delay();
P0 = tabel[ minute / 10 ];
P1 = wei[ 4 ];
delay();
P0 = tabel[ minute % 10 ];
P1 = wei[ 3 ];
delay();
P0 = tabel[ 10 ];
P1 = wei[ 2 ];
delay();
P0 = tabel[ second / 10 ];
P1 = wei[ 1 ];
delay();
P0 = tabel[ second % 10 ];
P1 = wei[ 0 ];
delay();
}
void timer0Int() interrupt 1
{
i++;
if( 20 == i ){
second++;
i = 0;
}
if( 60 == second ){
second = 0;
minute++;
}
if( 60 == minute ){
minute = 0;
hour++;
}
if( 24 == hour ){
hour = 0;
}
TH0 = 0x4B;
TL0 = 0xFC;
}
【必读】版权免责声明
1、本主题所有言论和内容纯属会员个人意见,与本论坛立场无关。2、本站对所发内容真实性、客观性、可用性不做任何保证也不负任何责任,网友之间仅出于学习目的进行交流。3、对提供的数字内容不拥有任何权利,其版权归原著者拥有。请勿将该数字内容进行商业交易、转载等行为,该内容只为学习所提供,使用后发生的一切问题与本站无关。 4、本网站不保证本站提供的下载资源的准确性、安全性和完整性;同时本网站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。 5、本网站所有软件和资料均为网友推荐收集整理而来,仅供学习用途使用,请务必下载后两小时内删除,禁止商用。6、如有侵犯你版权的,请及时联系我们(电子邮箱1370723259@qq.com)指出,本站将立即改正。
|
|