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

Thread: Port Pin Edge Detection

  1. #1
    Senior Member
    Join Date
    Feb 2017
    Posts
    321

    Port Pin Edge Detection

    Hi All.

    I’m wondering if it’s possible to use the edge detect capability of T3.2 Port Pins without actually causing an interrupt. I don’t want to miss the signal edge, but I don’t need the fast response of an interrupt -- polling would be fine. Looking through the device datasheet as well as source code for pinMode() and attachInterrupt(), I don’t see it. But, I could easily be missing something.

    Thanks.
    Last edited by gfvalvo; 10-30-2019 at 06:13 PM.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,850
    Yes, this can be done.

    The trick is to configure the pin change for DMA rather than an interrupt, but then don't set up a DMA transfer. When the pin changes, it will set the PORT_PCR_ISF bit in that pin's config register. Normally the DMA controller would automatically clear this bit when the DMA transfer starts, but you can manually clear it by writing a 1 to it.

    Here's a program to demonstrate how this works.

    Code:
    // connect pins 2 & 3 together
    
    void setup() {
      while (!Serial && millis() < 4000) ; // wait for Arduino serial monitor
      Serial.println("pin change polling test");
      analogWriteFrequency(3, 51.6); // create test waveform on pin 3
      analogWrite(3, 128);
      pinMode(2, INPUT);
      CORE_PIN2_CONFIG &= ~PORT_PCR_IRQC_MASK;
      CORE_PIN2_CONFIG |= PORT_PCR_IRQC(1); // 1=rising edge detect
      Serial.println("begin");
    }
    
    elapsedMicros usec;
    
    void loop() {
      if (CORE_PIN2_CONFIG & PORT_PCR_ISF) { // check if pin changed
        CORE_PIN2_CONFIG |= PORT_PCR_ISF; // clear pin change status
        Serial.print("pin change after ");
        Serial.print(usec);
        Serial.println(" us");
        usec = 0;
      }
    }
    Looking through the device datasheet as well as source code for pinMode() and attachInterrupt(), I don’t see it.
    The details are in section 11.14.1 of the MK20DX256 reference manual, on page 227.

    https://www.pjrc.com/teensy/datasheets.html

  3. #3
    Senior Member
    Join Date
    Feb 2017
    Posts
    321
    Nice. Thanks Paul.

Posting Permissions

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