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.

ComparatorIn.h

Committer:
frankvnk
Date:
2013-06-07
Revision:
10:1b9ac2b8c788
Parent:
8:8bc9784e04c0
Child:
12:0a0648bddb98

File content as of revision 10:1b9ac2b8c788:

/**************************************************************************************************
 *****                                                                                        *****
 *****  Name: ComparatorIn.h                                                                  *****
 *****  Date: 05/06/2013                                                                      *****
 *****  Auth: Frank Vannieuwkerke                                                             *****
 *****  Func: library for KL25Z Comparator                                                    *****
 *****                                                                                        *****
 **************************************************************************************************/

#ifndef COMPARATORIN_H
#define COMPARATORIN_H
 
/*
 * Includes
 */
#include "mbed.h"
#include "pinmap.h"
 
#ifndef TARGET_KL25Z
    #error "Target not supported"
#endif

/** ComparatorIn library
*
* INP/INM connection selection :
*     PTC6  (IN0)    CMP0_IN0
*     PTC7  (IN1)    CMP0_IN1
*     PTC8  (IN2)    CMP0_IN2
*     PTC9  (IN3)    CMP0_IN3
*     PTE30 (IN4)    CMP0_IN4
*     PTE29 (IN5)    CMP0_IN5
* Internal 6-bit DAC reference voltage = VDD (3.3V)
* Hysteresis, filter count and sample period will be initialised to these values when calling ComparatorIn: 
*    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 = 0xC4;  // DAC enabled, Vdd is 6bit reference, threshold set to 1/16 of full-scale (0.2V)
*/

typedef enum {
    CMP0_IN0  =  0,
    CMP0_IN1  =  1,
    CMP0_IN2  =  2,
    CMP0_IN3  =  3,
    CMP0_IN4  =  4,
    CMP0_IN5  =  5,
} CMPName;

/** Class to use KL25Z Comparator
*/ 
class ComparatorIn {
 
public:

    /** Create a ComparatorIn, connected to the specified pins
    *
    * @param pinP = positive ComparatorIn pin to connect to
    * @param pinM = negative ComparatorIn pin to connect to
    * @param . Valid values for pinP/pinM : PTC6, PTC7, PTC8, PTC9, PTE30, PTE29, NC
    * @param . When NC is specified, the corresponding input is connected to DAC0
    * @return none
    */
    ComparatorIn(PinName pinP,PinName pinM);
    
    /** Set the number of consecutive threshold samples
    * @param . Represents the number of consecutive samples that must agree
    * @param . prior to the comparator ouput filter accepting a new output state.
    * @param input Unsigned char - range : 1..7
    * @return none
    */
    void filter_count(unsigned char);

    /** Set the hysteresis
    *
    * @param input Unsigned char
    * @param . hysteresis
    * @param . 00      5mV
    * @param . 01      10mV
    * @param . 10      20mV
    * @param . 11      30mV
    * @return none
    */
    void hysteresis(unsigned char);

    /** 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);

    /** 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);

    /** Enable trigger mode
    * @param . CMP and DAC are configured to CMP Trigger mode when CMP_CR1[TRIGM] is set to 1.
    * @param . In addition, the CMP should be enabled. If the DAC is to be used as a reference
    * @param . to the CMP, it should also be enabled.
    * @param . CMP Trigger mode depends on an external timer resource to periodically enable the
    * @param . CMP and 6-bit DAC in order to generate a triggered compare. Upon setting TRIGM,
    * @param . the CMP and DAC are placed in a standby state until an external timer resource
    * @param . trigger is received.
    * @param input Unsigned char (0 or 1)
    * @return none
    */
    void trig_mode(unsigned char);

    /** Set the power mode
    * @param . 0 Low-Speed (LS) Comparison mode selected. In this mode, CMP has
    * @param .   slower output propagation delay and lower current consumption.
    * @param . 1 High-Speed (HS) Comparison mode selected. In this mode, CMP has
    * @param .   faster output propagation delay and higher current consumption.
    * @param input Unsigned char - 0 or 1
    * @return none
    */
    void power_mode(unsigned char);

    /** Set invert mode
    * @param . Allows selection of the polarity of the analog comparator function.
    * @param . It is also driven to the COUT output, on both the device pin and as SCR[COUT], when OPE=0.
    * @param . 0 Does not invert the comparator output.
    * @param . 1 Inverts the comparator output.
    * @param input Unsigned char - 0 or 1
    * @return none
    */
    void invert(unsigned char);

    /** Comparator Output Select
    * @param . 0 Set the filtered comparator output (CMPO) to equal COUT.
    * @param . 1 Set the unfiltered comparator output (CMPO) to equal COUTA.
    * @param input Unsigned char - 0 or 1
    * @return none
    */
    void output_select(unsigned char);

    /** 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
    */
    void output_pin_en(unsigned char);

    /** Comparator Module Enable
    * @param . Enables the Analog Comparator module. When the module is not enabled,
    * @param . it remains in the off state, and consumes no power. When the user
    * @param . selects the same input from analog mux to the positive and negative
    * @param . port, the comparator is disabled automatically.
    * @param . 0 Analog Comparator is disabled.
    * @param . 1 Analog Comparator is enabled.
    * @param input Unsigned char - 0 or 1
    * @return none
    */
    void enable(unsigned char);

    /** Set the filter sample period
    * @param . Specifies the sampling period, in bus clock cycles, of the comparator
    * @param . output filter, when CR1[SE]=0. Setting FILT_PER to 0x0 disables the filter.
    * @param input Unsigned char - range : 0..255
    * @return none
    */
    void filter_period(unsigned char);

    /** DMA Enable Control
    * @param . Enables the DMA transfer triggered from the CMP module. When this field is set,
    * @param . a DMA request is asserted when CFR or CFF is set.
    * @param . 0 DMA is disabled.
    * @param . 1 DMA is enabled.
    * @param input Unsigned char - 0 or 1
    * @return none
    */
    void dma_en(unsigned char);

    /** 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
    * @return none
    */
    void int_en_rising(unsigned char);

    /** 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);

    /** Analog Comparator Output
    * @param none
    * @return . Returns the current value of the Analog Comparator output, when read.
    * @return . The field is reset to 0 and will read as CR1[INV] when the Analog Comparator
    * @return . module is disabled, that is, when CR1[EN] = 0. Writes to this field are ignored.
    * @return comparator status (unsigned char)
    */
    unsigned char status(void);

    /** DAC Enable
    * @param . Enables the DAC. When the DAC is disabled, it is powered down to conserve power.
    * @param . 0 DAC is disabled.
    * @param . 1 DAC is enabled.
    * @param input Unsigned char - 0 or 1
    * @return none
    */
    void dac_en(unsigned char);

    /** Supply Voltage Reference Source Select
    * @param . 0 - V is selected as resistor ladder network supply reference Vin1 = VREFH
    * @param . 1 - V is selected as resistor ladder network supply reference Vin2 = VDD
    * @param .     (Use this option for the best ADC operation)
    * @param input Unsigned char - 0 or 1
    * @return none
    */
    void ref_source(unsigned char);

    /** 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
    * @return none
    */
    void treshold(unsigned char);

    /** Pass Through Mode Enable
    * @param . This bit is used to enable to MUX pass through mode. Pass through mode is always available
    * @param . but for some devices this feature must be always disabled due to the lack of package pins.
    * @param . 0 Pass Through Mode is disabled.
    * @param . 1 Pass Through Mode is enabled.
    * @param input Unsigned char - 0 or 1
    * @return none
    */
    void pass_through(unsigned char);

    /** Plus Input Mux Control
    * @param . Determines which input is selected for the plus input of the comparator.
    * @param . For INx inputs, see CMP, DAC, and ANMUX block diagrams.
    * @param . NOTE: When an inappropriate operation selects the same input for both muxes, the comparator
    * @param .       automatically shuts down to prevent itself from becoming a noise generator.
    * @param . 000 IN0   PTC6    CMP0_IN0
    * @param . 001 IN1   PTC7    CMP0_IN1
    * @param . 010 IN2   PTC8    CMP0_IN2
    * @param . 011 IN3   PTC9    CMP0_IN3
    * @param . 100 IN4   PTE30   CMP0_IN4
    * @param . 101 IN5   PTE29   CMP0_IN5
    * @param . 110 IN6   -       Bandgap reference (1V)
    * @param . 111 IN7   -       Internal 6-bit DAC0
    *
    * @param . NOTE : When using the PMC bandgap 1V reference voltage as CMP input, ensure that
    * @param .        you enable the bandgap buffer by setting the PMC_REGSC[BGBE] bit.
    *
    * @param input Unsigned char - range 0..7
    * @return none
    */
    void switch_plus(unsigned char pinP);

    /** Minus Input Mux Control
    * @param . Determines which input is selected for the plus input of the comparator.
    * @param . For INx inputs, see CMP, DAC, and ANMUX block diagrams.
    * @param . NOTE: When an inappropriate operation selects the same input for both muxes, the comparator
    * @param .       automatically shuts down to prevent itself from becoming a noise generator.
    * @param . 000 IN0   PTC6    CMP0_IN0
    * @param . 001 IN1   PTC7    CMP0_IN1
    * @param . 010 IN2   PTC8    CMP0_IN2
    * @param . 011 IN3   PTC9    CMP0_IN3
    * @param . 100 IN4   PTE30   CMP0_IN4
    * @param . 101 IN5   PTE29   CMP0_IN5
    * @param . 110 IN6   -       Bandgap reference (1V)
    * @param . 111 IN7   -       Internal 6-bit DAC0
    *
    * @param . NOTE : When using the PMC bandgap 1V reference voltage as CMP input, ensure that
    * @param .        you enable the bandgap buffer by setting the PMC_REGSC[BGBE] bit.
    *
    * @param input Unsigned char - range 0..7
    * @return none
    */
    void switch_min(unsigned char pinM);

    /**
    * Comparator ISR callback
    *
    * @param pointer to the user function to execute after IRQ assertion
    * @return . CMP_SCR_CFR_MASK (rising edge interrupt)
    * @return . CMP_SCR_CFF_MASK (falling edge interrupt)
    * @return returns rising/falling interrupt flag (unsigned int) to user function
    */
    void _callbackISR(void(*fptr)(unsigned int));

private:
    static const PinMap PinMap_CMP[7];
    static void _cmpISR(void);
    void hscmp_clear(void);
    char CMPnumberP, CMPnumberM;
};
 
#endif