初级会员
- 积分
- 54
- 金钱
- 54
- 注册时间
- 2016-3-17
- 在线时间
- 11 小时
|
1金钱
#include<reg52.h>
#include<intrins.h>
#include<stdio.h>
unsigned char code LEDchar[]={0xC0,0xF9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
unsigned char come[] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
sbit ds1302_ck = P1^7;
sbit ds1302_io = P2^3;
sbit ds1302_ce = P1^3;
bit flag200ms = 0;
//unsigned char T0RH = 0,T0RL = 0;
void delay(unsigned int t)
{
do{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}while(--t);
}
void dsiplay()
{
unsigned char i,j;
j = 0x80;
for(i=0;i<8;i++)
{
P2 = 0xE0;
P0 = LEDchar[come[i]];
P2 = 0xC0;
P0 = j;
j>>=1;
delay(100);
}
}
void ds1302bytewrite(unsigned char dat)
{
unsigned char mask;
for(mask=0x01;mask!=0;mask<<=1)
{
if((mask&dat)==0)
ds1302_io = 0;
else
ds1302_io = 1;
ds1302_ck = 1;
ds1302_ck = 0;
}
ds1302_io = 1;
}
unsigned char ds1302byteread()
{
unsigned char mask;
unsigned char dat = 0;
for(mask=0x01;mask!=0;mask<<=1)
{
if(ds1302_io!=0)
{
dat |= mask;
}
ds1302_ck = 1;
ds1302_ck = 0;
}
//ds1302_io = 1;
return dat;
}
void ds1302singlewrite(unsigned char reg,unsigned char dat)
{
ds1302_ce = 1;
ds1302bytewrite((reg<<1)|0x80);
ds1302bytewrite(dat);
ds1302_ce = 0;
}
unsigned char ds1302singleread(unsigned char reg)
{
unsigned char dat;
ds1302_ce = 1;
ds1302bytewrite((reg<<1)|0x81);
dat = ds1302byteread();
ds1302_ce = 0;
return dat;
}
void initds1302()
{
unsigned char i;
unsigned char code inittime[] = {0x48,0x58,0x47};
ds1302_ce = 0;
ds1302_ck = 0;
i = ds1302singleread(0);
if((i & 0x80)!=0)
{
ds1302singlewrite(7,0x00);
for(i=0;i<3;i++)
{
ds1302singlewrite(i,inittime[i]);
}
}
}
void main()
{
unsigned char i;
unsigned char psec = 0xaa;
unsigned char time[2];
unsigned char tmr200ms = 0;
TMOD = 0x21;
TH0 = 0xFC;
TL0 = 0x67;
ET0 = 1;
TR0 = 1;
EA = 1;
initds1302();
while(1)
{
if(flag200ms==1)
{
flag200ms = 0;
for(i=0;i<3;i++)
{
time[i] = ds1302singleread(i);
}
if(psec != time[0])
{
come[0] = 0x40;
come[1] = 0x40;
come[2] = time[0]&0x0f;
come[3] = time[0]>>4;
come[4] = time[1]&0x0f;
come[5] = time[1]>>4;
come[6] = time[2]&0x0f;
come[7] = time[2]>>4;
dsiplay();
psec = time[0];
}
}
}
}
void interrupttime() interrupt 1
{
static unsigned char tmr200ms = 0;
TH0 = 0xFC;
TL0 = 0x67;
tmr200ms++;
if(tmr200ms >= 200)
{
tmr200ms = 0;
flag200ms = 1;
}
}
|
|