FredrikNyman 3 Posted August 3, 2014 Share Posted August 3, 2014 More fun with LEDs! I took the Energia LED fader example from energia.nu and modified it to output to P1.4, P1.5, P1.6 and P1.7 so that all four LEDs would fade in sync. #include "Energia.h" void setup(); void loop(); /* http://energia.nu/Tutorial_Fade.html Fade This example shows how to fade LEDs on pins (P1.4 thru P1.7) using the analogWrite() function. This example code is in the public domain. */ int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by void setup() { // declare pins P1_4 through P1_7 to be outputs: pinMode(P1_4, OUTPUT); pinMode(P1_5, OUTPUT); pinMode(P1_6, OUTPUT); pinMode(P1_7, OUTPUT); } void loop() { // set the brightness of the LEDs: analogWrite(P1_4, brightness); analogWrite(P1_5, brightness); analogWrite(P1_6, brightness); analogWrite(P1_7, brightness); // change the brightness for next time through the loop: brightness = brightness + fadeAmount; // reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect delay(30); } Problem: it doesn't work. Or rather, P1.6 fades in and out as it should, but P1.4, P1.5 and P1.7 just turn on and off. If I change the original Energia code example to fade P1.5 instead of P1.6, the same thing happens: the LED just turns on and off. I've looked all over for a statement saying that Energia's AnalogWrite only works on P1.6, but have found nothing. What gives -- is my code broken? Thanks! Quote Link to post Share on other sites
spirilis 1,265 Posted August 3, 2014 Share Posted August 3, 2014 It's the chip. Energia only supports fine grained PWM with analogWrite for pins that have a timer output option, and I'm assuming the chip you're using doesn't have that on the P1.4/1.5/1.7 pins. So analogWrite does its best to "round" the value to 0 or 1 (off/on). Sent from my Galaxy Note II with Tapatalk 4 FredrikNyman 1 Quote Link to post Share on other sites
spirilis 1,265 Posted August 3, 2014 Share Posted August 3, 2014 FYI- inspect the diagrams on this page, find your chip and look for which pins are listed in purple- http://energia.nu/pin-maps/guide_msp430g2launchpad/ Sent from my Galaxy Note II with Tapatalk 4 FredrikNyman 1 Quote Link to post Share on other sites
FredrikNyman 3 Posted August 3, 2014 Author Share Posted August 3, 2014 Aha! Thank you so much! That turned out to be exactly right. I moved things around a bit, and using pins P2_1, P2_2, P2_4 and P2_6, everything works beautifully. Is the underlying issue -- that only some pins support analogWrite -- a limitation in the chip or is it an Energia thing? Quote Link to post Share on other sites
oPossum 1,083 Posted August 3, 2014 Share Posted August 3, 2014 It is a limitation of the G2553. You can see what each pin is capable of in the Port Schematics section of the G2553 spec sheet (SLAS735). Quote Link to post Share on other sites
FredrikNyman 3 Posted August 4, 2014 Author Share Posted August 4, 2014 Thanks again! I'm looking at the latest release of slas735, revision J. Can you confirm that page 16 in the PDF is the correct place to look, under "Timer_A3 (TA0, TA1)"? If I'm reading tables 12 and 13 right, the limitation that you can only use analogWrite on certain pins comes from how the CCR1 module block and TA1 output signal are connected. Would it also be correct to say that Energia could implement analogWrite on additional output pins by also using CCR0 and TA0, but even if they did, there still are some GPIO pins that won't allow analogWrite because they don't have any Timer_A3 signal connection. Quote Link to post Share on other sites
L.R.A 78 Posted August 4, 2014 Share Posted August 4, 2014 I belive energia uses Timer0 for milis() so it can't be used for PWM/AnalogWrite() Quote Link to post Share on other sites
oPossum 1,083 Posted August 4, 2014 Share Posted August 4, 2014 The timer info is listed a few placed in various ways. I like to look at the tables in the Port Schematics section starting on page 43. That shows all possible uses for all the pins. It is possible to do PWM on CCR0 with some software ISR assistance. This method is not supported by Energia - it only uses strictly hardware PWM. http://forum.43oh.com/topic/5619-pwm-using-timer-output-toggle-mode/ 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.