#include "OB6710.h"
#include "PWM.h"
#include "intrins.h"

#define PWMTB_PRESCALE  0x00        //PWM Time base PreScale

#define PWM_INTERRUPT   0x01        //PWM Period Interrupt

#define d_POLARITY0       (0x00 << 0x00)     //PWM0 Polarity active
#define d_POLARITY1       (0x00 << 0x01)     //PWM1 Polarity active
#define d_POLARITY2       (0x00 << 0x02)     //PWM2 Polarity active
#define d_POLARITY3       (0x00 << 0x03)     //PWM3 Polarity active
#define d_POLARITY4       (0x00 << 0x04)     //PWM4 Polarity active
#define d_POLARITY5       (0x00 << 0x05)     //PWM5 Polarity active


unsigned int PWM_PERIOD = 0x1000;      //PWM period

unsigned int PWM_DUTY0  = 0x800;      //PWM0 Duty
unsigned int PWM_DUTY1  = 0x800;      //PWM1 Duty
unsigned int PWM_DUTY2  = 0x800;      //PWM2 Duty
bit g_PwmInterrupt = 0;

//=============================================================================================
//
//=============================================================================================
unsigned int gTEST;
void PWM_Init(void)
{ 
	PWMEN       = (d_PWM_ENABLE_0 | d_PWM_ENABLE_1 | d_PWM_ENABLE_2 | d_PWM_ENABLE_3 | d_PWM_ENABLE_4| d_PWM_ENABLE_5);
	PWMTBC0     = (PWMTB_PRESCALE );
  PWMOPMOD    = (d_PWM_MODE_Complementary0 | d_PWM_MODE_Complementary1 | d_PWM_MODE_Complementary2 );
  PWMPOLARITY = (d_POLARITY0 | d_POLARITY1 | d_POLARITY2 | d_POLARITY3 | d_POLARITY4 | d_POLARITY5);
  PERIODH     = PWM_PERIOD / 256 ;
  PERIODL     = PWM_PERIOD % 256 ;
  PWMTBC1     = PWMTBC1 | 0x90 ; 
}

void PWM_Duty(unsigned int nPWMD0,unsigned int nPWMD1, unsigned int nPWMD2) //PWM Output Set
{ 
  DUTY0L  = nPWMD0 % 256;
  DUTY0H  = nPWMD0 / 256;
  DUTY1L  = nPWMD1 % 256;
  DUTY1H  = nPWMD1 / 256;
  DUTY2L  = nPWMD2 % 256;
  DUTY2H  = nPWMD2 / 256;	
}

void PWM_EnableIE(void)
{
	EA = 1;
	PWMTBC1     = (PWMTBC1 | PWM_INTERRUPT); 
}

void PWM_ISR(void) interrupt d_PWM_Vector
{
    PWMINTF &= 0xFE;
		g_PwmInterrupt = 1;
}


