|
| 1 | +/* |
| 2 | +// This file is subject to the terms and conditions defined in |
| 3 | +// file 'LICENSE.md', which is part of this source code package. |
| 4 | +*/ |
| 5 | + |
| 6 | +#define INT_PIN 16 |
| 7 | +#define AUTO_PIN D35 // connect this pin to INT_PIN for auto control |
| 8 | + |
| 9 | +rtos::Thread statusThread; |
| 10 | + |
| 11 | +volatile uint32_t count = 0; |
| 12 | +uint32_t expected_count = 0; |
| 13 | + |
| 14 | +void status_fn( void ){ |
| 15 | + while(1){ |
| 16 | + rtos::ThisThread::sleep_for(1000); |
| 17 | + printf("time (ms): %d, count: %d, expected: %d, diff: %d\n", millis(), count, expected_count, (expected_count - count)); |
| 18 | + digitalWrite(LED_BUILTIN, (digitalRead(LED_BUILTIN)) ? LOW : HIGH); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +void myISR(void){ |
| 23 | + count++; // access count because it is a global variable |
| 24 | +} |
| 25 | + |
| 26 | +void myParamISR(void *arg){ |
| 27 | + uint32_t* pcount = (uint32_t *)arg; |
| 28 | + *(pcount)++; // access count via the passed in argument |
| 29 | +} |
| 30 | + |
| 31 | +void setup() |
| 32 | +{ |
| 33 | + Serial.begin(115200); |
| 34 | + |
| 35 | + printf("Apollo3 - attachInterrupt\n\n"); |
| 36 | + |
| 37 | + pinMode(LED_BUILTIN, OUTPUT); |
| 38 | + pinMode(AUTO_PIN, OUTPUT); |
| 39 | + |
| 40 | + digitalWrite(AUTO_PIN, LOW); |
| 41 | + digitalWrite(LED_BUILTIN, LOW); |
| 42 | + |
| 43 | + pinMode(INT_PIN, INPUT_PULLUP); |
| 44 | + |
| 45 | + // interrupts can occur on several kinds of conditions |
| 46 | + attachInterrupt(INT_PIN, myISR, RISING); |
| 47 | + attachInterrupt(INT_PIN, myISR, FALLING); |
| 48 | + attachInterrupt(INT_PIN, myISR, LOW); |
| 49 | + attachInterrupt(INT_PIN, myISR, HIGH); |
| 50 | + attachInterrupt(INT_PIN, myISR, CHANGE); |
| 51 | + |
| 52 | + // there is another function that you can use to attach an interrupt with parameters |
| 53 | + // you can supply a pointer as the 'param' argument, this will be available in the |
| 54 | + // isr as the void* parameter |
| 55 | + // attachInterruptParam(INT_PIN, myParamISR, INT_MODE, void* param); |
| 56 | + attachInterruptParam(INT_PIN, myParamISR, RISING, (void*)&count); |
| 57 | + |
| 58 | + // attaching a different interrupt to the same pin overwrites the existing ISR |
| 59 | + attachInterrupt(INT_PIN, myISR, RISING); |
| 60 | + |
| 61 | + // finally you may notice that this example does not use 'digitalPinToInterrupt()' |
| 62 | + // on the Apollo3 all digital pins are interrupts and they are identified by their |
| 63 | + // normal pin numbers |
| 64 | + // you may still use 'digitalPinToInterrupt()' for compatibility with legacy code |
| 65 | + // #define digitalPinToInterrupt(P) (P) |
| 66 | + // attachInterrupt(digitalPinToInterrupt(INT_PIN), myISR, INT_MODE); |
| 67 | + // attachInterruptParam(digitalPinToInterrupt(INT_PIN), myParamISR, INT_MODE, void* param); |
| 68 | + |
| 69 | + statusThread.start(status_fn); |
| 70 | +} |
| 71 | + |
| 72 | +void loop() |
| 73 | +{ |
| 74 | + expected_count++; |
| 75 | + digitalWrite(AUTO_PIN, HIGH); |
| 76 | + digitalWrite(AUTO_PIN, LOW); |
| 77 | +} |
0 commit comments