4.2 MCC Melody Implementation
- Create a new MPLAB X IDE project for AVR16EB32.
- Open MCC from the toolbar (find more information on installing the MCC plug-in here).
- In MCC Content Manager Wizard, select MCC Melody, then click Finish.
- Go to Project Resources>System>Interrupt Manager. Toggle the Global Interrupt Enable button.
- Go to Project Resources>System>CLKCTRL. Disable the Prescaler enable button.
- From Device
Resources>Drivers>Timer, add the TCE module, then do the
following configuration:
– Enable Timer: This may be enabled by default. If not, 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)
- From Device
Resources>Drivers, add the WEX module, 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
- From Device
Resources>Drivers, add the EVSYS module, then follow the
below configuration:
– CHANNELS: CHANNEL0
– USERS: WEXA (Select the CHANNEL0 rectangle from the CHANNELS tab, hold and drag the cursor to the WEXA rectangle from the USERS tab)
- Check if the WEX_WO[0-7] pins are locked as outputs on PORTA in the Pin Grid View tab. The pins are also locked when checking the boxes from the Enable column from Waveform Output n. To change the PORT, click on a pin from another PORT in Pin Grid View.
- In the Project Resources tab, click the Generate button so that MCC will generate all the specified drivers and configurations.
- Edit the
main.c
file, as indicated below.Add in the include files the following:
#include "mcc_generated_files/system/system.h" #include <util/delay.h>
Add macro definitions:
#define TCE_PERIOD (0x3E8) #define MAX_DUTY_CYCLE (0x3DE)
Add ISR callback functions:
/* 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); }
Add functions:
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); }
Edit the main function:
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); } }
- Now, the project can be built and run using MPLAB X IDE. During the run time, the scaling value for the compare registers updates every 10 ms, adjusting the range of the duty cycles.