Jump to content
cambazz

Can not analogWrite on RED_LED, GREEN_LED, BLUE_LED

Recommended Posts

Hello,

 

I am trying to analogWrite on the RGB leds, with energia. Appearently this can be done, as the original came with a demo program of this sort.

 

Also the documentation of energia, the hardware documentation states that this RGB led pins are indeed analogWritable (purple) 

 

But when i try to use analogWrite - it will either take a 0 or 1 position, and no PWM will be outputted. It works as expected with digital output.

 

Any ideas?

 

Best Regards,

-C.B.

Share this post


Link to post
Share on other sites

analog rgb works ok:

void setup()
{
  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(BLUE_LED, OUTPUT);
 
  analogWrite(RED_LED,140);
  analogWrite(GREEN_LED,20);
  analogWrite(BLUE_LED,40);
 
  delay(2000);
}

void loop()
{
  uint8_t rr, rg, rb;
 
  while(1)
  {
    rr = (uint8_t)random(0, 192);
    rg = (uint8_t)random(0, 192);
    rb = (uint8_t)random(0, 192);
 
    analogWrite(RED_LED,rr);
    analogWrite(GREEN_LED,rg);
    analogWrite(BLUE_LED,rb);
    
    delay(100);
  }
}

Share this post


Link to post
Share on other sites

Hi,

I use Energia with a Stellaris LaunchPad and I can write pwm signals to some pins but not to others.  I have no trouble writing to the red, green or blue leds, or to PB_5.  I cannot seem to get a pwm signal out of PB_0 or PB_1. I thought that I might have damaged my LaunchPad so I tried with another board and got the same result.  Guessing that I could have two damaged boards, I got a new one out of the box and it behaved in the same way.  All the pins in question should be able to output pwm signals (according to the energia reference page).  The code I am using to test the output is just a modified blink sketch (copied below).  It is not a big problem to rewire my hardware, but there appears to be a problem somewhere.

Am I missing something simple?

Thanks,

Jack

 

////// modified blink to test pwm output on refPin ////////

  int refPin = RED_LED;
 
void setup() {                
  pinMode(refPin, OUTPUT);     
}
 
void loop() {
  analogWrite(refPin, 100);   
  delay(1000);               
  analogWrite(refPin, 0);    
  delay(1000);              
}

Share this post


Link to post
Share on other sites

 

Hi,

I use Energia with a Stellaris LaunchPad and I can write pwm signals to some pins but not to others.  I have no trouble writing to the red, green or blue leds, or to PB_5.  I cannot seem to get a pwm signal out of PB_0 or PB_1. I thought that I might have damaged my LaunchPad so I tried with another board and got the same result.  Guessing that I could have two damaged boards, I got a new one out of the box and it behaved in the same way.  All the pins in question should be able to output pwm signals (according to the energia reference page).  The code I am using to test the output is just a modified blink sketch (copied below).  It is not a big problem to rewire my hardware, but there appears to be a problem somewhere.

Am I missing something simple?

Thanks,

Jack

 

////// modified blink to test pwm output on refPin ////////

  int refPin = RED_LED;
 
void setup() {                
  pinMode(refPin, OUTPUT);     
}
 
void loop() {
  analogWrite(refPin, 100);   
  delay(1000);               
  analogWrite(refPin, 0);    
  delay(1000);              
}

 

I see to be able to analogWrite on PB0 and PB1, did you open Serial1 by any chance or are you using any external libraries as either may potentially cause analogWrite to fail on those pins

Share this post


Link to post
Share on other sites

Thanks for the response.  While the original code invoked serial communications, the modified blink program did not.  I would consider the possibility that the history of the board could affect its response, except that I was unable to get an analog output from the brand new board. 

Still it is good to know that serial communications affect what you can and cannot do with various pins.  This is still all pretty new to me and that is not something that I would have guessed.

Thanks again,

Jack 

Share this post


Link to post
Share on other sites

I assume you checked the Energia pin map to be sure that analog write is supported on those pins?

Using the Stellaris Pin Map utility I notice that T2CCP0 (timer pin used for PWM goes to PB0 and PF4),

did you do whatever setup is needed to make sure that it is outputting the PWM on PB0, rather than on PF4?

 

Edit: Actually I have not looked at the internals of Energia, so I don't know if the PWM uses the timer pins or not.

(I was thinking about the Arduino code for Atmel SAM processors.)

Still it would be worth checking to be sure whether it does use the timer (and therefore needs some incantation to tell it which pin to use).

Share this post


Link to post
Share on other sites

Below is a modified version of the Fading Sketch that works as expected on a Stellaris LaunchPad (EK-LM4F120XL REV A).

@@JStat60 can you test the Sketch bellow and post what LaunchPad you are running on (original StellarisEK-TM4C123GXL or the new TivaC EK-TM4C123GXL) and of what revision board is?

uint8_t fadeAmount = 5;

void setup()  { 
  // nothing happens in setup 
}

void fade(uint8_t ledPin)
{
  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0; fadeValue <= 255; fadeValue += fadeAmount) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255; fadeValue >= 0; fadeValue -= fadeAmount) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
}

void loop()  { 
  fade(RED_LED);
  fade(GREEN_LED);
  fade(BLUE_LED);
}

Share this post


Link to post
Share on other sites

Thanks for the replies.

This appears to be a problem with the newest version of Energia (0101E0011). 

I recently loaded the newer version and had not gone through and tested it with older code that I knew worked with the previous version (0101E0010).  Yesterday I tried loading a program using the new version and the program failed to run.  After a prolonged bout of mild cursing, I discovered that the problem was with a PWMWrite command on pin PB_6.  Thus, it appears that port B has problems with the new software that did not occur previously (ie, not a hardware issue).  I re-installed 0101E0011 and again could not get PWM outputs on PB_0 or PB_6 (again, it did output to PB_5 and RED_LED). 

Re-loading Energia 0101E0010 fixed the problem.  The old program works as it had before.

Jack

Share this post


Link to post
Share on other sites

@@energia, I have been using the Stellaris LauchPads (EK-LM4F120XL Rev B) and a PC running Windows 7.  I do have some of the Tiva boards but I did not try this out with them.

I have had this problem with multiple programs.  In addition to the modified blink sketch reproduced in my 13 Feb post, I was unable to run a rather involved program that can be found at https://github.com/SmokyMountainScientific/WheeStat5_0/tree/master/WheeStat5_0.  A third test program (presented below) Incorporates a pwm output into the setup of AnalogReadSerialOut.   When the pwm output is on one of the "non-functioning" pins, no output is written to the serial port (when using the most recent Energia revision). The code compiles and loads without any error message coming up, but nothing comes up on the serial monitor.  If the analogWrite command is executed on a "working" pin, like RED_LED, the LED lights up and the expected string of numbers comes spooling out of the monitor.  I first saw this problem with a sketch written to run a pH meter (which I could post f someone wants to read, but seems like overkill). 

Please let me know if anyone else sees this kind of issue with the new Energia revision.

Jack

 

//////////// modified ARSO sketch ///////////////// 

 
void setup() {
   Serial.begin(9600); 
  analogWrite(PB_0, 50);
}
 
void loop() {
  int sensorValue = analogRead(A3);
  Serial.println(sensorValue);
  delay(1); 
}

Share this post


Link to post
Share on other sites

Turns out that there is a bug in the analogWrite(). The base values for SYS_PERIPHERAL_TIMERXXX changed and with the current logic behind enabling the peripheral, the wrong timer peripheral was enabled. There are 2 ways to work around this. 

 

1: enable the peripheral in your setup with ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_<TIMER>); Replace <TIMER> with the timer the pin is on.

In this case it's PB_0 which is on TIMER2. You can figure out which timer the pin is on by looking at the digital_pin_to_timer[] array in pins_energia.h (git version here: https://github.com/energia/Energia/blob/master/hardware/lm4f/variants/stellarpad/pins_energia.h#L116)

2: apply the following patch to wiring_analog.c

diff --git a/hardware/lm4f/cores/lm4f/wiring_analog.c b/hardware/lm4f/cores/lm4f/wiring_analog.c
index 1964452..3b2c5d9 100644
--- a/hardware/lm4f/cores/lm4f/wiring_analog.c
+++ b/hardware/lm4f/cores/lm4f/wiring_analog.c
@@ -76,7 +76,7 @@ void enableTimerPeriph(uint32_t offset) {
         ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_WTIMER0 + offset - 4);
     }
     else {
-        ROM_SysCtlPeripheralEnable((SYSCTL_PERIPH_TIMER0 - 1) + (1 << offset));
+        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0 + offset);
     }
 
 }

Share this post


Link to post
Share on other sites

Hi, I appreciate the help.

It has taken me a while to get back to this and I am still having issues.  I have tried workaround #1 above and got the following error message:  "SYSCTL_PERIPH_TIMER2 not declared in this scope".  It will accept ROM_SysCtlPeripheralEnable(TIMER2) without generating the error, but that does not get the program working.  I am trying to control a digital pot using SPI module0 and I am watching the SPI clock with an osciloscope. When I comment out the analogWrite(PB_0, 100); command I can see the SPI signals on my scope but the program hangs up when I try to run the analogWrite and gives no output on the clock.

I dont believe that there is a hardware incompatibility issue since I have used this combination successfully before (in the previous Energia version). 

I am pretty new to digital electronics and I really wouldn't know where to start with workaround #2.

Thanks again for the help.

Jack

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×