/*
***********************************************************************************************************************
*                                     External interrupt example (EINT0) for LPC2103
*
* Programmer 	: 	John Leung, TechToys. Hong Kong
* Web presence  : 	www.TechToys.com.hk
* Language		: 	RealView MDK3.22a, evaluation copy
* Hardware		: 	LPC2103 demo board LPC2103 Eval 1A
* Date			: 	13 Sept 2008
* File			: 	EINT0_main.c
* Note			: 	This program demonstrate EINT0 function. Upon a button press at P0_16 (EINT0), which is the centre
*					up/down key of the 5-way joystick the LED at P0_8 will change state even though the program loops
*					dead by for(;;) in main(void)
***********************************************************************************************************************
*/            

#include <LPC2103.H>
#include "..\SYSTEM\system.h"
#include "..\IOCONFIG\ioconfig.h"

#define LED				P0_8		 		//LED connected to P0.8

#define LED_OFF			IOSET_bit.LED = 1
#define LED_ON			IOCLR_bit.LED = 1


void IRQ_EINT0(void) __irq;

/* IRQ routine */
void  IRQ_EINT0(void)	__irq
{
	if(IOPIN_bit.LED==1) 							//toggle the LED pin
		LED_ON;
	else
		LED_OFF;
	while(EXTINT_bit.EINT0==1)						//wait until key release here, no debounce yet!
	EXTINT_bit.EINT0 = 1;							//clear interrupt flag
	VICVectAddr = 0;
	
}								

/* main function */
int main (void)
{
		sysInit();									//Set CCLK and PCLK to 60MHz 
		LED_OFF;									//set LED off to start with
		IODIR_bit.LED = 1;							//Set LED pin as output

		VICIntSelect = 0x00000000;					//classify all interrupt sources as IRQ (default upon BOR, therefore it is just for remarks)
		VICDefVectAddr = (int) IRQ_EINT0;			//this register holds the address of non-vectored IRQs

		PINSEL1_bit.P0_16 = 1;						//assign P0.16 to EINT0 function

 		EXTMODE_bit.EXTMODE0 = 0;					//level sensitivity selected for EINT0 (default value, for remarks only)
		EXTPOLAR_bit.EXTPOLAR0 = 0;					//EINT0 interrupt triggered for a H-L transition
		EXTINT_bit.EINT0 = 1;						//writing 1 to EINT0 clears the corresponding bit

		VICIntEnable_bit.EINT0 = 1;					//enable the EINT0 interrupt
		
		for(;;);									//program "hang" here to demonstrate the activity of EINT0 action
		
}
