调试DS1302时遇到的问题

2020-02-02 09:28发布

这几天用DS1302,程序出来之后很不理想
先贴贴程序:
void WriteByte(uchar dat)
{
        uchar i;
        ACC=dat;
        for(i=8;i>0;i--)
        {
                Dat_1302=ACC0;
                CLK_1302=1;       
                _nop_();_nop_();_nop_();
                CLK_1302=0;        //产生上升沿输入数据
                ACC=ACC>>1;
        }       
}

void WriteSet1302(uchar Cmd,uchar dat)
{
        RST_1302=0;
        CLK_1302=0; //确保写数居前SCLK被拉低
        RST_1302=1; //启动数据传输
        WriteByte(Cmd);
        WriteByte(dat);
        RST_1302=0;
        CLK_1302=1;       
}

uchar ReadByte(void)
{
        uchar i;
        for(i=8;i>0;i--)
        {
                ACC7=Dat_1302;//ds1302读数据的时候,第一个数据读取在发一个Byte命令后,在第八位的下降沿
                ACC=ACC>>1;
                CLK_1302=1;
                CLK_1302=0;//产生下降沿输出一位数据
                _nop_();_nop_();_nop_();
        }
        return(ACC);
}

uchar ReadSet1302(uchar Cmd)
{
        uchar dat;
        RST_1302=0;
        CLK_1302=0; //确保写数居前SCLK被拉低
        RST_1302=1; //启动数据传输
        WriteByte(Cmd);
        dat=ReadByte();
        RST_1302=0;
        CLK_1302=1;
        return dat; //将读出的数据返回
}

void Init1302(void)
{
        WriteSet1302(0x8e,0x00);
        WriteSet1302(0x80,0x05);//初始化时分秒
        WriteSet1302(0x82,0x10);
        WriteSet1302(0x84,0x15);
        WriteSet1302(0x90,0xab);
        WriteSet1302(0x8e,0x80);
}
程序遇到的问题是读取时间时老是出错,有时候是秒的数据错了,有时候是分的数据,有时候是时间的数据
而且写进去时间后读出来的数据也有错
不知道是哪里的问题
在网上看了很多这方面的程序,感觉都没有错
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
24条回答
tianzhiying
1楼-- · 2020-02-04 05:25
别人都说可以的程序
放到我这里就不行了
ybs1902
2楼-- · 2020-02-04 06:17
 精彩回答 2  元偷偷看……
millwood0
3楼-- · 2020-02-04 09:23
本帖最后由 millwood0 于 2012-3-31 03:34 编辑
最好不要在C语言中用直接利用寄存器的操作,无论是处于可移植性和可靠性方面考虑。


that's just basic, embedded programming 101. because you never know, without looking at the assembly generated by the compiler, if the compiler is also utilizing ACC. if it is, writing to and reading from ACC can cause unpredictable behaviors that are impossible to debug.

关这一点就下结论"DS1302的时序是不受中断影响"是不科学的


I have not looked the datasheet for ds1302 but spi allows the master to hold the clock pulse indefinitely. and it would surprise me if DS1302 is an exception to that.

if so, the code doesn't have to factor in interrupt-caused delays.
zyyn123
4楼-- · 2020-02-04 13:01
楼主还是看看时序上是不是出了点问题,记得原来自已做的时候也出现过这样的问题,后来从网上找个程序参考了下,问题就解决了.
jjj2012
5楼-- · 2020-02-04 14:14
那个h文件啊不错
yihui184
6楼-- · 2020-02-04 15:57
ypradio 发表于 2012-3-28 17:55
不一定是软件问题,叫你的硬件工程师测试下信号,说不定硬件的问题。
出了问题,软硬件一定要一起处理,千 ...

说得太对!对!