xv4y

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

96 posts in this topic

That's a major drawback of these libraries. As almost all depend on your coop being in a certain state, it's hard to let them play nice together.

In my binary watch I use wdt for time keeping and the timer for display multiplexing, button debouncing, menu handling and buzzer. I did not use any libraries. I doubt that I would be able to do all this while maintaining low power when using libraries.

But mortals like me need libraries ; ) : ) i don't think i can handle it. I am not so good at programming at that level.

Share this post


Link to post
Share on other sites

Hi Sirri,

I have good news and bad news I'm afraid.

The good news is that I am about 99% done with modifying the RTC library to use the WDT. I just want to do a bit more testing before I release it. Also in the good news category is that when the RTC library is using the WDT I can use analogwrite or tone without issue.

 

The bad news is that analogwrite and tone don't play well together. I have knocked up a basic sketch that uses analogwrite to vary the brightness of an RGB led based on the time, and to sound a 2 second tone every 30 seconds.  If I don't use analogwrite, the tone plays properly every time. If I do use analogwrite, the 1st tone plays correctly but only for about half the time it should, and 2nd and subsequent tones are broken & jerky.

 

Since your project makes use of analogwrite and tone, I think the easiest option is to use a 555 to do your tone and just turn it on/off. Other options would be to use something like a TLC5940 to drive your leds or maybe even a complete re-write of analogwrite & tone.

 

EDIT: After a bit more playing and a bit more studying of the Energia code, it looks like Tone & AnalogWrite will play nice together so long as the pins you choose for AnalogWrite are connected to Timer1 - ie P2.1, P2.2, P2.4, P2.5,

sirri likes this

Share this post


Link to post
Share on other sites

Hi Sirri,

I have good news and bad news I'm afraid.

The good news is that I am about 99% done with modifying the RTC library to use the WDT. I just want to do a bit more testing before I release it. Also in the good news category is that when the RTC library is using the WDT I can use analogwrite or tone without issue.

 

The bad news is that analogwrite and tone don't play well together. I have knocked up a basic sketch that uses analogwrite to vary the brightness of an RGB led based on the time, and to sound a 2 second tone every 30 seconds.  If I don't use analogwrite, the tone plays properly every time. If I do use analogwrite, the 1st tone plays correctly but only for about half the time it should, and 2nd and subsequent tones are broken & jerky.

 

Since your project makes use of analogwrite and tone, I think the easiest option is to use a 555 to do your tone and just turn it on/off. Other options would be to use something like a TLC5940 to drive your leds or maybe even a complete re-write of analogwrite & tone.

 

EDIT: After a bit more playing and a bit more studying of the Energia code, it looks like Tone & AnalogWrite will play nice together so long as the pins you choose for AnalogWrite are connected to Timer1 - ie P2.1, P2.2, P2.4, P2.5,

Hi Grahamf,

Thank you for working on this. I have already made a 555 alarm stage. However for further projects, this library you are working on is very important. Do you mean using P2.1, P2.2, P2.4 or P2.5 for analogwrite will solve this problem? In the current library or the one you are working on? Thanks.

Share this post


Link to post
Share on other sites

Hi Grahamf,

Thank you for working on this. I have already made a 555 alarm stage. However for further projects, this library you are working on is very important. Do you mean using P2.1, P2.2, P2.4 or P2.5 for analogwrite will solve this problem? In the current library or the one you are working on? Thanks.

Basically the issue is, the RTC, AnalogWrite & Tone all want use of a Timer. The MSP430G2553 has 2 timers, the MSP430G2452 only has 1 timer. So we have 3 libraries attempting to use 1 or 2 timers, and problems will occur if we attempt to use 1 timer for 2 functions.

 

The existing RTC library uses Timer1 if it is available (eg 2553) or Timer0 if not (eg 2452).

Tone() uses Timer0

AnalogWrite() uses Timer0 for the pins on port 1, and Timer1 for the pins on port 2 (2553 only, you can't use AnalogWrite to port 2 with the 2452).

 

This means you simply cannot use all 3 functions. On the 2553 you can use any 2 with care, on the 2452 you can only use 1.  So with the 2553 you can use RTC + Tone or RTC + AnalogWrite to port 1 or Tone + AnalogWrite to port 2. 

 

The soon-to-be-released WDT version of the RTC library doesn't use one of the general purpose Timers so you can take the RTC out of the equation, and use any combination of Tone & AnalogWrite that your chip allows, ie:

2553 - Tone + AnalogWrite on Port 2 or AnalogWrite on Port 1 & 2

2452 - Tone or AnalogWrite

 

Hope this helps make things a bit clearer.

sirri likes this

Share this post


Link to post
Share on other sites

Thank you Grahamf, 

Especially your last message made me understand the concept better..

Just wondered, does it also cause problems to millis() function somehow?

Share this post


Link to post
Share on other sites

Does this mean that the existing clocking mechanism of Energia will be killed off when using these features together?

These features could be used together of they "agree" upon a frequency.

For example, if your analog write routine uses a pwm period of 10ms, you could hook up an rtc update in here which is called every 100th interrupt.

In a more generic approach one could have an "update" method on each library, which you can call from such a timer function. The only thing is you need to instruct each library how often or will be called. I minor drawback is that the user would need to set up the interrupt handling, instead of the library.

Share this post


Link to post
Share on other sites

Does this mean that the existing clocking mechanism of Energia will be killed off when using these features together?

These features could be used together of they "agree" upon a frequency.

For example, if your analog write routine uses a pwm period of 10ms, you could hook up an rtc update in here which is called every 100th interrupt.

In a more generic approach one could have an "update" method on each library, which you can call from such a timer function. The only thing is you need to instruct each library how often or will be called. I minor drawback is that the user would need to set up the interrupt handling, instead of the library.

That sounds like a good idea in theory, but in practice it proves a bit tricky.  To do software PWM, you need to be able to clock at your oscillation speed x resolution. So to do the equivalent of the current PWM module which runs at approx 500Hz, with a 256 level resolution, you need the interrupt to call at 128kHz. Likewise with Tone, if you want to be able to produce tones that are close to the musical pitches up to the F at the top of the treble clef, you need resolution of at least 10uS to get pitches which will sound close. This equates to a 100kHz clock. So if we are clocking at 128kHz, with a 16MHz main clock that only gives 125 clock cycles for the interrupt to do it's work. Not impossible but it won't leave much in the way of CPU resources for the main program.  By dropping the pulse frequency or the resolution we can get away with a slower interrupt, but we will also lose accuracy of the tones. 

 

The way PWM is actually implemented is through the use of the hardware timer/counters. This is done in a separate section of the hardware and doesn't require CPU resources. The actual method is slightly more complex than this example, but for simplicity sake lets say the counter counts from 1 to 256.  If you set an analog level of 100, the pin will be high while the counter counts 1 to 100, it then changes to low while it counts 101 to 256. The clock then restarts at 1 with the pin back to high.  This can all be done in the hardware without the CPU using any clock cycles. 

 

Tone is done partly in software, part in hardware. The timer is configured to trip an interrupt at the frequency of the selected tone. Software then toggles the pin. This allows it to be used with any pin, whereas the method for AnalogWrite only works with pins that are connected to the hardware timers.

 

The trouble with using the hardware timers is that there aren't many timers available, and only selected pins can be used with each one.  A software option would be far more flexible, but the 430 just doesn't have the grunt to be able to do it effectively. 

 

Of course the other issue if you are trying to share the RTC with other functions, is that to get accurate timing for an RTC, the clock source must be the 32.768kHz crystal. However this is just too slow to be able to do PWM or Tone. So apart from the fact that trying to use the one timer for multiple functions causes problems, the crystal clock is too slow to be able to do PWM or tone.

 

BTW, my new version of the RTC that uses the Watchdog Timer instead of TimerA is code complete. I am just working on the documentation before I post it.

sirri likes this

Share this post


Link to post
Share on other sites

i have DS1307 and msp430g2553 pad. IS there any RTc code where user can set and get date from rtc like for arduino. Is any code supported by Energia IDE

 

Arduino code :

 

 


#include"Wire.h"

#define DS1307_ADDRESS 0x44
byte zero = 0x00; //workaround for issue #527

void setup(){
  Wire.begin();
  Serial.begin(9600);
  setDateTime(); //MUST CONFIGURE IN FUNCTION
}

void loop(){
  printDate();
  delay(1000);
}

void setDateTime(){

  byte second =      45; //0-59
  byte minute =      06; //0-59
  byte hour =        11; //0-23
  byte weekDay =    2; //1-7
  byte monthDay =    27; //1-31
  byte month =       2; //1-12
  byte year  =       13; //0-99

 Wire.beginTransmission(DS1307_ADDRESS);

  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(zero); //start

  Wire.endTransmission();

}

byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
 
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

   int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

 /*  int second = 11;
  int minute =18;
  int hour = 18;
  int weekDay = 1;
  int monthDay = 2;
  int month = 11;
  int year = 12;
*/
  //print the date EG   3/1/11 23:59:59
  Serial.print(monthDay);
  Serial.print("/");
 
 
  Serial.print(month);
  Serial.print("/");
 
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);

}
 

Share this post


Link to post
Share on other sites

G'Day AMPS

Just a wee note about the DS1307.

It requires 5V's and the launchpad is 3V so make sure you do not zap it.

Share this post


Link to post
Share on other sites

Updated library which includes support for the WDT is now posted in the Libraries forum.  Sorry about the delay, my real job has been getting in the way for a couple of weeks.

Share this post


Link to post
Share on other sites

I have given external power supply to power up the Ds1307 keeping ground refernce to power up both ds1307 and MSP. This program i tried with arduino it worked. But it doesnot worked for this KIt

 

 

G'Day AMPS

Just a wee note about the DS1307.

It requires 5V's and the launchpad is 3V so make sure you do not zap it.

Share this post


Link to post
Share on other sites

You don't need to make any changes to wiring.c.  Because the library includes replacements for all the functions in wiring.c (most slightly changed but some exactly the same) the linker won't use the wiring.c versions. This is only the case if you use the compiler define #RTCUSEWDT in RTCconfig.h. Without #RTCUSEWDT the replacement functions aren't compiled, so the linker will use the original wiring.c versions. Hope this helps explain it.

bluehash likes this

Share this post


Link to post
Share on other sites

I get the same error for each example I try:

 

Applications/Energia.app/Contents/Resources/Java/hardware/tools/msp430/bin/msp430-g++ -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=msp430g2553 -DF_CPU=16000000L -MMD -DARDUINO=101 -DENERGIA=9 -I/Applications/Energia.app/Contents/Resources/Java/hardware/msp430/cores/msp430 -I/Applications/Energia.app/Contents/Resources/Java/hardware/msp430/variants/launchpad /var/folders/jk/cls6kzr1655435v5r89syzq00000gn/T/build6688268127573497319.tmp/example_Serial_Clock.cpp -o /var/folders/jk/cls6kzr1655435v5r89syzq00000gn/T/build6688268127573497319.tmp/example_Serial_Clock.cpp.o 
example_Serial_Clock.cpp:14:21: fatal error: RTCplus.h: No such file or directory
compilation terminated.
 
I'm relatively new to Energia and the MSP430 and this is probably so simple, but I've not come up with a fix for over an hour or two.
I've added the files via the Sketch menu, I've moved the folder to the Energia directory and other places, I've started new Sketches with the file contents... always this file.
 
Any ideas?

Share this post


Link to post
Share on other sites

@@russcky - a couple of thoughts:

have you restarted Energia since you copied the library in?

Are the library files copied to: /..pathtoenergia../hardware/msp430/libraries/RTCplus

Also, make sure none of the files have ended up with the wrong case (eg not RTCplus.h not rtcplus.h)

 

Beyond that, I'm not sure. I've occasionally had Energia complain of similar errors, but it usually fixes it if I restart Energia, or add the library again. I don't know why adding the library is any different to manually entering the #include, but it seems to behave differently.

russcky likes this

Share this post


Link to post
Share on other sites

Thanx so much for the help @@xv4y and @@grahamf72!  I tried everything again... but ultimately what got it working was by doing 'Show Package Contents' on the Energia Application and copy the library in the appropriate spot.

 

Thanx again!  Now onto my next problem...  :smile:

 

Russ

Share this post


Link to post
Share on other sites

Hi,

 

That's a solution. A dirty one, but still a solution.

As Graham suggested, you might have a "case sensitive" problem in your "libraries" directory.

Floating around between OS X (which is case sensitive or not depending on how you ask him), other UNIXes and Windows, it's a developer's nightmare to properly handle every case.

 

Yan.

Share this post


Link to post
Share on other sites

Agreed... not ideal, I'm still investigating it... but it got me past that point where I could work on the next component I want to have working which is an SD Card for storage and then the ability to send audio files from the SD card to a speaker.

 

Loads of fun though, thanx again.

 

Russ

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