Return to SDK Examples for Microchip Studio
#include "ClearCore.h"
#define motor ConnectorM0
#define baudRate 9600
#define SerialPort ConnectorUsb
int main() {
motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_PWM);
motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
SerialPort.Mode(Connector::USB_CDC);
SerialPort.Speed(baudRate);
uint32_t timeout = 5000;
SerialPort.PortOpen();
while (!SerialPort &&
Milliseconds() - startTime < timeout) {
continue;
}
while (true) {
MotorDriver::HlfbStates hlfbState = motor.HlfbState();
if (hlfbState == MotorDriver::HLFB_HAS_MEASUREMENT) {
float hlfbPercent = motor.HlfbPercent();
SerialPort.Send("Speed output: ");
if (hlfbPercent == MotorDriver::HLFB_DUTY_UNKNOWN) {
SerialPort.SendLine("UNKNOWN");
}
else {
char hlfbPercentStr[10];
snprintf(hlfbPercentStr, sizeof(hlfbPercentStr), "%.0f%%", hlfbPercent);
SerialPort.Send(hlfbPercentStr);
SerialPort.SendLine(" of maximum speed");
}
}
else if (hlfbState == MotorDriver::HLFB_DEASSERTED) {
SerialPort.SendLine("Disabled or Shutdown");
}
}
}