DS1302有问题还是程序出错?
时间:10-02
整理:3721RD
点击:
#include <REGX52.H>
#include <intrins.h>
#define uint8 unsigned char
uint8 a,b,c;
sbit io_DS1302_RST = P2^5;
sbit io_DS1302_IO = P2^6 ;
sbit io_DS1302_SCLK = P2^7 ;
//-------------------------------------常数宏---------------------------------//
#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 ;
/******************************************************
* 保存时间数据的结构体 *
******************************************************/
struct
{
uint8 Second ;
uint8 Minute ;
uint8 Hour ;
uint8 Day ;
uint8 Week ;
uint8 Month ;
uint8 Year ;
}CurrentTime ;
/*****************************************************************************
* 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 )
{
CurrentTime.Second = v_DS1302ReadByte_f( DS1302_SECOND_READ ) ;
CurrentTime.Minute = v_DS1302ReadByte_f( DS1302_MINUTE_READ ) ;
CurrentTime.Hour = v_DS1302ReadByte_f( DS1302_HOUR_READ ) ;
CurrentTime.Day = v_DS1302ReadByte_f( DS1302_DAY_READ ) ;
CurrentTime.Month = v_DS1302ReadByte_f( DS1302_MONTH_READ ) ;
CurrentTime.Week = v_DS1302ReadByte_f( DS1302_WEEK_READ ) ;
CurrentTime.Year = v_DS1302ReadByte_f( DS1302_YEAR_READ ) ;
}
void InitUART(void)
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFD;
TL1 = TH1;
PCON = 0x00;
EA = 1;
ES = 1;
TR1 = 1;
}
void SendOneByte(unsigned char c)
{
SBUF = c;
while(!TI);
TI = 0;
}
void delay1s(void) //误差 -0.000000000125us
{
unsigned char a,b,c;
for(c=212;c>0;c--)
for(b=160;b>0;b--)
for(a=80;a>0;a--);
_nop_(); //if Keil,require use intrins.h
_nop_(); //if Keil,require use intrins.h
}
void main(void)
{
InitUART();
v_ClockInit_f() ;
while(1)
{
v_ClockUpdata_f();
SendOneByte(CurrentTime.Hour );
SendOneByte(CurrentTime.Minute);
SendOneByte(CurrentTime.Second);
delay1s();
}
}
void UARTInterrupt(void) interrupt 4
{
if(RI)
{
RI = 0;
//add your code here!
}
}
[img]file:///C:/Documents%20and%20Settings/Administrator/Application%20Data/Tencent/Users/1073277486/QQ/WinTemp/RichOle/M]K@B%]9MA0W%)_][G9V$HN.jpg[/img]
#include <intrins.h>
#define uint8 unsigned char
uint8 a,b,c;
sbit io_DS1302_RST = P2^5;
sbit io_DS1302_IO = P2^6 ;
sbit io_DS1302_SCLK = P2^7 ;
//-------------------------------------常数宏---------------------------------//
#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 ;
/******************************************************
* 保存时间数据的结构体 *
******************************************************/
struct
{
uint8 Second ;
uint8 Minute ;
uint8 Hour ;
uint8 Day ;
uint8 Week ;
uint8 Month ;
uint8 Year ;
}CurrentTime ;
/*****************************************************************************
* 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 )
{
CurrentTime.Second = v_DS1302ReadByte_f( DS1302_SECOND_READ ) ;
CurrentTime.Minute = v_DS1302ReadByte_f( DS1302_MINUTE_READ ) ;
CurrentTime.Hour = v_DS1302ReadByte_f( DS1302_HOUR_READ ) ;
CurrentTime.Day = v_DS1302ReadByte_f( DS1302_DAY_READ ) ;
CurrentTime.Month = v_DS1302ReadByte_f( DS1302_MONTH_READ ) ;
CurrentTime.Week = v_DS1302ReadByte_f( DS1302_WEEK_READ ) ;
CurrentTime.Year = v_DS1302ReadByte_f( DS1302_YEAR_READ ) ;
}
void InitUART(void)
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFD;
TL1 = TH1;
PCON = 0x00;
EA = 1;
ES = 1;
TR1 = 1;
}
void SendOneByte(unsigned char c)
{
SBUF = c;
while(!TI);
TI = 0;
}
void delay1s(void) //误差 -0.000000000125us
{
unsigned char a,b,c;
for(c=212;c>0;c--)
for(b=160;b>0;b--)
for(a=80;a>0;a--);
_nop_(); //if Keil,require use intrins.h
_nop_(); //if Keil,require use intrins.h
}
void main(void)
{
InitUART();
v_ClockInit_f() ;
while(1)
{
v_ClockUpdata_f();
SendOneByte(CurrentTime.Hour );
SendOneByte(CurrentTime.Minute);
SendOneByte(CurrentTime.Second);
delay1s();
}
}
void UARTInterrupt(void) interrupt 4
{
if(RI)
{
RI = 0;
//add your code here!
}
}
[img]file:///C:/Documents%20and%20Settings/Administrator/Application%20Data/Tencent/Users/1073277486/QQ/WinTemp/RichOle/M]K@B%]9MA0W%)_][G9V$HN.jpg[/img]
DS1302地线接上显示就不对 83 (时)01(分) 01(秒)
不接地线显示就对 13 (时)33(分) 09(秒)
接不接地线显示的数据都有变化;
不知道为什么?
哪位能解释一下啊?
谢谢!
程序量太大,不能给你一一分析,因为接地与不接地的原因好像不是很大,如果是地的,就应该接地,但是不要接错。由于你有显示对的时候,所以估计你的软件应该没有问题,还是多注意硬件上的问题
应该是程序有问题吧,具体我也不是很清楚的
谢谢楼上两位!
还是找不出原因