NetBurner 3.5.7
PDF Version
Analog Comparator

Example Path: examples/PlatformSpecific/MODRT1171/ACMP

MODRT1171 Analog Comparator Example

Overview

This example exercises the two on-chip analog comparators (ACMP1 and ACMP2) of the i.MX RT1171 processor on the MODRT1171 module. Each comparator is set up with an external analog input on its positive side and the chip's internal 8-bit DAC on its negative side, so the comparator fires when the input crosses a programmable threshold. The example also shows how to route a comparator's digital output through the XBARA1 crossbar to a general-purpose pin, and how to wire edge-triggered interrupts.

A simple serial console lets you retune the ACMP1 threshold at runtime and watch the comparator output respond on the scope or logic analyzer.

What This Example Does

  1. Instantiates two MIMX_ACMP objects wrapping the RT1171 CMP peripherals:
    • ACMP1 – positive input channel 0, negative = internal DAC, DAC value 0x7F (~ VREF/2 ~ 1.65 V). Default filter/hysteresis.
    • ACMP2 – positive input channel 1, negative = internal DAC, DAC value 0xA0, inverted output polarity, digital output filter with period 240 and sample count 7.
  2. Enables the XBARA1 clock and connects the ACMP1 compare output to Inout17 of the signal crossbar, which is then routed to pin P1[13]. This demonstrates reaching an internal signal that would otherwise stay on-die.
  3. Muxes the example pins:
    • P1[4] -> ACMP1_IN1 (ACMP1 positive input)
    • P1[24] -> ACMP2_IN2 (ACMP2 positive input)
    • P2[6] -> ACMP1_CMPO (ACMP1 direct output)
    • P1[13] -> XBAR_INOUT17 (ACMP1 output via XBARA)
    • P2[7] -> ACMP2_CMPO, open-drain – combined with an external RC network, this can form a relaxation oscillator (variable-frequency pulse generator).
  4. Registers a single ISR that counts edges per comparator into irqCounts[HwIdx()]:
    • ACMP1 interrupts on both rising and falling edges.
    • ACMP2 interrupts on rising edges only.
  5. Enters a serial-console loop. Press **s** (or S) and type a decimal or 0x-prefixed hex value 0-255; the ACMP1 DAC threshold is updated live with acmp.SetDacVal(...).

Key API – MIMX_ACMP

Defined in src/mimx11xx_acmp.h.

Call Purpose
MIMX_ACMP(idx, posCh, negCh, dac, ...) Construct + configure one comparator (see header for defaults).
Enable() / Disable() Turn the comparator on/off.
SetInputs(pos, neg) Select positive and negative input channels at runtime.
SetDacVal(v) Change the internal-DAC threshold (0-255, v / 256 x VREF).
SetOutputPolarity(p) p < 0 inverts the output; otherwise non-inverting.
SetFilter(period, count) Enable the digital output filter (majority filter on samples).
RegisterIsr(handler) Install a callback invoked on edge interrupts.
EnableIrq(falling, rising) Enable interrupt on either or both edges.
GetState() Sample the current comparator output.

The ISR signature is:

void handler(MIMX_ACMP &cmp, uint32_t status);

cmp.HwIdx() returns 0 for ACMP1, 1 for ACMP2, etc., which the example uses to fan out into the irqCounts[] array.

Hardware Setup

  • Platform: MODRT1171 only (SUPPORTED_PLATFORMS = MODRT1171).
  • ACMP1 analog input: feed P1[4] from a signal source in the 0-VREF range (default VREF is the chip's 1.65 V internal reference, so useful input range is roughly 0-1.65 V unless you change VRSEL).
  • ACMP2 analog input: feed P1[24] similarly.
  • Outputs to observe:
    • P2[6] – ACMP1 direct compare output (push-pull).
    • P1[13] – ACMP1 output routed through XBARA1.
    • P2[7] – ACMP2 open-drain output (needs a pull-up to observe a logic high; shorting to a cap + resistor on the ACMP2 input will produce a self-sustained pulse train).

Using the Console

After programming the board, open the serial console. You should see:

Application: Analog Comparator Example
NNDK Revision: <version>
sizeof rxBuf[0]: 1

Then drive the ACMP1 input. The P2[6] and P1[13] pins will toggle as the input crosses the threshold. To move the threshold:

s
Enter new value: 0x40
Received => 0x40

ACMP1 now compares against 0x40 / 0x100 x VREF. The ISR counter in irqCounts[0] will tick up on every edge as the input crosses the new threshold.

Project Structure

ACMP/
|
+-- src/
| +-- main.cpp Example application, two comparators, serial console
| +-- mimx11xx_acmp.cpp CMP peripheral driver (ISR plumbing, setters)
| +-- mimx11xx_acmp.h MIMX_ACMP class declaration and inline helpers
|
+-- makefile Build configuration (MODRT1171 only)
+-- ReadMe.txt This file

Build / Load

make PLATFORM=MODRT1171
make load PLATFORM=MODRT1171 DEVIP=<device-ip>

Notes

  • The output filter on ACMP2 (filtPer = 240, filtCount = 7) is a majority filter over 7 samples taken every 240 clock ticks of the filter's sampling clock. It's intended to suppress chatter near the threshold; disable it by passing 0 for either value if you want raw response.
  • Only ACMP1's DAC value is reachable from the console loop – ACMP2's threshold is set once at construction (0xA0) and not updated.
  • EnableSystemDiagnostics() is left on for convenience. Remove it from production code.