4.2 MCC Melody Implementation
- Create a new MPLAB X IDE project for AVR16EB32.
- Open MCC from the toolbar (more information on installing the MCC plug-in can be found here).
- In MCC Content Manager Wizard select, MCC Melody, then click Finish.
- Click on Project Resources, go to System, select Interrupt Manager and toggle the Global Interrupt Enable button.
- Go to Project Resources, click System, then click CLKCTRL and disable the Prescaler enable button.
- From the Device Resources,
go to Drivers and click the Timer window, add the TCE module, then
do the following configuration:
– Enable Timer: May 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)
- From the Device Resources,
click the Drivers window, 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 the Device Resources,
go to the Drivers window, add the EVSYS module, then follow the below
configuration:
– CHANNELS: CHANNEL0
– USERS: WEXA (Select CHANNEL0 rectangle from CHANNELS tab, hold and drag the cursor to the WEXA rectangle from the USERS tab)
- In the Pin Grid View window check if the WEX_WO[0-7] pins are locked as outputs on PORTA. When the boxes from Enable column from Waveform Output n are checked, the pins are also locked. To change the PORT click on a pin from another PORT in Pin Grid View.
- In the Project Resources window, click the Generate button so that MCC will generate all the specified drivers and configurations.
- Edit the main.c file, as
following:
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 modifies every 10 ms, and the range of the duty cycles will be accordingly.