example of LPC1837 capture timer?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

example of LPC1837 capture timer?

1,883 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by smalouin on Mon Oct 27 06:37:05 MST 2014
Hello all,

I am trying to use the LPC1837 capture timer on port 5.0 (timer1 capture 0)... not having much luck (ie, I never get the IRQhandler for timer 1 called.

Anybody has an example that works?

Here is what I have, but it's all from scratch, I'm pretty sure an example would set me on the right track.

void rc5InterruptInit(void){
  /* Enable timer 1 clock and reset it */
   #define IR_TIMER              LPC_TIMER1
   #define IR_TIMER_IRQHANDLER   TIMER1_IRQHandler
   #define IR_TIMER_IRQn         TIMER1_IRQn

Chip_TIMER_Init(IR_TIMER);
Chip_RGU_TriggerReset(RGU_TIMER1_RST);
while (Chip_RGU_InReset(RGU_TIMER1_RST)) {}

/* Get timer 1 peripheral clock rate */
gRc5.timerFreq = Chip_Clock_GetRate(CLK_MX_TIMER1);

      
      /* Timer setup for match and interrupt at TICKRATE_HZ */
      Chip_TIMER_Reset(IR_TIMER);

      //Chip_SCU_PinMuxSet(5, 0, (SCU_MODE_INBUFF_EN | SCU_MODE_FUNC5));/* P5.0 pin as T1_CAP0 */
      Chip_SCU_PinMuxSet(5, 0, (SCU_MODE_FUNC5));/* P5.0 pin as T1_CAP0 */
      LPC_GIMA->CAP0_IN[1][0]=00000021;
      Chip_TIMER_TIMER_SetCountClockSrc(IR_TIMER,TIMER_CAPSRC_RISING_PCLK ,0);
      Chip_TIMER_ClearCapture(IR_TIMER,0);
      //Chip_TIMER_PrescaleSet(IR_TIMER,(gRc5.timerFreq /(RC5_FREQ)));
      Chip_TIMER_PrescaleSet(IR_TIMER,0);
      Chip_TIMER_CaptureEnableInt(IR_TIMER,0);/* start the capture timer */
      Chip_TIMER_CaptureFallingEdgeEnable(IR_TIMER,0);
      Chip_TIMER_CaptureRisingEdgeEnable(IR_TIMER,0);


Chip_TIMER_Enable(IR_TIMER);
      
/* Enable timer interrupt */
NVIC_EnableIRQ(IR_TIMER_IRQn);
NVIC_ClearPendingIRQ(IR_TIMER_IRQn);
}

void TIMER1_IRQHandler (void){
   
   /* using the capture compare */
   if (Chip_TIMER_CapturePending(IR_TIMER, 0)){
      rc5TimerCaptureIrq();
   }
}
Labels (1)
Tags (1)
0 Kudos
3 Replies

831 Views
olivierswinkels
Contributor I

I got exactly the same issue.

Did you eventually get it to work?

0 Kudos

831 Views
isaacavila
NXP Employee
NXP Employee

Hello All,

This is an example on LPCXpresso 4337 that uses P1_13 as the capture input for timer 0, you can use it as reference!

#include "board.h"

#include <stdio.h>

/*****************************************************************************

* Private types/enumerations/variables

****************************************************************************/

#define TIMER_NUMBER        0

#define CAP_NUMB            0

#define LPC_TIMER            LPC_TIMER0

#define RGU_TIMER_RST        RGU_TIMER0_RST

#define LPC_TIMER_IRQ        TIMER0_IRQn

#define LPC_TIMER_IRQH        TIMER0_IRQHandler

/*****************************************************************************

* Public types/enumerations/variables

****************************************************************************/

/*****************************************************************************

* Private functions

****************************************************************************/

/*****************************************************************************

* Public functions

****************************************************************************/

/**

* @brief    Handle interrupt from 32-bit timer

* @return    Nothing

*/

void LPC_TIMER_IRQH (void)

{

    static uint32_t times = 0;

    volatile static uint32_t captureValue = 0;

    /* Count how many times the interrupt has being trigger */

    times++;

    /* Get capture value on CAP0 */

    captureValue = Chip_TIMER_ReadCapture(LPC_TIMER, CAP_NUMB);

    DEBUGOUT(" %d\r\n", captureValue);

}

/**

* @brief    main routine for blinky example

* @return    Function should not exit.

*/

int main(void)

{

    SystemCoreClockUpdate();

    Board_Init();

    /* P1_13 as T0_CAP0, disable internal filter and enable input buffer */

    Chip_SCU_PinMuxSet(1, 13, (SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC4));

    /* Timer 0 CAP0 EDGE enabled and SELECT as 0x2 */

    LPC_GIMA->CAP0_IN[TIMER_NUMBER][CAP_NUMB] = (2 << 4) | (1 << 1);

    /* Timer0 init */

    Chip_TIMER_Init(LPC_TIMER);

    /* Reset timer0 */

    Chip_RGU_TriggerReset(RGU_TIMER_RST);

    while (Chip_RGU_InReset(RGU_TIMER_RST));

    Chip_TIMER_Reset(LPC_TIMER);

    /* Select PCLK as timer source */

    Chip_TIMER_TIMER_SetCountClockSrc(LPC_TIMER, TIMER_CAPSRC_RISING_PCLK, CAP_NUMB);

    /* Select prescaler value */

    Chip_TIMER_PrescaleSet(LPC_TIMER, 10);

    /* Clear capture value on T0.CAP0 */

    Chip_TIMER_ClearCapture(LPC_TIMER, CAP_NUMB);

    /* Enable rising trigger on T0.CAP0 */

    Chip_TIMER_CaptureRisingEdgeEnable(LPC_TIMER, CAP_NUMB);

    /* Enable interrupt */

    Chip_TIMER_CaptureEnableInt(LPC_TIMER, CAP_NUMB);

    /* Enable timer interrupt */

    NVIC_ClearPendingIRQ(LPC_TIMER_IRQ);

    NVIC_EnableIRQ(LPC_TIMER_IRQ);

    /* Enable timer */

    Chip_TIMER_Enable(LPC_TIMER);

    while (1) {

    }

    return 0;

}

Hope this helps!

Regards,

Isaac

0 Kudos

831 Views
yuanbinzhou
Contributor II

Hi Isaac,

I have used example above to test the capture timer on lpcxpresso 4337 board. I have changed line 69 from TIMER_CAPSRC_RISING_PCLK to TIMER_CAPSRC_RISING_CAPN. However the value of "captureValue" always equals to zero, while I printed the value of "times", it changed really fast which of course exceed the frequency of my input signal. So strange about that. Any ideas about that?

0 Kudos