#include
#include
#define uchar unsigned char
#define uint unsigned int
***it dula=P2^6; //数码管段选线
***it wela=P2^7; //数码管位选线
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x07,
0x39,0x5e,0x79,0x71
};
***it IO=P2^2;
***it SCK=P2^3;
***it RST=P2^1;
uint i,te,g;
unsigned char time_buf1[8] = {20,10,6,5,12,55,00,6};//空年月日时分秒周
unsigned char time_buf[8] ; //空年月日时分秒周
/*-------------------------------------- 向DS1302写入一字节数据
------------------------------------------------*/
void Ds1302_Write_Byte(unsigned char addr, unsigned char d)
{
unsigned char i;
RST=1;
//写入目标地址:addr
addr = addr & 0xFE; //最低位置零
for (i = 0; i < 8; i ++)
{
if (addr & 0x01)
{
IO=1;
}
else
{
IO=0;
}
SCK=1;
SCK=0;
addr = addr >> 1;
}
//写入数据:d
for (i = 0; i < 8; i ++)
{
if (d & 0x01)
{
IO=1;
}
else
{
IO=0;
}
SCK=1;
SCK=0;
d = d >> 1;
}
RST=0; //停止DS1302总线
}
/*------------------------------------------------
从DS1302读出一字节数据
------------------------------------------------*/
unsigned char Ds1302_Read_Byte(unsigned char addr)
{
unsigned char i;
unsigned char temp;
RST=1;
//写入目标地址:addr
addr = addr | 0x01;//最低位置高
for (i = 0; i < 8; i ++)
{
if (addr & 0x01)
{
IO=1;
}
else
{
IO=0;
}
SCK=1;
SCK=0;
addr = addr >> 1;
}
//输出数据:temp
for (i = 0; i < 8; i ++)
{
temp = temp >> 1;
if (IO)
{
temp |= 0x80;
}
else
{
temp &= 0x7F;
}
SCK=1;
SCK=0;
}
RST=0; //停止DS1302总线
return temp;
}
/*------------------------------------------------
向DS1302写入时钟数据
------------------------------------------------*/
void Ds1302_Write_Time(void)
{
unsigned char i,tmp;
for(i=0;i<8;i++)
{ //BCD处理变为十六进制
tmp=time_buf1[i]/10;
time_buf[i]=time_buf1[i]%10;
time_buf[i]=time_buf[i]+tmp*16;
}
Ds1302_Write_Byte(0x8e,0x00); //关闭写保护
Ds1302_Write_Byte(0x80,0x80); //暂停
//Ds1302_Write_Byte(ds1302_charger_add,0xa9); //涓流充电
Ds1302_Write_Byte(0x8c,time_buf[1]); //年
Ds1302_Write_Byte(0x88,time_buf[2]); //月
Ds1302_Write_Byte(0x86,time_buf[3]); //日
Ds1302_Write_Byte(0x8a,time_buf[7]); //周
Ds1302_Write_Byte(0x84,time_buf[4]); //时
Ds1302_Write_Byte(0x82,time_buf[5]); //分
Ds1302_Write_Byte(0x80,time_buf[6]); //秒
Ds1302_Write_Byte(0x8e,0x80); //打开写保护
}
/*------------------------------------------------
从DS1302读出时钟数据
------------------------------------------------*/
void Ds1302_Read_Time(void)
{
unsigned char i,tmp;
time_buf[1]=Ds1302_Read_Byte(0x8d); //年
time_buf[2]=Ds1302_Read_Byte(0x89); //月
time_buf[3]=Ds1302_Read_Byte(0x87); //日
time_buf[4]=Ds1302_Read_Byte(0x85); //时
time_buf[5]=Ds1302_Read_Byte(0x83); //分
time_buf[6]=(Ds1302_Read_Byte(0x81))&0x7F;//秒
time_buf[7]=Ds1302_Read_Byte(0x8b); //周
for(i=0;i<8;i++)
{ //BCD处理变为十进制
tmp=time_buf[i]/16;
time_buf1[i]=time_buf[i]%16;
time_buf1[i]=time_buf1[i]+tmp*10;
}
}
/*------------------------------------------------
DS1302初始化
------------------------------------------------*/
void Ds1302_Init(void)
{
RST=0; //RST脚置低
SCK=0; //SCK脚置低
Ds1302_Write_Byte(0x80,0x00);
}
void dis_temp(void)
{ te=time_buf1[4]/10;
g=time_buf1[4]%10;
dula=0;
wela=1;
P0=0xfe;
dula=1;
wela=0;
P0=table[te];
dula=0;
wela=1;
P0=0xfd;
dula=1;
wela=0;
P0=table[g];
dula=0;
wela=1;
P0=0xfb;
dula=1;
wela=0;
P0=0x4f;
te=time_buf1[5]/10;
g=time_buf1[5]%10;
dula=0;
wela=1;
P0=0xf7;
dula=1;
wela=0;
P0=table[te];
dula=0;
wela=1;
P0=0x7f;
dula=1;
wela=0;
P0=table[g];
dula=0;
wela=1;
P0=0xbf;
dula=1;
wela=0;
P0=0x4f;
te=time_buf1[6]/10;
g=time_buf1[6]%10;
dula=0;
wela=1;
P0=0xdf;
dula=1;
wela=0;
P0=table[te];
dula=0;
wela=1;
P0=0xef;
dula=1;
wela=0;
P0=table[g];
}
void main()
{
Ds1302_Init();
Ds1302_Write_Time();
while(1)
{
Ds1302_Read_Time();
dis_temp();
}
}
|