#include <LPC214x.h>
#include <ctl_api.h>
#define LED1 10 //My LED 1 is on P0.10
#define LED2 11 //My LED 1 is on P0.11
#define Btn1 15 //My Button 1 is on P0.15
#define GETBIT(var,bit) (((var)>>(bit))&1) //Get the BIT from the VAR
#define SETBIT(var,bit) ((var)|=(1<<(bit))) //SET a bit HIGH
#define CLRBIT(var,bit) ((var)&=(~(1<<(bit)))) //CLR a bit to LOW
void IRQ_Routine (void);
void init_isr(void);
void Initialize(void);
void DelayUs(int us);
void DelayMs(int ms);
void DelayS(int se);
int main(void);
int en;
char MyDat;
int main(void){
int loop1; //LED Blink Loop Var.
Initialize(); //Initialize the Clock and PLL
init_isr(); //Initialize the ISR (IRQ)
IODIR0=0xFFFFFFFF; //All Output
PINSEL0 = 0x80000000; //All pins GPIO except P0.15 aka BTN1 would be EINT2
CLRBIT(IODIR0,Btn1); //Make Btn1 a INPUT
while(1){
for(loop1=0;loop1<20;loop1++){
SETBIT(IOPIN0,LED1); //Turn off LED1
SETBIT(IOPIN0,LED2); //Turn off LED2
DelayMs(100); //Delay 100mS
if(MyDat) CLRBIT(IOPIN0,LED1); //Turn on LED1 if MyDat >= 1
if(!MyDat) CLRBIT(IOPIN0,LED2); //Turn on LED2 if MyDat = 0
DelayMs(100); //Delay 100mS
}
}
}
void init_isr(void)
{
VICIntSelect &= ~(1 << 16); //Make EINT2 a IRQ interrupt
VICIntEnable |= 1<<16; //enable EINT2 interrupt
VICVectCntl0 = (1 << 5) | 16; //use it for EINT2
VICVectAddr0 = (unsigned)IRQ_Routine; //set interrupt vector in 0
EXTINT = 4; //Clear interrupt Flag EINT2
EXTMODE = 4; //EINT2 is edge sensitive.
EXTPOLAR = 0; //GPIO port 0 is accessed via VPB addresses in a fashion
SCS = 0; //Compatible with previous LCP2000 devices.
en = libarm_set_irq(1); //Enable Global IRQ interrupts
}
void IRQ_Routine (void) {
if(MyDat) //If MyDat >= 1 then
MyDat = 0 ; //set it to a 0
else //if not
MyDat = 1; //set it to a 1
DelayMs(10); //10 millisecond delay
EXTINT = 4; //clear interrupt flag
libarm_set_irq(en); //re-enable interrupts
}
void DelayUs(int us) {
int tmp;
for (; us>0; us--);
for(tmp=0;tmp<1000;tmp++);
}
void DelayMs(int ms) {
for (; ms>0; ms--)
DelayUs(1000);
}
void DelayS(int se) {
for (; se>0; se--)
DelayMs(1000);
}
unsigned char GetPin(unsigned long tPin){
unsigned long tmp;
DelayMs(10);
tmp = IOPIN0 & (1<<tPin);
if(tmp == 0)
return 0;
else
return 1;
}
#define PLOCK 0x400
void Initialize(void) {
PLL0CFG=0x23;
PLL0FEED=0xAA;
PLL0FEED=0x55;
PLL0CON=0x1;
PLL0FEED=0xAA;
PLL0FEED=0x55;
while(!(PLL0STAT & PLOCK)) ;
PLL0CON=0x3;
PLL0FEED=0xAA;
PLL0FEED=0x55;
MAMCR=0x2;
MAMTIM=0x4;
VPBDIV=0x01;
}