You'll want to take a look at chapter 32 of the MK20DX256 reference manual:
The comparator (CMP) module provides a circuit for comparing two analog input
voltages. The comparator circuit is designed to operate across the full range of the supply
voltage, known as rail-to-rail operation.
The Analog MUX (ANMUX) provides a circuit for selecting an analog input signal from
eight channels. One signal is provided by the 6-bit digital-to-analog converter (DAC).
The mux circuit is designed to operate across the full range of the supply voltage.
For a comparator, do I need to create the signal to compare to with a resistor divider?
The chip contains one internal DAC for use with the comparator, so you can use that or an external voltage divider or signal:
The 6-bit DAC is 64-tap resistor ladder network which provides a selectable voltage
reference for applications where voltage reference is needed.
I thought there might be prebuilt libraries for the Arduino IDE, but had trouble finding the right thing.
Unfortunately, there is no comparator support included in the Teensyduino libraries (yet), but I did find this fork of the analogComp library that supports the Teensy 3.2 comparators.
https://github.com/orangkucing/analogComp
I haven't tried it yet, but it looks like a great place to start.
How do I know which teensy pins correspond to the comparator input signal? Could I set it digitally in some way in the MCU registers, avoiding the need for more circuit parts?
Luckily, the analogComp library has good documentation, including a table mapping from K20 comparator input pins to Teensy pins. The analog MUX module allows you to select the comparator inputs via MCU registers (the analogComp library handles that for you).
analogComparator (CMP0):
- 0. CMP0_IN0 (K20Pin 51 = TeensyPin 11)
- 1. CMP0_IN1 (K20Pin 52 = TeensyPin 12)
- 2. CMP0_IN2 (K20Pin 53 = TeensyPin 28)
- 3. CMP0_IN3 (K20Pin 54 = TeensyPin 27)
- 4. CMP0_IN4 (K20Pin 55 = TeensyPin 29)
- 5. VREF Output/CMP0_IN5 (K20Pin 17)
- 6. Bandgap
- 7. 6b DAC0 Reference
analogComparator1 (CMP1):
- 0. CMP1_IN0 (K20Pin 45 = TeensyPin 23)
- 1. CMP1_IN1 (K20Pin 46 = TeensyPin 9)
- 3. 12-bit DAC0_OUT/CMP1_IN3 (K20Pin 18 = TeensyPin 40/A14/DAC (Teensy 3.[12] only))
- 5. VREF Output/CMP1_IN5 (K20Pin 17)
- 6. Bandgap
- 7. 6b DAC1 Reference
analogComparator2 (CMP2 Teensy 3.[12] only):
- 0. CMP2_IN0 (K20Pin 28 = TeensyPin 3)
- 1. CMP2_IN1 (K20Pin 29 = TeensyPin 4)
- 6. Bandgap
- 7. 6b DAC2 Reference
Does the output of the comparator come from a certain pin?
Yes, it can. You'd have to write that code yourself though, since that's not currently supported in the analogComp library. Look for COMPO
, CMP0_OUT
, CMP1_OUT
, and CMP2_OUT
in the reference manual.
Each of the three comparators has only one output pin mapping available (I'm continuing the table here):
- CMP0_OUT (K20Pin 50 = TeensyPin 13)
- CMP1_OUT (K20Pin 49 = TeensyPin 10)
- CMP2_OUT (K20Pin 27 = TeensyPin 24)
Can I set it up to cause in interrupt instead and not use that pin, so that it executes code in the program and thats it?
Yes, that's the intended operation. The analogComp library makes that easy:
analogComparator.enableInterrupt(myFunction[, event]);