11 years, 9 months ago.

pulse frequency modulation

I'm trying to control a DC motor using PWM. i need to vary frequency keeping the duty cycle as 75%. can someone help out? with coding as well as logic. the frequency has to be varied using potentiometer

What have you tried yourself? Since this is fairly straightforward using AnalogIn and PwmOut.

posted by Erik - 07 Mar 2013

#include "mbed.h"
//PwmOut led(LED2); 
PwmOut ledd(LED1);
PwmOut Poutt(p22);
//PwmOut Pout(p23); 
AnalogIn ain(p20);
int main() {
    //Pout.period_us(1000); //100khz
    Poutt.period_us(1000); //100khz
    // Pout.period_ms(100000); 
    //led.period_ms(1000);
    ledd.period_ms(1000)  ;
   ; 
        while(1) 
        { 
            // led = (2/3)*ain; 
             Poutt=ain ;
             ledd= ain  ;   
             //Pout = (2/3)*ain; 
        }
    }

this is what i've done and working too.but i fear this is pwm rather than pfm... hence i need to confirm with someone who knows better abt this

posted by Kishhanth Renganathan 07 Mar 2013

Please use code tags: <<code>> and <</code>>

What you now do is changing the duty cycle of Poutt, but you want to change the frequency. So simply call:

Poutt.period(1/(999*ain+1));

Depending on ain it will change the period 1 second (1Hz) and 1ms (1kHz). Period keeps the duty cycle constant, so you only need to set it once.

posted by Erik - 07 Mar 2013

I'll implement using this and get back to you. thanks for the help.

posted by Kishhanth Renganathan 07 Mar 2013

hey. that worked. i have one more problem.. if i increase the input voltage from 0-3.3 to vary the frequency suddenly there shouldnt be a sudden rise in pfm.. it should rise slowly(say 5 secs). can u help out in this cide. is it using timers?of a loop will do?

posted by Kishhanth Renganathan 15 Mar 2013

Both options are possible, depending on your other requirements. What you could try is defining another variable 'slowinput'. Then use something like:

float step = 0.001;
float input = ain; //This way we have a fixed value for the following calculations and don't have to call the ADC everytime
// Check if we should increase slowinput, decrease it, or when it is close to input just make it equal to input
if ((slowinput - step) > input)
  slowinput = slowinput - step;
else if ((slowinput + step) < input)
  slowinput = slowinput + step;
else 
  slowinput = input;
 Poutt.period(1/(999*slowinput+1));

Then you can either add for example a 5ms wait and call it in a loop, or use a ticker.

posted by Erik - 15 Mar 2013
  1. include "mbed.h" PwmOut led(LED2); PwmOut Pout(p22); AnalogIn ain(p20); int main() { Poutt.period_us(1000); 100khz Pout.pulsewidth(1); led.pulsewidth(1); while(1) { led.period(1/(999*ain+1)); Pout.period(1/(999*ain+1)); } }

is the code right for pfm? its compiling bt not functioning.. sorry i m beginner to this mbed..

posted by Kishhanth Renganathan 05 Apr 2013
Be the first to answer this question.