Create your first Motor Control Application using MPLAB® Harmony v3


MPLAB® Harmony v3 is a flexible, fully integrated embedded software development framework for 32-bit microcontrollers (MCUs) and microprocessors (MPUs).

MPLAB Harmony v3 includes the MPLAB Harmony Configurator (MHC) tool, a set of modular devices, and middleware libraries. Additionally, there are numerous example applications, all of which are designed to help you quickly and easily develop powerful and efficient embedded software for Microchip’s 32-bit PIC® and SAM devices.

This tutorial shows you how to use MHC to create a motor control application Brushless DC (BLDC) block commutation using a hall sensor on a SAM E54 microcontroller.

This application reads the pattern from the hall sensor mounted on the motor shaft. Based on the hall pattern, the corresponding commutation is forced to the motor windings through three-phase inverters. The motor speed can be increased or decreased using the reference potentiometer in the MCLV-2 board. Two momentary switches are used, one for Start/Stop operation and the other for direction control (Forward/Reverse).

The application will utilize:

  • Position Decoder (PDEC) Peripheral Library (PLIB) to read the hall pattern from the hall sensor.
  • Timer/Counter for Control Applications (TCC0) PLIB to create three pairs of Pulse Width Modulation (PWM) frequency for three-phase inverters and to trigger the ADC0 periodically.
  • Analog-to-Digital Converter (ADC0) PLIB to read the output voltage of potentiometer to determine the speed.
  • Timer/Counter (TC0) PLIB used as an internal 1 mS timer counter.
  • Timer/Counter (TC1) PLIB used as a timer to measure the time elapsed between two consecutive hall edges.
  • Event System (EVSYS) PLIB used as a traffic controller between TCC0 (event generator) and ADC0 (event user).

Two ways to use this tutorial:

1. Create the project from scratch:

  • Use the provided source files and follow the step-by-step instructions.

2. Use the solution project as an example:

  • Build the solution project, download it to the target hardware, and observe the output.

Lab Objective:

  1. Create an MPLAB X IDE Harmony v3 project for a SAM E54 microcontroller.
  2. Use the MHC to configure and generate Harmony v3 PLIB code for the PDEC, TCC, TC, ADC, and EVSYS peripherals.
  3. Use the Harmony v3 PLIB Application Programming Interfaces (APIs) to implement the application.


Hardware Tools

Tool About Purchase
Development Board
ATSAME54 Motor Control PIM
Plug-in Module
24-Volt 3-phase Brushless DC Motor
Hurst motor
J-32 Debug Probe Debugger
Development Tool
Debugger Adapter Board

You can use any debugger (for example, Atmel-ICE) and adapter that support the ATSAME54 device.

Software Tools

This project has been verified to work with the following versions of software tools:

MPLAB XC32 Compiler v2.41
MPLAB Harmony v3 "csp" repo v3.7.1
MPLAB Harmony v3 "dev_packs" repo v3.7.0
MPLAB Harmony v3 "mhc" repo v3.4.0
MHC plugin v3.5.0

Because we regularly update our tools, occasionally you may discover an issue while using the newer versions. If you suspect that to be the case, we recommend that you double-check and use the same versions that the project was tested with.

Tool About Installers
Windows Linux Mac OSX
Integrated Development Environment
C/C++ Compiler

For this lab, download the following repositories from GitHub:

  • CSP: The following table shows the summary of contents.
Folder Description
apps Example applications for CSP library components
arch Initialization and starter code templates and data
docs CSP library help documentation
peripheral PLIB templates and configuration data
  • DEV_PACKS: The following table shows the summary of contents.
Folder Description
Microchip Peripheral register specific definitions
arm Core Specific Register Definitions (CMSIS)
  • MHC: The following table shows the summary of contents.
File/Folder Description
doc Help documentation and licenses for libraries used
np_templates New Project templates for supported toolchains
*.jar Java implementations of MHC modules
mhc.jar Main Java executable (run: java -jar mhc.jar -h)
runmhc.bat Windows cmd batch file to run standalone MHC Graphical User Interface (GUI)

Use the MPLAB Harmony 3 Content Manager (MHCM) to download the repositories.

How does a brushless DC motor work?

The BLDC motor is driven by an electronic drive which changes the supply voltage between the stator windings as the rotor turns. The rotor position is monitored by the hall sensor which supplies information to the electronic controller. Based on the hall pattern, the corresponding stator winding is energized. This electronic drive consists of MOSFETs (two for each phase) which are operated via a microcontroller.

Figure 2

The magnetic field generated by the permanent magnets interacts with the stator field produced by the stator windings, resulting in a mechanical torque. The electronic drive switches the supply current to the stator to maintain a constant angle of 0 to 90 degrees between the interacting fields.

In general, hall sensors are mostly mounted on the stator. When the rotor passes through the hall sensor, based on the North or South Pole, it generates a high or low signal. Based on the combination of these signals, the winding to be energized is defined. In order to keep the motor running, the magnetic field produced by the windings should shift position, as the rotor moves to catch up with the stator field.

Description on Block Commutation:

Brushless Direct Current electric motors or BLDC motors for short are electronically commutated motors powered by a Direct Current (DC) electric source via an external motor controller. The electronic commutation to rotate the motor is achieved by a three-phase inverter. The commutation technique is broadly classified as block commutation and sinusoidal commutation.

The block commutation has reduced system complexity as compared to the sinusoidal commutation. Hence, the block commutation technique is quite popular for low-cost applications where control precision, reduced efficiency, and higher acoustic noise are permitted.

The BLDC motor control using block commutation is done as follows:

  1. Read the hall sensor input pattern from three 120 degree spatially oriented hall sensors.
  2. Use the hall sensor pattern to determine the three-phase inverter commutation pattern.

Block Diagram:

Figure 3

Lets discuss how to calculate the commutation pattern for the given hall pattern and use the sequence to achieve one complete rotation.

Figure 4

The table shows U, V, W winding drive status for each Hall pattern. Where High Drive “1” means “+” current and Low Drive “1” means “- “current.

Figure 5 illustrates the current hall sensor value and the next commutation pattern to be forced.

Figure 5


U, V, and W are stator windings.
H1, H2, and H3 are hall sensors.
Red indicates positive current (+) on windings.
Blue indicated negative current (-) on windings.
The colored hall sensor indicates sensor output is 0 whenever it detects the North pole.

On capturing a hall sensor state change event, the PWM outputs controlling the motor-drive circuit are updated according to the commutation sequence.

Let us discuss STAGE1

If read, the hall sensor values are H1 = 1, H2 = 0, H3 = 1 which is 5, in order move the rotor in the clockwise direction, we have to energize windings V and U. Positive current to V windings and negative current to U windings which is V+ U-.

Calculation of Commutation Pattern:

Commutation patterns are calculated for windings (V+U-) and the calculated pattern is applied through the TCC pattern register (TCC_PATT) that has two register settings (Pattern Value register and Pattern Enable register).

Figure 6

These two registers control the PWM signal from the TCC. The configurations of two registers are given below.

Figure 7

For STAGE 1:

Pattern value configuration for V+U- is 0x10. This means the low side switch of Phase U is continuously ON.

Figure 8

The pattern that enables configuration is 0x75. Pattern output is disabled for Phase V high side switch, the PWM signal is directly fed to the TCC port pin. The duty cycle of the PWM signal is applied as per the speed command.

Figure 9

Note: Only six waveform outputs were needed here. So, leave the rest of the outputs (WO3 and WO7) as "don’t care" (“X”).

If the hall value read is 5, then to move the rotor in a clockwise direction, we must force value 0x1075 to the TCC Pattern register. Similarly, the commutation patterns are calculated for other stages.

Hall Sensor Based Motor Commutation Sequence (One Electrical Cycle)

Figure 10

Complete rotor rotation:

Application Overview:

This lab shows how to create an MPLAB Harmony v3 project, configure, and generate Harmony v3 Peripheral Library code for the PDEC, TCC0, ADC, TC0, EVSYS and PORTS peripherals. It reads the hall sensor input pattern from the hall sensor mounted on the motor shaft. Based on the hall pattern, the corresponding commutation is forced to the motor windings through three-phase inverters. It demonstrates motor speed change (increase or decrease) based on reference Potentiometer output. Two momentary switches are used. Motor state (Start/Stop) toggled for every event on switch_2 (S2) and Motor direction (Forward/ Reverse) is changed for every event on switch_3 (S3).

Application Flow Sequence:

Figure 11
Figure 12
Figure 13

Lab Source Files and Solutions

The associated ZIP package contains the completed solution project for this lab. It also contains the source files needed to perform the lab as per the following step-by-step instructions (see the Procedure section on this page).

The contents of this ZIP file need to be placed in the folder:

<Any directory of your choice>/training/
(example Directory = C:/microchip/harmony/v3.4.0)


  1. The project location of a Harmony v3 project is independent of the location of the Harmony Framework path. i.e. you need not create or place a Harmony v3 project in a relative path under the Harmony v3 framework folder. The project can be created or placed in any directory of your choice.
  2. The point above is true because when created, a Harmony v3 project generates all the referred source/header files and libraries (if any) under the project folder.
  3. Both points above contrast with Harmony v2 project location. In Harmony v2, the project was supposed to be created in a location under the Harmony framework.

Extracting the ZIP file creates the following folders:

  • mc_bldc_block_commutation contains the lab solution and source files in the firmware folder.
  • firmware has a subfolder src , which contains application source files and other supported files (if any) required to perform the lab.
  • firmware contains the completed lab solution project. It can be directly built and downloaded on the hardware to observe expected behavior.


All steps must be completed before you will be ready to build, download, and run the application.

Lab Index

Step 1: Create a New Project and Configure the SAM E54

  • Step 1.1 - Install the MPLAB® Harmony Configurator (MHC) Plug-in in MPLAB® X IDE
  • Step 1.2 - Create MPLAB Harmony v3 Project using MPLAB X IDE
  • Step 1.3 - Verify Clock Settings

Step 2: Configure Peripheral Libraries

  • Step 2.1 - Configure PDEC Peripheral Library and PDEC Pins
  • Step 2.2 - Configure TCC0 Peripheral Library and TCC0 Pins
  • Step 2.3 - Configure ADC0 Peripheral Library
  • Step 2.4 - Configure TC0 and TC1 Instances in TC Peripheral Library
  • Step 2.5 - Configure Event System Peripheral Library

Step 3: Configure Switches for Motor State and Direction

  • Step 3.1 - Configure Switch Button Pin with EIC
  • Step 3.2 - Configure LED Pin

Step 4: Generate Code

Step 5: Add Application Code to the Project

Step 6: Hardware Setup, Build, Program and Observe the Outputs

© 2020 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.