Below is an example of how to set a Timer Counter Type E (TCE) and a Waveform Extension (WEX) instance to generate eight complementary Pulse-Width Modulation (PWM) signals at 20 KHz with variable duty cycles using the buffering scheme. The signals are in pairs of two and are not overlapping due to the added dead time, a feature which is essential in Motor Control for avoiding the shoot-through current in transistor switching. The update of the Compare registers will happen during the compare match interrupts for each channel. In this example the fault is highlighted as well. When a software event is triggered, all the signals are driven low. This happens every 250 μs. To do this, the WEX will be configured for fault detection and the Event System (EVSYS) will be configured to generate a software event. In this example, the WEX module is used as a timer extension, not in Pattern Generation mode.
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, TCE, WEX, Event System are configured and the global interrupts are enabled, the Create_Fault
and Clear_Fault
functions are called in an infinite loop.
The first function creates a software fault that drives all the output port pins to the low 0
logic. The WEX module's fault handling mechanism is configured in Latched mode. Consequently, the fault will be cleared if the fault condition is not active anymore and a software clear command is given. The second function clears the fault and restarts the normal operation of the TCE. First the fault is triggered, then the fault is maintained using a delay of 250 μs. After that delay, the fault is cleared and the TCE works normally for another 250 μs. Then, another fault is triggered and the process repeats itself infinitely.
The TCE is configured in Single-Slope mode and runs at 20 KHz. The TCE generates four PWM signals with different duty cycles that are updated in Interrupt Service Routines (ISR). The WEX is configured to be an extension for the TCE and to generate complementary signals with dead time for the ones generated by the TCE. The WEX is also configured to handle faults and the Event System is configured to send software events to the WEX.
void Create_Fault(void)
{
/* Fault creation, repeat in main loop to see it on Logic Analyzer. This is an event generated using a software command */
EVSYS_SoftwareEventASet(EVSYS_SWEVENTA_CH0_gc);
}
void Clear_Fault(void)
{
/* Clear fault condition using a software command */
WEX0_SoftwareCommand(WEX_CMD_FAULTCLR_gc);
}
To generate this project using MPLAB X IDE and the MPLAB X Code Configurator (MCC Melody, MCC Clasic is not supported on this device), 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. In MCC Content Manager tab click the Select MCC Melody button and then click Finish.
4. Click on Project Resources>System>Interrupt Manager, then do the following configuration:
- Toggle the Global Interrupt Enable button
5. Click on Project Resources>System>CLKCTRL, then do the following configuration:
- Disable the Prescaler enable button
6. To add the TCE module, go to Device Resources>Drivers>Timer>TCE0, then do the following configuration:
- Enable Timer: Should be enabled by default, if not just toggle the button (it turns blue if enabled)
- Clock Divider: System clock (by default the divider should be 1 - System clock)
- Waveform Generation Mode: Single-slope PWM mode
- Requested Period[s]: 0.00005
- Waveform Output n : check the boxes from the Enable column for Waveform Output 0, 1, 2, 3
- Generate ISR: toggle the button (it turns blue if enabled)
- Enable Compare 0 Interrupt: toggle the button (it turns blue if enabled)
- Enable Compare 1 Interrupt: toggle the button (it turns blue if enabled)
- Enable Compare 2 Interrupt: toggle the button (it turns blue if enabled)
- Enable Compare 3 Interrupt: toggle the button (it turns blue if enabled)
7. To add the WEX module, go to Device Resources>Drivers>WEX>WEX0, then do the following configuration:
- Input Matrix: Direct
- Update Source: TCE (the update condition for the output signals will be dictated by TCE)
- Override Settings: Check all the boxes from the Output Enable column for the Waveform Output [0-7]
- Dead-time Insertion Channel 0 Enable: toggle the button (it turns blue if enabled)
- Dead-time Insertion Channel 1 Enable: toggle the button (it turns blue if enabled)
- Dead-time Insertion Channel 2 Enable: toggle the button (it turns blue if enabled)
- Dead-time Insertion Channel 3 Enable: toggle the button (it turns blue if enabled)
- Requested Dead-time Low Side (μs) : 0.150
- Requested Dead-time High Side (μs) : 0.250
- Fault Event Input A : toggle the button (it turns blue if enabled)
- Fault Enable: toggle the button (it turns blue if enabled)
- Fault Interrupt Enable: toggle the button (it turns blue if enabled)
- Fault Detection Restart Mode: Cycle-by-cycle
- Fault Detection Action: Low
8. To add the EVSYS module, go to Device Resources>Drivers>EVSYS, then do the following configuration:
- CHANNELS: CHANNEL0
- USERS: WEXA (from CHANNEL0 rectangle from CHANNELS tab hold and drag the cursor to the WEXA rectangle from the USERS tab)
9. In the Pin Grid View tab check if the WEX WO [0-7] pins are locked as outputs on PORTA. When the boxes from Output Enable column from Override Settings of WEX are checked, the pins are also locked. To change the PORT simply click a pin from another PORT in Pin Grid View.
Pin | Configuration |
---|---|
PA0 | WEX WO0 |
PA1 | WEX WO1 |
PA2 | WEX WO2 |
PA3 | WEX WO3 |
PA4 | WEX WO4 |
PA5 | WEX WO5 |
PA6 | WEX WO6 |
PA7 | WEX WO7 |
10. In the Project Resources tab, click the Generate button so that MCC will generate all the specified drivers and configurations.
11. After the MCC Melody generates the project files with the configuration explained above, overwrite the content from the main.c
file with this:
/* Calculated values for TCE's period and the maximum duty cycle */
#define TCE_PERIOD (0x3E8)
#define MAX_DUTY_CYCLE (0x3DE)
#include "mcc_generated_files/system/system.h"
#include <util/delay.h>
/* Callback function that is called in the ISR routine */
void UserCallback_CMP0(void)
{
static uint16_t duty_cycle = 0;
/* Duty cycle update in interrupt */
duty_cycle += 5;
if(duty_cycle >= MAX_DUTY_CYCLE)
duty_cycle = 0;
TCE0_PWM_BufferedDutyCycle0Set(duty_cycle);
}
/* Callback function that is called in the ISR routine */
void UserCallback_CMP1(void)
{
static uint16_t duty_cycle = 0;
/* Duty cycle update in interrupt */
duty_cycle += 15;
if(duty_cycle >= MAX_DUTY_CYCLE)
duty_cycle = 0;
TCE0_PWM_BufferedDutyCycle1Set(duty_cycle);
}
/* Callback function that is called in the ISR routine */
void UserCallback_CMP2(void)
{
static uint16_t duty_cycle = 0;
/* Duty cycle update in interrupt */
duty_cycle += 25;
if(duty_cycle >= MAX_DUTY_CYCLE)
duty_cycle = 0;
TCE0_PWM_BufferedDutyCycle2Set(duty_cycle);
}
/* Callback function that is called in the ISR routine */
void UserCallback_CMP3(void)
{
static uint16_t duty_cycle = 0;
/* Duty cycle update in interrupt */
duty_cycle += 35;
if(duty_cycle >= MAX_DUTY_CYCLE)
duty_cycle = 0;
TCE0_PWM_BufferedDutyCycle3Set(duty_cycle);
}
void Create_Fault(void)
{
/* Fault creation, repeat in main loop to see it on Logic Analyzer. This is an event generated using a software command */
EVSYS_SoftwareEventASet(EVSYS_SWEVENTA_CH0_gc);
}
void Clear_Fault(void)
{
/* Clear fault condition using a software command */
WEX0_SoftwareCommand(WEX_CMD_FAULTCLR_gc);
}
int main(void)
{
SYSTEM_Initialize();
TCE0_Compare0CallbackRegister(UserCallback_CMP0);
TCE0_Compare1CallbackRegister(UserCallback_CMP1);
TCE0_Compare2CallbackRegister(UserCallback_CMP2);
TCE0_Compare3CallbackRegister(UserCallback_CMP3);
while(1)
{
Create_Fault();
_delay_us(250);
Clear_Fault();
_delay_us(250);
}
}
12. Now the project can be built and run from MPLAB X IDE. At run time, the values for the compare registers modifiy at interrupts generated for each channel. A software fault is generated and cleared periodically, resuming normal operation.
-
Connect the board to the PC.
-
Open the
TCE_AND_WEX_8_Complementary_PWM_MCC.X
solution in MPLAB X IDE. -
Right click the project and select Set as main project.
- Build the
TCE_AND_WEX_8_Complementary_PWM_MCC.X
project by clicking on Clean and Build Project.
- Click Make and Program Device to program the project to the board.
Below is illustrated a logic analyzer capture, to understand better how the WEX generates a complementary waveform signal for each PWM signal generated by TCE and how the fault handling works in hardware:
The range of the duty cycles is 0-50% of the PERIOD
This project shows how to use the WEX and TCE to generate complementary PWM signals and how to use the fault feature of WEX. TCE and WEX can generate up to eight PWM signals complementary in four independent pairs.