Jump to content
KJH

[SOLVED] PWM Frequency in Energia

Recommended Posts

Hello!  

 

I am new to embedded programming and the forums.  For my first task I am trying to generate a clean square wave with a configurable frequency and I figured PWM would be a good way to do this.  Below is my code.  This works to an extent, but during the off time there are regular spikes to 3.6V at about 10kHz as seen in my scope plot.  The scope is a cheap Rigol DS1102.

 

How can I eliminate these spikes?  and is my approach the best approach or is there a better one that may not have the extra spikes?

 

thanks in advance for the help!

void setup() {
// declare pin 14 to be an output:
pinMode(GREEN_LED, OUTPUT);
}

void loop() {
// set the frequency:
analogFrequency(800);

//PWM (Pin, Duty Cycle) - Maximum = 100% = 255
analogWrite(GREEN_LED, 127);

}

800_Hz.png

 

 

MSP-EXP430G2 w/ crystal populated to pins XIN/XOUT

MSP430G2553

Energia 11

Windows 8.1 x64

Share this post


Link to post
Share on other sites

Move the call to analogFrequency(800); into setup. Only call analogWrite when the duty cycle is changed.

 

Below is a copy of the Fading Sketch as an example: 

 

int ledPin = 9;    // LED connected to digital pin 9

void setup()  { 
  // nothing happens in setup
  analogFrequency(800);
} 

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

Share this post


Link to post
Share on other sites

Thanks for the help!  That was the issue.

 

Now that I think about things the spikes makes sense the way I wrote the code.  Every loop that analogWrite would be called which was causing the spikes.  Thanks again!

 

Working code is below for completeness.

void setup() {
// set the frequency:
analogFrequency(800);

//PWM (Pin, Duty Cycle) - Maximum = 100% = 255
analogWrite(GREEN_LED, 127);
}

void loop() {
//Nothing in the loop

}

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

×