STM32F0 Discovery + DS1307 RTC

One of the many things I bought a while back was a small PCB containing a DS1307 RTC ( along with an EEPROM IC (not interfaced in this post…). The DS1307 as far as I know is the universal standard for RTCs offering BCD storage of time and date, along with some user accessible space. Now obviously, the STM32F0 has built in RTC section but on smaller chips like the STM32F050P6 that I used for the Watch 2, there is no option for an external 32.768kHz crystal and you can either choose between a divided version of the external crystal (8MHz) or the poor tolerance internal oscillator. In this instance, using an external chip like this can reduce power requirements and increase time keeping accuracy. The 1 second output could also be used to clock the wakeup input of the MCU allowing the MCU to wakeup and update the time on an LCD, further reducing power consumption.

The power consumption is really small on these chips with the datasheet claiming 500nA when running in battery backup mode. The PCB that I’ve used can also be obtained really cheap from the good ol’ eBay (£0.99! – Link will expire!).

Reading the clock in debug mode under CooCox.

To test the RAM reading and writing functions, a piece of test data was used, as seen above, it was good ol’ 123! As this is stored in RAM, this will be there until power is removed, probably for a long time as I never remove the battery! The time set function is commented out as I’d already set the time. With my code, I use the macros __TIME__ and __DATE__ which are preprocessor commands to fill an array with a time and date string at compilation time. This saves me having to change the time and date every time I run the program and generally only has a few seconds of error which I can tolerate as I never really use that as the final time. With regards to __DATE__, the actual month is presented as a text string so I’ve hardcoded this number in. If the month changes, this will need to be changed by hand!

Measuring the supply current consumption through a 100Ohm resistor.

886uA isn’t half bad! I’m only using the STM32F0 Internal pullups for the I2C bus which isn’t as ideal as using dedicated external 4.7k resistors. Fortuantely, this doesn’t actually cause any data errors and the bus works fine. Realistically, one should add 4.7k resistors pulling the bus up to V+!

The program wasn’t actually too hard to write as I generally copy my compass I2C functions as I don’t like writing the I2C stuff from scratch but nonetheless, you can still find it on my Github!

2 thoughts on “STM32F0 Discovery + DS1307 RTC

  1. Could it be possible to show and explain the time set function as well as the macros _TIME_ and _DATE_? We don’t know how to set the time on the DS1307. And on your code we don’t understand this part:
    //R.Date = ((C_DATE[4]-‘0’)*10) + (C_DATE[5]-‘0’);
    if(C_DATE[4] == ‘ ‘) R.Date = (C_DATE[5]-‘0’);
    else R.Date = ((C_DATE[4]-‘0’)*10) + (C_DATE[5]-‘0’);
    R.Month = 2;
    R.Year = ((C_DATE[9]-‘0’)*10) + (C_DATE[10]-‘0’);

    R.Hour = ((C_TIME[0]-‘0’)*10) + (C_TIME[1]-‘0’);
    R.Minute = ((C_TIME[3]-‘0’)*10) + (C_TIME[4]-‘0’);
    R.Second = ((C_TIME[6]-‘0’)*10) + (C_TIME[7]-‘0’);

    Thanks in advance.

    1. Hi Hartmut,

      Good question! The __TIME__ and __DATE__ macros are used to give the time at date at compilation. I used these to ensure I didn’t have to re-write the time and date every time I recompiled and let the compiler do it for me. Here is a good explanation of using the macros:

      In my case, I merely manipulate the strings without a dedicated function. for the date, if the 4th letter of the __DATE__ string is a space, we know the date is merely a single digit e.g. ” 1″ or ” 9″. If however the 4th letter is not a space, we have a two digit date number e.g. “12” or “28”. This then parses the string by converting these numbers into their actual integer numbers and stored them in the R struct. We need to subtract ‘0’ because the number “0” itself doesn’t have an ascii value of zero. Therefore ‘0’-‘0’ = 0, ‘1’-‘0’ = 1, ‘2’-‘0’ = 2 etc.

      Hope this helps!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s