Create your first Motor Control Application using MPLAB® Harmony v3

Last modified by Microchip on 2023/11/10 11:00

MPLAB® Harmony v3 is now configurable through MPLAB Code Configurator (MCC). Although the instructions in this tutorial are for the MHC, the flow and experience of creating a project, configuring peripherals, and generating code using MCC are similar. Refer to the accompanying links for specific instructions to use MPLAB Harmony v3 with MCC.

Objective

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 the 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).

Back to Top

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.

Back to Top

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.

Back to Top

Reference Materials

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

Hardware setup
 

Hardware Connection Setup

Apart from the hardware tools listed above, the following items are required:

24 V power supply and Serial RS232 Cable

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

MPLAB X IDE v5.40
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.

 

Back to Top

For this lab, download the following repositories from GitHub:

  • CSP: The following table shows the summary of contents.

FolderDescription
appsExample applications for CSP library components
archInitialization and starter code templates and data
docsCSP library help documentation
peripheralPLIB templates and configuration data
  • DEV_PACKS: The following table shows the summary of contents.
FolderDescription
MicrochipPeripheral register specific definitions
armCore Specific Register Definitions (CMSIS)
  • MHC: The following table shows the summary of contents.
File/FolderDescription
docHelp documentation and licenses for libraries used
np_templatesNew Project templates for supported toolchains
*.jarJava implementations of MHC modules
mhc.jarMain Java executable (run: java -jar mhc.jar -h)
runmhc.batWindows cmd batch file to run standalone MHC Graphical User Interface (GUI)

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

Back to Top

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.

BLDC motor

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.

Back to Top

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

BLDC Block Diagram

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

Calculation Chart

Note:
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.

Current hall sensor values and the next commutation pattern to be forced

Notes:

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 to 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).

Calculation Chart

These two registers control the PWM signal from the TCC. The configurations of two registers are given in the accompanying image:

Calculation Chart

For STAGE 1

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

Calculation Chart

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.

Calculation Chart

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)

Hall Sensor Based Motor Commutation Sequence

 

Back to Top

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:

Application Flow Sequence:

Application Flow Sequence

Application Flow Sequence

Back to Top

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)

Note:

  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.

Back to Top

Zip Files

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.

Back to Top

Procedure

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

Back to Top

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

Back to Top