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
ComparatorIn.h@3:aee9a742ddd4, 2013-06-05 (annotated)
- Committer:
- frankvnk
- Date:
- Wed Jun 05 17:07:30 2013 +0000
- Revision:
- 3:aee9a742ddd4
- Parent:
- 2:43fd96e8687d
- Child:
- 4:f66aa1098df9
doc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
frankvnk | 1:ccac56d8f1cb | 1 | /************************************************************************************************** |
frankvnk | 1:ccac56d8f1cb | 2 | ***** ***** |
frankvnk | 1:ccac56d8f1cb | 3 | ***** Name: ComparatorIn.h ***** |
frankvnk | 1:ccac56d8f1cb | 4 | ***** Ver.: 1.0 ***** |
frankvnk | 1:ccac56d8f1cb | 5 | ***** Date: 05/06/2013 ***** |
frankvnk | 1:ccac56d8f1cb | 6 | ***** Auth: Frank Vannieuwkerke ***** |
frankvnk | 1:ccac56d8f1cb | 7 | ***** Func: library for KL25Z Comparator ***** |
frankvnk | 1:ccac56d8f1cb | 8 | ***** ***** |
frankvnk | 1:ccac56d8f1cb | 9 | **************************************************************************************************/ |
frankvnk | 1:ccac56d8f1cb | 10 | |
frankvnk | 0:e742ad3d7dac | 11 | #ifndef COMPARATORIN_H |
frankvnk | 0:e742ad3d7dac | 12 | #define COMPARATORIN_H |
frankvnk | 0:e742ad3d7dac | 13 | |
frankvnk | 0:e742ad3d7dac | 14 | /* |
frankvnk | 0:e742ad3d7dac | 15 | * Includes |
frankvnk | 0:e742ad3d7dac | 16 | */ |
frankvnk | 0:e742ad3d7dac | 17 | #include "mbed.h" |
frankvnk | 0:e742ad3d7dac | 18 | #include "pinmap.h" |
frankvnk | 0:e742ad3d7dac | 19 | |
frankvnk | 0:e742ad3d7dac | 20 | #ifndef TARGET_KL25Z |
frankvnk | 0:e742ad3d7dac | 21 | #error "Target not supported" |
frankvnk | 0:e742ad3d7dac | 22 | #endif |
frankvnk | 0:e742ad3d7dac | 23 | |
frankvnk | 0:e742ad3d7dac | 24 | /** ComparatorIn library |
frankvnk | 0:e742ad3d7dac | 25 | * |
frankvnk | 0:e742ad3d7dac | 26 | * INP/INM connection selection : |
frankvnk | 0:e742ad3d7dac | 27 | * PTC6 (IN0) CMP0_IN0 |
frankvnk | 0:e742ad3d7dac | 28 | * PTC7 (IN1) CMP0_IN1 |
frankvnk | 0:e742ad3d7dac | 29 | * PTC8 (IN2) CMP0_IN2 |
frankvnk | 0:e742ad3d7dac | 30 | * PTC9 (IN3) CMP0_IN3 |
frankvnk | 0:e742ad3d7dac | 31 | * PTE30 (IN4) CMP0_IN4 |
frankvnk | 0:e742ad3d7dac | 32 | * PTE29 (IN5) CMP0_IN5 |
frankvnk | 0:e742ad3d7dac | 33 | * Internal 6-bit DAC reference voltage = VDD (3.3V) |
frankvnk | 0:e742ad3d7dac | 34 | * Hysteresis, filter count and sample period will be initialised to these values when calling ComparatorIn: |
frankvnk | 0:e742ad3d7dac | 35 | * CMP0->CR0 = 0x00; Filter and digital hysteresis disabled |
frankvnk | 0:e742ad3d7dac | 36 | * CMP0->CR1 = 0x17; // Continuous mode, high-speed compare, unfiltered output, output pin disabled |
frankvnk | 0:e742ad3d7dac | 37 | * CMP0->FPR = 0x00; // Filter disabled |
frankvnk | 0:e742ad3d7dac | 38 | * CMP0->SCR = 0x16; // Enable rising edge interrupt and flag (flags are cleared by this write) |
frankvnk | 0:e742ad3d7dac | 39 | * CMP0->DACCR = 0xC4; // DAC enabled, Vdd is 6bit reference, threshold set to 1/16 of full-scale (0.2V) |
frankvnk | 0:e742ad3d7dac | 40 | */ |
frankvnk | 0:e742ad3d7dac | 41 | |
frankvnk | 0:e742ad3d7dac | 42 | typedef enum { |
frankvnk | 0:e742ad3d7dac | 43 | CMP0_IN0 = 0, |
frankvnk | 0:e742ad3d7dac | 44 | CMP0_IN1 = 1, |
frankvnk | 0:e742ad3d7dac | 45 | CMP0_IN2 = 2, |
frankvnk | 0:e742ad3d7dac | 46 | CMP0_IN3 = 3, |
frankvnk | 0:e742ad3d7dac | 47 | CMP0_IN4 = 4, |
frankvnk | 0:e742ad3d7dac | 48 | CMP0_IN5 = 5, |
frankvnk | 0:e742ad3d7dac | 49 | } CMPName; |
frankvnk | 0:e742ad3d7dac | 50 | |
frankvnk | 2:43fd96e8687d | 51 | /** Class to use KL25Z Comparator |
frankvnk | 2:43fd96e8687d | 52 | */ |
frankvnk | 0:e742ad3d7dac | 53 | class ComparatorIn { |
frankvnk | 0:e742ad3d7dac | 54 | |
frankvnk | 0:e742ad3d7dac | 55 | public: |
frankvnk | 0:e742ad3d7dac | 56 | |
frankvnk | 0:e742ad3d7dac | 57 | /** Create a ComparatorIn, connected to the specified pins |
frankvnk | 0:e742ad3d7dac | 58 | * |
frankvnk | 0:e742ad3d7dac | 59 | * @param pinP = positive ComparatorIn pin to connect to |
frankvnk | 0:e742ad3d7dac | 60 | * @param pinM = negative ComparatorIn pin to connect to |
frankvnk | 3:aee9a742ddd4 | 61 | * @param Valid values for pinP/pinM : PTC6, PTC7, PTC8, PTC9, PTE30, PTE29, NC |
frankvnk | 3:aee9a742ddd4 | 62 | * @param When NC is specified, the corresponding input is connected to DAC0 |
frankvnk | 0:e742ad3d7dac | 63 | * @return none |
frankvnk | 0:e742ad3d7dac | 64 | */ |
frankvnk | 0:e742ad3d7dac | 65 | ComparatorIn(PinName pinP,PinName pinM); |
frankvnk | 0:e742ad3d7dac | 66 | |
frankvnk | 0:e742ad3d7dac | 67 | /** Set the number of consecutive threshold samples |
frankvnk | 3:aee9a742ddd4 | 68 | * @param Represents the number of consecutive samples that must agree |
frankvnk | 3:aee9a742ddd4 | 69 | * @param prior to the comparator ouput filter accepting a new output state. |
frankvnk | 0:e742ad3d7dac | 70 | * @param input Unsigned char - range : 1..7 |
frankvnk | 0:e742ad3d7dac | 71 | * @return none |
frankvnk | 0:e742ad3d7dac | 72 | */ |
frankvnk | 0:e742ad3d7dac | 73 | void filter_count(unsigned char); |
frankvnk | 0:e742ad3d7dac | 74 | |
frankvnk | 0:e742ad3d7dac | 75 | /** Set the hysteresis |
frankvnk | 0:e742ad3d7dac | 76 | * |
frankvnk | 0:e742ad3d7dac | 77 | * @param input Unsigned char |
frankvnk | 3:aee9a742ddd4 | 78 | * @param hysteresis 00 5mV |
frankvnk | 3:aee9a742ddd4 | 79 | * @param 01 10mV |
frankvnk | 3:aee9a742ddd4 | 80 | * @param 10 20mV |
frankvnk | 3:aee9a742ddd4 | 81 | * @param 11 30mV |
frankvnk | 0:e742ad3d7dac | 82 | * @return none |
frankvnk | 0:e742ad3d7dac | 83 | */ |
frankvnk | 0:e742ad3d7dac | 84 | void hysteresis(unsigned char); |
frankvnk | 0:e742ad3d7dac | 85 | |
frankvnk | 0:e742ad3d7dac | 86 | /** Enable sampling mode |
frankvnk | 3:aee9a742ddd4 | 87 | * @param Cannot be set when windowing mode is enabled |
frankvnk | 0:e742ad3d7dac | 88 | * @param input Unsigned char (0 or 1) |
frankvnk | 0:e742ad3d7dac | 89 | * @return none |
frankvnk | 0:e742ad3d7dac | 90 | */ |
frankvnk | 0:e742ad3d7dac | 91 | void sample_mode(unsigned char); |
frankvnk | 0:e742ad3d7dac | 92 | |
frankvnk | 0:e742ad3d7dac | 93 | /** Enable windowing mode |
frankvnk | 3:aee9a742ddd4 | 94 | * @param Cannot be set when sampling mode is enabled |
frankvnk | 0:e742ad3d7dac | 95 | * @param input Unsigned char (0 or 1) |
frankvnk | 0:e742ad3d7dac | 96 | * @return none |
frankvnk | 0:e742ad3d7dac | 97 | */ |
frankvnk | 0:e742ad3d7dac | 98 | void window_mode(unsigned char); |
frankvnk | 0:e742ad3d7dac | 99 | |
frankvnk | 0:e742ad3d7dac | 100 | /** Enable trigger mode |
frankvnk | 3:aee9a742ddd4 | 101 | * @param CMP and DAC are configured to CMP Trigger mode when CMP_CR1[TRIGM] is set to 1. |
frankvnk | 3:aee9a742ddd4 | 102 | * @param In addition, the CMP should be enabled. If the DAC is to be used as a reference |
frankvnk | 3:aee9a742ddd4 | 103 | * @param to the CMP, it should also be enabled. |
frankvnk | 3:aee9a742ddd4 | 104 | * @param CMP Trigger mode depends on an external timer resource to periodically enable the |
frankvnk | 3:aee9a742ddd4 | 105 | * @param CMP and 6-bit DAC in order to generate a triggered compare. Upon setting TRIGM, |
frankvnk | 3:aee9a742ddd4 | 106 | * @param the CMP and DAC are placed in a standby state until an external timer resource |
frankvnk | 3:aee9a742ddd4 | 107 | * @param trigger is received. |
frankvnk | 0:e742ad3d7dac | 108 | * @param input Unsigned char (0 or 1) |
frankvnk | 0:e742ad3d7dac | 109 | * @return none |
frankvnk | 0:e742ad3d7dac | 110 | */ |
frankvnk | 0:e742ad3d7dac | 111 | void trig_mode(unsigned char); |
frankvnk | 0:e742ad3d7dac | 112 | |
frankvnk | 0:e742ad3d7dac | 113 | /** Set the power mode |
frankvnk | 3:aee9a742ddd4 | 114 | * @param 0 Low-Speed (LS) Comparison mode selected. In this mode, CMP has |
frankvnk | 3:aee9a742ddd4 | 115 | * @param slower output propagation delay and lower current consumption. |
frankvnk | 3:aee9a742ddd4 | 116 | * @param 1 High-Speed (HS) Comparison mode selected. In this mode, CMP has |
frankvnk | 3:aee9a742ddd4 | 117 | * @param faster output propagation delay and higher current consumption. |
frankvnk | 0:e742ad3d7dac | 118 | * @param input Unsigned char - 0 or 1 |
frankvnk | 0:e742ad3d7dac | 119 | * @return none |
frankvnk | 0:e742ad3d7dac | 120 | */ |
frankvnk | 0:e742ad3d7dac | 121 | void power_mode(unsigned char); |
frankvnk | 0:e742ad3d7dac | 122 | |
frankvnk | 0:e742ad3d7dac | 123 | /** Set invert mode |
frankvnk | 3:aee9a742ddd4 | 124 | * @param Allows selection of the polarity of the analog comparator function. |
frankvnk | 3:aee9a742ddd4 | 125 | * @param It is also driven to the COUT output, on both the device pin and as SCR[COUT], when OPE=0. |
frankvnk | 3:aee9a742ddd4 | 126 | * @param 0 Does not invert the comparator output. |
frankvnk | 3:aee9a742ddd4 | 127 | * @param 1 Inverts the comparator output. |
frankvnk | 0:e742ad3d7dac | 128 | * @param input Unsigned char - 0 or 1 |
frankvnk | 0:e742ad3d7dac | 129 | * @return none |
frankvnk | 0:e742ad3d7dac | 130 | */ |
frankvnk | 0:e742ad3d7dac | 131 | void invert(unsigned char); |
frankvnk | 0:e742ad3d7dac | 132 | |
frankvnk | 0:e742ad3d7dac | 133 | /** Comparator Output Select |
frankvnk | 3:aee9a742ddd4 | 134 | * @param 0 Set the filtered comparator output (CMPO) to equal COUT. |
frankvnk | 3:aee9a742ddd4 | 135 | * @param 1 Set the unfiltered comparator output (CMPO) to equal COUTA. |
frankvnk | 0:e742ad3d7dac | 136 | * @param input Unsigned char - 0 or 1 |
frankvnk | 0:e742ad3d7dac | 137 | * @return none |
frankvnk | 0:e742ad3d7dac | 138 | */ |
frankvnk | 0:e742ad3d7dac | 139 | void output_select(unsigned char); |
frankvnk | 0:e742ad3d7dac | 140 | |
frankvnk | 0:e742ad3d7dac | 141 | /** Comparator Output Pin Enable |
frankvnk | 3:aee9a742ddd4 | 142 | * @param 0 CMPO is not available on the associated CMPO output pin. |
frankvnk | 3:aee9a742ddd4 | 143 | * @param If the comparator does not own the pin, this field has no effect. |
frankvnk | 3:aee9a742ddd4 | 144 | * @param 1 CMPO is available on the associated CMPO output pin. |
frankvnk | 0:e742ad3d7dac | 145 | * |
frankvnk | 3:aee9a742ddd4 | 146 | * @param The comparator output (CMPO) is driven out on the associated CMPO |
frankvnk | 3:aee9a742ddd4 | 147 | * @param output pin if the comparator owns the pin. If the comparator does |
frankvnk | 3:aee9a742ddd4 | 148 | * @param not own the field, this bit has no effect. |
frankvnk | 0:e742ad3d7dac | 149 | * @param input Unsigned char - 0 or 1 |
frankvnk | 0:e742ad3d7dac | 150 | * @return none |
frankvnk | 0:e742ad3d7dac | 151 | */ |
frankvnk | 0:e742ad3d7dac | 152 | void output_pin_en(unsigned char); |
frankvnk | 0:e742ad3d7dac | 153 | |
frankvnk | 0:e742ad3d7dac | 154 | /** Comparator Module Enable |
frankvnk | 3:aee9a742ddd4 | 155 | * @param Enables the Analog Comparator module. When the module is not enabled, |
frankvnk | 3:aee9a742ddd4 | 156 | * @param it remains in the off state, and consumes no power. When the user |
frankvnk | 3:aee9a742ddd4 | 157 | * @param selects the same input from analog mux to the positive and negative |
frankvnk | 3:aee9a742ddd4 | 158 | * @param port, the comparator is disabled automatically. |
frankvnk | 3:aee9a742ddd4 | 159 | * @param 0 Analog Comparator is disabled. |
frankvnk | 3:aee9a742ddd4 | 160 | * @param 1 Analog Comparator is enabled. |
frankvnk | 0:e742ad3d7dac | 161 | * @param input Unsigned char - 0 or 1 |
frankvnk | 0:e742ad3d7dac | 162 | * @return none |
frankvnk | 0:e742ad3d7dac | 163 | */ |
frankvnk | 0:e742ad3d7dac | 164 | void enable(unsigned char); |
frankvnk | 0:e742ad3d7dac | 165 | |
frankvnk | 0:e742ad3d7dac | 166 | /** Set the filter sample period |
frankvnk | 3:aee9a742ddd4 | 167 | * @param Specifies the sampling period, in bus clock cycles, of the comparator |
frankvnk | 3:aee9a742ddd4 | 168 | * @param output filter, when CR1[SE]=0. Setting FILT_PER to 0x0 disables the filter. |
frankvnk | 0:e742ad3d7dac | 169 | * @param input Unsigned char - range : 0..255 |
frankvnk | 0:e742ad3d7dac | 170 | * @return none |
frankvnk | 0:e742ad3d7dac | 171 | */ |
frankvnk | 0:e742ad3d7dac | 172 | void filter_period(unsigned char); |
frankvnk | 0:e742ad3d7dac | 173 | |
frankvnk | 0:e742ad3d7dac | 174 | /** DMA Enable Control |
frankvnk | 3:aee9a742ddd4 | 175 | * @param Enables the DMA transfer triggered from the CMP module. When this field is set, |
frankvnk | 3:aee9a742ddd4 | 176 | * @param a DMA request is asserted when CFR or CFF is set. |
frankvnk | 3:aee9a742ddd4 | 177 | * @param 0 DMA is disabled. |
frankvnk | 3:aee9a742ddd4 | 178 | * @param 1 DMA is enabled. |
frankvnk | 0:e742ad3d7dac | 179 | * @param input Unsigned char - 0 or 1 |
frankvnk | 0:e742ad3d7dac | 180 | * @return none |
frankvnk | 0:e742ad3d7dac | 181 | */ |
frankvnk | 0:e742ad3d7dac | 182 | void dma_en(unsigned char); |
frankvnk | 0:e742ad3d7dac | 183 | |
frankvnk | 0:e742ad3d7dac | 184 | /** Comparator Interrupt Enable Rising |
frankvnk | 3:aee9a742ddd4 | 185 | * @param Enables the CFR interrupt from the CMP. When this field is set, |
frankvnk | 3:aee9a742ddd4 | 186 | * @param an interrupt will be asserted when CFR is set. |
frankvnk | 3:aee9a742ddd4 | 187 | * @param 0 Interrupt is disabled. |
frankvnk | 3:aee9a742ddd4 | 188 | * @param 1 Interrupt is enabled. |
frankvnk | 0:e742ad3d7dac | 189 | * @param input Unsigned char - 0 or 1 |
frankvnk | 0:e742ad3d7dac | 190 | * @return none |
frankvnk | 0:e742ad3d7dac | 191 | */ |
frankvnk | 0:e742ad3d7dac | 192 | void int_en_rising(unsigned char); |
frankvnk | 0:e742ad3d7dac | 193 | |
frankvnk | 0:e742ad3d7dac | 194 | /** Comparator Interrupt Enable Falling |
frankvnk | 3:aee9a742ddd4 | 195 | * @param Enables the CFF interrupt from the CMP. When this field is set, |
frankvnk | 3:aee9a742ddd4 | 196 | * @param an interrupt will be asserted when CFF is set. |
frankvnk | 3:aee9a742ddd4 | 197 | * @param 0 Interrupt is disabled. |
frankvnk | 3:aee9a742ddd4 | 198 | * @param 1 Interrupt is enabled. |
frankvnk | 0:e742ad3d7dac | 199 | * @param input Unsigned char - 0 or 1 |
frankvnk | 0:e742ad3d7dac | 200 | * @return none |
frankvnk | 0:e742ad3d7dac | 201 | */ |
frankvnk | 0:e742ad3d7dac | 202 | void int_en_falling(unsigned char); |
frankvnk | 0:e742ad3d7dac | 203 | |
frankvnk | 0:e742ad3d7dac | 204 | /** Analog Comparator Output |
frankvnk | 3:aee9a742ddd4 | 205 | * @param Returns the current value of the Analog Comparator output, when read. |
frankvnk | 3:aee9a742ddd4 | 206 | * @param The field is reset to 0 and will read as CR1[INV] when the Analog Comparator |
frankvnk | 3:aee9a742ddd4 | 207 | * @param module is disabled, that is, when CR1[EN] = 0. Writes to this field are ignored. |
frankvnk | 0:e742ad3d7dac | 208 | * @param none |
frankvnk | 0:e742ad3d7dac | 209 | * @returns comparator status (unsigned char) |
frankvnk | 0:e742ad3d7dac | 210 | */ |
frankvnk | 0:e742ad3d7dac | 211 | unsigned char status(void); |
frankvnk | 0:e742ad3d7dac | 212 | |
frankvnk | 0:e742ad3d7dac | 213 | /** DAC Enable |
frankvnk | 3:aee9a742ddd4 | 214 | * @param Enables the DAC. When the DAC is disabled, it is powered down to conserve power. |
frankvnk | 3:aee9a742ddd4 | 215 | * @param 0 DAC is disabled. |
frankvnk | 3:aee9a742ddd4 | 216 | * @param 1 DAC is enabled. |
frankvnk | 0:e742ad3d7dac | 217 | * @param input Unsigned char - 0 or 1 |
frankvnk | 0:e742ad3d7dac | 218 | * @return none |
frankvnk | 0:e742ad3d7dac | 219 | */ |
frankvnk | 0:e742ad3d7dac | 220 | void dac_en(unsigned char); |
frankvnk | 0:e742ad3d7dac | 221 | |
frankvnk | 0:e742ad3d7dac | 222 | /** Supply Voltage Reference Source Select |
frankvnk | 3:aee9a742ddd4 | 223 | * @param 0 - V is selected as resistor ladder network supply reference Vin1 = VREFH |
frankvnk | 3:aee9a742ddd4 | 224 | * @param 1 - V is selected as resistor ladder network supply reference Vin2 = VDD |
frankvnk | 3:aee9a742ddd4 | 225 | * @param (Use this option for the best ADC operation) |
frankvnk | 0:e742ad3d7dac | 226 | * @param input Unsigned char - 0 or 1 |
frankvnk | 0:e742ad3d7dac | 227 | * @return none |
frankvnk | 0:e742ad3d7dac | 228 | */ |
frankvnk | 0:e742ad3d7dac | 229 | void ref_source(unsigned char); |
frankvnk | 0:e742ad3d7dac | 230 | |
frankvnk | 0:e742ad3d7dac | 231 | /** Set the detection threshold level (6-bit DAC0) - DAC Output Voltage Select |
frankvnk | 3:aee9a742ddd4 | 232 | * @param Selects an output voltage from one of 64 distinct levels. |
frankvnk | 3:aee9a742ddd4 | 233 | * @param DACO = (V in /64) * (VOSEL[5:0] + 1) , so the DACO range is from V in /64 to V in . |
frankvnk | 0:e742ad3d7dac | 234 | * @param input Unsigned char - range 0..63 |
frankvnk | 0:e742ad3d7dac | 235 | * @return none |
frankvnk | 0:e742ad3d7dac | 236 | */ |
frankvnk | 0:e742ad3d7dac | 237 | void treshold(unsigned char); |
frankvnk | 0:e742ad3d7dac | 238 | |
frankvnk | 0:e742ad3d7dac | 239 | /** Pass Through Mode Enable |
frankvnk | 3:aee9a742ddd4 | 240 | * @param This bit is used to enable to MUX pass through mode. Pass through mode is always available |
frankvnk | 3:aee9a742ddd4 | 241 | * @param but for some devices this feature must be always disabled due to the lack of package pins. |
frankvnk | 3:aee9a742ddd4 | 242 | * @param 0 Pass Through Mode is disabled. |
frankvnk | 3:aee9a742ddd4 | 243 | * @param 1 Pass Through Mode is enabled. |
frankvnk | 0:e742ad3d7dac | 244 | * @param input Unsigned char - 0 or 1 |
frankvnk | 0:e742ad3d7dac | 245 | * @return none |
frankvnk | 0:e742ad3d7dac | 246 | */ |
frankvnk | 0:e742ad3d7dac | 247 | void pass_through(unsigned char); |
frankvnk | 0:e742ad3d7dac | 248 | |
frankvnk | 0:e742ad3d7dac | 249 | /** Plus Input Mux Control |
frankvnk | 3:aee9a742ddd4 | 250 | * @param Determines which input is selected for the plus input of the comparator. |
frankvnk | 3:aee9a742ddd4 | 251 | * @param For INx inputs, see CMP, DAC, and ANMUX block diagrams. |
frankvnk | 3:aee9a742ddd4 | 252 | * @param NOTE: When an inappropriate operation selects the same input for both muxes, the comparator |
frankvnk | 3:aee9a742ddd4 | 253 | * @param automatically shuts down to prevent itself from becoming a noise generator. |
frankvnk | 3:aee9a742ddd4 | 254 | * @param 000 IN0 PTC6 CMP0_IN0 |
frankvnk | 3:aee9a742ddd4 | 255 | * @param 001 IN1 PTC7 CMP0_IN1 |
frankvnk | 3:aee9a742ddd4 | 256 | * @param 010 IN2 PTC8 CMP0_IN2 |
frankvnk | 3:aee9a742ddd4 | 257 | * @param 011 IN3 PTC9 CMP0_IN3 |
frankvnk | 3:aee9a742ddd4 | 258 | * @param 100 IN4 PTE30 CMP0_IN4 |
frankvnk | 3:aee9a742ddd4 | 259 | * @param 101 IN5 PTE29 CMP0_IN5 |
frankvnk | 3:aee9a742ddd4 | 260 | * @param 110 IN6 - Bandgap reference (1V) |
frankvnk | 3:aee9a742ddd4 | 261 | * @param 111 IN7 - Internal 6-bit DAC0 |
frankvnk | 0:e742ad3d7dac | 262 | * |
frankvnk | 3:aee9a742ddd4 | 263 | * @param NOTE : When using the PMC bandgap 1V reference voltage as CMP input, ensure that |
frankvnk | 3:aee9a742ddd4 | 264 | * @param you enable the bandgap buffer by setting the PMC_REGSC[BGBE] bit. |
frankvnk | 0:e742ad3d7dac | 265 | * |
frankvnk | 0:e742ad3d7dac | 266 | * @param input Unsigned char - range 0..7 |
frankvnk | 0:e742ad3d7dac | 267 | * @return none |
frankvnk | 0:e742ad3d7dac | 268 | */ |
frankvnk | 0:e742ad3d7dac | 269 | void switch_plus(unsigned char pinP); |
frankvnk | 0:e742ad3d7dac | 270 | |
frankvnk | 0:e742ad3d7dac | 271 | /** Minus Input Mux Control |
frankvnk | 3:aee9a742ddd4 | 272 | * @param Determines which input is selected for the plus input of the comparator. |
frankvnk | 3:aee9a742ddd4 | 273 | * @param For INx inputs, see CMP, DAC, and ANMUX block diagrams. |
frankvnk | 3:aee9a742ddd4 | 274 | * @param NOTE: When an inappropriate operation selects the same input for both muxes, the comparator |
frankvnk | 3:aee9a742ddd4 | 275 | * @param automatically shuts down to prevent itself from becoming a noise generator. |
frankvnk | 3:aee9a742ddd4 | 276 | * @param 000 IN0 PTC6 CMP0_IN0 |
frankvnk | 3:aee9a742ddd4 | 277 | * @param 001 IN1 PTC7 CMP0_IN1 |
frankvnk | 3:aee9a742ddd4 | 278 | * @param 010 IN2 PTC8 CMP0_IN2 |
frankvnk | 3:aee9a742ddd4 | 279 | * @param 011 IN3 PTC9 CMP0_IN3 |
frankvnk | 3:aee9a742ddd4 | 280 | * @param 100 IN4 PTE30 CMP0_IN4 |
frankvnk | 3:aee9a742ddd4 | 281 | * @param 101 IN5 PTE29 CMP0_IN5 |
frankvnk | 3:aee9a742ddd4 | 282 | * @param 110 IN6 - Bandgap reference (1V) |
frankvnk | 3:aee9a742ddd4 | 283 | * @param 111 IN7 - Internal 6-bit DAC0 |
frankvnk | 0:e742ad3d7dac | 284 | * |
frankvnk | 3:aee9a742ddd4 | 285 | * @param NOTE : When using the PMC bandgap 1V reference voltage as CMP input, ensure that |
frankvnk | 3:aee9a742ddd4 | 286 | * @param you enable the bandgap buffer by setting the PMC_REGSC[BGBE] bit. |
frankvnk | 0:e742ad3d7dac | 287 | * |
frankvnk | 0:e742ad3d7dac | 288 | * @param input Unsigned char - range 0..7 |
frankvnk | 0:e742ad3d7dac | 289 | * @return none |
frankvnk | 0:e742ad3d7dac | 290 | */ |
frankvnk | 0:e742ad3d7dac | 291 | void switch_min(unsigned char pinM); |
frankvnk | 0:e742ad3d7dac | 292 | |
frankvnk | 0:e742ad3d7dac | 293 | /** |
frankvnk | 0:e742ad3d7dac | 294 | * Comparator ISR callback |
frankvnk | 0:e742ad3d7dac | 295 | * |
frankvnk | 0:e742ad3d7dac | 296 | * @param pointer to the user function to execute after IRQ assertion |
frankvnk | 0:e742ad3d7dac | 297 | * @return none |
frankvnk | 0:e742ad3d7dac | 298 | */ |
frankvnk | 0:e742ad3d7dac | 299 | void _callbackISR(void(*fptr)(unsigned int)); |
frankvnk | 0:e742ad3d7dac | 300 | |
frankvnk | 0:e742ad3d7dac | 301 | private: |
frankvnk | 0:e742ad3d7dac | 302 | static const PinMap PinMap_CMP[7]; |
frankvnk | 0:e742ad3d7dac | 303 | static void _cmpISR(void); |
frankvnk | 0:e742ad3d7dac | 304 | void hscmp_clear(void); |
frankvnk | 0:e742ad3d7dac | 305 | char CMPnumberP, CMPnumberM; |
frankvnk | 0:e742ad3d7dac | 306 | }; |
frankvnk | 0:e742ad3d7dac | 307 | |
frankvnk | 0:e742ad3d7dac | 308 | #endif |
frankvnk | 0:e742ad3d7dac | 309 | |
frankvnk | 0:e742ad3d7dac | 310 | |
frankvnk | 0:e742ad3d7dac | 311 |