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.cpp

Committer:
frankvnk
Date:
2013-06-10
Revision:
12:0a0648bddb98
Parent:
11:c0ef9948278b
Child:
13:2d499824ba05

File content as of revision 12:0a0648bddb98:

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

#include "ComparatorIn.h"
 
void (*user_fptr)(unsigned int);    // Pointer to user function - called after IRQ assertion.

const PinMap ComparatorIn::PinMap_CMP[] = {
    {PTC6,  CMP0_IN0, 0},
    {PTC7,  CMP0_IN1, 0},
    {PTC8,  CMP0_IN2, 0},
    {PTC9,  CMP0_IN3, 0},
    {PTE30, CMP0_IN4, 0},       // ADC0_SE23
    {PTE29, CMP0_IN5, 0},       // ADC0_SE4b
    {NC,    NC,       0}
};
 
ComparatorIn::ComparatorIn(PinName pinP, PinName pinM)
{
    user_fptr = NULL;
    CMPnumberP = (CMPName)pinmap_peripheral(pinP, PinMap_CMP);
    if (CMPnumberP == (uint32_t)NC)         // When NC, use DAC0
        CMPnumberP = 0x07;
    CMPnumberM = (CMPName)pinmap_peripheral(pinM, PinMap_CMP);
    if (CMPnumberM == (uint32_t)NC)         // When NC, use DAC0
        CMPnumberM = 0x07;

    SIM->SCGC4 |=SIM_SCGC4_CMP_MASK; // Enable HSCMP module clock

    hscmp_clear();
    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)
    CMP0->MUXCR = (CMPnumberP << 3) | (CMPnumberM & 0x07);  // P-input/M-input are ext.channels defined by CMPnumberP/CMPnumberN

    if(CMPnumberP < 6) pinmap_pinout(pinP, PinMap_CMP); //Map pins
    if(CMPnumberM < 6) pinmap_pinout(pinM, PinMap_CMP); //Map pins
};
 
void ComparatorIn::filter_count(unsigned char fico)
{
    if((fico > 0) && (fico < 8))
    {
        unsigned char tmp;
        tmp = (CMP0->CR0 & 0x8F) | CMP_CR0_FILTER_CNT(fico);        // Replace old value
        CMP0->CR0 = tmp;                                            // Set filter count
    }
}

void ComparatorIn::hysteresis(unsigned char hyst)
{
    if(hyst < 4)
    {
        unsigned char tmp;
        tmp = (CMP0->CR0 & 0xFC) | CMP_CR0_HYSTCTR(hyst);           // Replace old value
        CMP0->CR0 = tmp;                                            // Set hysteresis
    }
}

void ComparatorIn::sample_mode(unsigned char samp_en)
{
    if((CMP0->CR1 & CMP_CR1_WE_MASK) == 0)                  // Only allow change when window mode is inactive
    {
        if(samp_en == 1) CMP0->CR1 |= CMP_CR1_SE_MASK;      // Enable
        else CMP0->CR1 &= ~CMP_CR1_SE_MASK;                 // Disable
    }
}

void ComparatorIn::window_mode(unsigned char win_en)
{
    if((CMP0->CR1 & CMP_CR1_SE_MASK) == 0)                  // Only allow change when sample mode is inactive
    {
        if(win_en == 1) CMP0->CR1 |= CMP_CR1_WE_MASK;       // Enable
        else CMP0->CR1 &= ~CMP_CR1_WE_MASK;                 // Disable
    }
}

void ComparatorIn::trig_mode(unsigned char trig_en)
{
    if(trig_en == 1) CMP0->CR1 |= CMP_CR1_TRIGM_MASK;       // Enable
    else CMP0->CR1 &= ~CMP_CR1_TRIGM_MASK;                  // Disable
}

void ComparatorIn::power_mode(unsigned char pmode)
{
    if(pmode == 1) CMP0->CR1 |= CMP_CR1_PMODE_MASK;      // Set high speed
    else CMP0->CR1 &= ~CMP_CR1_PMODE_MASK;               // Set low speed
}

void ComparatorIn::invert(unsigned char inv)
{
    if(inv == 1) CMP0->CR1 |= CMP_CR1_INV_MASK;          // Enable
    else CMP0->CR1 &= ~CMP_CR1_INV_MASK;                 // Disable
}

void ComparatorIn::output_select(unsigned char cos)
{
    if(cos == 1) CMP0->CR1 |= CMP_CR1_COS_MASK;          // Enable
    else CMP0->CR1 &= ~CMP_CR1_COS_MASK;                 // Disable
}

void ComparatorIn::output_pin_en(PinName ope)
{
    PinName pin_stat;
    pin_stat = op_status();                                  // Get pin status
    // Only change settings if new pin differs from old pin AND the correct pin is selected.
    if((ope != pin_stat) && ((ope == PTC0) || (ope == PTC5) || (ope == PTE0) || (ope == NC)))
    {
        if(ope == NC)
        {
            if (pin_stat != NC) op_disable(pin_stat);        // disconnect current pin
            CMP0->CR1 &= ~CMP_CR1_OPE_MASK;                  // Disable comparator output pin connect
        }
        else
        {
            op_enable(ope, pin_stat);                        // Connect new pin
            CMP0->CR1 &= ~CMP_CR1_OPE_MASK;                  // Enable comparator output pin connect
        }
    }
}

void ComparatorIn::enable(unsigned char en)
{
    if(en == 1) CMP0->CR1 |= CMP_CR1_EN_MASK;            // Enable
    else CMP0->CR1 &= ~CMP_CR1_EN_MASK;                  // Disable
}

void ComparatorIn::filter_period(unsigned char fipe)
{
    CMP0->FPR = CMP_FPR_FILT_PER(fipe);
}

void ComparatorIn::dma_en(unsigned char dmaen)
{
    if(dmaen == 1) CMP0->SCR |= CMP_SCR_DMAEN_MASK;      // Enable
    else CMP0->SCR &= ~CMP_SCR_DMAEN_MASK;               // Disable
}

void ComparatorIn::int_en_rising(unsigned char ier)
{
    if(ier == 1) CMP0->SCR |= CMP_SCR_IER_MASK;          // Enable
    else CMP0->SCR &= ~CMP_SCR_IER_MASK;                 // Disable
}

void ComparatorIn::int_en_falling(unsigned char ief)
{
    if(ief == 1) CMP0->SCR |= CMP_SCR_IEF_MASK;          // Enable
    else CMP0->SCR &= ~CMP_SCR_IEF_MASK;                 // Disable
}

unsigned char ComparatorIn::status(void)
{
    return (CMP0->SCR & 0x01);
}

void ComparatorIn::dac_en(unsigned char den)
{
    if(den == 1) CMP0->DACCR |= CMP_DACCR_DACEN_MASK;    // Enable
    else CMP0->DACCR &= ~CMP_DACCR_DACEN_MASK;           // Disable
}

void ComparatorIn::ref_source(unsigned char res)
{
    if(res == 1) CMP0->DACCR |= CMP_DACCR_VRSEL_MASK;    // Enable
    else CMP0->DACCR &= ~CMP_DACCR_VRSEL_MASK;           // Disable
}

void ComparatorIn::treshold(unsigned char vo_sel)
{
    if(vo_sel < 64)
    {
        unsigned char tmp;
        tmp = (CMP0->DACCR & 0xC0) | vo_sel;             // Mask old value
        CMP0->DACCR = tmp;                               // Set Vout DAC to vo_sel
    }
}

void ComparatorIn::pass_through(unsigned char ptm)
{
    if(ptm == 1) CMP0->MUXCR |= CMP_MUXCR_MSEL_MASK;     // Enable
    else CMP0->MUXCR &= ~CMP_MUXCR_MSEL_MASK;            // Disable
}

void ComparatorIn::switch_plus(unsigned char pinP)
{
}
 
void ComparatorIn::switch_min(unsigned char pinM)
{
}
 
void ComparatorIn::hscmp_clear(void)
{
  CMP0->CR0   = 0;
  CMP0->CR1   = 0;
  CMP0->FPR   = 0;
  CMP0->SCR   = 0x06;  // Clear flags if set.
  CMP0->DACCR = 0;
  CMP0->MUXCR = 0;
}

void ComparatorIn::_callbackISR(void(*fptr)(unsigned int))  
{
    NVIC_SetVector(CMP0_IRQn, (uint32_t)&_cmpISR);  // Set comparator ISR to _cmpISR routine
    NVIC_EnableIRQ(CMP0_IRQn);                      // Enable comparator ISR
    user_fptr = fptr;
}

void ComparatorIn::_cmpISR(void)  
{
    // clear interrupt flags by reading them
    //If rising edge
    if (((CMP0->SCR & CMP_SCR_IER_MASK)==CMP_SCR_IER_MASK) && ((CMP0->SCR & CMP_SCR_CFR_MASK)==CMP_SCR_CFR_MASK))
    {
        CMP0->SCR |= CMP_SCR_CFR_MASK;                              // Clear the flag
        if (user_fptr != NULL) user_fptr(CMP_SCR_CFR_MASK);         // call user function
    }

    //If falling edge
    if (((CMP0->SCR & CMP_SCR_IEF_MASK)==CMP_SCR_IEF_MASK) && ((CMP0->SCR & CMP_SCR_CFF_MASK)==CMP_SCR_CFF_MASK))
    {
        CMP0->SCR |= CMP_SCR_CFF_MASK;                              // Clear the flag
        if (user_fptr != NULL) user_fptr(CMP_SCR_CFF_MASK);         // call user function
    } 
}

/*
IMPORTANT : Do not alter the if... sequence in op_status.
            We need to check if the port is active (using SIM->SCGC5) BEFORE reading PORTn->PCR[x].
            Reading PORTn->PCR[x] while a port is inactive will block the system.
            At startup, SIM->SCGC5 = 00000380h. This means only PORTA is enabled.
*/
PinName ComparatorIn::op_status(void)
{
    if((SIM->SCGC5 & SIM_SCGC5_PORTE_MASK) == 1)
    {
        if((PORTE->PCR[0] & PORT_PCR_MUX_MASK) == 0x500u) return(PTE0);  // check if current pin = PTE0
    }
    if((SIM->SCGC5 & SIM_SCGC5_PORTC_MASK) == 1)
    {
        if((PORTC->PCR[0] & PORT_PCR_MUX_MASK) == 0x500u) return(PTC0);  // check if current pin = PTC0
        if((PORTC->PCR[5] & PORT_PCR_MUX_MASK) == 0x600u) return(PTC5);  // check if current pin = PTC5
    }
    return(NC);
}

void ComparatorIn::op_enable(PinName pen, PinName pstat)
{
    if(pstat != NC) op_disable(pstat);               // If a pin is connected - disconnect before connecting new pin
    switch (pen)
    {
        case PTC0:
            if((SIM->SCGC5 & SIM_SCGC5_PORTC_MASK) == 0) SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK;    // If PORTC is inactive: Enable
            PORTC->PCR[0] = PORT_PCR_MUX(5);                                                    // Set PTC0 mux to CMP0
            break;
        case PTC5:
            if((SIM->SCGC5 & SIM_SCGC5_PORTC_MASK) == 0) SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK;    // If PORTC is inactive: Enable
            PORTC->PCR[5] = PORT_PCR_MUX(6);                                                    // Set PTC5 mux to CMP0
            break;
        case PTE0:
            if((SIM->SCGC5 & SIM_SCGC5_PORTE_MASK) == 0) SIM->SCGC5 |= SIM_SCGC5_PORTE_MASK;    // If PORTE is inactive: Enable
            PORTE->PCR[0] = PORT_PCR_MUX(5);                                                    // Set PTE0 mux to CMP0
            break;
        default:
        break;
    }
}

void ComparatorIn::op_disable(PinName pdi)
{
    switch (pdi)
    {
        case PTC0:
            PORTC->PCR[0] &= PORT_PCR_MUX(1);             // Set PTC0 mux to ALT1
            break;
        case PTC5:
            PORTC->PCR[5] &= PORT_PCR_MUX(1);             // Set PTC5 mux to ALT1
            break;
        case PTE0:
            PORTE->PCR[0] &= PORT_PCR_MUX(1);             // Set PTE0 mux to ALT1
            break;
        default:
        break;
    }
}