Help with two timers LPC1227

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Help with two timers LPC1227

754件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by nelmak on Mon Sep 17 17:18:38 MST 2012
Hi,

I have the following code where I am timing the routine of my 2nd interrupt timer. Timer0 should execute an interrupt every millisecond while Timer1 should execute every 50ms. However, when i print the counter to my console, the counter is wrong.

Can someone explain what the problem is because I feel that my implementation is correct?

//Enable Timer
void enable_timer16(uint8_t timernum)
{
    if(timernum == 0)
    {
      LPC_CT16B0->TCR = 1;
      //printf("Timer 0 enabled\n");
      return;
    }
    else if(timernum == 1)
    {
        LPC_CT16B1->TCR = 1;
        //printf("Timer 1 enabled\n");
        return;
    }
}

void init_timer16(uint8_t timernum, uint32_t delay)
{
    if (timernum == 0)
    {
        NVIC_DisableIRQ(13);
        LPC_SYSCON->PRESETCTRL |= (0x1<<4);     //De-assert Reset
        LPC_SYSCON->SYSAHBCLKCTRL |= (0x1<<7);    //Enable clock for CT16B0
        LPC_CT16B0->PR  = 0x00;        /* set prescaler to zero */
        LPC_CT16B0->MR[0] = delay * (SystemCoreClock / 1000 - 1);
        LPC_CT16B0->IR  = 0xff;        /* reset all interrrupts */
        LPC_CT16B0->MCR = (0x3<<0);                //Interrupt and reset on MR0
        hrcounter = 0;
        NVIC_EnableIRQ(13);                        //Enable timer0_16 interrupt

    }
    else if(timernum == 1)
    {
        NVIC_DisableIRQ(14); //Disable timer1_16 interrupt
        LPC_SYSCON->PRESETCTRL |= (0x1<<5);     //De-assert Reset
        LPC_SYSCON->SYSAHBCLKCTRL |= (0x1<<8);    //Enable clock for CT16B0
        LPC_CT16B1->PR  = 0x00;        /* set prescaler to zero */
        LPC_CT16B1->MR[0] = delay * (SystemCoreClock / 1000 - 1);
        LPC_CT16B1->IR  = 0xff;        /* reset all interrrupts */
        LPC_CT16B1->MCR = (0x3<<0);                //Interrupt and reset on MR0
        NVIC_EnableIRQ(14); //Enable timer1_16 interrupt
    }
    //printf("Timer %d initialized\n", timernum);

}

//Disable timer
void disable_timer16(uint8_t timernum)
{
//      printf("Timer Disabled");
    if(timernum == 0)
    {
        LPC_CT16B0->TCR = 0;
          return;
    }
    else if(timernum == 1)
    {
        LPC_CT16B1->TCR = 0;
    }
}

void reset_timer16(uint8_t timernum)
{
    LPC_CT16B0->TC = 0;
    hrcounter = 0;
}

void TIMER16_0_IRQHandler()
{
      if ( LPC_CT16B0->IR & (0x1<<0) )
      {
        LPC_CT16B0->IR = 0x1<<0;            /* clear interrupt flag */
        hrcounter++;                    //increment counter
      }
}

void TIMER16_1_IRQHandler()        //Executes 20 times per second
{

    if(LPC_CT16B1->IR & 0x1<<0)
    {
        disable_timer16(0);
        printf("cnt=%d\n", hrcounter);
        hrcounter = 0;

        LPC_CT16B1->IR = 0x1<<0;
        hrval = ADCRead(0);
        enable_timer16(0);
    }
}


int main(void)
{
    //LPC_SYSCON->SYSAHBCLKCTRL |= 0xE0010000UL;

    uartinit(UART_BAUD);
    ADCInit(1000000);
    init_timer16(TIMER0, 1); //Start timer0 that calls interrupt at 1ms
    init_timer16(TIMER1, 50); // Start timer1 that calls interrupt at 50ms to achieve 20Hz
    enable_timer16(0); //Enable timer 0
    enable_timer16(1); //Enable timer 1

    LPC_GPIO0->DIR |= 1<<7;
    LPC_GPIO0->CLR |= 1<<7;

    //printf("Hello World!");
    //unsigned int dummy;
    //for(dummy = 0; dummy < 10; dummy++)
    //printf("Test: %d.", dummy);

    while(1)
    {
        __WFI();
    }
}
I am setting my delay to 1ms and 50ms in the timer initializations so I cannot see what the problem is.

Thanks
0 件の賞賛
返信
2 返答(返信)

718件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by nelmak on Mon Sep 17 21:05:34 MST 2012
Good point my friend I did not think of that. I will look into it tomorrow but thank you for the tip.
0 件の賞賛
返信

718件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ex-kayoda on Mon Sep 17 17:30:27 MST 2012

Quote: nelmak

        LPC_CT16B0->PR  = 0x00;        /* set prescaler to zero */
        LPC_CT16B0->MR[0] = delay * (SystemCoreClock / 1000 - 1);
 



Isn't this a 16 bit register? And you are filling it with what :eek:

Better solution is to use prescale register to generate a ms  :)
0 件の賞賛
返信