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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: ReadDigitalInputRiseFall
3 *
4 * Objective:
5 * This example demonstrates how to read the transition state (risen or
6 * fallen since last checked) of a ClearCore digital input.
7 *
8 * Description:
9 * This example repeatedly reads the transition state of a defined digital
10 * input. Information on how the input state has transitioned is printed
11 * to the USB serial port every 2 seconds.
12 *
13 * Requirements:
14 * ** An input device, such as a switch or sensor, connected to DI-6.
15 *
16 * Links:
17 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
18 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
19 *
20 *
21 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
22 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
23 */
24
25#include "ClearCore.h"
26
27// Specify which input connector to use.
28// ConnectorIO0 through ConnectorA12 all have digital input capability.
29#define InputConnector ConnectorDI6
30
31// Declares two boolean variables used to hold information on whether the input
32// has risen or fallen
33bool risen, fallen;
34
35// Select the baud rate to match the target serial device
36#define baudRate 9600
37
38// Specify which serial to use: ConnectorUsb, ConnectorCOM0, or ConnectorCOM1.
39#define SerialPort ConnectorUsb
40
41int main() {
42
43 InputConnector.Mode(Connector::INPUT_DIGITAL); //Set the input as a digital Input
44
45 // Set up serial communication at a baud rate of 9600 bps then wait up to
46 // 5 seconds for a port to open.
47 // Serial communication is not required for this example to run, however the
48 // example will appear to do nothing without serial output.
49 SerialPort.Mode(Connector::USB_CDC);
50 SerialPort.Speed(baudRate);
51 uint32_t timeout = 5000;
52 uint32_t startTime = Milliseconds();
53 SerialPort.PortOpen();
54 while (!SerialPort && Milliseconds() - startTime < timeout) {
55 continue;
56 }
57
58 while (true) {
59 // Check whether the digital input has risen, fallen, or is unchanged
60 // since the last time we checked.
61 risen = InputConnector.InputRisen();
62 fallen = InputConnector.InputFallen();
63
64 SerialPort.Send("DI-6 Transitions: ");
65
66 if (risen && fallen) {
67 SerialPort.SendLine("RISEN and FALLEN");
68 }
69 else if (risen) {
70 SerialPort.SendLine("RISEN");
71 }
72 else if (fallen) {
73 SerialPort.SendLine("FALLEN");
74 }
75 else {
76 SerialPort.SendLine("NO CHANGE");
77 }
78
79 // Wait a couple seconds then repeat...
80 Delay_ms(2000);
81 }
82}
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.