Jump to content
bluehash

[Energia Library] MSTimer2

Recommended Posts

From: Porting MSTimer2

 

I've ported a version of MsTimer2 to the Energia / MSP430 environment. Its called TwoMsTimer and it provides a way to get a method called on regular intervals, as fast as once very two msec. You just create a TwoMsTimer object passing in the (void) method that you want called and TwoMsTimer will take care of the rest. The timer relies on Timer 1 in a way that is compatible with PWM letting you use the Timer 1 PWM pins and get timed function invocation. It is named TwoMsTimer because the PWM CCR0 interrupt rate is about 500Hz giving us 2 msec resolution.

 

You can find the sample code and a demo showing how it works on http://joe.blog.free...ng-running.html

 

The code is really simple with no "if define" behavior for different processors. I've only tested it on the Launchpad with the 2553 processor.

 

 

Github

Share this post


Link to post
Share on other sites

Thank you for the library. I have checked the example code too but can you write us an easier example here please? How can we call the function ?

Share this post


Link to post
Share on other sites

The example below is taken from Joe's blog here:http://joe.blog.freemansoft.com/2012/09/msp430-fading-and-other-long-running.html.

void setup()  { 
  // open the hardware serial port
  Serial.begin(9600);
  pinMode(heartbeatPin, OUTPUT);
  TwoMsTimer::set(500, flash); // 500ms period
  TwoMsTimer::start();
} 
 
// interrupt handler passed to TwoMsTimer
void flash(){
  // log every time the handler is called should be every 500msec
  Serial.println("flash");
  heartbeatPinOn = !heartbeatPinOn;
  digitalWrite(heartbeatPin,heartbeatPinOn);
} 

Share this post


Link to post
Share on other sites

Source on the site won't work with latest Energia nor R1.5LP with 2553- no "loop" function in the code and serial in the "flash" handler clobbers Serial & I2C. Do comm's in "loop" and things work but things depending on this library might be delayed.

 

Besides that, with two functionally identical codes...

... why is it this code compiles to 989 bytes (G2553 target / Energia 0101E0012):

#if defined(__MSP430G2452__) || defined(__MSP430G2553__) || defined(__MSP430G2231__) // LaunchPad specific
#include "Energia.h"
#else
#error Board not supported
#endif

#include "TwoMsTimer.h"

void setup()  { 
  pinMode(RED_LED, OUTPUT);
  TwoMsTimer::set(1000, flash); // some ms period
  TwoMsTimer::start();
} 

void loop(){
  for (int d = 1000; d > 0; d -= 10) {
    TwoMsTimer::set(d, flash); // some ms period
    delay(d * 2);
  }
}

// interrupt handler passed to TwoMsTimer
void flash(){
   digitalWrite(RED_LED, !digitalRead(RED_LED));
}
... and this compiles to 917 bytes:

#if defined(__MSP430G2452__) || defined(__MSP430G2553__) || defined(__MSP430G2231__) // LaunchPad specific
#include "Energia.h"
#else
#error Board not supported
#endif

#include "TwoMsTimer.h"

int heartbeatPin = RED_LED;
boolean heartbeatPinOn = false;

void setup()  { 
  pinMode(heartbeatPin, OUTPUT);
  TwoMsTimer::set(1000, flash); // some ms period
  TwoMsTimer::start();
} 

void loop(){
  for (int d = 1000; d > 0; d -= 1) {
    TwoMsTimer::set(d, flash); // some ms period
    delay(2000);
  }
}

// interrupt handler passed to TwoMsTimer
void flash(){
  heartbeatPinOn = !heartbeatPinOn;
  digitalWrite(heartbeatPin, heartbeatPinOn);
}
Only things changed are the definition of "heartbeatPin" and "heartbeatPinOn" from directly referencing the constants in the first example to assigning variables that reference those same constants in the second example.

Share this post


Link to post
Share on other sites

You added a new function, digitalRead. Replace that with the boolean and you're back to 917 (or maybe 2 bytes less as using the RED_LED directly instead of an extra int)

@@chicken

You're probably right. I feel embarrassed.

Share this post


Link to post
Share on other sites

Quick check....

Code:

#if defined(__MSP430G2452__) || defined(__MSP430G2553__) || defined(__MSP430G2231__) // LaunchPad specific
#include "Energia.h"
#else
#error Board not supported
#endif

#include "TwoMsTimer.h"

boolean heartbeatPinOn = false;

void setup()  { 
  pinMode(RED_LED, OUTPUT);
  TwoMsTimer::set(1000, flash); // some ms period
  TwoMsTimer::start();
} 

void loop(){
  for (int d = 1000; d > 0; d -= 1) {
    TwoMsTimer::set(d, flash); // some ms period
    delay(2000);
  }
}

// interrupt handler passed to TwoMsTimer
void flash(){
  heartbeatPinOn = !heartbeatPinOn;
  digitalWrite(RED_LED, heartbeatPinOn);
}
911 bytes?

Makes senses since it's not allocating memory to store a variable for a constant.

Share this post


Link to post
Share on other sites

I have got this working on the MSPF5529LP simply by adding (__MSP430G2452__) to your launchpad specific line.

 

However if stops my WiFi application working.  Do you know if there is a workaround for this, or is it trying to share the same timer?

 

thanks

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

×