_FOSC( FRC_PLL16 );  // 6mhz * PLL16  / 4 = 24mips
_FBORPOR(MCLR_EN & PBOR_ON & BORV_42 & RST_PWMPIN & PWMxH_ACT_HI & PWMxL_ACT_HI);
_FWDT(WDT_OFF); 



PWMCON1 = (PWM_MOD1_COMP & PWM_MOD2_COMP & PWM_MOD3_COMP &
				PWM_PDIS1L & PWM_PDIS1H &
				PWM_PEN2L & PWM_PEN2H &					 // PWM2&3 for H bridge; PWM3 Mirrored on PWM1
				PWM_PEN3L & PWM_PEN3H 
				);
  

/* Holds the PWM interrupt configuration value*/
    unsigned int config;
    /* Configure pwm interrupt enable/disable and set interrupt priorties */
    config = (PWM_INT_DIS & PWM_FLTA_EN_INT & PWM_INT_PR1 & PWM_FLTA_INT_PR4);

    /* clear the Interrupt flags */
    IFS2bits.PWMIF = 0;	
    IFS2bits.FLTAIF = 0;	

    /* Set priority for the period match */
    IPC9bits.PWMIP      = (0x0007 & config);

    /* Set priority for the Fault A */
    IPC10bits.FLTAIP    = (0x0070 & config)>> 4;

    /* enable /disable of interrupt Period match */
    IEC2bits.PWMIE      = (0x0008 & config) >> 3;

    /* enable /disable of interrupt Fault A.*/
	IEC2bits.FLTAIE     = (0x0080 & config) >> 7;

 /* Configure PWM to generate 0 current*/
    PWMCON2bits.UDIS = 0;
    
  PDC1 = (FCY/FPWM - 1);	// PWM1 used as sign/magnitude output

	PDC2 = PDC3 = 0;
    PTPER = ((FCY/FPWM)/2 - 1);      // set the pwm period register(/2 for cnt up/dwn)
    
    SEVTCMP = 0x00;

    



/* set dead time options, scale = 1, 5*4TCY (about 400ns) */
	DTCON1 = PWM_DTAPS4 & PWM_DTA5;
	
 FLTACON =	PWM_FLTA_MODE_CYCLE &
				PWM_FLTA2_EN &
				PWM_FLTA3_EN &
				PWM_OVA2L_ACTIVE &
				PWM_OVA2H_INACTIVE &
				PWM_OVA3L_ACTIVE &
				PWM_OVA3H_INACTIVE ;

/* set special event post scaler, output override sync select and pwm update enable */
    PWMCON2 = (PWM_SEVOPS1 & PWM_OSYNC_PWM & PWM_UEN);
    PTCON   = (PWM_EN & PWM_IDLE_CON & PWM_OP_SCALE1 & PWM_IPCLK_SCALE1 & PWM_MOD_UPDN);
    OVDCON = 0xFFFF;	// no output override

