Jump to content
43oh

[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

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);                            
  } 
}

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

}
Link to post
Share on other sites
  • 1 year later...

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