bluehash 1,581 Posted November 14, 2012 Share Posted November 14, 2012 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 manhdan and sirri 2 Quote Link to post Share on other sites
sirri 28 Posted January 5, 2013 Share Posted January 5, 2013 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 ? Quote Link to post Share on other sites
energia 485 Posted January 7, 2013 Share Posted January 7, 2013 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); } sirri 1 Quote Link to post Share on other sites
sirri 28 Posted January 7, 2013 Share Posted January 7, 2013 oh! now i understand much better. i will try it this evening. but i got the idea now, thanks. Quote Link to post Share on other sites
vicks777 0 Posted February 12, 2014 Share Posted February 12, 2014 Your timer is awesome. Is there anything similar for Stellaris Launchpad? If not, can you make one? It will be really useful. Quote Link to post Share on other sites
abecedarian 330 Posted March 26, 2014 Share Posted March 26, 2014 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. Quote Link to post Share on other sites
chicken 630 Posted March 26, 2014 Share Posted March 26, 2014 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) abecedarian 1 Quote Link to post Share on other sites
abecedarian 330 Posted March 26, 2014 Share Posted March 26, 2014 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. Quote Link to post Share on other sites
abecedarian 330 Posted March 26, 2014 Share Posted March 26, 2014 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. Quote Link to post Share on other sites
dotma 0 Posted April 1, 2014 Share Posted April 1, 2014 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 Quote Link to post Share on other sites
rtpburnsville 1 Posted July 18, 2014 Share Posted July 18, 2014 Thanks for sharing the timer routine. I have been needing this feature for some time and your library works great on my 2553 board. Robert Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.