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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: ReadCCIODigitalInput
3 *
4 * Objective:
5 * This example demonstrates how to initialize a CCIO-8 Expansion Board and
6 * read from one of its inputs.
7 *
8 * Description:
9 * This example sets up COM-0 to control a CCIO-8 Expansion Board then reads
10 * the state of an input on the CCIO-8's connector 0. During operation, the
11 * state of the input is printed to the USB serial port.
12 *
13 * Requirements:
14 * ** A CCIO-8 Expansion Board powered and connected to COM-0.
15 * ** An digital input device such as a switch connected to the CCIO-8's
16 * connector 0.
17 *
18 * Links:
19 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
20 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
21 *
22 *
23 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
24 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
25 */
26
27#include "ClearCore.h"
28
29// Specify which serial to use: ConnectorUsb, ConnectorCOM0, or ConnectorCOM1.
30#define SerialPort ConnectorUsb
31
32// Specify which ClearCore serial COM port is connected to the COM IN port
33// of the CCIO-8 board: ConnectorCOM0 or ConnectorCOM1.
34#define CcioPort ConnectorCOM0
35
36// Select the baud rate to match the target serial device.
37#define baudRate 9600
38
39int main() {
40 // Set up serial communication to display CCIO-8 state.
41 SerialPort.Mode(Connector::USB_CDC);
42 SerialPort.Speed(baudRate);
43 uint32_t timeout = 5000;
44 uint32_t startTime = Milliseconds();
45 SerialPort.PortOpen();
46 while (!SerialPort && Milliseconds() - startTime < timeout) {
47 continue;
48 }
49
50 // Set up the CCIO-8 COM port.
51 CcioPort.Mode(Connector::CCIO);
52 CcioPort.PortOpen();
53
54 // Make sure the input connector is in input mode (the default for all
55 // CCIO-8 pins).
56 CcioMgr.PinByIndex(CLEARCORE_PIN_CCIOA0)->Mode(Connector::INPUT_DIGITAL);
57
58 while (true) {
59 // Read the state of the input connector
60 int16_t state = CcioMgr.PinByIndex(CLEARCORE_PIN_CCIOA0)->State();
61
62 // Display the state of the input connector.
63 SerialPort.Send("CCIOA0 Input state: ");
64 if (state) {
65 SerialPort.SendLine("ON");
66 }
67 else {
68 SerialPort.SendLine("OFF");
69 }
70
71 // Wait a second then repeat...
72 Delay_ms(1000);
73 }
74}
@ CLEARCORE_PIN_CCIOA0
CCIO-8 board 1, connector 0.
Definition SysConnectors.h:89
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.
CcioPin * PinByIndex(ClearCorePins connectorIndex)
Accessor for the individual CCIO-8 pin connectors.
int16_t State() override
In input mode, get the connector's last filtered sampled value. In output mode, get the connector's o...
virtual ConnectorModes Mode() override
Get the connector's operational mode.
Definition CcioPin.h:71