chipKIT® Development Platform

Inspired by Arduino™

RTCC - majenko

Created Mon, 21 Jul 2014 13:03:53 +0000 by dferasmus


Mon, 21 Jul 2014 13:03:53 +0000

Dear majenko

I wandered across your website regarding your RTCC implementation for the UNO32, and would like to use it in my projects. I managed to get a version from your website, RTCC-0.1.2. I then found your post and upgraded to vesion, RTCC-r2.

With both of these libraries I am having trouble modifying your example code to perform any but 1/2 second and 1 second interrupts. I would like to, specifically, use AL_MINUTE for 1 minute intervals. I am using the LED (13) as a test.

Please test this on your system to confirm my problem. Here is my modification;

#include <RTCC.h>

char cycle;

void setup()
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);


void loop()

void Pulse()
    digitalWrite(13, LOW);
    digitalWrite(13, HIGH);

Thanks dferasmus


Mon, 21 Jul 2014 15:30:10 +0000

The current RTCC code is on Github: [url][/url] - it has many improvements and bug fixes since r2.


Thu, 24 Jul 2014 17:36:22 +0000


Thanks, I managed to find it and will keep updated.

I have however found that the version I downloaded does not have the following included;

#include <stdint.h>

After adding that line, your demo worked nicely, even for 1 minute and 10 minute settings.

I then tried to implement a sleep operation as a stepping stone to battery operation and ran into more problems.

Firstly a return from sleep seems to reset OSCCON.SLPEN to 0. This means I had to reenable sleep mode before every sleep instruction, otherwise the core timer would wake the processor every 1ms. I have not delved into this problem in detail, but hoped some one would know something. Am I messing with the core timer and core functions? I checked the MPIDE source and could find nothing that suggested so. The core timer would only halt and then resume.

Secondly, serial communication right before a sleep instruction would miss sending the last character, regardless of "Serial.flush();" Does "Serial.flush();" do anything? Is Serial interrupt driven? Is U1TXIF queried?

Here is my modified version of the demo;

#include <stdint.h>
#include <RTCC.h>

//                A B C D  E F G H I J K L M N  O  P Q R S T U V W X Y Z
char months[26]= {4,0,0,12,0,2,0,0,0,1,0,0,3,11,10,0,0,0,9,0,0,0,0,0,0,0};

void setup ( )
    Serial.begin ( 57600 ) ;
    delay (4000);
    Serial.println("Compiled " __DATE__ " " __TIME__ "\n");

    // I n i t i a l i z e the RTCC module
    RTCC. begin ( ) ;
    // Set the time to something s e n s i b l e
    RTCCValue rv = RTCC.value();
    char aaStr[9] = __TIME__;
    unsigned char hours = (10*(aaStr[0]-'0'))+aaStr[1]-'0';
    unsigned char minutes = (10*(aaStr[3]-'0'))+aaStr[4]-'0';
    unsigned char seconds = (10*(aaStr[6]-'0'))+aaStr[7]-'0';
    rv.time(hours, minutes, seconds);
    char bStr[12]= __DATE__;
    unsigned char year = (10*(bStr[9]-'0'))+bStr[10]-'0';
    unsigned char month = months[bStr[0]-'A'];
    if (month == 4 && bStr[1] == 'u') {
        month = 8;
    if (month == 3 && bStr[2] != 'r') {
        month = 5;
    if (month == 1 && bStr[1] == 'u') {
        if (bStr[2] == 'n') month = 6;
        else month = 7;
    if (bStr[4] == ' ') bStr[4] = '0';
    unsigned char day = (10*(bStr[4]-'0'))+bStr[5]-'0';, month, day);

    rv.setValidity(RTCC_VAL_GCC); // date/time approximated using compilation time: works for developers only!!!

    rv.seconds(0); // Align alarms to the minute
    // Set the alarm to t r i g g e r every second
    RTCC.alarmMask (AL_MINUTE) ;
    RTCC.chimeEnable ( ) ;
    RTCC.alarmEnable ( ) ;
    // Attach our routine to send the time through the serial port
    RTCC.attachInterrupt (&outputTime ) ;

void loop ( )
    // Remember last chime: most useful action of an interrupt routine like this one...
    RTCCValue lastChime = RTCC.value();

    char time [ 50 ] ;
    // Format the time and print it .
    sprintf ( time ,"%02d/%02d/%02d %02d:%02d:%02d\n" ,
   ( ) ,
            lastChime.month ( ) ,
            lastChime.year ( ) ,
            lastChime.hours ( ) ,
            lastChime.minutes ( ) ,
            lastChime.seconds ( )
        ) ;
    Serial.println ( time ) ;

Serial.println ("<") ;
Serial.println (OSCCON, HEX) ;
Serial.println (">") ;

asm volatile ("wait");

Serial.println ("{") ;
Serial.println (OSCCON, HEX) ;
Serial.println ("}") ;

RTCCValue lastChime = RTCCValue();

void outputTime ( )


Thu, 24 Jul 2014 17:48:50 +0000


I noted that the MPIDE seems to have code for an RTCC. Is there any built in functionality, and documents?


Lines 209 to 218;

RtccInit();	// init the RTCC
// while(RtccGetClkStat() != RTCC_CLK_ON); // wait for the SOSC to be actually running and RTCC to have its clock source
// could wait here at most 32ms

// time is MSb: hour, min, sec, rsvd. date is MSb: year, mon, mday, wday.
RtccOpen(0x10073000, 0x11010901, 0);
RtccSetTimeDate(0x10073000, 0x10101701);
// please note that the rsvd field has to be 0 in the time field!


Thu, 24 Jul 2014 18:12:30 +0000

Serial.flush() waits until all data is sent out of the transmit queue, so should be called before sleeping.

I have no idea what the internal references to Rtcc are.

I was having awful problems with sleep mode and the watchdog timer - the first sleep would work, the second would break. Finally worked round it with some better startup code in the bootloader. The code the FRM was recommending was just plain wrong. There does seem to be some issue with sleep mode being lost, yes.

While you are sleeping nothing is running (except the RTCC and watchdog IIRC). That means that as far as the millis() function etc is concerned no time has passed. Even the main oscillator is stopped.


Mon, 28 Jul 2014 15:56:20 +0000


Thanks, I dredged through the code and did find the section that waits for transmit to complete.

:D I wasnt going to go near the WDT, as I see that as strictly a sanity timer.

Glad you agree that I wont 'break' anything with the way I implemented the sleep.

Thanks for the help, and the easy RTCC library. dferasmus