SPI with DS1306(RTC)

08 Mar 2010

hello,everyone.

         I use spi  write and read data to DS1306(RTC). I want to use DS1306 control mbed on or off on right time.

but I can't get good result. I hope someone can help me.the question , I can't read the data which I set(write) before. why.    here is my code result.

                                                                              h:8 m:28 s:20
                                                                          a_h:0 a_m:0 a_s:43

all data is not my set.  s:20 always change.beause rtc run. here is my code.hope someone help me.

DigitalOut myled(LED1);
SPI spi(p5, p6, p7); // mosi, miso, sclk
DigitalOut cs(p20);

Serial pc (USBTX,USBRX);

void reset()
{
  cs=0;
  cs=1; 
}
void wait_2u()
{
   cs=0;
   wait_us(10);
}
void write_time()
{
        reset();
    spi.write(0x82);
    spi.write(0x01);//write hour
    wait_2u();
        reset();
    spi.write(0x81);
    spi.write(0x02);//write min
    wait_2u();
        reset();
    spi.write(0x80);
    spi.write(0x03);//write sec
    wait_2u();
}

void write_aram()
{
        reset();
    spi.write(0x89);
    spi.write(0x01); //write arm hour
    wait_2u();
        reset();
    spi.write(0x88); 
    spi.write(0x02);//write aram min
    wait_2u();
        reset();
    spi.write(0x87);
    spi.write(0x15);//write aram sec
    wait_2u();
}
void read_time()
{
    reset();
    spi.write(0x02); // read hour
    pc.printf("h:%X ",spi.write(0x00));
    wait_2u();
        reset();
    spi.write(0x01);// read min
    pc.printf("m:%X ",spi.write(0x00));
    wait_2u();
        reset();
    spi.write(0x00);// read sec
    pc.printf("s:%X \n",spi.write(0x00));
    wait_2u();
}

void read_aram()
{
    reset();
    spi.write(0x09);
    pc.printf("a_h:%X ",spi.write(0x00));
    wait_2u();
        reset();
    spi.write(0x08);
    pc.printf("a_m:%X ",spi.write(0x00));
    wait_2u();
        reset();
    spi.write(0x07);
    pc.printf("a_s:%X ",spi.write(0x00));
    wait_2u();
}


int main()
{
    spi.format(8,0);
    
    reset();
    spi.write(0x8F); //set wp 0
    spi.write(0x03);
    wait_us(10);
    
    reset();
    while(1)
    {
        write_time();
        write_aram();
        wait(2);
        read_time();
        read_aram();
    }
}

best wishes.

09 Mar 2010

can anybody help me ?  i am waiting for you.

10 Mar 2010 . Edited: 10 Mar 2010

Hi Wen,

The first thing i'd suggest is stripping back to basics, and not trying to wrap up the functionality too soon. As you  have not established reliable communication with the DS1306, it is not clear if the higher level tasks are failing because of basic protocol, or because of a program sequence error.

Can you also provide a schematic? Looking at the datasheet at :

http://datasheets.maxim-ic.com/en/ds/DS1306.pdf

It seems there is a pin called SERMODE that needs to configure the interface. It seem silly to ask but it is often the simple things that trip us up.

Also I notice in you code you set the mode to "0", i.e. CPOL = 0, CPHA = 0.

Page 11, Figure 5 says that CPOL for this part is "1", therefore your mode should be "2".

I dont have a DS1306 to hand, but you might consider using a program like this with some simple printf style debug, and basic operations.

http://mbed.org/users/chris/programs/DS1306/5zlv4/

Here I am simply clearing the WP bit for the RAM, then trying to write and read back from the RAM location. I've not read the data sheet in much detail, but it seems that if the hardware is powered up and SERMODE configured correctly, this should work, or at least get you a bit closer to the sort of thing that might help.

Good luck, let us know how you get on!

Cheers,
Chris

 

10 Mar 2010

hi Chris Styles :

        Thanks for your help. I have changed  the mode to 2 (spi.format(8,2);). and changed the mbed pin 20 to pin 8,I think pin20 and pin8 all same. and run my code .I got the same result like before. and then I run your code "/chris/programs/DS1306/5zlv4/", it does not work on my hardware. the result is :"Read from location 0x20 and received 0x7F".  I think it should be "...received 0xa5"is right.
        by the way,hardware is powered up and SERMODE configured correctly.SERMODE connect VCC,because using spi.  basic protocol i copy DS1305's code. the datasheet between ds1305 and ds1306 very likely.   to be honest,i am beginer. thanks for your help.

best regards.

10 Mar 2010

connect pictures:

       

 

I want to konw ,is that wrong?  

sorry my poor english.

if you have enough time ,tell me more. I really want to study.

thank you

11 Mar 2010

hi Chris Styles :

      thank you very much. I have got answer.  I success.   it get work.

the sermode should be 3.   thank you.