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
  • 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
  • DMA transfer support not yet implemented in this library
    • 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

/media/uploads/frankvnk/kl25z_comparator_block_diagram.jpg

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 inputCMP0Comment
PTC6000IN0CMP0_IN0
PTC7001IN1CMP0_IN1
PTC8010IN2CMP0_IN2
PTC9011IN3CMP0_IN3
PTE30100IN4CMP0_IN412-bit DAC0
PTE29101IN5CMP0_IN5
110IN61V internal Bandgap*
NC111IN7internal 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.
Revision:
12:0a0648bddb98
Parent:
10:1b9ac2b8c788
Child:
13:2d499824ba05
--- a/ComparatorIn.h	Sun Jun 09 14:45:25 2013 +0000
+++ b/ComparatorIn.h	Mon Jun 10 18:44:53 2013 +0000
@@ -69,7 +69,7 @@
     * @param input Unsigned char - range : 1..7
     * @return none
     */
-    void filter_count(unsigned char);
+    void filter_count(unsigned char fico);
 
     /** Set the hysteresis
     *
@@ -81,21 +81,21 @@
     * @param . 11      30mV
     * @return none
     */
-    void hysteresis(unsigned char);
+    void hysteresis(unsigned char hyst);
 
     /** Enable sampling mode
     * @param . Cannot be set when windowing mode is enabled
     * @param input Unsigned char (0 or 1)
     * @return none
     */
-    void sample_mode(unsigned char);
+    void sample_mode(unsigned char samp_en);
 
     /** Enable windowing mode
     * @param . Cannot be set when sampling mode is enabled
     * @param input Unsigned char (0 or 1)
     * @return none
     */
-    void window_mode(unsigned char);
+    void window_mode(unsigned char win_en);
 
     /** Enable trigger mode
     * @param . CMP and DAC are configured to CMP Trigger mode when CMP_CR1[TRIGM] is set to 1.
@@ -108,7 +108,7 @@
     * @param input Unsigned char (0 or 1)
     * @return none
     */
-    void trig_mode(unsigned char);
+    void trig_mode(unsigned char trig_en);
 
     /** Set the power mode
     * @param . 0 Low-Speed (LS) Comparison mode selected. In this mode, CMP has
@@ -118,7 +118,7 @@
     * @param input Unsigned char - 0 or 1
     * @return none
     */
-    void power_mode(unsigned char);
+    void power_mode(unsigned char pmode);
 
     /** Set invert mode
     * @param . Allows selection of the polarity of the analog comparator function.
@@ -128,7 +128,7 @@
     * @param input Unsigned char - 0 or 1
     * @return none
     */
-    void invert(unsigned char);
+    void invert(unsigned char inv);
 
     /** Comparator Output Select
     * @param . 0 Set the filtered comparator output (CMPO) to equal COUT.
@@ -136,20 +136,17 @@
     * @param input Unsigned char - 0 or 1
     * @return none
     */
-    void output_select(unsigned char);
+    void output_select(unsigned char cos);
 
-    /** Comparator Output Pin Enable
-    * @param . 0 CMPO is not available on the associated CMPO output pin.
-    * @param .   If the comparator does not own the pin, this field has no effect.
-    * @param . 1 CMPO is available on the associated CMPO output pin.
-    *
-    * @param . The comparator output (CMPO) is driven out on the associated CMPO
-    * @param . output pin if the comparator owns the pin. If the comparator does
-    * @param . not own the field, this bit has no effect.
-    * @param input Unsigned char - 0 or 1
-    * @return none
+    /** Connect the comparator Output Pin to an external pin
+    * @param input NC   disconnect CMPO from the associated CMPO output pin.
+    * @param input PTC0 connect CMPO to PTC0.
+    * @param input PTC5 connect CMPO to PTC5.
+    * @param input PTE0 connect CMPO to PTE0.
+    * @param . Only one pin can be connected at any time.
+    * @param . Each time this function is called, the last pin will be disabled before the new pin is enabled
     */
-    void output_pin_en(unsigned char);
+    void output_pin_en(PinName ope);
 
     /** Comparator Module Enable
     * @param . Enables the Analog Comparator module. When the module is not enabled,
@@ -161,7 +158,7 @@
     * @param input Unsigned char - 0 or 1
     * @return none
     */
-    void enable(unsigned char);
+    void enable(unsigned char en);
 
     /** Set the filter sample period
     * @param . Specifies the sampling period, in bus clock cycles, of the comparator
@@ -169,7 +166,7 @@
     * @param input Unsigned char - range : 0..255
     * @return none
     */
-    void filter_period(unsigned char);
+    void filter_period(unsigned char fipe);
 
     /** DMA Enable Control
     * @param . Enables the DMA transfer triggered from the CMP module. When this field is set,
@@ -179,7 +176,7 @@
     * @param input Unsigned char - 0 or 1
     * @return none
     */
-    void dma_en(unsigned char);
+    void dma_en(unsigned char dmaen);
 
     /** Comparator Enable Rising Interrupt
     * @param . Enables the CFR interrupt from the CMP. When this field is set,
@@ -189,7 +186,7 @@
     * @param input Unsigned char - 0 or 1
     * @return none
     */
-    void int_en_rising(unsigned char);
+    void int_en_rising(unsigned char ier);
 
     /** Comparator Enable Falling Interrupt
     * @param . Enables the CFF interrupt from the CMP. When this field is set,
@@ -199,7 +196,7 @@
     * @param input Unsigned char - 0 or 1
     * @return none
     */
-    void int_en_falling(unsigned char);
+    void int_en_falling(unsigned char ief);
 
     /** Analog Comparator Output
     * @param none
@@ -217,7 +214,7 @@
     * @param input Unsigned char - 0 or 1
     * @return none
     */
-    void dac_en(unsigned char);
+    void dac_en(unsigned char den);
 
     /** Supply Voltage Reference Source Select
     * @param . 0 - V is selected as resistor ladder network supply reference Vin1 = VREFH
@@ -226,7 +223,7 @@
     * @param input Unsigned char - 0 or 1
     * @return none
     */
-    void ref_source(unsigned char);
+    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.
@@ -234,7 +231,7 @@
     * @param input Unsigned char - range 0..63
     * @return none
     */
-    void treshold(unsigned char);
+    void treshold(unsigned char vo_sel);
 
     /** Pass Through Mode Enable
     * @param . This bit is used to enable to MUX pass through mode. Pass through mode is always available
@@ -244,7 +241,7 @@
     * @param input Unsigned char - 0 or 1
     * @return none
     */
-    void pass_through(unsigned char);
+    void pass_through(unsigned char ptm);
 
     /** Plus Input Mux Control
     * @param . Determines which input is selected for the plus input of the comparator.
@@ -304,6 +301,9 @@
     static const PinMap PinMap_CMP[7];
     static void _cmpISR(void);
     void hscmp_clear(void);
+    PinName op_status(void);
+    void op_enable(PinName pen, PinName pstat);
+    void op_disable(PinName pdi);
     char CMPnumberP, CMPnumberM;
 };