Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

Thread: Teensy 3.0 Analog Comparator Interrupt

  1. #1

    Teensy 3.0 Analog Comparator Interrupt

    Hi all,

    I am would like to setup the analog comparator interrupt on teensy 3 but I can't seem to find any tutorials or sample codes to help me get started.

    Anyone can provide some pointers? I tried looking at the data sheet but its too difficult to understand.

    Thanks in advance.


    Cheers

  2. #2
    I was reading the datasheet and found out that the analog comparator input pins are 11 and 12 for comparator 0, and pins 23 and 9 for comparator 1.

    I need to use both comparators and also SPI at the same time. Its seems to be getting more complicated. I might need to use the other SPI on pin 7 (DOUT), 8 (DIN), and 14 (SCK). How do I reassign SPI to used these pins instead?

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    25,025
    Quote Originally Posted by Kian View Post
    How do I reassign SPI to used these pins instead?
    I haven't used the comparators, but this part I can answer....

    You can access the pin config registers with CORE_PINxx_CONFIG, where "xx" is the pin number. You can also use PORTx_PCRy, where "x" is the Freescale port letter and "y" the number.

    You'll need to refer to the manual for info...

    http://www.pjrc.com/teensy/K20P64M50SF0RM.pdf

    The config register is documented in section 11.4.1 starting on page 213. The MUX bits are the part which controls the function each pin uses. The value to write to those MUX bits comes from the big table in section 10.3.1 starting on page 193.

    For example, to make pin 7 use the SPI DOUT signal, you'd write:

    CORE_PIN7_CONFIG = PORT_PCR_MUX(2);

  4. #4
    There were some discussion about using alternate pin functions.

    CORE_PIN13_CONFIG = PORT_PCR_MUX(0);
    CORE_PIN14_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);

    Why are are doing a logic OR with PORT_PCR_DSE?

  5. #5
    Administrator Paul's Avatar
    Join Date
    Oct 2012
    Posts
    415
    The config register has many bits that control other stuff. Logical OR lets you set more bits for the other features. All 32 bits are written. Any you do not set with logical OR of those constants is written to zero. Look at the reference manual, pages 213-215, for documentation on the features available.

  6. #6
    I was fiddling around with some codes, reading on the forum about how others use ISR in their codes. I tried the codes below and the program compile without errors. However it only uploads if I press the reset pin on the Teensy and I can't monitor serial messages because it doesn't detect the serial port...

    #define CORE_PIN_CONFIG(pin) (CORE_PIN##pin##_CONFIG)
    #define pinAlt(pin, alt) {CORE_PIN_CONFIG(pin) = PORT_PCR_DSE | PORT_PCR_MUX(alt);}

    void setup()
    {
    cli();
    CMP0_CR1 = 1; // enable comparator
    CMP0_SCR = B00010100; // set triggering edge
    CMP0_MUXCR = B00010011; // set comparator input

    NVIC_ENABLE_IRQ(IRQ_CMP0);
    sei();

    pinAlt(27,0);
    pinAlt(28,0);

    Serial.begin(9600);
    delay(5000);
    Serial.println("Comparator Configured!");
    }

    void loop()
    {

    }

    void CMP0_ISR(void)
    {
    Serial.println("Triggered");
    }

  7. #7
    Anyone had any luck using the analog comparator on teensy3? I am still hoping to solve it.


    Cheers,
    Kian

  8. #8
    Member
    Join Date
    Aug 2013
    Location
    Whidbey Island
    Posts
    33

    analog comparator on teensy3

    Quote Originally Posted by Kian View Post
    Anyone had any luck using the analog comparator on teensy3? I am still hoping to solve it.


    Cheers,
    Kian
    I need the comparator for AC zero crossing, but I do not need an interrupt. I already have an ADC interrupt happenning very often, so
    I can just poll the comparator output for tripping, or better yet the trip register. So have you had any luck Kian? I am planning on testing now,
    starting with your code and taking out the interrupt. I'll post what I find.

    Tim

  9. #9
    Nope. I had no luck on that. Gave up trying. Sorry! Hope someone manages to pick up on this and can solve the problem.

  10. #10
    Member
    Join Date
    Aug 2013
    Location
    Whidbey Island
    Posts
    33
    Hey Kian:
    I've had similar results with the following code.
    int led = 13; // name the LED pin
    int inCompNeg = 12; // neg comparator input
    int inCompPos = 11; // pos comparator input
    bool debugMode = false;


    void setup() {
    pinMode(led, OUTPUT); // name LED output
    pinMode(inCompNeg, INPUT); // name comparator neg in
    pinMode(inCompPos, INPUT);

    Serial.begin(9600);
    delay(7000); // wait for serial monitor to open

    // setup comparator CMP0 on pins 11 and 12 (CMP0 output is CMP0_OUT) PAGE 633 onward in ref manual
    // PTC6 and PTC7 default as Comp 0 inputs
    // setup for no filter, clock or hyst
    if (debugMode == true)
    {
    CMP0_CR0 = B00000000; //cmp control reg equals 0 at start, leave
    CMP0_CR1 = B00000011; //also 0 reset, set to CMP output pin and comp module enable
    CMP0_SCR = B00010000; // read LSB for analog comparator output state
    CMP0_MUXCR = B00000001; // Input pins select = IN0 = plus and IN1 = neg

    // now select DAC set at Vdd/2 as input to CMP- input
    CMP0_DACCR = B10011111; // enable DAC, use ref as Vin1in and set output voltage to Vin/2
    }
    Serial.println("Comparator Configured");
    }

    void loop()
    {
    // now trigger comparator and print trigger
    digitalWrite(led, HIGH); // turn the LED off by making the voltage LOW

    if (debugMode == true)
    {
    if (CMP0_SCR > 16) // if pos trig
    {
    Serial.println("Comp postive trigger");
    delay(500);
    }
    else
    {
    // not trig
    Serial.println("Comp not trigger");
    }
    }
    else Serial.println("debugMode = false; to ...");
    delay(1000);
    digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
    delay(500);
    }
    If the program is run, it works fine, will print to Serial Monitor, but does not config the comparator if the bool debugMode = false.

    If that statement is changed to bool debugMode = true;
    the program will not upload without a T3 switch press, and then the Serial Monitor fails with the message:
    processing.app.SerialNotFoundException: Serial port 'COM11' not found. Did you select the right one from the Tools > Serial Port menu?
    The image below shows the Windows Device Manager with the correct COM11 "USB Serial )Communications Class, Abstract Control Module) COM11.

    This code was taken by studying the Freescale Ref Manual from page 633 onward, and using the native Freescale language.

    Maybe Paul can help with why the comparator language is choking the Arduino software?

    Thanks for reading,
    Tim

    P.S. Please disregard this image, I'm unable to delete, and it's just wrong!!
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	Capture.PNG 
Views:	287 
Size:	151.1 KB 
ID:	818  
    Last edited by offgrid; 09-04-2013 at 01:51 AM. Reason: clarity and removed wrong stuff!

  11. #11
    Member
    Join Date
    Aug 2013
    Location
    Whidbey Island
    Posts
    33
    I'm really needing the internal comparator (OK I'm cheap and don't want to buy another IC!), so I looked into this problem again.
    I've narrowed the problem down to one line in the code, the comparator 0 control register configuration code:
    CMP0_CR0 = 0x00;


    #include "mk20dx128.h"
    // actual defines from the header file "mk20dx128.h"

    //#define CMP0_CR0 *(volatile uint8_t *)0x40073000 // CMP Control Register 0

    int led = 2; // name the LED pin for scope connection

    void setup() {
    pinMode(led, OUTPUT); // name LED output

    Serial.begin(9600);

    // Set CMP0 to a known state
    //CMP0_CR1 = 0x00;
    //CMP0_CR0 = 0x00;

    Serial.println("Comparator 0 Configured");
    }

    void loop()
    {
    // now trigger comparator and print trigger
    digitalWrite(led, HIGH); // turn the LED on
    delay(1000);
    Serial.println("Comparator 0 running?");
    digitalWrite(led, LOW); // turn the LED off
    delay(1000);
    }
    This code as-is compiles and runs fine, and prints a stupid "Comparator 0 running?" every other second.
    When either "// Set CMP0 to a known state" code lines are uncommented, the code compiles but locks up the serial port.
    I've checked that the register addresses are correct, they are. Also looked at some app notes and found this in Freescale Semiconductor Document Number:AN4734
    Application Note:
    4.2 Single-Channel, 6-bit DAC CMP configuration and operation
    This example takes advantage of the trigger mode feature of the Kinetis L Family comparator. Thus, the CMP is configured
    in the initialize_CMP function before entering the Run_RAM_Loop function. The first step in the initialize_CMP function is
    to clear the CMP control registers 1 and 2. This places the CMP in a known (disabled) state.
    // Set CMP0_CR1 to a known state
    CMP0_CR1 = 0x00;

    // Set CMP Filter Count and Hysteresis control to 0.
    // Filter should be disabled to allow for low lag time.
    CMP0_CR0 = 0x00;
    So my question is...why is teensy (or Arduino?) bombing on the Comparator setup, and the bigger question is,
    will it be possible to have some library function in Teensy for this important function?

    Thanks
    Tim

  12. #12
    Junior Member
    Join Date
    Jun 2018
    Posts
    3
    Quote Originally Posted by offgrid View Post
    I'm really needing the internal comparator (OK I'm cheap and don't want to buy another IC!), so I looked into this problem again.
    I've narrowed the problem down to one line in the code, the comparator 0 control register configuration code:
    CMP0_CR0 = 0x00;




    This code as-is compiles and runs fine, and prints a stupid "Comparator 0 running?" every other second.
    When either "// Set CMP0 to a known state" code lines are uncommented, the code compiles but locks up the serial port.
    I've checked that the register addresses are correct, they are. Also looked at some app notes and found this in Freescale Semiconductor Document Number:AN4734
    Application Note:


    So my question is...why is teensy (or Arduino?) bombing on the Comparator setup, and the bigger question is,
    will it be possible to have some library function in Teensy for this important function?

    Thanks
    Tim
    Hi,


    I know I'm a few years late to this post, but did you find a solution to this issue? I'm using all three comparators on a Teensy 3.6 in windowing mode. When I try to run the following setup code, the system fails (no serial, PWM, etc):

    /*setup comparators for windowed mode
    under this mode, all that has to be done is set the window bit, then wait
    for the interrupt. */
    CMP0_CR1 |= 0b00010001; //set high speed and power on, window off
    CMP1_CR1 |= 0b00010001;
    CMP2_CR1 |= 0b00010001;

    CMP0_SCR |= 0b00010000; //enable interrrupts on rising edge
    CMP1_SCR |= 0b00010000;
    CMP2_SCR |= 0b00010000;

    CMP0_MUXCR = 1; //set + to pin 11 (IN0), - to pin 12 (IN1)
    CMP1_MUXCR = 1; //set others appropriately
    CMP2_MUXCR = 1;

    Please let me know if you've had any luck.

    -Grant

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •