KL25Z Comparator library
Dependents: ComparatorIn_demo TEMT6200_demo 05_comparator_demo 05_comparator_demo ... more
You are viewing an older revision! See the latest version
Homepage
KL25Z Comparator library¶
Comparator features¶
- Operational over the entire supply range
- Inputs may range from rail to rail
- Programmable hysteresis control
- Selectable interrupt on rising-edge, falling-edge, or both rising or falling edges of the comparator output
- Selectable inversion on comparator output
- Capability to produce a wide range of outputs such as:
- Sampled
- Windowed, which is ideal for certain PWM zero-crossing-detection applications
- Digitally filtered:
- Filter can be bypassed
- Can be clocked via external SAMPLE signal or scaled bus clock
- Filter can be bypassed
- Sampled
- External hysteresis can be used at the same time that the output filter is used for internal functions
- Two software selectable performance levels:
- Shorter propagation delay at the expense of higher power
- Low power, with longer propagation delay
- Shorter propagation delay at the expense of higher power
- DMA transfer support not yet implemented in this library
- A comparison event can be selected to trigger a DMA transfer
- A comparison event can be selected to trigger a DMA transfer
- Functional in all modes of operation
- The window and filter functions are not available in the following modes:
- Stop
- VLPS
- LLS
- VLLSx
Block diagram¶
Introduction¶
This library allows us to create comparator objects between different inputs.
The comparator +
and -
inputs can be routed to one out of eight reference inputs (see table).
Input selection
Pin name | # | MUX input | CMP0 | Comment |
---|---|---|---|---|
PTC6 | 000 | IN0 | CMP0_IN0 | |
PTC7 | 001 | IN1 | CMP0_IN1 | |
PTC8 | 010 | IN2 | CMP0_IN2 | |
PTC9 | 011 | IN3 | CMP0_IN3 | |
PTE30 | 100 | IN4 | CMP0_IN4 | 12-bit DAC0 |
PTE29 | 101 | IN5 | CMP0_IN5 | |
110 | IN6 | 1V internal Bandgap* | ||
NC | 111 | IN7 | internal 6-bit DAC0 | |
(table 1) |
* Not yet implemented
Using the library¶
Selecting pins on initialisation
Upon initialisation, the comparator registers are set to following values:
CMP0->CR0 = 0x00; // Filter and digital hysteresis disabled CMP0->CR1 = 0x17; // Continuous mode, high-speed compare, unfiltered output, output pin disabled CMP0->FPR = 0x00; // Filter disabled CMP0->SCR = 0x16; // Enable rising edge interrupt and flag (flags are cleared by this write) CMP0->DACCR = 0xE0; // DAC enabled, Vdd is 6bit reference, threshold set to 1/2 of full-scale (1.65V)
The library accepts two input parameters:
example:
ComparatorIn compi(PTC8, NC);
- First parameter :
+
input pin.
- Second parameter :
-
input pin.
Every pin from the 'Pin names' column in (table 1) can be selected.
notes
- IN6 (internal 1V bandgap reference) has no pin name and is not selectable on init.
However, we can use the switch_plus and switch_min functions to change the corresponding input to IN6. - A special case is the
NC
pinname. Declaring this pin will connect the internal 6-bit DAC0.
Example
Following code demonstrates interrupt callback
and polling mode
.
/*********************************************************** CODE EXAMPLE FOR AMBIENT LIGHT SENSOR ON KL25Z + Wi-Go BOARD ***********************************************************/ #include "ComparatorIn.h" #include "mbed.h" DigitalOut myled(LED1); // Blue LED (blinks at a constant rate) DigitalOut cmpled(LED2); // Green LED (lit when comparator threshold is reached) DigitalOut cmp_en (PTD5); // Op-amp enable pin on Wi-Go board ComparatorIn compi(PTC8, NC); // light sensor comparator input // Comparator callback function void cmp_ISR(unsigned int cmp_ret) { if ((cmp_ret & CMP_SCR_CFR_MASK)==CMP_SCR_CFR_MASK) { printf("<INT=Rising edge on HSCMP0>\n"); } if ((cmp_ret & CMP_SCR_CFF_MASK)==CMP_SCR_CFF_MASK) { printf("<INT=Falling edge on HSCMP0>\n "); } } int main() { cmp_en = 1; compi._callbackISR(&cmp_ISR); // Set comparator interrupt callback to cmp_ISR compi.treshold(0x20); // Set comparator threshold to 1.65V = 32 * 3.3V / 64 while(1) { myled = 1; wait(1); myled = 0; wait(0.2); if (compi.status() == 0x01) // Poll comparator output { cmpled = 0; } else { cmpled = 1; } } }
Notes
- When interrupts are enabled, we need to declare a user callback function AND initialise the pointer to this function
example:
compi._callbackISR(&cmp_ISR);
- Use of an
unsigned int
in the user declared callback function is mandatory. - The interrupt returns two possible values:
CMP_SCR_CFR_MASK
to indicate a rising interrupt (when enabled).CMP_SCR_CFF_MASK
to indicate a falling interrupt (when enabled).
- Currently, following functions are not yet implemented:
- On the fly MUX switching for
+
input (switch_plus function). - On the fly MUX switching for
-
input (switch_min function). - Connecting the comparator output to a physical pin.
- DMA transfer.
- On the fly MUX switching for