/******************************************************************************/
/*  This file is part of the uVision/ARM development tools                    */
/*  Copyright KEIL ELEKTRONIK GmbH 2002-2004                                  */
/******************************************************************************/
/*                                                                            */
/*  PWM.C:  LED Flasher                                                    */
/*                                                                            */
/******************************************************************************/
                  
#include "lpc21xx_keil.h"                    /* LPC21xx definitions  */
#include "config.h"

#define LED2Y    23
#define LED2BITY (1<<LED2Y)
#define LED3Y    22
#define LED3BITY (1<<LED3Y)


unsigned char ISRflag=0;


void __attribute__ ((interrupt("IRQ"))) PWM0_isr(void)
{
  //unsigned cpsr = disableIRQ();
  
  long int regval;
  //PWMIR       |= 0x00000001;               /* Clear match0 interrupt */
  regval = PWMIR;
  PWMIR = regval;
  VICVectAddr  = 0x00000000;
  
  
  if (ISRflag ==0 )
   {
      IOSET1 = LED3BITY; // enable LED3
	  ISRflag = 1;
   }
   else if(ISRflag == 1)
   { 
      IOCLR1 = LED3BITY; // disable LED3
	  ISRflag = 0;
   }
  
  
 // restoreIRQ(cpsr);
}


void init_PWM (void) {

  VICIntSelect = 0x00000000;
  VICIntEnable = 0x00000100;                /* Enable the interrupt */
  VICVectCntl8 = 0x00000028;                /* Set channel */
  VICVectAddr8 = (unsigned)PWM0_isr;        /* Set the PWM ISR vector address */
  
  
 
	PINSEL0 |= 0x00020000; // Change GPIO P0.8 to PWM4
	PWMPR = 0x00000000; // Set Pre Scale to 0
	PWMPCR = 0x00001000; // Enable PWM4 Output (bit12)
	PWMMR0 = 0x00000300; // Max Value of Pulse Width
	PWMMR4 = 0x00000020; // PWM4 Match Value
	PWMMCR = 0x00000003; // Reset and interrupt PWMMTC when Matched with PWMMR0
	PWMLER = 0x7F; // Latch in all PWMMMR's
	PWMTCR = 0x02; // Reset PWMMTC, (bit1)
	PWMTCR = 0x09; // Counter Enable, PWM Mode Enabled(bit0, bit3)  
  
}


