搜索
bottom↓
回复: 48

DS1302用示波器测量了 晶振起阵.SCLK IO RST 都正常工作!为什么读出来的时间是不多

[复制链接]
头像被屏蔽

出0入0汤圆

发表于 2011-8-31 13:45:16 | 显示全部楼层 |阅读模式
提示: 作者被禁止或删除 内容自动屏蔽

出0入0汤圆

发表于 2011-8-31 13:50:47 | 显示全部楼层
有示波器的话测量下,你发出的波形合符规范不
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-8-31 13:51:24 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-8-31 13:52:24 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

出0入0汤圆

发表于 2011-8-31 13:55:43 | 显示全部楼层
我现在用的,你试试

/**************************************
   DS1302头文件
**************************************/

#ifndef __DS1302_H__   
#define __DS1302_H__

sbit    SCLK  = P3^5;
sbit    IO    = P2^0;
sbit    CE    = P3^7;

uchar nian,yue,ri,xqi,shi,fen;         //年,月,日,星期,时,分

/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
函数:向DS1302指定地址写一字节数据
入口:(操作地址,数据)
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
void DS1302_W(uchar addr,uchar dat)         
{
       uchar i;
       CE = 0;                          //CE 常态低电平,禁止数据传送
       SCLK = 0;                        //清零时钟总线
       CE = 1;                          //CE 引脚为高,逻辑控制
//发送地址
       for(i=8;i;i--)           
       {      
              SCLK = 0;
              IO = (bit)(addr&0x01);    //每次传输低字
                          addr >>= 1;               //右移一位
              SCLK = 1;
       }        
//发送数据
       for (i=8;i;i--)
       {      
              SCLK = 0;
              IO = (bit)(dat&0x01);  
                          dat >>= 1;                             
              SCLK = 1;
       }
       CE = 0;                          //禁止数据传送
}

/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
函数:向DS1302指定地址读一字节数据
入口:操作地址addr
出口:dat
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
uchar DS1302_R(uchar addr)
{
     uchar i,dat;
     CE=0;            
     SCLK=0;              
     CE = 1;   
//发送地址
     for(i=8;i;i--)            
     {      
        SCLK = 0;
        IO = (bit)(addr&0x01);          //每次传输低字
                addr >>= 1;                     //右移一位
                SCLK = 1;
     }
//读取数据
     for(i=8;i;i--)
     {
        SCLK = 0;
                dat >>= 1;
                if(IO)dat |= 0x80;
        SCLK = 1;
     }
             
     CE=0;                                                                 //常态低电平
     return dat;
}

/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
函数:保存 DS1302
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
void DS1302_Keep(void)      
{
   DS1302_W(0x8e,0x00);                 //关闭写保护,WP(8EH.7)=0时关闭写保护,此时可以写操作
   DS1302_W(0x8c,nian);                 //年
   DS1302_W(0x88,yue);                  //月
   DS1302_W(0x86,ri);                   //日
   DS1302_W(0x8a,xqi);                  //星期
   DS1302_W(0x84,shi);                  //时,84H.7=0时为24小时模式
   DS1302_W(0x82,fen);                  //分
   DS1302_W(0x80,0x00);                 //秒,CH(80H.7)=0时启动电子钟,80H.6←80H.4秒的十位,80H.3←80H.0秒的个位
   DS1302_W(0x8e,0x80);                 //打开写保护
}

/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
函数:初始化 DS1302 为 2011年8月22日星期一12:20:00
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
void DS1302_Init(void)      
{
   nian = 0x11;
   yue  = 0x08;
   ri   = 0x22;
   xqi  = 0x01;
   shi  = 0x12;
   fen  = 0x20;
   DS1302_Keep();
}

#endif
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-8-31 14:15:48 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

出0入42汤圆

发表于 2011-8-31 14:23:09 | 显示全部楼层
测量SPI,片选外触发,两通道分别时钟和数据.
人肉分析下.
参考文档的时序. 你自己写的话,能写出来就能调出来.
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-8-31 14:32:43 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

出0入0汤圆

发表于 2011-8-31 14:37:07 | 显示全部楼层
时序的问题
以前调的时候碰到过
对照时序把你的两个最基本的输入输出函数写正确。
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-8-31 14:46:27 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

出0入0汤圆

发表于 2011-8-31 14:54:03 | 显示全部楼层
呵呵,没pp没真相。
建议用示波器或逻辑分析仪抓段波形放上来,让大家判断。
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-8-31 15:06:09 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

出0入0汤圆

发表于 2011-8-31 15:22:56 | 显示全部楼层
回复【9楼】xiatianzhang
回复【8楼】jigsaw  
-----------------------------------------------------------------------
我是按照时序写的!!
-----------------------------------------------------------------------

要给时序图,别自以为没问题。真奇怪你哪来的自信
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-8-31 16:24:04 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

出0入0汤圆

发表于 2011-8-31 16:47:11 | 显示全部楼层
回复【13楼】xiatianzhang
-----------------------------------------------------------------------

倒,看来我要检讨自己的语文水平。
我说的是,把你的程序运行时的波形(时序),用示波器抓段放上来。
1302是很简单的,肯定是你疏忽了某些你自认为没问题的地方。
通过波形,别人更容易发现问题,帮你解决
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-8-31 16:51:43 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

出75入4汤圆

发表于 2011-8-31 16:55:33 | 显示全部楼层
好像写秒寄存器要其中要写1,不能写0.大概是这个意思。具体你再看看

出0入0汤圆

发表于 2011-8-31 16:57:26 | 显示全部楼层
数字示波器吗,能存住波形吗,拍照,上照片(压缩下)
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-8-31 17:05:00 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-8-31 17:21:30 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-8-31 18:04:01 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

出0入0汤圆

发表于 2011-8-31 18:17:52 | 显示全部楼层
如果是公司开发,建议拿来主义先试下别人的程序,测试下呗..

出0入0汤圆

发表于 2011-8-31 18:25:36 | 显示全部楼层
你没双路探头?这哪能看出时序关系

给你看看我请别人分析问题时,给的时序图。我是用逻辑分析仪


(原文件名:559c_duty.jpg)


(原文件名:559c_olderr1.jpg)
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-8-31 18:27:40 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

出0入0汤圆

发表于 2011-8-31 19:04:21 | 显示全部楼层
看出问题了,你还是时序错误
注意4楼程序里这段,先将clk置低,才读的io
uchar DS1302_R(uchar addr)  
{
//读取数据
     for(i=8;i;i--)  
     {  
        SCLK = 0;
            dat >>= 1;
            if(IO)dat |= 0x80;
        SCLK = 1;  
     }  

}
你还是对时序图没理解。
write时,master先准备好数据,然后上升沿锁存进1302.
read时,先下降沿通知1302准备好数据,然后在读io。


(原文件名:ds1302.JPG)
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-8-31 19:16:22 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

出0入0汤圆

发表于 2011-8-31 19:22:51 | 显示全部楼层
当然是你错。4楼要和你一样,人也上来抓狂了。
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-9-1 09:35:41 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

出0入0汤圆

发表于 2011-9-1 10:05:42 | 显示全部楼层
你的程序居然能对,表示不能理解。

看看你的读程序:
uchar  output_ds1302_1byte()
{
   uchar i;
   for(i=8;i>0;i--)
   {
     ACC=ACC>>1;
     ACC7=DS_SDA;
   DS_CLK=1;
   DS_CLK=0;
   }
   return ACC;
}

正确的:
uchar DS1302_R(uchar addr)   
{
//读取数据  
     for(i=8;i;i--)   
     {   
        SCLK = 0;  
            dat >>= 1;  
            if(IO)dat |= 0x80;  
        SCLK = 1;   
     }   

}

要先产生下降沿,然后去读io口。
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-9-1 11:07:28 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

出0入0汤圆

发表于 2011-9-1 11:23:07 | 显示全部楼层
我擦,我说话你都不看吗:“要先产生下降沿,然后去读io口。 ”

uchar read1302byte(void)  
{   
   for(i=8; i>0; i--)  
    {  
      ACC7 = IO;     //先读了IO
        CLK = 1;     //后产生下降沿
        CLK = 0;  
                    //<---- 应该在此处 ACC7 = IO;
      ACC = ACC >>1;  
    }   
}  

怪不得马老师说现在人浮躁,看看你9楼说的话,没看懂时序,敢说自己就按时序写的。
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-9-1 11:31:13 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

出0入0汤圆

发表于 2011-9-1 14:23:27 | 显示全部楼层
我看你一点都不着急。不去试试正确的程序,非要研究不正确的程序为什么得出正确的结果。(这个结果是否正确,我表示强烈怀疑)
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-9-1 14:55:25 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
头像被屏蔽

出0入0汤圆

 楼主| 发表于 2011-9-1 15:04:06 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

出0入0汤圆

发表于 2011-9-1 15:32:45 | 显示全部楼层
程序。怎么个不对法

出0入0汤圆

发表于 2011-9-1 22:33:37 | 显示全部楼层
以前做过的一个项目软件中使用的代码,提取出来,以供参考。

能使用的,好像ds1302直接接晶振,通讯口要上拉电阻的:

在main.c中主程序循环体内,每隔25ms调用一次如下的代码

#undef EXT
#define EXT extern

#include "rtc.h"

void main(void)
{

       while(1)
        {
               //25ms ticker entry
               if(25ms_ticker)
               {
                    vClkBurstRead(RTCReadedBuf);            
                  
                    Time.Hour = BCD2Dec( RTCReadedBuf[HOUR] & (~( (1<<7)|(1<<5) ) ) );
                    Time.Minute = BCD2Dec( RTCReadedBuf[MINUTE] );
                    Time.Second = BCD2Dec( RTCReadedBuf[SECOND] & (~(1<<7)));                 
                }
          }
}

在rtc.h中:

#ifndef DS1302_RTC_USING_DEBUG
   #define DS1302_RTC_USING_DEBUG  1
#else
   #define EXT

#endif

   


#if DS1302_RTC_USING_DEBUG

    typedef struct
    {
            unsigned char Hour;
            unsigned char Minute;
    }HOUR_MINUTE_FORMAT;
    EXT HOUR_MINUTE_FORMAT RTCTime;

    #define RTC_PORT_DDR_USING_DEBUG
    #ifdef RTC_PORT_DDR_USING_DEBUG
//st72f324 mcu

        #define RTC_Port_DDR    PEDDR
        /*
        #define DAT_Out PEDDR |= (1<<0); PEOR |= (1<<0);
        #define DAT_In  PEDDR &= ~(1<<0);PEOR &= ~(1<<0);
        #define CE_Out  PCDDR |= (1<<1); PCOR |= (1<<1);
        #define CLK_Out PEDDR |= (1<<1); PEOR |= (1<<1);
        */
        #define DAT_Out PEDDR |= (1<<0);
        #define DAT_In  PEDDR &= ~(1<<0);
        #define CE_Out  PCDDR |= (1<<1);
        #define CLK_Out PEDDR |= (1<<1);

    #endif

    //PA3, PA4, PA6 is corresponding connect to DS1302 CE, IO, SCK
    #define RTC_Port            PEDR.byte

    #define RTC_CLK_Set            (RTC_Port |= (1<<1) )           // PE.1
    #define RTC_CLK_Clr            (RTC_Port &= ~(1<<1))
    #define RTC_DATA_Set    (RTC_Port |= (1<<0) )           // PE.0
    #define RTC_DATA_Clr    (RTC_Port &= ~(1<<0))
    #define RTC_CE_Set            (PCDR |= (1<<1) )           // PC.1
    #define RTC_CE_Clr            (PCDR &= ~(1<<1))

    #define DATA_Pin                  (RTC_Port & (1<<0) )


        // Address/Command Byte
        #define WR_SEC          0x80
        #define WR_MIN          0x82
        #define WR_HOUR         0x84
        #define WR_DATE         0x86
        #define WR_MON          0x88
        #define WR_DAY          0x8A // day of a week
        #define WR_YEAR         0x8C
        #define WR_CTRL         0x8E
        #define WR_CHG          0x90 // trickle-charge register
        #define WR_CLK_BURST    0xBE
        #define WR_RAM_BURST    0xFE

        #define RD_SEC          WR_SEC | 0x01
        #define RD_MIN          WR_MIN | 0x01
        #define RD_HOUR         WR_HOUR | 0x01
        #define RD_DATE         WR_DATE | 0x01
        #define RD_MON          WR_MON | 0x01
        #define RD_DAY          WR_DAY | 0x01
        #define RD_YEAR         WR_YEAR | 0x01
        #define RD_CTRL         WR_CTRL | 0x01
        #define RD_CHG          WR_CHG | 0x01
        #define RD_CLK_BURST    WR_CLK_BURST | 0x01
        #define RD_RAM_BURST    WR_RAM_BURST | 0x01

    //regiseter address
    enum
    {
            SECOND,
            MINUTE,
            HOUR,
            DATE,
            MONTH,
            DAY,    //WEEK,
            YEAR,
            WRITE_PROTECT,
            CHARGE_MANAGE,

            TIME_CALENDAR_END
    };
    EXT unsigned char RTCReadedBuf[TIME_CALENDAR_END];


    //for rtc set step define
    typedef enum
    {
            YEAR_SET,
            MONTH_SET,
            DATE_SET,
            DAY_SET,    //WEEK_SET,
            HOUR_SET,
            MINUTE_SET,
            SECOND_SET,
            SET_STEP_MAX
    }Set_RTC_Step;
    EXT unsigned char SetRTCStep;

    typedef enum
    {
        RUNNING,
        SETTING,
        OTHERS
    }RTC_State;
    EXT unsigned char RTCState;

    #define RTC_DS1302_BURST_OPERATION_DEBUG                        //Conditional Debug
   
   



    EXT void vWriteCommand(unsigned char ucAddr, unsigned char ucData);
    EXT unsigned char ucReadCommand(unsigned char ucAddr);
    EXT void vInitRTC(void);
    EXT void vWriteByte(unsigned char ucData);
    EXT unsigned char ucReadByte(void);

    #ifdef RTC_DS1302_BURST_OPERATION_DEBUG
    EXT void vClkBurstWrite(unsigned char *ucData);
    EXT void vClkBurstRead(unsigned char *ucData);
    EXT void vRamBurstWrite(unsigned char *ucRAMData);
    EXT void vRamBurstRead(unsigned char *ucRAMData);
    #endif


#endif




在rtc.c中

#if  DS1302_RTC_USING_DEBUG


//----------------------------------------------- Function ---------------------------------------------
//                Description:
//
//              Creation Date:
//                  Author(s):  Sean Zang
//
//        Related Document(s):
//
//                  Arguments:
//               Return Value:
//
//   Required excitation rate:
//
//------------------------------------------------------------------------------------------------------
//------------------------- Copyright 2006. All rights reserved - CONFIDENTIAL -------------------------
//------------------------------------------------------------------------------------------------------
void vInitRTC(void)
{
    unsigned char temp;
   
#ifdef RTC_PORT_DDR_USING_DEBUG
    CE_Out;
    CLK_Out;
    DAT_Out;
#endif

    RTC_CE_Clr;
    RTC_CLK_Clr;
    RTC_DATA_Clr;

    //vWriteCommand(WR_SEC, 0x00); // clock halt flag=false, second=0
    vWriteCommand(WR_CHG, 0xA5); // 1 diode, 2Kohm
   
    temp = ucReadCommand(RD_HOUR);
    temp |= (1<<7);                 //12hour format
    vWriteCommand(WR_HOUR, temp);
   
    Re_Adjust_RTC_Finished = 0;
}



//----------------------------------------------- Function ---------------------------------------------
//                Description:
//
//              Creation Date:
//                  Author(s):  Sean Zang
//
//        Related Document(s):
//
//                  Arguments:
//               Return Value:
//
//   Required excitation rate:
//
//------------------------------------------------------------------------------------------------------
//------------------------- Copyright 2006. All rights reserved - CONFIDENTIAL -------------------------
//------------------------------------------------------------------------------------------------------
void vWriteCommand(unsigned char ucAddr, unsigned char ucData)
{
    RTC_CE_Set;
    vWriteByte(ucAddr);
    vWriteByte(ucData);
    RTC_CE_Clr;
}

//----------------------------------------------- Function ---------------------------------------------
//                Description:
//
//              Creation Date:
//                  Author(s):  Sean Zang
//
//        Related Document(s):
//
//                  Arguments:
//               Return Value:
//
//   Required excitation rate:
//
//------------------------------------------------------------------------------------------------------
//------------------------- Copyright 2006. All rights reserved - CONFIDENTIAL -------------------------
//------------------------------------------------------------------------------------------------------
unsigned char ucReadCommand(unsigned char ucAddr)
{
    unsigned char temp;

    RTC_CE_Set;
    vWriteByte(ucAddr);
    temp = ucReadByte();
    RTC_CE_Clr;
    return(temp);
}

//----------------------------------------------- Function ---------------------------------------------
//                Description:
//
//              Creation Date:
//                  Author(s):  Sean Zang
//
//        Related Document(s):
//
//                  Arguments:
//               Return Value:
//
//   Required excitation rate:
//
//------------------------------------------------------------------------------------------------------
//------------------------- Copyright 2006. All rights reserved - CONFIDENTIAL -------------------------
//------------------------------------------------------------------------------------------------------
void vWriteByte(unsigned char ucData)
{
    unsigned char i;

    for (i=0; i<8; i++)
    {
        if (ucData & 0x01)
        {
            RTC_DATA_Set;
        }
        else
        {
            RTC_DATA_Clr;
        }
        ucData >>= 1;
        RTC_CLK_Set;
        vDelay(5);
        RTC_CLK_Clr;
        vDelay(5);
    }
}

//----------------------------------------------- Function ---------------------------------------------
//                Description:
//
//              Creation Date:
//                  Author(s):  Sean Zang
//
//        Related Document(s):
//
//                  Arguments:
//               Return Value:
//
//   Required excitation rate:
//
//------------------------------------------------------------------------------------------------------
//------------------------- Copyright 2006. All rights reserved - CONFIDENTIAL -------------------------
//------------------------------------------------------------------------------------------------------
unsigned char ucReadByte(void)
{
    unsigned char i, temp;

#ifdef RTC_PORT_DDR_USING_DEBUG
    DAT_In;
#endif

    for(i=0,temp=0; i<8; i++)
    {
        temp >>= 1;         //It must be place here, very Importmenting
        if(DATA_Pin)
        {
            temp |= (1<<7);
        }
        else
        {
            temp &= ~(1<<7);
        }
        //
        RTC_CLK_Set;
        vDelay(5);
        RTC_CLK_Clr;
        vDelay(5);
    }

#ifdef RTC_PORT_DDR_USING_DEBUG
    DAT_Out;
#endif

    return(temp);
}


#ifdef RTC_DS1302_BURST_OPERATION_DEBUG
//----------------------------------------------- Function ---------------------------------------------
//                Description:
//
//              Creation Date:
//                  Author(s):  Sean Zang
//
//        Related Document(s):
//
//                  Arguments:
//               Return Value:
//
//   Required excitation rate:
//
//------------------------------------------------------------------------------------------------------
//------------------------- Copyright 2006. All rights reserved - CONFIDENTIAL -------------------------
//------------------------------------------------------------------------------------------------------
void vClkBurstWrite(unsigned char *ucData)
{
    unsigned char i;

    RTC_CE_Set;
    vWriteByte(WR_CLK_BURST);
    for(i=0; i<8; i++)
    {
        vWriteByte(*ucData);
        ucData++;
    }
    RTC_CE_Clr;
}


//----------------------------------------------- Function ---------------------------------------------
//                Description:
//
//              Creation Date:
//                  Author(s):  Sean Zang
//
//        Related Document(s):
//
//                  Arguments:
//               Return Value:
//
//   Required excitation rate:
//
//------------------------------------------------------------------------------------------------------
//------------------------- Copyright 2006. All rights reserved - CONFIDENTIAL -------------------------
//------------------------------------------------------------------------------------------------------
void vClkBurstRead(unsigned char *ucData)
{
    unsigned char i;

    RTC_CE_Set;
    vWriteByte(RD_CLK_BURST);
    for (i=0; i<8; i++)
    {
        *ucData = ucReadByte();
        ucData++;
    }
    RTC_CE_Clr;
}

//----------------------------------------------- Function ---------------------------------------------
//                Description:
//
//              Creation Date:
//                  Author(s):  Sean Zang
//
//        Related Document(s):
//
//                  Arguments:
//               Return Value:
//
//   Required excitation rate:
//
//------------------------------------------------------------------------------------------------------
//------------------------- Copyright 2006. All rights reserved - CONFIDENTIAL -------------------------
//------------------------------------------------------------------------------------------------------
void vRamBurstWrite(unsigned char *ucRAMData)
{
    unsigned char i;

    RTC_CE_Set;
    vWriteByte(WR_RAM_BURST);
    for (i=0; i<31; i++)
    {
        vWriteByte(*ucRAMData);
        ucRAMData++;
    }
    RTC_CE_Clr;
}

//----------------------------------------------- Function ---------------------------------------------
//                Description:
//
//              Creation Date:
//                  Author(s):  Sean Zang
//
//        Related Document(s):
//
//                  Arguments:
//               Return Value:
//
//   Required excitation rate:
//
//------------------------------------------------------------------------------------------------------
//------------------------- Copyright 2006. All rights reserved - CONFIDENTIAL -------------------------
//------------------------------------------------------------------------------------------------------
void vRamBurstRead(unsigned char *ucRAMData)
{
    unsigned char i;

    RTC_CE_Set;
    vWriteByte(RD_RAM_BURST);
    for (i=0; i<31; i++)
    {
        *ucRAMData = ucReadByte();
        ucRAMData++;
    }
    RTC_CE_Clr;
}


#endif

//----------------------------------------------- Function ---------------------------------------------
//                Description:
//
//              Creation Date:
//                  Author(s):  Sean Zang
//
//        Related Document(s):
//
//                  Arguments:
//               Return Value:
//
//   Required excitation rate:
//
//------------------------------------------------------------------------------------------------------
//------------------------- Copyright 2008. All rights reserved - CONFIDENTIAL -------------------------
//------------------------------------------------------------------------------------------------------
#if RTC_SETTING_ENABLE       

#define PM  0   //1

void vForcibledSetRTC(void)
{
    RTCReadedBuf[YEAR] = 0x08;
    RTCReadedBuf[DAY] = 0x06;       //week
    RTCReadedBuf[MONTH] = 0x11;
    RTCReadedBuf[DATE] = 0x08;
#if PM   
    RTCReadedBuf[HOUR] = 0x11 | (1<<7) | (1<<5);    //12format PM
#else
    RTCReadedBuf[HOUR] = 0x01 | (1<<7) | (0<<5);    //12format AM
#endif
    RTCReadedBuf[MINUTE] = 0x50;
    RTCReadedBuf[SECOND] = 0x00;
    RTCReadedBuf[WRITE_PROTECT] = 0x00;
    RTCReadedBuf[CHARGE_MANAGE] = 0xa5;

    vClkBurstWrite(RTCReadedBuf);
}

#endif

#endif

#endif
//2008-7-25 9:00 added

出0入0汤圆

发表于 2011-9-1 22:38:20 | 显示全部楼层
unsigned char ucReadByte(void)
{
    unsigned char i, temp;

#ifdef RTC_PORT_DDR_USING_DEBUG
    DAT_In;
#endif

    for(i=0,temp=0; i<8; i++)
    {
        temp >>= 1;         //It must be placed here, it is very Important
        if(DATA_Pin)
        {
            temp |= (1<<7);
        }
        else
        {
            temp &= ~(1<<7);
        }
        //
        RTC_CLK_Set;
        vDelay(5);
        RTC_CLK_Clr;
        vDelay(5);
    }

#ifdef RTC_PORT_DDR_USING_DEBUG
    DAT_Out;
#endif

    return(temp);
}

出0入0汤圆

发表于 2011-9-1 22:49:28 | 显示全部楼层
you sound like those people who cannot be helped.

but I will venture one last try:

"void write1302byte(uchar u)        //写入1位字节
{
    uchar i;
    ACC = u;
    for(i=8; i>0; i--)
    {
        IO = ACC0;      
        CLK = 0;
_nop_();  
        CLK = 1;
        ACC = ACC >> 1;
    }
} "

only a moron would write something like "ACC = u;".

the general approach is to produce a spi read / write routine. and then go from there.

for example, here is what I would do:

void write1302byte(uchar u)        //写入1位字节
{
    uchar i= 0;
    for(i=8; i>0; i--)
    {
        CLK = 0;
        IO = u & 0x01;       //lsb first
_nop_();  
        CLK = 1;
        u = u >> 1;  //shift to the next bit
    }
}

you can try to finish the rest.

出0入0汤圆

发表于 2011-9-1 23:43:01 | 显示全部楼层
here is what I have:

========================================
//********DS1302????************/
void write1302byte(uchar u)        //??1???
{
    uchar i;
    //ACC = u;
    for(i=8; i>0; i--)
    {
        CLK = 0;
        IO = u & 0x01;                                //ACC0;      
                _nop_();  
        CLK = 1;
        u = u >> 1;                                        //ACC = ACC >> 1;
    }
        IO = 1;                                                        //float the io line
}
/*****************************************************************/
uchar read1302byte(void)                 //??1???
{
    uchar i, u=0;
        IO = 1;                                                        //float to high
    for(i=8; i>0; i--)
    {
                u = u >> 1;                                        //ACC7 = IO;
               CLK = 0;
                _nop_();  
                if (IO) u |= 0x80;
                else u |= 0;
            CLK = 1;
                //ACC = ACC >>1;
    }
    return u;
}
/*******************write to ucAddr**********************************************/
void W1302(uchar ucAddr, uchar ucDa)
{
    RST = 0;                                                //deselect the chip
        //_nop_();  
    //CLK = 0;
        //_nop_();  
    RST = 1;                                                //select the chip
    write1302byte(ucAddr);                            // ????????????????????????
    write1302byte(ucDa);                              //?1Byte??
    //CLK = 1;
        //_nop_();  
    RST = 0;
}
/****************read from ucAddr*************************************************/
unsigned char R1302(uchar ucAddr)
{
    unsigned char u;
        RST = 0;                                                //deselect the chip
        //_nop_();  
    //CLK = 0;
        //_nop_();  
    RST = 1;                                                //select the chip
    write1302byte(ucAddr);                            // ????????????????????????
    u=read1302byte();                              //?1Byte??
    //CLK = 1;
        //_nop_();  
    RST = 0;
        return u;
}
=========================
you can confirm if it works by the following:

  W1302(0x80, 0x55); //set second to 55
  W1302(0x8c, 0x12); //set year to 12
  while (1) {
    P3=R1302(0x81);  //read second and output it on P3
  }

出0入0汤圆

发表于 2011-9-2 10:52:57 | 显示全部楼层
这种情况以前也碰到过 就是时序问题

出0入0汤圆

发表于 2011-9-4 12:16:16 | 显示全部楼层
学习氛围很好。顶

出0入0汤圆

发表于 2011-9-5 08:55:03 | 显示全部楼层
回复【29楼】packer
你的程序居然能对,表示不能理解。
看看你的读程序:
uchar  output_ds1302_1byte()  
{  
   uchar i;  
   for(i=8;i&gt;0;i--)  
   {  
     acc=acc&gt;&gt;1;  
     acc7=ds_sda;  
   ds_clk=1;  
   ds_clk=0;  
   }  
   return acc;  
}  
正确的:
uchar ds1302_r(uchar addr)   
{  
//读取数据   
     for(i=8;i;i--)   
     {   
        sclk = 0;   
            dat &gt;&gt;= 1;   
            if(io)dat |= 0x80;   
        sclk = ......
-----------------------------------------------------------------------

碰巧了.....1302可能是在CLK上升沿后就把数据放出来了. 用低速的CMU去读,正好也能读到正确数据....

出0入0汤圆

发表于 2011-9-5 09:05:42 | 显示全部楼层
没有时间帮你阅读上面的程序了
以前我也做过这个东西,一开始出现的问题貌似和你的差不多,时间很久了,记不清了,反正是程序反复的修改,就是解决不了,
后来解决方法是在时钟、数据线上加一个上拉电阻就好了……

出0入0汤圆

发表于 2011-9-5 17:04:28 | 显示全部楼层
mark

出0入0汤圆

发表于 2012-2-18 22:20:22 | 显示全部楼层
讨论帖视好贴

出0入0汤圆

发表于 2012-2-18 22:21:18 | 显示全部楼层
讨论帖视好贴

出0入0汤圆

发表于 2012-2-18 22:21:37 | 显示全部楼层
好贴
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-7 18:27

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表