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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: ReadAnalogInput
3 *
4 * Objective:
5 * This example demonstrates how to read the analog voltage of an analog
6 * input. ClearCore analog inputs are natively compatible with 0-10V signals,
7 * and 0-20mA signals with addition of an external resistor (see ClearCore
8 * manual link below for wiring).
9 *
10 * Description:
11 * This example sets up pin A-12 as an analog input, queries the value on
12 * that connector every second, and calculates the input voltage. This
13 * calculated voltage is written/displayed to the USB serial port.
14 * Connectors IO-0 through IO-5 will act as a coarse meter of the voltage
15 * read-in by turning on their outputs and LEDs (with more than 0V and less
16 * than 2V, IO-0 will be on as an output. With ~2V and less than 4V, IO-0 and
17 * IO-1 will be on; and so on until, with near 10V read in, IO-0 through IO-5
18 * will be turned on).
19 *
20 * Requirements:
21 * ** An analog input source connected to A-12.
22 * ** Optional: LEDs connected to IO-0 through IO-5 to act as a more prominent
23 * voltage meter than the ClearCore's onboard LEDs.
24 *
25 * Links:
26 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
27 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
28 *
29 *
30 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
31 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
32 */
33
34#include "ClearCore.h"
35
36// Defines the bit-depth of the ADC readings (8-bit, 10-bit, or 12-bit)
37// Supported adcResolution values are 8, 10, and 12
38#define adcResolution 12
39
40// Select the baud rate to match the target device.
41#define baudRate 9600
42
43// Specify which serial connector to use:
44// ConnectorUsb, ConnectorCOM0, or ConnectorCOM1
45#define SerialPort ConnectorUsb
46
47int main() {
48 // Initialize the serial port for printing analog voltage readings and wait
49 // up to 5 seconds for a port to open. Serial communication is not required
50 // for this example to run, however without it only the coarse LED meter
51 // can be used to read the analog signal.
52 SerialPort.Mode(Connector::USB_CDC);
53 SerialPort.Speed(baudRate);
54 uint32_t timeout = 5000;
55 uint32_t startTime = Milliseconds();
56 SerialPort.PortOpen();
57 while (!SerialPort && Milliseconds() - startTime < timeout) {
58 continue;
59 }
60
61 // Make a voltage meter display with the I/O pins.
62 ConnectorIO0.Mode(Connector::OUTPUT_DIGITAL);
63 ConnectorIO1.Mode(Connector::OUTPUT_DIGITAL);
64 ConnectorIO2.Mode(Connector::OUTPUT_DIGITAL);
65 ConnectorIO3.Mode(Connector::OUTPUT_DIGITAL);
66 ConnectorIO4.Mode(Connector::OUTPUT_DIGITAL);
67 ConnectorIO5.Mode(Connector::OUTPUT_DIGITAL);
68
69 // Clear out the state of our voltage meter to start.
70 ConnectorIO0.State(false);
71 ConnectorIO1.State(false);
72 ConnectorIO2.State(false);
73 ConnectorIO3.State(false);
74 ConnectorIO4.State(false);
75 ConnectorIO5.State(false);
76
77 // Since analog inputs default to analog input mode, there's no need to
78 // call Mode().
79
80 // Set the resolution of the ADC.
81 AdcMgr.AdcResolution(adcResolution);
82
83 while (true) {
84 // Read the analog input (A-9 through A-12 may be configured as analog
85 // inputs).
86 int16_t adcResult = ConnectorA12.State();
87 // Convert the reading to a voltage.
88 double inputVoltage = 10.0 * adcResult / ((1 << adcResolution) - 1);
89
90 // Display the voltage reading to the serial port.
91 SerialPort.Send("A-12 input voltage: ");
92 SerialPort.Send(inputVoltage);
93 SerialPort.SendLine("V.");
94
95 // Write the voltage reading to the voltage meter display pins
96 // (IO-0 through IO-5).
97 if (inputVoltage > 0.1) {
98 ConnectorIO0.State(true);
99 }
100 else {
101 ConnectorIO0.State(false);
102 }
103 if (inputVoltage > 2.0) {
104 ConnectorIO1.State(true);
105 }
106 else {
107 ConnectorIO1.State(false);
108 }
109 if (inputVoltage > 4.0) {
110 ConnectorIO2.State(true);
111 }
112 else {
113 ConnectorIO2.State(false);
114 }
115 if (inputVoltage > 6.0) {
116 ConnectorIO3.State(true);
117 }
118 else {
119 ConnectorIO3.State(false);
120 }
121 if (inputVoltage > 8.0) {
122 ConnectorIO4.State(true);
123 }
124 else {
125 ConnectorIO4.State(false);
126 }
127 if (inputVoltage >= 9.9) {
128 ConnectorIO5.State(true);
129 }
130 else {
131 ConnectorIO5.State(false);
132 }
133
134 // Wait a second before the next reading.
135 Delay_ms(1000);
136 }
137}
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.
bool AdcResolution(uint8_t resolution)
Configure the ADC conversion resolution.
int16_t State() override
Get the connector's last majority-filtered sampled value.
int16_t State() override
Get connector's last sampled digital value.
virtual ConnectorModes Mode() override
Get the connector's operational mode.
Definition DigitalInOutAnalogOut.h:70
int16_t State() override
Get connector's last sampled value.
virtual ConnectorModes Mode() override
Get the connector's operational mode.
Definition DigitalInOutHBridge.h:124
virtual ConnectorModes Mode() override
Get the connector's operational mode.
Definition DigitalInOut.h:85
int16_t State() override
Get the connector's last majority-filtered sampled value.