KJH 0 Posted January 1, 2014 Share Posted January 1, 2014 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); } MSP-EXP430G2 w/ crystal populated to pins XIN/XOUT MSP430G2553 Energia 11 Windows 8.1 x64 Quote Link to post Share on other sites
L.R.A 78 Posted January 1, 2014 Share Posted January 1, 2014 I had the same problem. Just call the PWM fuction when you change duty. Look here, it's for a stellaris but the principle is the same: http://forum.stellarisiti.com/topic/1798-pwm-changes-depending-on-the-code-its-running/ KJH 1 Quote Link to post Share on other sites
energia 484 Posted January 1, 2014 Share Posted January 1, 2014 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); } } KJH 1 Quote Link to post Share on other sites
KJH 0 Posted January 1, 2014 Author Share Posted January 1, 2014 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 } Quote Link to post Share on other sites
ThatAintWorking 0 Posted November 6, 2015 Share Posted November 6, 2015 *deleted* 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.