Create your first Motor Control Application using MPLAB® Harmony v3
Contents
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).
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
- Create an MPLAB X IDE Harmony v3 project for a SAM E54 microcontroller.
- Use the MHC to configure and generate Harmony v3 PLIB code for the PDEC, TCC, TC, ADC, and EVSYS peripherals.
- Use the Harmony v3 PLIB Application Programming Interfaces (APIs) to implement the application.
Reference Materials
- About dsPICDEM™ MCLV-2
- Purchase dsPICDEM™ MCLV-2
- About ATSAME54 Motor Control PIM
- Purchase ATSAME54 Motor Control PIM
- About 24-Volt 3-phase Brushless DC Motor
- Purchase 24-Volt 3-phase Brushless DC Motor
- About J-32 Debug Probe Debugger
- About Debugger Adapter Board
- Purchase Debugger Adapter Board
- MPLAB X IDE
- MPLAB XC32 Compiler
- MPLAB Harmony v3
- MHC Plug-in
- About SAM C21 Xplained Pro
- Purchase SAM C21 Xplained Pro
- About I/O1 Xplained Pro
- Purchase I/O1 Xplained Pro
- About Power Debugger
- Purchase Power Debugger
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.
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:
- Read the hall sensor input pattern from three 120 degree spatially oriented hall sensors.
- Use the hall sensor pattern to determine the three-phase inverter commutation pattern.
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.
Figure 5 illustrates the current hall sensor value and the next commutation pattern to be forced.
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).
These two registers control the PWM signal from the TCC. The configurations of two registers are given in the accompanying image:
For STAGE 1
Pattern value configuration for V+U- is 0x10. This means the low side switch of Phase U is continuously ON.
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.
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)
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:
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).
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.
Procedure
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 5: Add Application Code to the Project
Step 6: Hardware Setup, Build, Program and Observe the Outputs