Jump to content
43oh

[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

Link to post
Share on other sites
  • 1 month later...

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);
} 
Link to post
Share on other sites
  • 1 year later...
  • 1 month later...

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.
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.

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

Link to post
Share on other sites
  • 3 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...