How To Generate 4 PWM Channels Simultaneously Using Timer/Counter Type E

Last modified by Microchip on 2024/06/24 06:32

Objective

This tutorial demonstrates how to configure the Timer/Counter Type E (TCE) to produce a simultaneous 4-channel PWM output. MPLAB® Code Configurator (MCC) Melody is used to configure the TCE and General-Purpose Input/Outputs (GPIOs).

Overview

This tutorial makes use of AVR16EB32 Curiosity Nano board to simplify hardware integration.

Derived from MCC Classic, MCC Melody offers seamless code generation and an enhanced and flexible architecture for effortless configuration of devices, peripherals and libraries. It provides a clear visualization of component dependencies, streamlining the development process and providing content versioning at the driver level for easy project maintenance. It enables easy migration across microcontrollers (MCUs) to keep pace with your application requirements.

MCC Melody provides libraries, drivers, Peripheral Libraries (PLIBs) and Hardware Initializers (HWIs) for the development of embedded software for our PIC® and AVR® MCUs and dsPIC® Digital Signal Controllers (DSCs).

The application configures the TCE for four simultaneous outputs at different duty cycles then endlessly loops while running. Very little CPU processing is required, which makes this an ideal 8-bit MCU solution.

Two Ways to Use This Tutorial

  • Use the solution project as an example:
    • Build the solution project, SingleSlopePWM, and program it to the AVR16EB32 Curiosity Nano board to observe the expected behavior.
  • Follow the lab procedure and MCC will add the missing files to complete the project.

Lab Objectives

  • Install the MCC Melody project for the AVR16EB32 microcontroller.
  • Use Melody to configure and generate peripheral libraries (PLIB) code for the following peripherals:
    • TCE
    • GPIO

Reference Materials

Information

Note: The Curiosity series evaluation kits include an on-board, embedded debugger. No external tools are necessary to program or debug the AVR16EB32. For programming/debugging, the debugger connects to the host PC through the USB-C connector on the AVR16EB32 Curiosity Nano board.

Hardware Connection Setup

Timer Counter E 4 Channel Output Hardware Setup

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

  • USB Type-C male cable
  • Oscilloscope or logic analyzer

Software Tools

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

  • MPLAB X IDE v6.20 (installation guide)
  • MPLAB XC8 v2.46 (installation guide)
  • Pins Driver v4.5.1 (select MCC > Content Manager > Content Libraries > System > Pins Driver)
  • AVR-Ex-DFP Device File Pack v2.9.197
  • MPLAB Code Configurator plugin v5.5.1
  • File extraction utility such as 7-Zip that supports ZIP files

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

Lab Source Files and Solutions

This ZIP file contains the completed solution projects for this lab. The contents of this ZIP file can be placed in a folder of your choice. This file is stored in a GitHub repository. You will need to create a free account to download the files.

Information

Note: Because MCC generates source and header files and libraries under the project folder, the contents of this ZIP file can be placed in any folder of your choice.

The hardware connections are simplified using the AVR16EB32 Curiosity Nano board. We will cover the required hardware connections.

Back to Top

Procedure

Connect Computer to AVR16EB32 Curiosity Nano Board

Connect the USB to the AVR16EB32 Curiosity Nano board.
Connect the USB from the AVR16EB32 Curiosity Nano board to the computer.

Back to Top


Create/Set Up MPLAB X IDE Project for AVR16EB32

Install MPLAB X IDE if it’s not already installed.

Install MPLAB XC8 Compiler if it’s not already installed or if your current version does not support AVR16EB32.

Information

Note: MPLAB code configurators such as MCC Melody are pre-installed into MPLAB X IDE, therefore, a separate installation is not required.

Create a standalone project in MPLAB X IDE.

Back to Top


Configure MCU Resources and Generate Code with Melody

Launch Melody

Open and initialize MCC by clicking on the MCC button.

MPLAB Code Configurator Icon

TCE

Navigate to the Device Resources section and add the TCE0 resource to the Project Resources by clicking on the green plus sign next to the peripheral listing:

Melody TCE Device Resources

Select TCE0 from Project Resources to open the peripheral’s Easy View window:

MPLAB Melody TCE0 Project Resources

Configure the Hardware Settings in the Easy View window to enable module:           

MPLAB Melody TCE0 Module Enable

Configure Waveform Generation Settings for SINGLESLOPE.

Request period of 0.001s.

Duty Cycle 0 (%): 10

Duty Cycle 1 (%): 50

Duty Cycle 2 (%): 75

Duty Cycle 3 (%): 93

Information

Note:  Requested Duty Cycle is shown in a table below the settings along with Real Duty Cycle value indicating the closest possible Duty Cycle to the requested based on current clock settings for the module.

Finally, enable Waveform Outputs for each of the four possible outputs.

MPLAB Melody TCE0 Waveform Generation Settings Window

Leave all other settings as their defaults.

  

GPIO

Confirm that the PWM channels have been assigned to output pins on the MCU with a green locked icon. Select Device Resources > Pins:

MPLAB Melody TCE0 Pin Resources

Generate Code

Click on the Generate button in the Project Resources pane to generate API libraries and configuration files based on the Melody settings.

MPLAB Melody Generate Button

Successful generation of all files should be confirmed in the MCC output window at the bottom of the IDE:

MPLAB Melody Generation Complete Message

Back to Top


Build and Program

Build Project and Program MCU

Click on the Make and Program Device Main Project button in the IDE toolbar to compile and program the project onto the AVR16EB32 microcontroller populating the board:

MPLAB IDE Program Icon

Verify success compilation and programming in the output window at the bottom of the IDE:

MPLAB IDE Programming Complete Message

Back to Top


Test Application

Verify Correct Operation

Results

Connect an oscilloscope or logic analyzer to the corresponding waveform output pins on RA0, RA1, RA2 and RA3. The four PWM signals with predefined duty cycles should be visible as shown below:

TCE 4 Channel PWM Output Waveforms

Back to Top

Conclusions

You successfully demonstrated TCE functionality using MCC Melody. Your project used fundamental elements to configure TCE for simultaneous 4-channel PWM outputs.

You used MCC Melody to configure TCE0 and GPIOs and accessed fundamental PWM functions in MCC Melody to successfully complete this training.

Back to Top

Learn More

Back to Top