Moving from "real world" duty cycle to correct microcontroller value

Discussion in 'Embedded Systems and Microcontrollers' started by darkfeffy, Jul 17, 2012.

  1. darkfeffy

    Thread Starter Member

    Apr 1, 2009
    12
    2
    Hi,

    So, let us suppose I have sampled two signals Vin and Vout using the ADC modules of my 16 bit dsPIC, and using the integer format (meaning sampled values range from 0 to 1023). I want to compute the duty cycle (which will update the duty cycle register of the PWM module) according to the equation:

    duty = Vin/(Vin + Vout); //duty therefore has a value between 0 and 1.

    According to the data sheet, the value to be fed to the duty cycle register must be calculated according to the equation:

    duty_cycle_register_value = 7.48*duty*PWM_period/(7.37*1.04nanoseconds)

    Where PWM_period is 6microseconds.

    Given that all calculations must be done in fixed point, what is the best way(s) to go about it while minimizing instruction cycle usage?

    Thanks for your help.
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    Your formula is:

    duty_cycle_register_value = 7.48*duty*PWM_period/(7.37*1.04nanoseconds)

    where duty = Vin/(Vin + Vout)

    and PWM_period is 6microseconds

    Hence when you plug the variables into the equation the only variables are Vin and Vout.
    Work out the constant values and you will get

    duty = 5855 * Vin/(Vin + Vout)

    Do the 5855 * Vin first in 16x16-bit integer multiply to give 32-bit result followed by the 32/16 integer divide.
     
    darkfeffy likes this.
  3. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    Where is this equation coming from?

    Are Vin and Vout essentially DC signals?

    What does each one of them represent?

    What does the sum of them, Vin+Vout, represent?

    If Vin is the peak voltage of the PWM waveform and Vout is the desired average value, then the duty cycle would be simply Vout/Vin. So it really makes a difference just want these various things represent.
     
Loading...