#include <hidef.h>      /* common defines and macros */
#include "derivative.h"      /* derivative-specific definitions */

/*------------------------------------------------------------*/
/*PWM01, 16-bit, 100,8kHz, 74,2% duty cycle, left aligned-------*/
/*------------------------------------------------------------*/
//For PWM01 are inportnant these bites/pins CON01 PWME1 PPOL1 PCLK1 CAE1 PWM1
void  PWM_Init(void) {

PWMCTL = 0b00011100;      // PWM0 and PWM1 connected to 16-bit PWM01
PWMCAE = 0b00000000;      // Left aligned output mode
PWMPRCLK = 0b01110000;    // Clock A prescaler fBUS, Clock B prescaler fBUS/128, there Clock A=6.25MHz
PWMCLK = 0b00000010;      // Clock SA for PWM01 
//----
PWMSCLA = 0b00000001;     // Clock SA = Clock A/(2*PWMSCLA), there Clock A/2=3.125MHz Clock input is there channel 1 
//PWM01
PWMPER01 = 0x001F;        // PWM0 frequency = Clock SA / PWMPER0, there 3.125MHz/31=100.8kHz
PWMDTY01 = 0x0008;        // PWM0 duty cycle = (PWMPER0-PWMDTY0)/PWMPER0*100%, there (31-8)/31*100%=74,2%
//----
PWMPOL = 0b00000000;      // Starting polarity - PWM0 begining low - inluence to duty cycle formula
PWME = 0b00000010;        // PWM01 enable (PWM01 setup on bit PWME1)
}
/*--------------------------------------------------------------*/

void main(void) {
  /* put your own code here */
  
  DDRP = 0x01;            // Pin PP0 used as GPIO
  PWM_Init();

	EnableInterrupts;


  for(;;) {
  PTP_PTP0 = ~PTP_PTP0;   // toggle at PP0
    _FEED_COP(); /* feeds the dog */
  } /* loop forever */
  /* please make sure that you never leave main */
}
