单片机/MCUwilliam hill官网
登录
直播中
天满
12年用户
65经验值
私信
关注
[问答]
DS1302有问题还是程序出错?
开启该帖子的消息推送
#include
#include
#define uint8 unsigned char
uint8 a,b,c;
***it io_DS1302_RST = P2^5;
***it io_DS1302_IO = P2^6 ;
***it 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 ;
}Current
ti
me ;
/*****************************************************************************
* 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!
}
}
回帖
(5)
天满
2012-11-5 21:27:26
DS1302地线接上显示就不对 83 (时)01(分) 01(秒)
不接地线显示就对 13 (时)33(分) 09(秒)
接不接地线显示的数据都有变化;
不知道为什么??
哪位能解释一下啊??
谢谢!!!!
DS1302地线接上显示就不对 83 (时)01(分) 01(秒)
不接地线显示就对 13 (时)33(分) 09(秒)
接不接地线显示的数据都有变化;
不知道为什么??
哪位能解释一下啊??
谢谢!!!!
举报
张三
2012-11-6 08:50:12
程序量太大,不能给你一一分析,因为接地与不接地的原因好像不是很大,如果是地的,就应该接地,但是不要接错。由于你有显示对的时候,所以估计你的软件应该没有问题,还是多注意硬件上的问题
程序量太大,不能给你一一分析,因为接地与不接地的原因好像不是很大,如果是地的,就应该接地,但是不要接错。由于你有显示对的时候,所以估计你的软件应该没有问题,还是多注意硬件上的问题
举报
戴晓辉
2012-11-6 09:49:27
应该是程序有问题吧,具体我也不是很清楚的
应该是程序有问题吧,具体我也不是很清楚的
举报
天满
2012-11-6 19:16:46
谢谢楼上两位!!!!
谢谢楼上两位!!!!
举报
天满
2012-11-6 19:19:09
还是找不出原因
还是找不出原因
举报
更多回帖
rotate(-90deg);
回复
相关问答
ds1302
问题
2013-03-03
1793
求助,求发一份
ds1302
的汇编
程序
2017-12-19
3314
【急】
DS1302
不能写入时间
2017-01-21
10003
DS1302
和
DS
1307
有
什么区别
2013-01-18
26162
DS1302
写数据写BCD码
还是
十进制吗?
2023-10-26
256
修改后密码怎么存入
DS1302
中
2013-02-20
3161
ds1302
和
ds
12r885的差别大不大?
ds1302
只能用display语句吗?
2015-01-10
4217
请教关于
ds1302
的问题
2019-10-16
1776
关于
DS1302
中for循环的疑问。。
2012-03-30
2477
怎么测
DS1302
是烧了?
2013-02-26
4235
发帖
登录/注册
20万+
工程师都在用,
免费
PCB检查工具
无需安装、支持浏览器和手机在线查看、实时共享
查看
点击登录
登录更多精彩功能!
英国威廉希尔公司网站
william hill官网 版块
小组
免费开发板试用
ebook
直播
搜索
登录
×
20
完善资料,
赚取积分