Jump to content
xv4y

Real-Time Clock (RTC) Library for the MSP430/LaunchPad

Recommended Posts

Hi B@tto, 

I've tested your code and have worked out the issues.  As I suspected, it is indeed a typecasting issue. If you explicitly typecast MyClock.RTC_sec to integer as follows, it will work correctly.

  Serial.println((int)MyClock.RTC_sec);

 

Also, I couldn't get it to compile with your definition of TicTac(), it kept throwing an error at the MyClock++ saying it wasn't defined. Even when I tried incrementing an integer as I suggested in my previous post, it still through an error.

 

What I discovered though is that to get the format described by xv4y to compile, you need to include legacyMSP430.h as follows:

#include <legacyMSP430.h>
.
.
.
interrupt(TIMER1_A0_VECTOR) Tic_Tac(void) {
  MyClock++;            // Update chunks  
}

 

Hope this helps.

Share this post


Link to post
Share on other sites

Hi and thanks everyone for your help.

 

So, I did some test this morning and here are my conclusions : grahamf72 you're right, there was on cast and on interrupt, using yours suggestions it works fine as you. But I still surprised : why my interrupt syntax doesn't work here ? I mean I use this code to put my MSP into sleep mode in another sketch and it works :

 

 

#pragma vector=(WDT_VECTOR)
__interrupt void watchdog_timer(void)
{
  _BIC_SR_IRQ(LPM3_bits);
}

Why here I have to use legacymsp430.h ?

 

About cast problem, I think it's a problem of IDE. If you use that :

 

 

 Serial.println(RTC.RTC_sec,DEC);

It works too. The library use char type so when you use print command it print it as a char, note as a byte. I tried to change variable types to byte or uint8_t but the compilation failed.

 

Share this post


Link to post
Share on other sites

Hi Guys,

 

Sorry but yesterday I have been busy away from home and I was not able to work on this.

Thanks a lot Graham for debugging this. I will have a look at the "legacyMSP430" problem.

I remember I included it somewhere but don't remember why I needed it...

 

Yan.

Share this post


Link to post
Share on other sites

Attached is my modified version of Yannick's RTC library.

 

The main changes I have made are that it also does dates, and allows either 1 second or 1/256 second resolution.  To avoid code bloat for features you don't need, these features are controlled by editing the header file to comment out / uncomment some #defines. Hopefully a future release of Energia will allow specifying -D defines that get passed to the compiler, avoiding the need to edit the header file.

 

The features that I have are:

#define RTCSUBSECONDS

When RTCSUBSECONDS is defined, the library generates ticks every 1/256 of a second. This is the same timing that Yannick's library used. Useful if you need fractional second resolution.  If this is turned off, the library will tick once every second. 

 

#define RTCWITHDATE

Fairly self explanatory - this turns on date features in addition to the time.  Basic leap year detection is included, however it will get leap years wrong in century years, so in the unlikely event your project will still be around in 2100, this will not work correctly for you.  No effort is made to account for leap-seconds.

 

Also changed from Yannick's code, the Inc_sec, Inc_min, Inc_hour etc functions have been made public. I use this to allow user adjusting of the values (press a button to increase the hour etc).

 

For smallest code size, comment out the #define lines for RTCSUBSECONDS and RTCWITHDATE.  With RTCSUBSECONDS turned on, and RTCWITHDATE turned off, the code size is exactly the same as what I was getting with Yannick's library - reflecting the fact that the code is basically identical.  

 

Full documentation of how to use the library is included in the header file. Please let me know if you find any bugs / problems etc or need any guidance in using the library.

RTCplus.zip

Share this post


Link to post
Share on other sites

energia-0101E0009

was a problem with:

class RealTimeClock {    RealTimeClock& operator++();  }

changed to:

class RealTimeClock {   void PlusPlus(void);     }

works normally! Thanks!

Share this post


Link to post
Share on other sites
energia-0101E0009

was a problem with:

class RealTimeClock {
    RealTimeClock& operator++();    // Overload ++ operator for writing convenience (Prefix Variant)
} 

That's curious as it compiles without issue for me. What was the actual error you were getting?

Share this post


Link to post
Share on other sites

Maybe you're trying to compile C code, operator overloading is a C++ feature, which would cause a C compiler to fail. Make sure all files in your project are called .cpp, not .c

Also note that the overloaded operator is the prefix increment, not the postfix. This means that you could write ++MyRTC; but not MyRTC++;. This is some subtle difference, as a postfix increment requires the underlying code to make a copy of the entire object: the object is copied, then the original is incremented, then the copy (with the old data in it) is returned by-reference to the code. After the current line is executed the copy is discarded. The prefix code will just increment the current object and return a reference to itself, no copying or discarding needed.

Share this post


Link to post
Share on other sites

Maybe you're trying to compile C code, operator overloading is a C++ feature, which would cause a C compiler to fail. Make sure all files in your project are called .cpp, not .c

Classes are a c++ feature, so if it was trying to compile c code it would fail long before here.

Also note that the overloaded operator is the prefix increment, not the postfix. This means that you could write ++MyRTC; but not MyRTC++;. This is some subtle difference, as a postfix increment requires the underlying code to make a copy of the entire object: the object is copied, then the original is incremented, then the copy (with the old data in it) is returned by-reference to the code. After the current line is executed the copy is discarded. The prefix code will just increment the current object and return a reference to itself, no copying or discarding needed.

The example above is the prefix operator, but the class also includes the postfix operator. The code for the postfix operator depends on the prefix operator, so I can't see how removing the prefix operator could possibly compile unless other major changes have been made to the library.

Share this post


Link to post
Share on other sites
    interrupt(TIMER1_A0_VECTOR) Tic_Tac(void) {        myClock.++;      // Update chunks   };

 error: expected unqualified-id before '++' token

    interrupt(TIMER1_A0_VECTOR) Tic_Tac(void) {        myClock++;      // Update chunks    };

work! I'm sorry, was inattentive.  ;-)

 

finished size of my code with LCD1602 4bit optimized library (blank sketch will be less):

- myClock++;                    2`587 byte

- myClock.PlusPlus();       2`305 byte

Share this post


Link to post
Share on other sites

 

    interrupt(TIMER1_A0_VECTOR) Tic_Tac(void) {
        myClock.++;            // Update chunks
    };

 error: expected unqualified-id before '++' token

    interrupt(TIMER1_A0_VECTOR) Tic_Tac(void) {
        myClock++;            // Update chunks
    };

work! I'm sorry, was inattentive.  ;-)

 

finished size of my code with LCD1602 4bit optimized library (blank sketch will be less):

- myClock++;                    2`587 byte

- myClock.PlusPlus();       2`305 byte

 

 

 

I discovered I actually had the stray dot in my documentation - oops!! sorry.

 

I haven't found as much difference in compilation size as you are getting. I am only finding 36 bytes difference between directly calling Clock.Inc_sec() and using Clock++ but given that every byte is precious, I have updated my library to create an inline function Inc() which calls Inc_sec() or Inc_chunk() depending on the tick size. I have left the ++ operators in so you can still use them if you wish - the compiler is smart enough that the ++ operators don't create any overhead if you don't use them.  

 

Attached is the latest version of my RTCplus library, with the Inc function and corrected documentation. I have also removed the documentation from the .cpp file and put it all in the .h file, it makes it easier for me to not have to keep two sets of the same stuff synchronised.

 

I am curious about this optimised LCD1602 4 bit library you mention. I have been using the LiquidCrystal library that is included with Energia. Where did you find this 4 bit optimised library?

RTCplus.zip

Share this post


Link to post
Share on other sites

grahamf72, nice work!

about code size:
#include <RTCplus.h>
RealTimeClock myClock;
void setup() {  myClock.begin(); }
void loop() {}
interrupt(TIMER1_A0_VECTOR) Tic_Tac(void) {
 //myClock++;      //Binary sketch size: 867 bytes (with RTCSUBSECONDS), 585 bytes (w/o RTCSUBSECONDS)
 //++myClock;      //Binary sketch size: 593 bytes (with RTCSUBSECONDS), 563 bytes (w/o RTCSUBSECONDS)
 //myClock.Inc();  //Binary sketch size: 579 bytes (with RTCSUBSECONDS), 549 bytes (w/o RTCSUBSECONDS)
};

 Where did you find this 4 bit optimised library?

 

I optimised the LiquidCrystal library that is included with Energia for 4bit mode (minimal optimization -214 bytes). 

 

P.S. grahamf72, You can post LIb into Energia Libraries

Share this post


Link to post
Share on other sites

grahamf72, i have tried your RTC library last night (till morning) in my light alarm clock project. the previous RTC library was lagging up to 5 minutes for me but yours work awesome. thanks to everyone who has worked on this library.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×