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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: WritePwmOutput
3 *
4 * Objective:
5 * This example demonstrates how to write a digital PWM signal to a ClearCore
6 * digital output.
7 *
8 * Description:
9 * This example sets the defined pin as an output then writes a series of
10 * PWM signals with varying duty cycles to the output.
11 *
12 * Requirements:
13 * ** Connect a device that takes in a PWM signal to IO-1.
14 *
15 * Links:
16 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
17 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
18 *
19 *
20 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
21 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
22 */
23
24#include "ClearCore.h"
25
26// Specify which output pin to write digital PWM to.
27// PWM-capable pins: IO-0 through IO-5.
28// Note: IO-4 and IO-5 are capable of bi-directional and higher-current PWM
29// output using an H-Bridge. See the WriteHBridgeOutput example.
30#define outputPin ConnectorIO1
31
32int main() {
33 // Set up the output pin for PWM output mode.
34 outputPin.Mode(Connector::OUTPUT_PWM);
35
36 while (true) {
37 // Write some digital PWM signals to the output connector.
38 // Valid values range from 0 (0% duty cycle / always off)
39 // to 255 (100% duty cycle / always on).
40
41 // Output a low duty cycle for 1 second.
42 outputPin.PwmDuty(10);
43 Delay_ms(1000);
44
45 // Output a medium duty cycle for 1 second.
46 outputPin.PwmDuty(120);
47 Delay_ms(1000);
48
49 // Output a high duty cycle for 1 second.
50 outputPin.PwmDuty(230);
51 Delay_ms(1000);
52 }
53}
void Delay_ms(uint32_t ms)
Blocks operations for ms milliseconds.
Definition SysTiming.h:287