1.Delay.h
#ifndef __DELAY_H__
#define __DELAY_H__
void Delay(unsigned int xms);
#endif
2.Delay.c
void Delay(unsigned int xms)
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
3.Nixie.h
#ifndef __NIXIE_H__
#define __NIXIE_H__
void Nixie(unsigned char Location,unsigned int Number,Xms);
#endif
4.Nixie.c
#include <REGX52.H>
#include "Delay.h" //包含Delay头文件
//数码管段码表 0-9
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
//数码管显示函数
void Nixie(unsigned char Location,unsigned int Number,Xms)
{
switch(Location)
{
case 1:P2_4=1;P2_3=1;P2_2=1;break; //LED7
case 2:P2_4=1;P2_3=1;P2_2=0;break; //LED6
case 3:P2_4=1;P2_3=0;P2_2=1;break; //LED5
case 4:P2_4=1;P2_3=0;P2_2=0;break; //LED4
case 5:P2_4=0;P2_3=1;P2_2=1;break; //LED3
case 6:P2_4=0;P2_3=1;P2_2=0;break; //LED2
case 7:P2_4=0;P2_3=0;P2_2=1;break; //LED1
case 8:P2_4=0;P2_3=0;P2_2=0;break; //LED0
}
P0=NixieTable[Number]; //段码输出
Delay(Xms); //显示一段时间
P0=0x00; //段码清0,消影
}
5.主程序代码
#include <REGX52.H>
#include "Delay.h"
#include "Nixie.h"
unsigned long int i=0;
unsigned long int a=0;
unsigned long int b=0;
unsigned long int c=0;
unsigned long int d=0;
unsigned long int e=0;
unsigned long int f=0;
void main()
{
while(1)
{
for(;i<=86400;i++)
{
a=i%60%10; //秒钟个位
b=i%60/10; //秒钟十位
c=i/60%60%10; //分钟个位
d=i/60%60/10; //分钟十位
e=i/(60*60)%10; //小时个位
f=i/(60*60)/10; //小时十位
Nixie(1,f,0);
Nixie(2,e,0);
Nixie(3,d,0);
Nixie(4,c,0);
Nixie(5,b,0);
//Delay(1000);
Nixie(6,a,0);
}
if(i==86400)
{
i=0;
}
}
}
注意:此代码功能通过秒的计算分别求出小时、分钟、秒的各个位置的值,仅展示快速时钟效果,缺陷在于如果每秒刷新会出现闪烁效果