#include
#include
//595端口
sbit SD=P3^5;
sbit SCK=P3^7;
sbit RCK=P3^6;
sbit io_DS1302_RST = P2^0 ;
sbit io_DS1302_IO = P2^1 ;
sbit io_DS1302_SCLK = P2^2 ;
//-------------------------------------常数宏---------------------------------//
#define DS1302_SECOND_WRITE 0x80 //写时钟芯片的寄存器位置
#define DS1302_MINUTE_WRITE 0x82
#define DS1302_HOUR_WRITE 0x84
#define DS1302_WEEK_WRITE 0x8A
#define DS1302_DAY_WRITE 0x86
#define DS1302_MONTH_WRITE 0x88
#define DS1302_YEAR_WRITE 0x8C
#define DS1302_SECOND_READ 0x81 //读时钟芯片的寄存器位置
#define DS1302_MINUTE_READ 0x83
#define DS1302_HOUR_READ 0x85
#define DS1302_WEEK_READ 0x8B
#define DS1302_DAY_READ 0x87
#define DS1302_MONTH_READ 0x89
#define DS1302_YEAR_READ 0x8D
//-----------------------------------操作宏----------------------------------//
#define DS1302_SCLK_HIGH io_DS1302_SCLK = 1 ;
#define DS1302_SCLK_LOW io_DS1302_SCLK = 0 ;
#define DS1302_IO_HIGH io_DS1302_IO = 1 ;
#define DS1302_IO_LOW io_DS1302_IO = 0 ;
#define DS1302_IO_READ io_DS1302_IO
#define DS1302_RST_HIGH io_DS1302_RST = 1 ;
#define DS1302_RST_LOW io_DS1302_RST = 0 ;
#define uint8 unsigned int
#define uchar unsigned char
uchar code disp_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
/******************************************************
* 保存时间数据的结构体 *
******************************************************/
uint8 Second,Minute,Hour, Day, Week,Month, Year ;
void delay()
{
uchar n;
for(n=0;n<100;n++) ;
}
//595串入并出
void WR1_595(uchar num1)
{
uchar date;
uchar b;
date=num1;
for(b=0;b<=7;b++)
{
if((date&0x80)==0x80) //最高位为1,则向SDATA_595发送1
SD=1; //发出数据的最高位
else
SD=0;
date<<=1; //左移位
SCK=0;
_nop_();
_nop_();
SCK=1; //产生上升沿
}
}
/******************************************************************************
* Function: static void v_DS1302Write_f( uint8 Content ) *
* Description:向DS1302写一个字节的内容 *
* Parameter:uint8 Content : 要写的字节 *
* *
******************************************************************************/
static void v_DS1302Write_f( uint8 Content )
{
uint8 i ;
for( i = 8 ; i > 0 ; i-- )
{
if( Content & 0x01 )
{
DS1302_IO_HIGH
}
else
{
DS1302_IO_LOW
}
Content >>= 1 ;
DS1302_SCLK_HIGH
DS1302_SCLK_LOW
}
}
/******************************************************************************
* Function: static uint8 v_DS1302Read_f( void ) *
* Description: 从DS1302当前设定的地址读取一个字节的内容 *
* Parameter: *
* Return: 返回读出来的值(uint8) *
******************************************************************************/
static uint8 v_DS1302Read_f( void )
{
uint8 i, ReadValue ;
DS1302_IO_HIGH
for( i = 8 ; i > 0 ; i-- )
{
ReadValue >>= 1 ;
if( DS1302_IO_READ )
{
ReadValue |= 0x80 ;
}
else
{
ReadValue &= 0x7f ;
}
DS1302_SCLK_HIGH
DS1302_SCLK_LOW
}
return ReadValue ;
}
/******************************************************************************
* Function: void v_DS1302WriteByte_f( uint8 Address, uint8 Content ) *
* Description: 从DS1302指定的地址写入一个字节的内容 *
* Parameter: Address: 要写入数据的地址 *
* Content: 写入数据的具体值 *
* Return: *
******************************************************************************/
void v_DS1302WriteByte_f( uint8 Address, uint8 Content )
{
DS1302_RST_LOW
DS1302_SCLK_LOW
DS1302_RST_HIGH
v_DS1302Write_f( Address ) ;
v_DS1302Write_f( Content ) ;
DS1302_RST_LOW
DS1302_SCLK_HIGH
}
/******************************************************************************
* Function: uint8 v_DS1302ReadByte_f( uint8 Address ) *
* Description:从DS1302指定的地址读出一个字节的内容 *
* Parameter:Address: 要读出数据的地址 *
* *
* Return: 指定地址读出的值(uint8) *
******************************************************************************/
uint8 v_DS1302ReadByte_f( uint8 Address )
{
uint8 ReadValue ;
DS1302_RST_LOW
DS1302_SCLK_LOW
DS1302_RST_HIGH
v_DS1302Write_f( Address ) ;
ReadValue = v_DS1302Read_f() ;
DS1302_RST_LOW
DS1302_SCLK_HIGH
return ReadValue ;
}
/******************************************************************************
* Function: void v_ClockInit_f( void ) *
* Description:初始化写入DS1302时钟寄存器的值(主程序中只需调用一次即可) *
* Parameter: *
* *
* Return: *
******************************************************************************/
void v_ClockInit_f( void )
{
if( v_DS1302ReadByte_f( 0xc1) != 0xf0 )
{
v_DS1302WriteByte_f( 0x8e, 0x00 ) ; //允许写操作
v_DS1302WriteByte_f( DS1302_YEAR_WRITE, 0x08 ) ; //年
v_DS1302WriteByte_f( DS1302_WEEK_WRITE, 0x04 ) ; //星期
v_DS1302WriteByte_f( DS1302_MONTH_WRITE, 0x12 ) ; //月
v_DS1302WriteByte_f( DS1302_DAY_WRITE, 0x11 ) ; //日
v_DS1302WriteByte_f( DS1302_HOUR_WRITE, 0x13 ) ; //小时
v_DS1302WriteByte_f( DS1302_MINUTE_WRITE, 0x06 ) ; //分钟
v_DS1302WriteByte_f( DS1302_SECOND_WRITE, 0x40 ) ; //秒
v_DS1302WriteByte_f( 0x90, 0xa5 ) ; //充电
v_DS1302WriteByte_f( 0xc0, 0xf0 ) ; //判断是否初始化一次标识写入
v_DS1302WriteByte_f( 0x8e, 0x80 ) ; //禁止写操作
}
}
/******************************************************************************
* Function: void v_ClockUpdata_f( void ) *
* Description:读取时间数据,并保存在结构体CurrentTime中 *
* Parameter: *
* *
* Return: *
******************************************************************************/
void v_ClockUpdata_f( void )
{
Second = v_DS1302ReadByte_f( DS1302_SECOND_READ ) ;
Minute = v_DS1302ReadByte_f( DS1302_MINUTE_READ ) ;
Hour = v_DS1302ReadByte_f( DS1302_HOUR_READ ) ;
Day = v_DS1302ReadByte_f( DS1302_DAY_READ ) ;
Month = v_DS1302ReadByte_f( DS1302_MONTH_READ ) ;
Week = v_DS1302ReadByte_f( DS1302_WEEK_READ ) ;
Year = v_DS1302ReadByte_f( DS1302_YEAR_READ ) ;
}
void display()
{
RCK=0;
WR1_595(disp_code[Second]);
RCK=1;
}
void main()
{
v_ClockUpdata_f();
while(1)
{
display();
delay();
}
} |