KL25Z Comparator library
Dependents: ComparatorIn_demo TEMT6200_demo 05_comparator_demo 05_comparator_demo ... more
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 = 0x06; // Disable all interrupts and clear flags (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 name' 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 SwitchPlus and SwitchMin functions to change the corresponding input to IN6. - There are two special cases for the input parameters:
NC
: Connect the internal 6-bit DAC0 to IN7.PTE30
: configures PTE30 as 12-bit DAC0 output and connect to IN4.
IMPORTANT: Make sure no external output is connected to PTE30 when you declare this pin, otherwise you WILL destroy the external device and/or the CPU.
Example
Following code demonstrates interrupt callback
and polling mode
.
Note: Polling the comparator this way won't always trigger the printf because of the wait() instructions (if we go above and below the threshold during the wait() time, then the polling cannot not see this change).
/*********************************************************** CODE EXAMPLE FOR AMBIENT LIGHT SENSOR ON KL25Z + Wi-Go BOARD ***********************************************************/ #include "ComparatorIn.h" #include "mbed.h" DigitalOut blinker(LED_BLUE); DigitalOut cmpled(LED_GREEN); DigitalOut cmp_en (PTD5); AnalogIn cmp_lvl (PTB0); ComparatorIn compi(PTC8, NC); // in+ = PTC8, in- = internal 6-bit DAC // Comparator callback functions void cmp_rise_ISR(void) { cmpled = 0; } void cmp_fall_ISR(void) { cmpled = 1; } int main() { cmp_en = 1; cmpled = 1; compi.rising(&cmp_rise_ISR); // Set pointer to rising interrupt function compi.falling(&cmp_fall_ISR); // Set pointer to falling interrupt function compi.treshold(0.5); // Set comparator threshold to 1.65V = 32 * 3.3V / 64 while(1) { printf("Light sensor : %7.5f Volt\n",cmp_lvl*3.3); blinker = 1; wait(1); blinker = 0; wait(0.2); if (compi.status() == 0x01) { printf("*** Treshold reached : %7.5f\n",cmp_lvl*3.3); } } }
Notes
- When we enable the comparator interrupt, we need to declare user callback function(s) (rising and/or falling interrupt) AND initialise the function pointer(s).
example:
compi.rising(&cmp_rise_ISR); compi.falling(&cmp_fall_ISR);
- Currently, following functions are not yet implemented:
- On the fly MUX switching for
+
input (SwitchPlus function). - On the fly MUX switching for
-
input (SwitchMin function). - DMA transfer.
- On the fly MUX switching for
Diff: ComparatorIn.h
- Revision:
- 13:2d499824ba05
- Parent:
- 12:0a0648bddb98
- Child:
- 14:875486333dc2
--- a/ComparatorIn.h Mon Jun 10 18:44:53 2013 +0000 +++ b/ComparatorIn.h Sat Jun 15 19:34:16 2013 +0000 @@ -34,10 +34,15 @@ * 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->SCR = 0x06; // Disable all interrupts and clear flags (flags are cleared by this write) * CMP0->DACCR = 0xC4; // DAC enabled, Vdd is 6bit reference, threshold set to 1/16 of full-scale (0.2V) */ +#define DISABLE 0 +#define RISING 1 +#define FALLING 2 +#define BOTH 3 + typedef enum { CMP0_IN0 = 0, CMP0_IN1 = 1, @@ -178,25 +183,16 @@ */ void dma_en(unsigned char dmaen); - /** Comparator Enable Rising Interrupt - * @param . Enables the CFR interrupt from the CMP. When this field is set, - * @param . an interrupt will be asserted when CFR is set. - * @param . 0 Interrupt is disabled. - * @param . 1 Interrupt is enabled. - * @param input Unsigned char - 0 or 1 + /** Comparator set Interrupt + * @param . Enables the CFR/CFF interrupt from the CMP. When this field is set, + * @param . an interrupt will be asserted when CFR/CFF is set. + * @param input RISING enable rising interrupt. + * @param input FALLING enable falling interrupt. + * @param input RISING enable rising and falling interrupts. + * @param input DISABLE no interrupts. * @return none */ - void int_en_rising(unsigned char ier); - - /** Comparator Enable Falling Interrupt - * @param . Enables the CFF interrupt from the CMP. When this field is set, - * @param . an interrupt will be asserted when CFF is set. - * @param . 0 Interrupt is disabled. - * @param . 1 Interrupt is enabled. - * @param input Unsigned char - 0 or 1 - * @return none - */ - void int_en_falling(unsigned char ief); + void int_en(unsigned char ien); /** Analog Comparator Output * @param none @@ -226,12 +222,12 @@ void ref_source(unsigned char res); /** Set the detection threshold level (6-bit DAC0) - DAC Output Voltage Select - * @param . Selects an output voltage from one of 64 distinct levels. - * @param . DACO = (V in /64) * (VOSEL[5:0] + 1) , so the DACO range is from V in /64 to V in . - * @param input Unsigned char - range 0..63 + * @param . Sets DAC output voltage. + * @param . 6-bit DACO = (V in /64) * (VOSEL[5:0] + 1) , so the DACO range is from V in /64 to V in . + * @param input float - range 0.0 .. 1.0 * @return none */ - void treshold(unsigned char vo_sel); + void treshold(float vo_pct); /** Pass Through Mode Enable * @param . This bit is used to enable to MUX pass through mode. Pass through mode is always available @@ -304,6 +300,7 @@ PinName op_status(void); void op_enable(PinName pen, PinName pstat); void op_disable(PinName pdi); + void dac6_write(unsigned int value); char CMPnumberP, CMPnumberM; };