[MCX A153] LPCMP Beginner's Guide (Japanese Blog) 1. Introduction
This article explains the LPCMP ( Low - Power Compulator ) included in the NXP MCX A series ( MCX A153 ), using the sample from the MCUXpresso SDK frdmmcxa153_lpcmp_interrupt and the FRDM-MCXA153 board as examples.
LPCMP is a peripheral function that compares the relative magnitudes of two analog inputs and obtains the result as a digital output. It is used in a wide range of applications, including threshold detection, zero-crossing detection, power supply monitoring, and touch input. The MCX A153's LPCMP features a built-in 8-bit DAC and 8-channel analog multiplexer, enabling threshold comparison without the need for additional external components.
This sample assigns an external voltage (VDD or GND) to the positive input (P2_2 / CMP0_IN0) of CMP0 and the internal DAC output (≒ VREFI/2 = VDD/2) to the negative input, and turns the LED_RED on/off using rising/falling edge interrupts of the comparison result.
(Reading time: 20 minutes)
2. Overview of LPCMP
LPCMP consists of the following subblocks:
Comparator core : Compares positive and negative inputs to generate a digital output (COUT).
8-bit DAC : Vin is divided using a 256-tap resistor ladder. VREFH0 (VDD) or VREFH1 (VREFI input) can be selected as Vin.
Output formula: DACO = Vin / 256 × (DAC_DATA + 1) , DAC_DATA = 0 to 255
Output range: 256 levels from Vin × 1/256 to Vin × 256/256 (= Vin) (0V cannot be output)
ANMUX (PMUX / MMUX) : 8:1 analog selector. External pins (IN0-IN5), internal DAC output, and Reserved selector can be assigned to positive/negative inputs.
Window / Sampling / Filter : Debouncing, Timing Control
Output pin / Interrupt : Three flags: CFR (rising edge), CFF (falling edge), RRF (output change).
3. Internal Block Diagram
3.1 Input Selector and Core
3.2 Built-in DAC
When VRSEL = 0, VREFH0 (VDD) becomes Vin, and when VRSEL = 1, VREFH1 (VREFI input) becomes Vin.
4. Unique information in MCX A153
item
Content
instance
CMP0 (used in this sample), CMP1
Analog input pins
CMP0_IN0~IN5
Built-in DAC resolution
8-bit (256 levels)
DAC reference voltage
VDD (VREFH0) or VREFI external input (VREFH1)
Interrupt flag
CFR (Climate Change), CFF (Climate Fall), RRF (Reduced Output Frequency)
Functional clock source
Select from FRO12M, FRO HF, CLK_1M, etc. using CLOCK_AttachClk
Analog power supply
Enable the analog power supply for the CMP/DAC using SPC0 ACTIVE_CFG1.
5. Sample Operation
5.1 Wiring
Connect the FRDM-MCXA153 pin headers as follows:
signal
pin
Board Location
Destination
role
VREFI
P2_7
J3-1
JP8-2 (VDD)
DAC reference voltage Vin (= VDD)
CMP0_IN0
P2_2
J2-9
Manually switch between VDD and GND.
Plus input
LED_RED
P3_12
Onboard
―
Displaying comparison results
By jumpering J3-1 to JP8-2 (VDD), VREFI = VDD, and the Vin of the built-in DAC becomes VDD. DAC_DATA is set to ((LPCMP_DCR_DAC_DATA_MASK >> SHIFT) >> 1) = 127, so DACO ≈ VDD/2. Connecting J2-9 (CMP0_IN0) to VDD lights up LED_RED, and connecting it to GND turns it off.
5.2 Sequence
5.3 Behavior of Comparison Results
6. Clock settings
The LPCMP requires a separate function clock (CMP_FUNC) for sampling/filtering, in addition to the bus clock. Furthermore, because it's an analog circuit, the analog power supply for the CMP/DAC must be enabled in the SPC (System Power Controller).
hardware_init.c The relevant code is the following three lines:
CLOCK_AttachClk(kFRO12M_to_CMP0); /* 1) 機能クロックソースを選択 */
CLOCK_SetClockDiv(kCLOCK_DivCMP0_FUNC, 1U); /* 2) 分周を 1 に設定 (12MHz のまま) */
SPC0->ACTIVE_CFG1 |= ((1U << 16U) | (1U << 20U)); /* 3) CMP0/DAC0 アナログ電源 ON */
Structure of the SPC0->ACTIVE_CFG1 register
6.1 Clock Tree
Refer to Figure 46 in the MCX A153 Reference Manual .
6.2 The corresponding bit of SPC ACTIVE_CFG1
bit
Content
bit16 (CMP0_AEN)
Enable the CMP0 analog circuit in Active mode.
bit20 (CMP0_DAC_AEN)
Enable the CMP0's built-in DAC in Active mode.
If you forget to set these two bits, LPCMP_Init() itself won't cause an error, but the comparison result will always remain 0, or the DAC output will not start up.
7. PINMUX Settings
pin_mux.c We have set up the following four systems (a total of 5 pins).
pin
function
ALT
Setting points
P2_2
CMP0_IN0 (analog input)
ALT0
Pull-Down, Input Buffer Disable
P2_7
VREFI (DAC Reference Voltage)
ALT0
Pull-Disable, Input Buffer Disable
P3_12
GPIO (LED_RED)
ALT0
Output, initial value 1 (light off)
P0_2
LPUART0_RX
ALT2
Pull-Up
P0_3
LPUART0_TX
ALT2
Pull-Up
7.1 Precautions when using analog pins
When using P2_2 / P2_7 as analog inputs, be sure to disable the digital input buffer . If it is left enabled, a shoot-through current will flow every time the input voltage passes near the logic threshold, leading to increased current consumption, heat generation, and distortion of the analog signal.
const port_pin_config_t port2_2_pin16_config = {
kPORT_PullDown,
kPORT_LowPullResistor,
kPORT_FastSlewRate,
kPORT_PassiveFilterDisable,
kPORT_OpenDrainDisable,
kPORT_LowDriveStrength,
kPORT_NormalDriveStrength,
kPORT_MuxAlt0,
kPORT_InputBufferDisable, /* <-- 必須 */
kPORT_UnlockRegister,
};
PORT_SetPinConfig(PORT2, 2U, &port2_2_pin16_config);
7.2 PORT / GPIO Clock and Reset Release
Before touching PINMUX, you need to supply a clock to the PORT and GPIO you will be using and disable the reset.
CLOCK_EnableClock(kCLOCK_GatePORT0);
CLOCK_EnableClock(kCLOCK_GatePORT2);
CLOCK_EnableClock(kCLOCK_GatePORT3);
CLOCK_EnableClock(kCLOCK_GateGPIO3);
RESET_ReleasePeripheralReset(kPORT0_RST_SHIFT_RSTn);
RESET_ReleasePeripheralReset(kPORT2_RST_SHIFT_RSTn);
RESET_ReleasePeripheralReset(kPORT3_RST_SHIFT_RSTn);
RESET_ReleasePeripheralReset(kGPIO3_RST_SHIFT_RSTn);
Forgetting this will result in a fault occurring when writing to the GPIO register, or the changes being written not being reflected.
8. Code Explanation Using the SDK API
8.1 Definition of app.h
#define DEMO_LPCMP_BASE CMP0
#define DEMO_LPCMP_USER_CHANNEL 0U /* CMP0_IN0 */
#define DEMO_LPCMP_DAC_CHANNEL 7U /* 内蔵 DAC 出力の MUX チャネル */
#define DEMO_LPCMP_IRQ_ID CMP0_IRQn
#define DEMO_LPCMP_IRQ_HANDLER_FUNC CMP0_IRQHandler
8.2 Initialization Sequence in main()
lpcmp_config_t mLpcmpConfigStruct;
lpcmp_dac_config_t mLpcmpDacConfigStruct;
BOARD_InitHardware(); /* clocks + pinmux + console */
/* 1) デフォルト設定を取得し、必要箇所だけ上書き */
LPCMP_GetDefaultConfig(&mLpcmpConfigStruct);
/* 2) コンパレータ初期化 */
LPCMP_Init(DEMO_LPCMP_BASE, &mLpcmpConfigStruct);
/* 3) 内蔵 DAC 設定: Vin = VREFI(=VDD), DAC_DATA = 127 (フルスケールの半分)
* DACO = VREFI / 256 * (127 + 1) = VREFI * 128/256 = VREFI/2 */
mLpcmpDacConfigStruct.enableLowPowerMode = false;
mLpcmpDacConfigStruct.referenceVoltageSource = kLPCMP_VrefSourceVin2; /* VREFI */
mLpcmpDacConfigStruct.DACValue =
((LPCMP_DCR_DAC_DATA_MASK >> LPCMP_DCR_DAC_DATA_SHIFT) >> 1U); /* = 127 */
LPCMP_SetDACConfig(DEMO_LPCMP_BASE, &mLpcmpDacConfigStruct);
/* 4) PMUX = IN0, MMUX = DAC ch7 */
LPCMP_SetInputChannels(DEMO_LPCMP_BASE,
DEMO_LPCMP_USER_CHANNEL,
DEMO_LPCMP_DAC_CHANNEL);
/* 5) 立ち上がり/立ち下がり両エッジ割り込みを有効化 */
LPCMP_EnableInterrupts(DEMO_LPCMP_BASE,
kLPCMP_OutputRisingInterruptEnable |
kLPCMP_OutputFallingInterruptEnable);
EnableIRQ(DEMO_LPCMP_IRQ_ID);
while (1) { } /* あとは割り込み駆動 */
8.3 Interrupt Handlers
void DEMO_LPCMP_IRQ_HANDLER_FUNC(void)
{
LPCMP_ClearStatusFlags(DEMO_LPCMP_BASE,
kLPCMP_OutputRisingEventFlag | kLPCMP_OutputFallingEventFlag);
if (kLPCMP_OutputAssertEventFlag ==
(kLPCMP_OutputAssertEventFlag & LPCMP_GetStatusFlags(DEMO_LPCMP_BASE)))
{
LED_ON(); /* V+ > V- (COUT=1) : LED 点灯 */
}
else
{
LED_OFF(); /* V+ < V- (COUT=0) : LED 消灯 */
}
SDK_ISR_EXIT_BARRIER;
}
In both rising and falling interrupts, both flags are cleared first, and then the LED is branched on/off based on kLPCMP_OutputAssertEventFlag (= current COUT).
9. How to operate
frdmmcxa153_lpcmp_interrupt Open it in the IDE and build it.
Jumper J3-1 (P2_7 / VREFI) to JP8-2 (VDD) to set VREFI = VDD.
Write to the target and open a terminal (115200 bps).
Connect J2-9 (P2_2 / CMP0_IN0) to VDD with a jumper wire → LED_RED lights up.
Reconnect the same pin to GND → LED_RED turns off
10. Common stumbling blocks
Symptoms
cause
handle
The LPCMP output is not switching.
Forgot to enable the analog power supply for CMP0/DAC in SPC0 ACTIVE_CFG1.
SPC0->ACTIVE_CFG1 |= (1<<16)
No interruption
LPCMP_EnableInterrupts or EnableIRQ(CMP0_IRQn) leak
Check both calls
DAC output does not start up.
VRSEL and VREFI input pin mismatch, VREFI not connected
kLPCMP_VrefSourceVin2 When in use, apply the reference voltage to P2_7.
Chattering near the threshold
Hysteresis 0, filter disabled
hysteresisMode Set to Level 1-3, or enable filtering.
Fault when writing to GPIO registers
PORT clock not supplied / Reset not released
CLOCK_EnableClock() And confirm the call to RESET_ReleasePeripheralReset()
11. Summary
The LPCMP in the MCX A153 has the following characteristics:
With 8 ANMUX inputs and a built-in 8-bit DAC, threshold comparison is possible with minimal external circuitry.
The DAC output is DACO = Vin/256 × (DAC_DATA + 1) (DAC_DATA = 0 to 255), with 256 steps ranging from Vin × 1/256 to Vin.
Low-power mode, hysteresis, and filtering functions can be configured with a single line of SDK API code.
To use (1) Functional Clock Attachment + Divider (2) SPC Analog Power Supply Enable (3) Disable the Input Buffer for analog pins These three points are essential.
This sample uses a minimal configuration, but it can be extended to dynamically change thresholds with a DAC, use window mode, or use DMA/external triggers instead of interrupts. We hope this article will be helpful as a starting point for developing applications that use LPCMP.
=========================
We are currently unable to respond to comments left in the "Comment" section of this post. We apologize for the inconvenience, but please refer to " Technical Questions to NXP - How to Contact Us( Japanese Blog) " when making an inquiry. (If you are already an NXP distributor or have a relationship with NXP, you may ask your representative directly.) This article explains the LPCMP ( Low - Power Compulator ) included in the NXP MCX A series ( MCX A153 ), using the sample from the MCUXpresso SDK frdmmcxa153_lpcmp_interrupt and the FRDM-MCXA153 board as examples.
LPCMP is a peripheral function that compares the relative magnitudes of two analog inputs and obtains the result as a digital output. It is used in a wide range of applications, including threshold detection, zero-crossing detection, power supply monitoring, and touch input. The MCX A153's LPCMP features a built-in 8-bit DAC and 8-channel analog multiplexer, enabling threshold comparison without the need for additional external components.
This sample assigns an external voltage (VDD or GND) to the positive input (P2_2 / CMP0_IN0) of CMP0 and the internal DAC output (≒ VREFI/2 = VDD/2) to the negative input, and turns the LED_RED on/off using rising/falling edge interrupts of the comparison result.
(Reading time: 20 minutes) MCUXpresso SDK MCX Japanese Blog
View full article