This article mainly introduce how to use CTIMER measures pulse-width in LPC845, in fact, it applies to all LPC products that include CTIMER module.
CTIMER have below features:
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.)
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.
Main steps as below:
1) 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, Falling edge, and enable interrupt.
CTIMER_SetupCapture(CTIMER,CTIMER_CAP0_INT, CTIMER_CAP_FALL, TRUE);
2）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, configures Channel 0 rising edge clears the timer, enables clearing of the timer and the pre-scaler.
CTIMER->CTCR = CTIMER_CTCR_CTMODE(0)|CTIMER_CTCR_SELCC(1)
3)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. The
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.
Input a signal as below into channel 0 (P1_0), pulse width is 10us.
Print the measurement result on Console view of MUXpresso IDE: