3.2 MCC Melody Implementation

To generate this project using MPLAB Code Configurator Melody, MCC Melody (MCC Classic is not supported), follow the next steps:
  1. Create a new MPLAB X IDE project for AVR16EB32.
  2. Open MCC from the toolbar (more information on installing the MCC plug-in can be found here).
  3. In MCC Content Manager Wizard, select MCC Melody, then click Finish.
  4. From the Device Resources, go to Drivers window, Timer and add TCE module, then do the following configuration:

    – Module Enable - toggle the button (it turns blue if enabled).

  5. From the Device Resources, go to 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 the TCE module)

    – Override Settings: Check all the boxes from the Output Enable column for the Waveform Output[0-7]

    – Pattern Generation Mode Enable: Toggle the button (it turns blue if enabled)

    – Pattern Generation Actions: Check all the boxes from the Override Enable column and set for each output a desired state (LOW or HIGH), to set a pattern

  6. In the Pin Grid View window - check if the WEX_WO[0-7] pins are locked as outputs on PORTA. The pins are locked when the boxes from Output Enable column from Override Settings are checked. To change the PORT,click on a pin from another PORT in Pin Grid View.
  7. In the Project Resources window, click the Generate button so that MCC will generate all the specified drivers and configurations.
  8. 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:

    
    /* Patterns that are written in the PGMOUT register */
    #define COMPLEMENTARY_PATTERN           (WEX_PGMOUT6_bm | WEX_PGMOUT4_bm | 
                                             WEX_PGMOUT2_bm | WEX_PGMOUT0_bm)
    #define STAIRCASE0_PATTERN              (WEX_PGMOUT0_bm)
    #define STAIRCASE1_PATTERN              (WEX_PGMOUT1_bm)
    #define STAIRCASE2_PATTERN              (WEX_PGMOUT2_bm)
    #define STAIRCASE3_PATTERN              (WEX_PGMOUT3_bm)
    #define STAIRCASE4_PATTERN              (WEX_PGMOUT4_bm)
    #define STAIRCASE5_PATTERN              (WEX_PGMOUT5_bm)
    #define STAIRCASE6_PATTERN              (WEX_PGMOUT6_bm)
    #define STAIRCASE7_PATTERN              (WEX_PGMOUT7_bm)
    #define PATTERN_RESET                   (0x00)

    Add functions:

    
    void Complementary_Pattern_Set(void)
    {
        WEX0_PatternGenerationOutputSet(COMPLEMENTARY_PATTERN);
        uint8_t complementary_pattern = COMPLEMENTARY_PATTERN;
        _delay_us(25);
    
        /* Complementary signals pattern */
        for(uint8_t i = 0; i < 9; i++)
        {
            /* Complementary_pattern variable changes at every step */
            complementary_pattern = ~complementary_pattern;
    
            /* Toggle the pattern for each of the WEX's output */
            WEX0_PatternGenerationOutputSet(complementary_pattern);
    
            /* Software delay added in order for the toggle to be visible */
            _delay_us(25);
        }
        
        /* Put all signals in low '0' logic and wait 250us to see the transition from one of
         * the complementary patterns to the stairs pattern*/
        WEX0_PatternGenerationOutputSet(PATTERN_RESET);
    }
    
    void Stairs_Pattern_Set(void)
    {
        /* Each of the signals switch from low to high one at a time in increasing order
         * generate a stairs increment pattern*/
        WEX0_PatternGenerationOutputSet(STAIRCASE7_PATTERN);
    
        /* Software delay added in order for the increment to be visible */
        _delay_us(5);
        WEX0_PatternGenerationOutputSet(STAIRCASE6_PATTERN);
        _delay_us(5);
        WEX0_PatternGenerationOutputSet(STAIRCASE5_PATTERN);
        _delay_us(5);
        WEX0_PatternGenerationOutputSet(STAIRCASE4_PATTERN);
        _delay_us(5);
        WEX0_PatternGenerationOutputSet(STAIRCASE3_PATTERN);
        _delay_us(5);
        WEX0_PatternGenerationOutputSet(STAIRCASE2_PATTERN);
        _delay_us(5);
        WEX0_PatternGenerationOutputSet(STAIRCASE1_PATTERN);
        _delay_us(5);
        
        /* Each of the signals switch from low to high one at a time in decreasing order
         * generate a stairs decrement pattern*/
        WEX0_PatternGenerationOutputSet(STAIRCASE0_PATTERN);
    
        /* Software delay added in order for the decrement to be visible */
        _delay_us(5);
        WEX0_PatternGenerationOutputSet(STAIRCASE1_PATTERN);
        _delay_us(5);
        WEX0_PatternGenerationOutputSet(STAIRCASE2_PATTERN);
        _delay_us(5);
        WEX0_PatternGenerationOutputSet(STAIRCASE3_PATTERN);
        _delay_us(5);
        WEX0_PatternGenerationOutputSet(STAIRCASE4_PATTERN);
        _delay_us(5);
        WEX0_PatternGenerationOutputSet(STAIRCASE5_PATTERN);
        _delay_us(5);
        WEX0_PatternGenerationOutputSet(STAIRCASE6_PATTERN);
        _delay_us(5);
        WEX0_PatternGenerationOutputSet(STAIRCASE7_PATTERN);
        _delay_us(5);
        
        /* Put all signals in low '0' logic and wait 250us to see the transition from the
         * stairs pattern to one of the complementary patterns */
        WEX0_PatternGenerationOutputSet(PATTERN_RESET);
    }

    Edit the main function:

    int main(void)
    {
        SYSTEM_Initialize();
    
        while(1)
        {
            Complementary_Pattern_Set();
            _delay_us(250);
            Stairs_Pattern_Set();
            _delay_us(250);
        }    
    }
  9. Now, the project can be built and run using MPLAB X IDE.