Hi,
I would like to use a comparator in my project on FRDM-MCXN947, I want to trigger an interrupt in case the voltage at its input reaches 4.6V.
Is it possible to use the comparator on the FRDM-MCXN947 board in this way? If so, how is it necessary to set up the VREF, DAC and CMP modules in this case?
John Adams
Hi @John_Adams
Based on your needs, i think you can refer to the lpcmp_interrupt demo in SDK.
BR
Harry
Hi Harry,
I have already done that, I tried to run the demo with the original pin P1_0 (CMP0_IN0) and everything worked (i.e. the comparator reacted to changes in input voltage), but then I tried to change the pin on P0_22 where the channel CMP1_IN2 is output and adjusted the SW accordingly:
const port_pin_config_t port0_22_pin_config = {/* Internal pull-up/down resistor is disabled */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Pin is configured as CMP1_IN1 */
kPORT_MuxAlt0,
/* Digital input disabled; it is required for analog functions */
kPORT_InputBufferDisable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT0_22 is configured as CMP1_IN2 */
PORT_SetPinConfig(PORT0, 22U, &port0_22_pin_config);
And also changed the demo app accordingly, but CMP is not reacting:
#include "fsl_debug_console.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
#include "fsl_lpcmp.h"
#include "fsl_vref.h"
#include "fsl_spc.h"
/*******************************************************************************
* Definitions
******************************************************************************/
#define DEMO_LPCMP_BASE CMP1
#define DEMO_LPCMP_USER_CHANNEL 1U
#define DEMO_LPCMP_DAC_CHANNEL 7U
#define DEMO_LPCMP_IRQ HSCMP1_IRQn
#define LED_INIT() LED_RED_INIT(LOGIC_LED_ON)
#define LED_ON() LED_RED_ON()
#define LED_OFF() LED_RED_OFF()
#define DEMO_LPCMP_IRQ_HANDLER_FUNC HSCMP1_IRQHandler
#define DEMO_VREF_BASE VREF0
#define DEMO_SPC_BASE SPC0
/*******************************************************************************
* Prototypes
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
/*******************************************************************************
* Code
******************************************************************************/
/*!
* @brief Main function
*/
int main(void)
{
lpcmp_config_t mLpcmpConfigStruct;
lpcmp_dac_config_t mLpcmpDacConfigStruct;
/* Initialize hardware. */
vref_config_t vrefConfig;
/* attach FRO 12M to FLEXCOMM4 (debug console) */
CLOCK_SetClkDiv(kCLOCK_DivFlexcom4Clk, 1U);
CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
/* attach FRO 12M to CMP1 */
CLOCK_SetClkDiv(kCLOCK_DivCmp1FClk, 1U);
CLOCK_AttachClk(kFRO12M_to_CMP1F);
/* enable CMP1, CMP1_DAC and VREF. */
SPC_EnableActiveModeAnalogModules(SPC0, kSPC_controlAllModules);
VREF_GetDefaultConfig(&vrefConfig);
/* Initialize the VREF mode. */
VREF_Init(VREF0, &vrefConfig);
/* Get a 1.8V reference voltage. */
VREF_SetTrim21Val(VREF0, 8U);
BOARD_InitPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
PRINTF("LPCMP Interrupt Example.\r\n");
/*
* k_LpcmpConfigStruct->enableStopMode = false;
* k_LpcmpConfigStruct->enableOutputPin = false;
* k_LpcmpConfigStruct->useUnfilteredOutput = false;
* k_LpcmpConfigStruct->enableInvertOutput = false;
* k_LpcmpConfigStruct->hysteresisMode = kLPCMP_HysteresisLevel0;
* k_LpcmpConfigStruct->powerMode = kLPCMP_LowSpeedPowerMode;
* k_LpcmpConfigStruct->functionalSourceClock = kLPCMP_FunctionalClockSource0;
*/
LPCMP_GetDefaultConfig(&mLpcmpConfigStruct);
/* Init the LPCMP module. */
LPCMP_Init(CMP1, &mLpcmpConfigStruct);
/* Configure the internal DAC to output half of reference voltage. */
mLpcmpDacConfigStruct.enableLowPowerMode = false;
mLpcmpDacConfigStruct.referenceVoltageSource = kLPCMP_VrefSourceVin2;
mLpcmpDacConfigStruct.DACValue = 0xFF;
//((LPCMP_DCR_DAC_DATA_MASK >> LPCMP_DCR_DAC_DATA_SHIFT) >> 1U); /* Half of reference voltage. */
LPCMP_SetDACConfig(CMP1, &mLpcmpDacConfigStruct);
/* Configure LPCMP input channels. */
LPCMP_SetInputChannels(CMP1, 0x02, 0x07);
/* Init the LED. */
LED_INIT();
/* Enable the interrupt. */
EnableIRQ(HSCMP1_IRQn);
LPCMP_EnableInterrupts(CMP1, kLPCMP_OutputRisingInterruptEnable | kLPCMP_OutputFallingInterruptEnable);
while (1)
{
;
}
}
so how come the comparator isn't responding?
Thank you,
John Adams
Hi @John_Adams
Is the voltage at P0_22 actually reaching the expected 4.6V?
If it does not work.
You can try switching to another pin that supports CMP1.
BR
Harry
More likely,
how do I enable CMP on P0_22 (CMP1_IN2).
/* Configure LPCMP input channels. */
LPCMP_SetInputChannels(CMP1, 2, 7);
This is probably not correct, isn't it?
Thank you