• AVR Freaks

Hot![solved] Problem starting a DS1302 RTC chip off a 32MX custom setup

Author
Spottymaldoon
Super Member
  • Total Posts : 223
  • Reward points : 0
  • Joined: 2010/02/05 09:25:22
  • Location: 0
  • Status: offline
2020/05/27 06:36:19 (permalink)
0

[solved] Problem starting a DS1302 RTC chip off a 32MX custom setup

I'm having a struggle getting a DS1302 to run, interfaced via bit-banging to my 32MX.  The data sheet says that the write protect bit needs to be cleared before attempting to write to the device (which is already confusing) - the power-on state is not defined. Also, for the same reason, the clock halt bit needs to be cleared.
 
If somebody here is familiar with this chip I'd like to ask if the clock should start to run after the WP and the CH bits have been cleared and, as a test, that the seconds register should then be incrementing? Right now I am getting nothing and I don't want to post a bunch of code if I am simply forgetting something obvious in start-up.
 
All my pulses look to be where they ought to be on the scope and I made sure SCK is low when setting the CS bit - the pulse widths are delayed to around 2uS and I remembered to set the TRIS bit for reads.
 
post edited by Spottymaldoon - 2020/06/08 07:51:58
#1

1 Reply Related Threads

    Spottymaldoon
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2010/02/05 09:25:22
    • Location: 0
    • Status: offline
    Re: Problem starting a DS1302 RTC chip off a 32MX custom setup 2020/05/28 08:32:29 (permalink)
    0
     
    What's important is to "start" this chip by clearing the write protect register before doing anything else - then clear the seconds register. Once you've done that you'll see the oscillator start up on your scope (very small amplitude).
     
    I've managed to get the read working for the seconds register - scope trace (attached) shows the 8 bit read command (0x81) and 8 bits of response which, while rather ugly, appears to be counting and correct. After a struggle, I find the code below works perfectly. I see that quite a few people have had difficulty with this chip so it seems sensible to post the problem solved here. Note especially that clock bit 8 of 16 triggers both the write of the last command bit AND the read of the first data bit. Again, that valuable advice so freely given and so universally resented: "read the (effing) data sheet!"
     
    Alas, I am far from deserving the title "Super Member" but I have learned a lot here. If anybody cares to take a moment to re-write my code in a more elegant style, I'd be delighted and grateful!
     
     
     

     
     
     
    //****************************************************
    //***************CLOCK READ*******************
    //****************************************************
    unsigned char ClockRead(unsigned char command)
    {

    unsigned char i,temp;
    temp=command;
    _TRISC3=0;//just to be sure
    _LATC3=0;//zero output
    _LATB12=0;//note: sck must be low before CS taken high
    NopDelay(2);//~1uS
    _LATA2=1;//CS
    for(i=0;i<8;i++)
        {
    NopDelay(2);
    _LATB12=0;//tock
    NopDelay(2);
    _LATC3=temp&0x01;//write the command
    _LATB12=1;//tick

    temp>>=1;
        }
    temp=0;
    _TRISC3=1;//now to read
    NopDelay(2);
    for(i=0;i<8;i++)
        {
    _LATB12=0;//tock
    NopDelay(2);
    if(_RC3)temp|=(0x80>>(7-i));//read the RTC byte
    _LATB12=1;//tick
        }
    _LATB12=0;//tock
    _LATA2=0;//CS
    _TRISC3=0;
    _LATC3=0;
    return temp;
    }

     
     
    post edited by Spottymaldoon - 2020/06/01 07:03:11

    Attached Image(s)

    #2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5