How To Generate 4 PWM Channels Simultaneously Using Timer/Counter Type E
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
Hardware Connection 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.
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.
The hardware connections are simplified using the AVR16EB32 Curiosity Nano board. We will cover the required hardware connections.
Procedure
Connect Computer to AVR16EB32 Curiosity Nano Board
Create/Set Up MPLAB X IDE Project for AVR16EB32
Install MPLAB XC8 Compiler if it’s not already installed or if your current version does not support AVR16EB32.
Configure MCU Resources and Generate Code with Melody
Launch Melody
Open and initialize MCC by clicking on the MCC button.
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:
Select TCE0 from Project Resources to open the peripheral’s Easy View window:
Configure the Hardware Settings in the Easy View window to enable module:
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
Finally, enable Waveform Outputs for each of the four possible outputs.
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:
Generate Code
Click on the Generate button in the Project Resources pane to generate API libraries and configuration files based on the Melody settings.
Successful generation of all files should be confirmed in the MCC output window at the bottom of the IDE:
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:
Verify success compilation and programming in the output window at the bottom of the IDE:
Test Application
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:
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.