相关代码:
主函数部分:
#include “STC15F2K60S2.H”
#include “absacc.h”
#include “ds1302.h”
unsigned char SMGNoDot_CA[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char Write_DS1302[7] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char Read_DS1302[7] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8c};
//21年2月26日星期五24点59分30秒
unsigned char Time[7] = {0x30,0x59,0x23,0x26,0x02,0x05,0x21};
void Delay(unsigned char t)
{
while(t–);
}
void config()
{
unsigned char i;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<7;i++)
{
Write_Ds1302_Byte( Write_DS1302[i],Time[i]);
}
Write_Ds1302_Byte(0x8e,0x80);
}
void Read_DS1302C()
{
unsigned char i;
for(i=0;i<7;i++)
{
Time[i] = Read_Ds1302_Byte(Read_DS1302[i]);
}
}
void Clear()
{
XBYTE[0xc000] = 0xff;
XBYTE[0xe000] = 0xff;
}
void DisSMG(unsigned char pos,unsigned char dat)
{
XBYTE[0xe000] = 0xff;
XBYTE[0xc000] = 0x01 << pos;
XBYTE[0xe000] = dat;
}
void Display()
{
DisSMG(0,SMGNoDot_CA[Time[2] / 16]);
Delay(100);
DisSMG(1,SMGNoDot_CA[Time[2] % 16]);
Delay(100);
DisSMG(2,0xbf);
Delay(100);
DisSMG(3,SMGNoDot_CA[Time[1] / 16]);
Delay(100);
DisSMG(4,SMGNoDot_CA[Time[1] % 16]);
Delay(100);
DisSMG(5,0xbf);
Delay(100);
DisSMG(6,SMGNoDot_CA[Time[0] / 16]);
Delay(100);
DisSMG(7,SMGNoDot_CA[Time[0] % 16]);
Delay(100);
Clear();
}
void main(void)
{
XBYTE[0x8000] = 0xff;
config();
while(1)
{
Read_DS1302C();
Display();
}
}
DS1302驱动.c文件:
/*
程序说明: DS1302驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include <reg52.h>
#include <intrins.h>
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302复位
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; nop();
SCK=0; nop();
RST=1; nop();
Write_Ds1302(address);
Write_Ds1302(dat);
RST=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0; nop();
SCK=0; nop();
RST=1; nop();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0; nop();
SCK=0; nop();
SCK=1; nop();
SDA=0; nop();
SDA=1; nop();
return (temp);
}
DS1302驱动.h文件:
#ifndef __DS1302_H
#define __DS1302_H
//void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
#endif