基于8051单片机和DS12C8878位数码管显示的时钟
#include
#include
#define sec XBYTE[0xed00]//秒寄存器地址
#define min XBYTE[0xed02]//分寄存器地址
#define hour XBYTE[0xed04]//小时寄存器地址
#define DS12887a XBYTE[0xed0a]//12887A寄存器地址
#define DS12887b XBYTE[0xed0b]//12887B寄存器地址
sbit beep=P1^4;
//sbit DS12887CS=P2^1;
sbit DISP=P2^4;
sbit S1=P2^2;
void delay2ms(unsigned char t) //单位延时2ms函数
{
unsigned char i,j;
for(;t>0;t--)
for(i=4;i>0;i--)
for(j=248;j>0;j--);
}
void clock_initial(void) //12887初始化,所有中断禁止,24h制,BCD码格式
{
//DS12887CS=0;
DS12887b=0x86;//禁止时钟更新
hour=0x12;
min=0x0D;
sec=0x00;
DS12887a=0x20;//启动内部锂电池
DS12887b=0x06;//启动时钟更新
//DS12887CS=1;
}
void disp (void)
{
unsigned char i,h,m,s,j;
unsigned char a[8];
unsigned char code b[8]={0x1f,0x9f,0x5f,0xdf,0x3f,0xbf,0x7f,0xff};
unsigned char code c[12]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0x00};
//DS12887CS=0;
h=hour;
m=min;
s=sec;//读取时间
//DS12887CS=1;
if(s%2)
a[0]=c[11];
else
a[0]=c[h/10];//小时十位
a[1]=c[h%10];//小时个位
if(s%2)
a[2]=c[11];
else
a[2]=c[10];//显示“- ”“
a[3]=c[m/10];//分钟十位
a[4]=c[m%10];//分钟个位
if(s%2)
a[5]=c[11];
else
a[5]=c[10];//显示“- ”“
a[6]=c[s/10];//秒十位
a[7]=c[s%10];//秒个位
for(j=0;j<3;j++)
for(i=0;i<8;i++)
{
DISP=0;
P0=a[i];
DISP=1;
P2=b[i];
delay2ms(1);
S1=0;
}
}
void main (void)
{
S1=0;
clock_initial();
while(1)
disp();
}