RTC how to read seconds?

Discussion in 'Embedded Systems and Microcontrollers' started by KCHARROIS, Jul 9, 2013.

  1. KCHARROIS

    Thread Starter Member

    Jun 29, 2012
    292
    1
    Hello,

    So I'm preparing myself on communicating with an RTC with SPI but I'm not quite sure how to read the seconds from the RTC. I look at the datasheet and I don't quite get it, it shows the first 4 bits being seconds and the last 3 bits 10 seconds, any ideas?

    Thanks
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    The seconds are stored in BCD format, 3 bits for the tens place (0-5), and 4 bits for the ones place (0-9)...see the bottom of page 6.
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    BCD: Binary Coded Decimal. The secconds start at 0x00 and count like so:

    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
    and so on until it gets to 0x59, then back to 0x00.

    I prefer to hold things like this in a structure but math works well also:

    sec = ReadRTC();
    time = (sec & 0xF0)* 10 + sec & 0x0F;
     
  4. KCHARROIS

    Thread Starter Member

    Jun 29, 2012
    292
    1
    Perfect thanks alot. One more question, the RTC is 3 wire interface DS1302 and the pic I'm using is PIC18F4520, I've never done 3 wire interface before so do I connect the I/O line of the RTC to both the IN and OUT line of the PIC with a resistor on the input? Or do I have the wrong PIC to do the job?

    Thanks
     
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Any microcontroller can do this. Looking at page 2, all that is required is two outputs(from the PIC) and a bi-directional pin: SCLK, CE, and the data line.
    [​IMG]

    Read the rest of page 2/the datasheet for more information...
     
Loading...