websterling 12 Posted June 1, 2012 Share Posted June 1, 2012 I'm trying to use Energia for a project and I'm having problems with PWM. My understanding is that the following code should continually toggle the green led between full on and pwm with a value of 14. This is on a 2553 on a Rev. 1.4 Launchpad with the crystal installed. void setup() { pinMode(GREEN_LED, OUTPUT); } void loop(){ digitalWrite(GREEN_LED, HIGH); delay(2000); analogWrite(GREEN_LED,14); delay(2000); } When the code runs the led is full on. After 2 seconds it goes dim with the pwm. After 2 more seconds I expected the led to go to full brightness as the code loops, but it remains dim. The problem also exists using analogWrite(GREEN_LED, 255) instead of digitalWrite(GREEN_LED, HIGH). Am I missing something here, or is there a bug in Energia? Quote Link to post Share on other sites
energia 485 Posted June 1, 2012 Share Posted June 1, 2012 Which release are you on? Release 0101E0006 has a bug. TA0CTL is setup incorrectly for T0A1 which is P1.6 (GREEN_LED). The fix has already been checked into master. Diff is here: https://github.com/energia/Energia/comm ... 9b3ed05479 analogWrite(GREEN_LED, 255) should work though. Will look into it. Robert bluehash 1 Quote Link to post Share on other sites
websterling 12 Posted June 1, 2012 Author Share Posted June 1, 2012 Hey, Robert, I'm on 0101E0006, and I saw the fix and applied it a few days ago. Are there any other fixes that might need to be applied in this situation? Should the code I posted work the way I described it? Thanks, George Quote Link to post Share on other sites
energia 485 Posted June 1, 2012 Share Posted June 1, 2012 No other fixes have been done in this area. Code should work but you might have hit a new bug. I will have a look at it with an o-scope this afternoon and see if I can reproduce it. Quote Link to post Share on other sites
energia 485 Posted June 1, 2012 Share Posted June 1, 2012 This is confirmed to be a bug. digitalWrite leaves the pin in PWM mode. A digitalWrite drives the line high and then PWM takes over again immediately after that. Thank you for the report. WIll file a bug and post the patch in this thread. A way around this is: void setup() { pinMode(GREEN_LED, OUTPUT); } void loop(){ analogWrite(GREEN_LED,254); // write with 255 does a digitalWrite(, HIGH) so will not work delay(2000); analogWrite(GREEN_LED,14); delay(2000); } Quote Link to post Share on other sites
websterling 12 Posted June 1, 2012 Author Share Posted June 1, 2012 Thanks for filing the bug report. I already tried the work around while I was trying to get a handle on the problem. I'll be glad when there's a patch. Thanks, George Quote Link to post Share on other sites
energia 485 Posted June 2, 2012 Share Posted June 2, 2012 Bug is fixed. Pull request with fix has been posted for review: https://github.com/energia/Energia/pull/64/files --- a/hardware/msp430/cores/msp430/wiring_digital.c +++ b/hardware/msp430/cores/msp430/wiring_digital.c @@ -79,9 +79,17 @@ void digitalWrite(uint8_t pin, uint8_t val) uint8_t bit = digitalPinToBitMask(pin); uint8_t port = digitalPinToPort(pin); volatile uint16_t *out; + volatile uint16_t *sel; if (port == NOT_A_PORT) return; + /* + * Clear bit in PxSEL register to select GPIO function. Other functions like analogWrite(...) + * will set this bit so need to clear it. + */ + sel = portSelRegister(port); /* get the port function select register address */ + *sel &= ~bit; /* clear bit in pin function select register */ + out = portOutputRegister(port); if (val == LOW) { bluehash and websterling 2 Quote Link to post Share on other sites
websterling 12 Posted June 2, 2012 Author Share Posted June 2, 2012 Thanks, Robert, It now works as expected. 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.