[LPC845] Pulse-width Measurement Using CTIMER

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

[LPC845] Pulse-width Measurement Using CTIMER

[LPC845] Pulse-width Measurement Using CTIMER

This article mainly introduces how to use CTIMER measuring pulse-width in LPC845, in fact, it can applies to all LPC products  including CTIMER modules.

1  CTIMER has below features:

  • A 32-bit timer/counter with a programmable 32-bit prescaler.
  • Four 32-bit match registers that allow interrupt generation on  match.
  • The timer and prescaler may be configured to be cleared on a designated capture event. This feature permits easy pulse width measurement by clearing the timer on the leading edge of an input pulse and capturing the timer value on the trailing edge.(This article mainly use this feature.)
  • Up to four match registers can be configured for PWM operation.

 

2 Introduction

There is neither pulse-width measurement nor input capture demo under SDK, so write this article and related code for this topic. The principle is clearing the timer and prescaler on the leading edge of an input and capturing the timer valued on the trailing edge.

 

3  Main steps  

Step1 Choose CAP input channel, capture edge, and enable interrupt if needed. Using “Capture control register”.

The Capture control register is used to control whether one of the four capture registers is loaded with the value in the timer counter when the capture event occurs, and whether an interrupt is generated by the capture event. Setting both the rising and falling bits at the same time is a valid configuration, resulting in a capture event for both edges. In the description below, n represents the timer number, 0 or 1.

In this example, choose capture channel 0 as input channel, falling edge as capture edge, and enable capture interrupt.

SDK code:

CTIMER_SetupCapture(CTIMER,CTIMER_CAP0_INT, 

CTIMER_CAP_FALL,TRUE);

Step2 Select which capture input edge will cause the timer and pre-scaler to be cleared. Using “Count control register”.

The Count Control Register (CTCR) is used to select between timer and counter mode, and in counter mode to select the pin and edge(s) for counting. When counter mode is chosen as a mode of operation, the CAP input (selected by the CTCR bits 3:2) is sampled on every rising edge of the APB bus clock. After comparing two consecutive samples of this CAP input, one of the following four events is recognized: rising edge, falling edge, either of edges or no changes in the level of the selected CAP input. The timer counter register is incremented only if the identified event occurs and the event corresponds to the one selected by bits 1:0 in the CTCR register. Effective processing of the externally supplied clock to the counter has some limitations. Since two successive rising edges of the APB bus clock are used to identify only one edge on the CAP selected input, the frequency of the CAP input cannot exceed one half of the APB bus clock. Consequently, duration of the HIGH/LOWLOW levels on the same CAP input in this case cannot be shorter than 1/APB bus clock. Bits 7:4 of this register are also used to enable and configure the capture-clears-timer feature. This feature allows for a designated edge on a particular CAP input to reset the timer to all zeros. Using this mechanism to clear the timer on the leading edge of an input pulse and performing a capture on the trailing edge, permits direct pulse-width measurement using a single capture input without the need to perform a subtraction operation in software.

In this example, we choose timer mode, configure Channel 0 rising edge clearing the timer, enable clearing of the timer and the pre-scaler.

SDK code:

  CTIMER->CTCR = CTIMER_CTCR_CTMODE(0)|CTIMER_CTCR_SELCC(1)

  |CTIMER_CTCR_ENCC_MASK ;

Step3 Read pulse-width value from “Capture register”.

Each Capture register is associated with one capture channel and may be loaded with the counter/timer value when a specified event occurs on the signal defined for that capture channel. The signal could originate from an external pin or from an internal source.  

SDK code:

CTIMER_GetCAPCounter(HW_CTIMER0, HW_CTIMER_CH0);

We can read capture value on capture interrupt.

Detail code please refer to attached project, it based on MCUXpresso IDE v11.3, SDKv2.9, LPCxpresso845MAX board.

 

4  Test Result

Input a signal as below into channel 0 (P1_0), pulse width is 10us.

Alice_Yang_0-1617272633162.png

Print the measurement results on Console view of MUXpresso IDE:

Alice_Yang_1-1617272689268.png

 

 

 

 

Attachments
100% helpful (1/1)
Version history
Last update:
‎11-12-2021 03:18 AM
Updated by: