RTC how to read seconds?

Thread Starter

KCHARROIS

Joined Jun 29, 2012
311
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
 

Attachments

tshuck

Joined Oct 18, 2012
3,534
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.
 

ErnieM

Joined Apr 24, 2011
8,377
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;
 

Thread Starter

KCHARROIS

Joined Jun 29, 2012
311
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
 

tshuck

Joined Oct 18, 2012
3,534
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.


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

Attachments

Top