Jump to content
43oh

AnalogWrite only works on P1.6?


Recommended Posts

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!

Link to post
Share on other sites

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

Link to post
Share on other sites

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?

Link to post
Share on other sites

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.

Link to post
Share on other sites

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/

Link to post
Share on other sites

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