Friday, 24 August 2012

1ms TIMER0 led blinky(LPC21xx)




#include "LPC214x.H"
//CCLK=12xM=12x5=60MHZ
//PCLK=CCLK (VPBDIV=0x01)

#define LIMIT_1_MS 59999     //PCLK * X    = 1ms, ie X = 1ms/60mHz, ie X = 60000
void Timer_ISR(void) __irq;

unsigned int ms_count, flag = 0;
int main()
{
VPBDIV = 0X01;
//int timer0
T0TCR = 0x00;
T0TC = 0x0;
T0PC = 0x0; //The Prescale Counter is incremented on every PCLK. When it reaches the value
            //stored in the Prescale Register, the Timer Counter is incremented and
            //the Prescale Counter is reset on the next PCLK.
T0PR = 0x0;    //Prescale Register    set to zero
T0MR0 = LIMIT_1_MS;  //Match Register 0. MR0 can be enabled through the
                //MCR to reset the TC, stop both the TC and PC,
                //and/or generate an interrupt every time MR0 matches the TC.
T0MCR = 0x03;    //bit 0- Interrupt on MR0: an interrupt is generated when MR0 matches the value in the TC.
                //bit 1- Reset on MR0: the TC will be reset if MR0 matches it.

//interrupt
VICVectCntl11 = 0x00000024;
VICVectAddr11 = (unsigned int) Timer_ISR;
VICIntEnable = 0x00000010;

T0TCR = 0x01;

IO0DIR = 0x80000000;

    while(1)
    {
    }
}

void Timer_ISR(void) __irq
{
     if(ms_count == 1000)
    {
        if(flag == 0)
        {
            IO0CLR = 0x80000000;
            flag = 1;
            }
        else if(flag == 1)
        {
            IO0SET = 0x80000000;
            flag = 0;   
            }
        ms_count = 0;
    }

    ms_count++;
    T0IR = 0x01;
    VICVectAddr = 0x00;
}

No comments:

Post a Comment