/********************************************/
/*PWM Motion Code                           */
/*dsPIC33CK512MP608                         */
/********************************************/
#include "xc.h"
#include "MainMotion.h"

void PWM_Init(void)
{
    /*PWM control register configuration*/
    PCLKCONbits.MCLKSEL = 0;
    //Host clock selected by the MCLKSEL[1:0] (PCLKCON[1:0]) control bits
    PG4CONLbits.CLKSEL = 1;
    //Independent Edge PWM mode
    PG4CONLbits.MODSEL = 0; 
    //PWM Generator produces two PWM cycles after triggered
    PG4CONLbits.TRGCNT = 1;
    //Standard Resolution mode
    PG4CONLbits.HREN = 0;
    //PWM Generator does not control the PWMxH output pin
    PG4IOCONHbits.PENH = 1;
    //PWM Generator broadcasts software set/clear of the UPDATE status 
    //  bit and EOC signal to other PWM Generators
    PG4CONHbits.MSTEN = 1;
    //PWM Generator operates in Retriggerable mode
    PG4CONHbits.TRGMOD = 1;
    //
    PG4CONHbits.UPDMOD = 0;
    PG4CONHbits.MDCSEL = 0;
    PG4EVTLbits.UPDTRG = 1;
    
    //PWM Phase
    PG4PHASE = 0;
    /*PWM frequency is 40kHz*/
    PG4PER = 2500;
    //Duty Cycle
    PG4DC = 1000; 
    
    //Enable PWM
    PG4CONLbits.ON = 1;
}

