2.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 Project Resources, go to System, select CLKCTRL and disable the Prescaler enable button; Clock Select: PLL Oscillator; PLL Multiplication Factor: Multiply by 16; PLL Source Division: DIV4; Prescaler B division PDIVB: DIV4.
- From the Device Resources,
go to Drivers and Timer window, add the TCE module, then do the
following configuration:
– Module Enable: Must be enabled by default. If not, toggle the button (it turns blue if enabled)
– Clock Selection: System clock (by default, the divider must be 1 - System clock)
– Waveform Generation Mode: Dual-Slope PWM mode with overflow on TOP and BOTTOM (DSBOTH)
– Requested Period[s]: 0.0001
– Duty Cycle 0 [%]: 20
– Duty Cycle 1 [%]: 40
– Duty Cycle 2 [%]: 60
– Duty Cycle 3 [%]: 80
– Waveform Output n : Check the boxes from the Enable column for Waveform Output 0, 1, 2, 3
– Duty Cycle High Resolution: Resolution increased by 4
– Scale mode: CMP values are scaled from Bottom, 0% DC
– Scaled Writing to registers: Fractional
– Amplitude Control Enable: Toggle the button (it turns blue if enabled)
– Amplitude Value: 1
- In the Pin Grid View window - check if the TCE_WO[0-3] 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. Select the PIN5 of PORTD as an output, then toggle it to see the amplitude and compare values changes.
- 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 section:
#include "mcc_generated_files/system/system.h" #include <util/delay.h>
Add macro definitions:
/* Calculated values for Period, CMP, Amplitude and OFFSET registers */ #define DUTY_CYCLE_20_PERCENT (0x1999) #define DUTY_CYCLE_40_PERCENT (0x3333) #define DUTY_CYCLE_60_PERCENT (0x4CCC) #define DUTY_CYCLE_80_PERCENT (0x660C) #define AMPLITUDE_MAX_DCY_50_PERCENT (0x4000) #define AMPLITUDE_MAX_DCY_75_PERCENT (0x6000) #define AMPLITUDE_MAX_DCY_100_PERCENT (0x8000) #define AMPLITUDE_MAX_DCY_150_PERCENT (0xC000)
Add function:
void Amplitude_Value_Set(uint16_t value) { /* Set the value of amplitude */ TCE0_AmplitudeSet(value); /* Rewrite the values in CMPBUF registers so that the duty cycle get scaled according * to the new value of amplitude */ TCE0_CompareAllChannelsBufferedSet(DUTY_CYCLE_20_PERCENT, DUTY_CYCLE_40_PERCENT, DUTY_CYCLE_60_PERCENT, DUTY_CYCLE_80_PERCENT); /* Toggle PD5 pin to know when the value of amplitude has changed */ IO_PD5_Toggle(); }
Edit the main function:
int main(void) { SYSTEM_Initialize(); while(1) { Amplitude_Value_Set(AMPLITUDE_MAX_DCY_50_PERCENT); _delay_ms(10); Amplitude_Value_Set(AMPLITUDE_MAX_DCY_75_PERCENT); _delay_ms(10); Amplitude_Value_Set(AMPLITUDE_MAX_DCY_100_PERCENT); _delay_ms(10); Amplitude_Value_Set(AMPLITUDE_MAX_DCY_150_PERCENT); _delay_ms(10); } }
- Now, the project can be built and run from MPLAB X IDE. At run time, the scaling value for the compare registers modifies every 10 ms, and the range of the duty cycles will be modified accordingly.