Create Your First Motor Control Application Using MPLAB® Harmony v3

Last modified by Microchip on 2023/11/15 12:43

Objective

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

MPLAB Code Configurator (MCC), includes the MPLAB Harmony v3 framework, 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 MCC 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 Harmony v3 project for a SAM E54 microcontroller.
  2. Use the MCC to configure and generate MPLAB Harmony v3 PLIB code for the PDEC, TCC, TC, ADC, and EVSYS peripherals.
  3. Use the MPLAB 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 supports the ATSAME54 device.

Hardware setup

Figure 1

Hardware Connection Setup

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

24V power supply and Serial RS232 Cable

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

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)

Use the MCC Framework 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

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° 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. 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° spatially oriented hall sensors.
  2. Use the hall sensor pattern to determine the three-phase inverter commutation pattern.

Block Diagram

BLDC Block Diagram

Figure 3

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

Figure 4

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

Figure 5

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

Figure 6

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

Calculation Chart

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.

Calculation Chart

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.

Calculation Chart

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)

Hall Sensor Based Motor Commutation Sequence

Figure 10

Complete Rotor Rotation

 

Back to Top

Application Overview

This lab shows how to create an MPLAB Harmony v3 project, and configure and generate MPLAB 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

Figure 11

Application Flow Sequence

Figure 12

Application Flow Sequence

Figure 13

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

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

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

Note:

  1. The project location of an MPLAB Harmony v3 project is independent of the location of the MPLAB Harmony Framework path. i.e. you need not create or place an MPLAB 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, an MPLAB Harmony v3 project generates all the referred source/header files and libraries (if any) under the project folder.
  3. Both points above contrast with MPLAB Harmony v2 project location. In MPLAB 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

  • Create MPLAB Harmony v3 Project using MPLAB X IDE
  • Verify Clock Settings

Step 2: Configure Peripheral Libraries

  • Configure PDEC Peripheral Library and PDEC Pins
  • Configure TCC0 Peripheral Library and TCC0 Pins
  • Configure ADC0 Peripheral Library
  • Configure TC0 and TC1 Instances in TC Peripheral Library
  • Configure Event System Peripheral Library

Step 3: Configure Switches for Motor State and Direction

  • Configure Switch Button Pin with EIC
  • 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