PFM on MBED

07 Jan 2012

Can the mbed implement a Pulse Frequency Modulation (PFM) sort of like the PWM? If so, is there an example out there?

Thanks, Scott

07 Jan 2012

Depends on the frequency and on the rate of frequency change that you try to achieve. The frequency of the PWM can be changed easily, keep the duty factor at 50% and there you go. You may also be able to use the SPI or the I2S clock outputs. You just need to keep them going by using interrupts or DMA. The actual data output is ignored. For low frequencies you could also use the Ticker and toggle a DigitalOut pin. Better performance will probably require low-level timer manipulation.

An analog outputsignal could be generated through the DAC and a DMA feed that sends data from a sinewave table or something like that. PFM is achieved when you vary the DMA transferrate.

08 Jan 2012

"depends on the frequency and on the rate of frequency change that you try to achieve. The frequency of the PWM can be changed easily, keep the duty factor at 50% and there you go. "

Are you suggesting that all i need to do is keep the duty cycle constant to generate a PFM?

08 Jan 2012

Generating a squarewave output that varies between 2 or more frequencies can be done with the PWM outputs. Just define a Pin as PwmOut, set the duty cycle to 50%, set the frequency to its desired value. Code below will toggle the freq every 2 seconds between 1000Hz and 500Hz.

#include "mbed.h"

PwmOut PwmPin (p21);

DigitalOut myled1(LED1);
DigitalOut myled2(LED2);

int main() {

    PwmPin.period(0.001);  // Set frequency to 1000 Hz (period = 1 ms)
    PwmPin.write(0.5);     // Set the duty cycle to 50%
    myled1 = 1;            // Show what is going on 
    myled2 = 0;                      

    while(1) {
      PwmPin.period (0.001);  // Set frequency to 1000 Hz (period = 1 ms)
                              // Dutycycle remains 50%                                                  

      myled1 = 1;             // Show what is going on 
      myled2 = 0;                      

      wait(2); // wait 2 sec


      PwmPin.period (0.002);  // Set frequency to 500 Hz (period = 2 ms)
                              // Dutycycle remains 50%                                                                       
      myled1 = 0;             // Show what is going on 
      myled2 = 1;                      
      wait(2); // wait 2 sec                     
   }
}

Checkout http://mbed.org/handbook/PwmOut