Port Pin Edge Detection

Status
Not open for further replies.

gfvalvo

Well-known member
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:
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
 
Status
Not open for further replies.
Back
Top