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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: WriteHBridgeOutput
3 *
4 * Objective:
5 * This example demonstrates how to output a differential/bi-directional PWM
6 * signal from a ClearCore H-Bridge connector.
7 *
8 * Description:
9 * This example sets up a ClearCore H-Bridge connector for H-Bridge output,
10 * then repeatedly ramps the PWM duty cycle output up and down, both sourcing
11 * and sinking current.
12 *
13 * Requirements:
14 * ** A device capable of receiving an H-Bridge bi-directional PWM signal, like
15 * a bi-directional brushed DC motor, connected to IO-4. Refer to the
16 * ClearCore System Diagram on how to wire a device to the H-Bridge capable
17 * connectors.
18 *
19 * Links:
20 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
21 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
22 *
23 *
24 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
25 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
26 */
27
28#include "ClearCore.h"
29
30// Defines which H-Bridge capable connector to use: ConnectorIO4 or ConnectorIO5
31#define HBridgeConnector ConnectorIO4
32
33// Declares our user-defined helper function, which is used to check for
34// overloads and reset the H-Bridge output as necessary. The definition/
35// implementation of this function is at the bottom of the example.
36void CheckHBridgeOverload();
37
38int main() {
39 // H-Bridge output is supported on connectors IO-4 and IO-5 only.
40 // Set the H-Bridge connector into H-Bridge output mode.
41 HBridgeConnector.Mode(Connector::OUTPUT_H_BRIDGE);
42
43 while (true) {
44 // Output bi-directional PWM on the H-Bridge connector over the full range
45 // of output values/duty cycles (-INT16_MAX-1 to INT16_MAX).
46
47 // Positive values (between 1 and INT16_MAX) will sink current into the
48 // signal pin.
49 // Negative values (between -1 and -INT16_MAX-1) will source current
50 // from the signal pin.
51
52 for (int16_t i = 0; i < INT16_MAX; i++) {
53 // Check for overloads and reset if needed
54 CheckHBridgeOverload();
55
56 // Write to the output.
57 HBridgeConnector.State(i);
58 Delay_us(125);
59 }
60
61 for (int16_t i = 0; i < INT16_MAX; i++) {
62 CheckHBridgeOverload();
63
64 HBridgeConnector.State(INT16_MAX - i);
65 Delay_us(125);
66 }
67
68 for (int16_t i = 0; i < INT16_MAX; i++) {
69 CheckHBridgeOverload();
70
71 HBridgeConnector.State(-i);
72 Delay_us(125);
73 }
74
75 for (int16_t i = 0; i < INT16_MAX; i++) {
76 CheckHBridgeOverload();
77
78 HBridgeConnector.State(-INT16_MAX + i);
79 Delay_us(125);
80 }
81 }
82}
83
84/*------------------------------------------------------------------------------
85 * CheckHBridgeOverload
86 *
87 * Checks whether any of the ClearCore's H-Bridge connectors are experiencing
88 * an overload. If an overload is detected the H-Bridge connectors are reset.
89 *
90 * Parameters:
91 * None
92 *
93 * Returns: None
94 */
95void CheckHBridgeOverload() {
98 Delay_ms(10);
99 }
100}
101//------------------------------------------------------------------------------
void Delay_ms(uint32_t ms)
Blocks operations for ms milliseconds.
Definition SysTiming.h:287
void Delay_us(uint32_t usec)
Blocks for operations usec microseconds.
Definition SysTiming.h:296
StatusRegister StatusRT(StatusRegister mask=UINT32_MAX)
The real time status register.
void HBridgeReset()
Starts a reset pulse to the DigitalInOutHBridge connectors.
StatusManager & StatusMgr
Status manager.
struct ClearCore::StatusManager::StatusRegister::@5 bit
uint32_t HBridgeOverloaded
Definition StatusManager.h:71