#include <LPC213x.H>                       // LPC213x definitions

volatile long SysTickCnt;                  // System Tick Counter (each 10 msec)

__irq void tc0 (void) {                    // Timer 0 Interrupt, each 10ms @ 60 MHz CPU Clock
    SysTickCnt++;
    T0IR        = 1;                       // Clear interrupt flag
    VICVectAddr = 0;                       // Acknowledge Interrupt
}

void Init_T0 (void) {                      // Setup Timer 0 Interrupt
    T0MR0   = 600000-1;                    // 60MHz / 600000 = 100Hz = 10 msec
    T0MCR   = 3;                           // Interrupt and Reset on MR0
    T0TCR   = 1;                           // Timer0 Enable
    VICVectAddr0 = (unsigned long)tc0;     // set interrupt vector in 0
    VICVectCntl0 = 0x20 | 4;               // use it for Timer0 Interrupt
    VICIntEnable = 0x00000010;             // Enable Timer0 Interrupt
}

void delay (long tick) {                   // delay function
  long cnt;

    cnt = SysTickCnt;
    while ((SysTickCnt - cnt) < tick) ;
}


int main (void) {
    IODIR0 = 0x00400000;                   // P0.22 (LED1) defined as output
    Init_T0();
  
    while (1) {                            // Loop forever
        IOCLR0 = 0x00400000;               // Turn on LED1
        delay(20);                         // 200 msec delay
        IOSET0 = 0x00400000;               // Turn off LED1
        delay(20);                         // 200 msec delay
    }
}
