SAMA5D29 Curiosity – MPLAB® Harmony-Based Complementary PWM Application with Dead-Time

Last modified by Microchip on 2025/06/04 05:24

Introduction

In this training, you will explore the process of configuring, building, and debugging a complementary Pulse-Width Modulation (PWM) application on the SAMA5D29 Curiosity development board  (P/N: EV07R15A) using MPLAB® X Integrated Development Environment (IDE). Complementary PWM is essential in applications such as half-bridge convertermotor control, power inverters, and DC-DC converters, where precise control of switching devices is required to ensure efficient operation and minimize power losses. Dead-time is a critical feature in complementary PWM, as it prevents simultaneous conduction of high-side and low-side switches, which could lead to short circuits and damage to the hardware.

This training also covers two key PWM Push-Pull modes, Left-aligned mode and Center-aligned mode. In Left aligned mode, the PWM signal starts at the beginning of the period and adjusts its duty cycle accordingly, making it suitable for simpler timing requirements. In contrast, center-aligned mode generates a symmetric waveform by aligning the PWM signal around the center of the period, which helps reduce harmonics and is often preferred in motor control applications.

EV07R15A SAMA5D29 Curiosity Development Board
You will learn how to configure these modes, implement dead-time, build a PWM application, and debug it using the MPLAB Harmony v3 Software Framework. This hands-on training provides a comprehensive understanding of the importance of complementary PWM, its practical applications, and the process of developing and debugging PWM applications for the SAMA5D29 Curiosity development board.

Prerequisites

Information

​This training was developed with MPLAB X IDE v6.25 and MPLAB XC32 v4.60.

Back to top

Hardware

For this training, you will use the SAMA5D29 Curiosity development board.

SAMA5D29 Curiosity Development Board

Jumpers

SD Memory Cards

  • Ensure no SD memory cards are plugged into the SD card (J6) connector.

Power

Debug Communications

  • The SAMA5D29 Curiosity development board contains a 20-pin JTAG debug port. Connect a MPLAB PICkit™ 5 in-circuit debugger to the 20-pin JTAG debug port (J27) and the host computer running MPLAB X IDE.

Console Serial Communications

Others

  • Three male-to-male jumper wires
  • Oscilloscope with two probes

Hardware Modification

To generate a complementary PWM signal, we need both the PWMH and PWML pins of the same PWM channel.

In this example, we will generate the PWM signal using Channel 1. However, some modifications are required on the SAMA5D29 Curiosity development board to enable this configuration:

  • Channel 1 PWMH1 is mapped to the mikroBUS2 PWM pin.
  • Channel 1 PWML1 is mapped to the mikroBUS2 INT pin.

These mappings ensure proper functionality for generating the complementary PWM signal.

mikroBUS 2 interface

The mikroBUS2 INT pin is pulled up with a 100 kΩ resistor. To enable proper functionality, remove the R187 resistor located on the bottom side of the SAMA5D29 Curiosity development board.

mikroBUS2 INT pin

                                      mikroBUS2 INT PWM

Note: If the R187 pull-up resistor is not removed, the mikroBUS 2 INT pin will always be in high-state.

Hardware Setup

Power up the board by connecting the USB Type-C® cable to the USB_A port J1 on the SAMA5D29 Curiosity development board.

Back to Top


Connect the MPLAB PICkit 5 debugger  to the 20-pin JTAG debug port (J27).

Back to Top


Connect FTDI cable to the Serial Debug Port (J28) on the SAMA5D29 Curiosity development board.

Back to Top


Connect one end of the male-to-male jumper wires to PWMH1, PWML1, and GND pins on the mikroBUS™ 2 connector.                                                                                                                                  
mikroBUS™ 2 connector

Back to Top


Connect the other end of the male-to-male jumper wires to the oscilloscope.
male-to-male jumper wires to oscilloscope connection

The complete setup is shown in the accompanying image.
complete setup

Back to Top

Developing a PWM Demo Application

Create an application project and add peripheral blocks in the project graph.

Open MPLAB X IDE from the main menu.

Back to Top


Create a new project by clicking the New Project icon New Project icon or by selecting File > New Project.

Back to Top


In the New Project window, under Projects, choose Application Project(s) and click NextChoose Project pane

Back to Top


In the Select Device dialog window, fill in or select the following information:

  • Family: 32-bit MCUs (PIC32C/SAM)
  • Device: SAMA2D29

Select Device pane

Back to Top


In the Select Compiler window, for Compiler Toolchains select XC32, and click Next.

Select Compiler dialog window

Back to Top


Enter Project Location, Project Folder, and Project Name. Click Finish.

Select project name and folder pane

This creates an empty project and set this project as the main project. If there are other projects open in the project explorer window, set this project as the main project by right clicking on the project, choose Set as Main Project.

 sama5d29_cu_pwm Set as Main Project 

Back to Top


After the project is created, MPLAB Code Configurator (MCC) will be automatically launched. (To launch MCC manually from main menu, click on Tools > Embedded > MPLAB Code Configurator or click the MCC button in the MPLAB X IDE toolbar.) It will launch Content Manger Wizard. Then click Select MPLAB Harmony.

MCC Content Manager Wizard Select a Content Type

Back to Top


In addition to the required packages (csp, dev_packs), download the optional packages bsp and then click Finish. The content download will take some time. Wait for all the content to download.

MCC Content Manager Wizard Finish

Back to Top


A project graph will be displayed. From Device Resource add Board Support Packages for SAM A5D29 Curiosity BSP to the Project Graph.

Board Support Packages to SAM A5D29 Curiosity BSP to Project Graph

Back to Top


From Device Resource, Peripherals > UART, add UART0 to the Project Graph. This helps to print the debug messages (console serial communication messages) via comm port.

UART0 to the Project Graph

Back to Top


In the Project Graph, right click on UART in UART0 and then add Consumers > STDIO.

project graph

project graph

Back to Top


From the Project Graph window, click System and in the Configuration Options pane, select Clock (PMC) > Generic Clock Menu > Generic Clock for Peripheral UART0. Check the Enable Generic Clock box. Select MCK_CLK for Generic Clock Source Selection.

Project Graph tab

Back to Top


From the Project Graph window, click the UART0 and in the Configuration Options pane, apply the following settings:

  • Select Clock Source: MCK
  • Baud Rate: 115,200

UART0 Configuration Options

Back to Top


From Device Resources, expand Peripherals > PWM, and add PWM to the Project Graph.

adding PWM to the Project Graph

Back to Top

PWM Configuration in Project graph

This demo demonstrates the basic configuration of Left-aligned and Center-aligned PWM modes. You can select the PWM mode that best suits your application's requirements.

Note: If your application requires Left-aligned mode, you can skip the Center-aligned mode configuration.

Left-Aligned Mode Configuration

From the Project Graph window, click the PWM and in the Configuration Options > PWM > Channel Configuration > Channel 1.

  • Enable: Channel 1
  • Select Alignment: Left-aligned
  • Select Channel Clock: Peripheral clock/2.
  • Output Polarity: Waveform starts at low level
  • Period Value: 41,500 for 1 KHz PWM Frequency
  • Duty Value: 7,000 for ~17 percent duty cycle
  • Dead Time for low-Side Output: 100
  • Dead Time for high-Side Output: 100
  • Not enabled: Enable Fault 
  • Not enabled: Enable Counter Period Interrupt

Note: The PWM frequency is automatically calculated based on the period value entered by the user. For detailed guidance on calculating the period value, refer to the SAMA5D29 datasheet, specifically Section 56.7.43 (PWM Channel Period Register) and Section 56.7.44 (PWM Channel Period Update Register). The duty cycle can be configured up to the specified period value. For example, if the period value is 41,500 and the duty cycle is set to 50 percent, the resulting duty cycle value will be 20,750. The configuration of dead time depends on the specific requirements of the application.

Period value
Where;
          Peripheral clock = 166 MHz.
          Prescaler value = 2 (peripheral clock/2).
          Desired PWM Frequency = 1 KHz.
Calculate the divided clock frequency
Calculate the Period Value

Configuration Options tab

Back to Top

Center-Aligned Mode Configuration

From the Project Graph window, click the PWM and in the Configuration Options > PWM > Channel Configuration > Channel 1.

  • Enable: Channel 1
  • Select Channel Clock: Peripheral clock/2
  • Select Alignment: Center-aligned
  • Select Duty-Cycle Update Trigger: At the next end of PWM period
  • Select Counter Event Occurrence: At the end of PWM period
  • Output Polarity: Waveform starts at low level
  • Period Value: 20,750 for 1 KHz PWM Frequency
  • Duty Value: 7,000 for ~17 percent duty cycle
  • Dead Time for low-Side Output: 100
  • Dead time for high-Side Output: 100
  • Not enabled: Enable Fault 
  • Not enabled: Enable Counter Period Interrupt

Note: The PWM frequency is automatically calculated based on the period value entered by the user. For detailed guidance on calculating the period value, refer to the SAMA5D29 datasheet, specifically Section 56.7.43 (PWM Channel Period Register) and Section 56.7.44 (PWM Channel Period Update Register). The duty cycle can be configured up to the specified period value. For example, if the period value is 20,750 and the duty cycle is set to 50 percent, the resulting duty cycle value will be 10,375. The configuration of dead-time depends on the specific requirements of the application.

Period value
Where;
          Peripheral clock = 166 MHz.
          Prescaler value = 2 (peripheral clock/2).
          Desired PWM Frequency = 1 KHz.

Calculate the divided clock frequency

Calculate the Period Value

Configuration Options tab

Back to Top

Add Pin Configuration and Generate the Code for the Added Peripherals

From the Project Graph window, select the Pin Configuration from the Plugins drop-down. The Pin Setting window will open.

project graph

  • Ensure all the pins are configured as follows:
    Pin IDFunction
    PA7LED_AH
    PA8LED_AH
    PA9LED_AH
    PB26UART0_URXD0
    PC27UART0_UTXD0
    PB0PWM_PWMH1

    PB1

    PWM_PWML1

    Pins table

Back to Top


Save all and then click the Generate button. This will generate code for all the device resource that have been added in the project graph.Generate button

Back to Top


PWM Application Code

  • Functions to be called from PWM application as follows:

                  PWM_ChannelPeriodGet() : Read the period.
                  PWM_ChannelsStart() : Start all synchronous channels by starting channel 1.
                  PWM_ChannelDutySet() : To set the PWM duty cycle .
                  PWM_ChannelDeadTimeSet() : To set the PWM dead-time.

  • Refer to the main.c for demo code.

Note: The code for Left-aligned and Center-aligned modes in the PWM application remains the same; only the MCC configuration differs. Next, you can either debug or run the PWM application on the target. If you choose to run the pre-built application, you can skip the debugging process.

  • PWM demo application is now ready.

Debugging the PWM Application on the Target

To debug the project on MPLAB, make sure the board is powered, and the external debugger is connected. Refer to the Hardware setup section in this document for more details.

Right click on the project and click Properties. For Compiler Toolchain, select XC32. Under Packs, expand SAMA5D2_DFP and select 1.12.233 or the latest and click Apply. Project Properties window

Back to Top


Configure the debug interface of PICKit 5 as shown in the accompanying:
Project Properties window PICkit 5

Back to Top


By default, MPLAB X IDE only generates ELF and HEX format output files. To generate application output in binary format, a post build step needs to be added to the project properties. To do this, right click on project and select Properties, select Building, click the Execute this line after build check box, then enter the following command:

${MP_CC_DIR}/xc32-objcopy -O binary ${DISTDIR}/${PROJECTNAME}.${IMAGE_TYPE}.elf ${DISTDIR}/harmony.bin

Project Properties window Building

Back to Top


In the Categories pane, select Bootstrap.

Ensure that Use bootstrap is enabled and the pre-complied at91bootstrap.elf is selected.

Project Properties window Bootstrap

While in Debug mode, the second-stage bootloader, at91bootstrap, is used by MPLAB X IDE to initialize DRAM so that it can continue loading the MPLAB Harmony v3 project binary (harmony.bin). Once the project has been loaded onto the SAMA5D29 Curiosity development board, MPLAB X IDE can be used to run and debug the application.​

Back to Top


Clean and build the project. You should see a message on the output console that the project was successfully built. This completes the building of the MPLAB Harmony application.Build Successful message

Back to Top


The harmony.bin binaries will be available at ./project_directory/sama5d29_curiosity.X/dist/default/production.

Now start debugging the project by step in, step out, halt or run.

debugging the project by step in, step out, halt or run

Back to Top


Now observe the prints in Tera Term or any serial terminal software and oscilloscope. Enter the Duty-cycle and Dead-Time in serial terminal software and press Enter.
Duty-cycle and Dead-Time in serial terminal software
prints in TeraTerm
prints in TeraTerm

Back to Top

Running the Pre-built MPLAB Harmony Application From the SD Card

The pre-built application BIN file can be programmed by following these steps.

Steps to Program the BIN File on an SD Card

Take a microSD card formatted with FAT32 file system. See the "How to Format and Partition an SD Card Larger Than 32GB Using DiskPart in Windows® for MPU" page.

Back to Top


Copy the boot.bin files from this location to the microSD card using your PC.

Back to Top


Copy harmony.bin files (available in the ./project_directory/sama5d29_curiosity.X/dist/default/production) to the microSD card using your PC.

Note: Download the pre-built harmony.bin file for Left aligned mode and the pre-built harmony.bin file for Center-aligned PWM mode.

Back to Top


Insert the SD card to J6 on the SAMA5D29 Curiosity development board.

Back to Top

Steps to Run the BIN File From an SD Card

Press the reset button.

Back to Top


It will display the automotive dashboard graphics application on the display connected with SAMA5D29 Curiosity development board.

Back to Top


Now observe the prints in Tera Term or any serial terminal software and oscilloscope. Enter the Duty-cycle and Dead-Time in serial terminal software and press Enter.
Duty-cycle and Dead-Time in serial terminal software
prints in tera term
prints in TeraTerm

Back to Top

Summary

In this training, you learned how to perform hardware modifications and set up. You also learned how to configure various complementary PWM modes with dead-time, build a PWM application, and debug it using the MPLAB Harmony v3 Software Framework within the MPLAB X IDE. The project was specifically developed for the SAMA5D29 Curiosity development board.

Back to Top

Learn More

Back to Top