This project shows how to set a Timer Counter type E (TCE) instance to generate four Pulse-Width Modulation (PWM) signals, highlighting the use of high-resolution feature. The user can increase the PWM signal resolution up to three bits.
More details and code examples on the AVR16EB32 can be found at the following links:
- MPLAB® X IDE v6.15 or newer
- AVR-Ex DFP-2.8.189 or newer Device Pack
- MPLAB® XC8 compiler v2.45
- MPLAB® Code Configurator (MCC) v5.3.7
- MPLAB® Code Configurator (MCC) Melody Core v2.6.2 or newer
The AVR16EB32 Curiosity Nano Development board is used as a test platform.
After the peripheral clock, the output port pins and TCE are initialized, the Duty_Cycle_Update
function is called in an infinite loop. This function is registered as a callback and is called in the TCE Overflow Interrupt Service Routine (ISR). In this example TCE generates four PWM channels that have one peripheral clock cycle difference between each other.
Initially, the CMP0 register has the value of 10 peripheral clock cycles, while the CMP1, CMP2 and CMP3 registers have the values of 11, 12 and 13, respectively. The values from CMPn registers increase until a maximum value is reached, then they restart from the beginning and the cycle is repeated.
The High Resolution feature can be set into 4X or 8X mode. In 4X mode, one pulse duration (one peripheral clock cycle) is shortened by four times. For example, if the peripheral clock frequency is 20 MHz, then one clock cycle duration is 50 ns. Applying the 4X resolution results in a 12.5 ns clock duration cycle. If the 8X resolution is applied, then one clock cycle duration becomes 6.25 ns. The difference between CMP channels remains one clock cycle, only the pulse duration shortens and frequency increases. The duty cycles that have values below one clock cycle without High Resolution enabled (50 ns), are set to 0
logic and are not visible on the logic analyzer. They become visible after they reach the threshold of one clock cycle without High Resolutioin eanbled (values > 50 ns).
At run time the resolution changes between three states: High Resolution OFF, 4X Resolution and 8X Resolution. Between each change there is a 2 seconds delay added in software. One General Purpose Input/Putput (GPIO) pin is used to visualize when the resolution changes on the logic analyzer.
void Duty_Cycle_Update(void)
{
/* Start with a duty cycle value of 10 peripheral clocks */
static uint8_t dcy = 10;
/* Increase duty cycle with one peripheral clock on each overflow interrupt */
dcy++;
/* Check if the duty cycle value has reached maximum */
if(dcy > MAX_DCY)
dcy = 10;
/* Update the values from CMP channels */
TCE0_CompareAllChannelsBufferedSet(dcy, dcy + 1, dcy + 2, dcy + 3);
}
This project is generated using MPLAB X IDE and the MPLAB X Code Configurator (MCC) Melody (MCC Classic is not supported on this device). To do this follow the next steps:
1. Open MPLAB X IDE and create a new project for the AVR16EB32 device.
2. Open MCC from the toolbar (more information on how to install the MCC plug-in can be found here).
3. From the MCC Content Manager tab click the Select MCC Melody button and then click Finish.
4. Click Project Resources>System>Interrupt Manager, then do the following configuration:
5. Click Project Resources>System>CLKCTRL, then do the following configuration:
- Disable the Prescaler enable button
- Clock Select: PLL Oscillator
- PLL Multiplication Factor: Multiply by 16
- PLL Source Division: DIV4
- Prescaler B division PDIVB: DIV4
6. To add the TCE module, go to Device Resources>Drivers>Timer>TCE0, then do the following configuration:
- Module Enable: Must be enabled by default, if not, just toggle the button (it turns blue if enabled)
- Clock Selection: System clock (by default the divider must be 1 - System clock)
- Counter Direction: UP
- Waveform Generation Mode: Single-Slope PWM mode with overflow on TOP
- Requested Period [s]: 0.001
- Duty Cycle 0 [%]: 0
- Duty Cycle 1 [%]: 0
- Duty Cycle 2 [%]: 0
- Duty Cycle 3 [%]: 0
- Waveform Output n : Check the boxes from the Enable column for Waveform Output 0, 1, 2, 3
- Duty Cycle High Resolution: OFF (this is modified at run time)
- Generate ISR: Toggle the button (it turns blue if enabled)
- Overflow Interrupt Enable: Toggle the button (it turns blue if enabled)
7. In the Pin Grid View tab check if the TCE WO [0-3] pins are locked as outputs on PORTA. When the boxes from the Enable column in the Output Settings of TCE are checked, the pins are also locked. To change the PORT simply click a pin from another PORT in Pin Grid View. Select from Pin Grid View PIN5 of PORTD as an output to toggle when the resolution is changed at run time.
Pin | Configuration |
---|---|
PA0 | TCE WO0 |
PA1 | TCE WO1 |
PA2 | TCE WO2 |
PA3 | TCE WO3 |
PD5 | Digital output |
8. In the Project Resources tab, click the Generate button so that MCC will generate all the specified drivers and configurations.
9. After MCC Melody generates the project files with the configuration explained above, overwrite the content from the main.c
file with this:
/* Maximum value for duty cycle */
#define MAX_DCY (0x20)
#include "mcc_generated_files/system/system.h"
#include <util/delay.h>
void Duty_Cycle_Update(void)
{
/* Start with a duty cycle value of 10 peripheral clocks */
static uint8_t dcy = 10;
/* Increase duty cycle with one peripheral clock on each overflow interrupt */
dcy++;
/* Check if the duty cycle value has reached maximum */
if(dcy > MAX_DCY)
dcy = 10;
/* Update the values from CMP channels */
TCE0_CompareAllChannelsBufferedSet(dcy, dcy + 1, dcy + 2, dcy + 3);
}
int main(void)
{
SYSTEM_Initialize();
TCE0_OverflowCallbackRegister(Duty_Cycle_Update);
while(1)
{
TCE0_HighResSet(TCE_HREN_OFF_gc);
IO_PD5_Toggle();
_delay_ms(2000);
TCE0_HighResSet(TCE_HREN_4X_gc);
IO_PD5_Toggle();
_delay_ms(2000);
TCE0_HighResSet(TCE_HREN_8X_gc);
IO_PD5_Toggle();
_delay_ms(2000);
}
}
10. Now the project can be built and run from MPLAB X IDE. At run time, the duty cycle values are updated at each overflow ISR of TCE, every 50 μs. The PWM resolution changes every 2 seconds.
-
Connect the board to the PC.
-
Open the
TCE_HighResolution.X
solution in MPLAB X IDE. -
Right click the project and select Set as main project.
- Build the
TCE_HighResolution.X
project by clicking Clean and Build Project.
- Click Make and Program Device to program the project to the board.
The figures below show logic analyzer captures to help understand how the four PWM signals look and how the high-resolution feature works:
This project shows how to use the TCE to generate PWM signals in Single-Ramp mode and how to use the high-resolution feature of TCE.