#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include "mcu.h"

char pdb1_cont_times = 0;


void main(void) {
  //EnableInterrupts;
  /* include your code here */

  mcu_init();
#if defined _PDB1_
  rtc_init();
#elif defined _PDB_FTM_TR_ADC_
  ftm1_init();
  adc_init();
#endif	
  pdb1_init();

  
  pdb1_cont_times = 0;

  EnableInterrupts;
  for(;;) {
    __RESET_WATCHDOG();	/* feeds the dog */
  } /* loop forever */
  /* please make sure that you never leave main */
}

void interrupt rtc_isr(void){
	PTDD_PTDD3 ^= 1;
	RTCSC_RTIF = 1;		/* Clear RTC interrupt */
}

void interrupt pdb1_isr(){
	if(PDB1SCR_COF == 1){ 
		PDB1SCR_COF = 1;
		if(PDB1CTRL2_CONT)
		{
			if(++pdb1_cont_times >= 5)
			{
				PDB1CTRL2_CONT = 0;	//Disable continuous mode
				pdb1_cont_times =0;
			}
		}else
		{
			PDB1CTRL2_CONT = 1;
			pdb1_cont_times = 0;
		}
	}
	if(PDB1SCR_DAF == 1) 
		PDB1SCR_DAF = 1;
	if(PDB1SCR_DBF == 1) 
		PDB1SCR_DBF = 1;
}

void interrupt adc_isr(void){
	PTDD_PTDD3 ^= 1;
}
