ClearCore Library
Loading...
Searching...
No Matches
InputInterrupts.cpp

Return to SDK Examples for Microchip Studio

1/*
2 * Title: InputInterrupts
3 *
4 * Objective:
5 * This example demonstrates how to configure a digital interrupt on a
6 * ClearCore input.
7 *
8 * Description:
9 * This example sets up and attaches a callback function to be triggered by
10 * a digital interrupt. Interrupts are useful when a function needs to be
11 * called asynchronously from the rest of the code's execution.
12 *
13 * This example's interrupt blinks the on-board user LED when the interrupt
14 * pin goes from on to off (aka "falling"). You may notice multiple blinks
15 * depending on how much bounce the input device has.
16 *
17 * The interrupt callback function's ability to run is turned on and off
18 * periodically by this example. The callback function can only run when
19 * interrupts are turned "on", regardless of the interrupt pin state. If the
20 * interrupt pin is triggered while interrupts are "off", the callback will
21 * execute when interrupts are next turned on. This on/off state is printed
22 * to the USB serial port.
23 *
24 * Requirements:
25 * ** A digital signal source, such as a switch or sensor, connected to DI-6 to
26 * trigger the interrupt
27 *
28 * Links:
29 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
30 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
31 *
32 *
33 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
34 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
35 */
36
37#include "ClearCore.h"
38
39// Connectors that support digital interrupts are:
40// DI-6, DI-7, DI-8, A-9, A-10, A-11, A-12
41#define interruptConnector ConnectorDI6
42
43// Select the baud rate to match the target serial device
44#define baudRate 9600
45
46// Specify the serial connector to use: ConnectorUsb, ConnectorCOM0, or
47// ConnectorCOM1
48#define SerialPort ConnectorUsb
49
50// Declare the signature for our interrupt service routine (ISR). The function
51// is defined below
52void MyCallback();
53
54int main() {
55 // Set up the interrupt connector in digital input mode.
56 interruptConnector.Mode(Connector::INPUT_DIGITAL);
57
58 // Set an ISR to be called when the state of the interrupt pin goes from
59 // true to false.
60 interruptConnector.InterruptHandlerSet(MyCallback, InputManager::FALLING,
61 false);
62
63 // Set up serial communication and wait up to 5 seconds for a port to open
64 // Serial communication is not required for this example to run.
65 SerialPort.Mode(Connector::USB_CDC);
66 SerialPort.Speed(baudRate);
67 uint32_t timeout = 5000;
68 uint32_t startTime = Milliseconds();
69 SerialPort.PortOpen();
70 while (!SerialPort && Milliseconds() - startTime < timeout) {
71 continue;
72 }
73
74 while (true) {
75 // Enable digital interrupts.
76 interruptConnector.InterruptEnable(true);
77
78 SerialPort.SendLine("Interrupts are turned on.");
79
80 // Test that the ISR is triggered when the state of the interrupt connector
81 // transitions from true to false by toggling your switch.
82
83 // Wait while the interrupt is triggered.
84 Delay_ms(5000);
85
86 // Disable digital interrupts.
87 interruptConnector.InterruptEnable(false);
88
89 SerialPort.SendLine("Interrupts are turned off.");
90
91 // Test that the ISR does not get triggered when the state of the interrupt
92 // connector transitions from true to false by toggling your switch.
93 Delay_ms(5000);
94 }
95}
96
97// The function to be triggered on an interrupt.
98// This function blinks the user-controlled LED once.
99/*------------------------------------------------------------------------------
100 * MyCallback
101 *
102 * Flashes the ClearCore's built-in LED (next to the USB port) on and off.
103 *
104 * Parameters:
105 * None
106 *
107 * Returns: None
108 */
109void MyCallback() {
110 ConnectorLed.State(true);
111 Delay_ms(100);
112 ConnectorLed.State(false);
113 Delay_ms(100);
114}
115//------------------------------------------------------------------------------
void Delay_ms(uint32_t ms)
Blocks operations for ms milliseconds.
Definition SysTiming.h:287
uint32_t Milliseconds(void)
Number of milliseconds since the ClearCore was initialized.
int16_t State() override
Get LED's last sampled state.
LedDriver ConnectorLed
User-driven LED instance.