Preventing Microchip’s MCC Melody from Overwriting Your Code

Microchip’s MPLAB Code Configurator (MCC) is a well-documented graphical interface designed to simplify microcontroller configuration. Instead of spending hours in the datasheet and manually configuring the peripherals, a programmer can intuitively configure the microcontroller in minutes.

This article identifies function registration as the essential bridge to join the MCC generated code with the programmer’s main.c program. This is an elegant solution, but it is also a gotcha that traps novice programmers. With proper function registration your code will no longer be overwritten by the MCC tool.

We will use the PIC16F13145 curiosity evaluation board as an example (Figure 1). The seed program may be downloaded here.
Digital.X.zip (957.8 KB)

Figure 1: Image of the PIC16F13145 Curiosity on the author’s breadboard with supporting pushbuttons and LEDs.

Using the MCC

A typical MCC project is shown in Figure 2. In this example, the Configuration Logic Block (CBL) and Timer Zero (TMR0) have been added to the project. There are also system-level aspects including the interrupt service routine and items such as clock and pin configurations.

To keep this article short, we will focus on what happens after we press the highlighted “Generate” button. Refer to this page for more information.

Figure 2: Project configuration of a PIC16 using MPLAB’s MCC with the Generate button highlighted.

Where does MCC code generator place its data?

Figure 3 shows the project file structure. The MCC generated code is placed in the clearly labeled mcc_generated_files branch.

Must-Know Facts about the MCC

  • MCC will overwrite the contents of mcc_generated_files whenever the generate button (Figure 2) is pressed.

  • You will lose any code that was placed in these files, causing a great deal of frustration.

  • Files outside this structure will not be touched. For example, main.c is not modified by MCC.


Digital.X.zip (957.8 KB)

Figure 3: File structure for the MPLAB project with emphasis on the mcc_generated_files.

How do we link MCC generated files with main.c?

This is the heart of the article. Instead of modifying the MCC files, we take advantage of the function registration mechanism built into C.

Example of Function Registration.

Suppose we had a function Bob() that is called from main(). At the machine level, we know that function Bob is assigned a memory address that is acted upon by the program counter. We could use a function pointer to redirect Bob to Alice(). Now, when we call Bob, it’s really Alice that does the work.
The preferred MPLAB code template is shown in this listing:

  • The super loop does nothing.
  • The TMR0 interrupt service routine is encapsulated in the function My_1ms_Callback.
    • TMR0 produces a 1 ms interrupt.
    • The LED connected to port B, pin 5 toggles once a second.
#include "mcc_generated_files/system/system.h"

volatile uint16_t msTicks = 0;

static void My_1ms_Callback(void){
    if (++msTicks >= 500) {
        msTicks = 0;
        LATBbits.LATB5 ^= 1;
    }
}

int main(void){
    
    SYSTEM_Initialize();
    
    /*
      * Register the custom 1ms callback with MCC’s timer module.
    */
    TMR0_OverflowCallbackRegister(My_1ms_Callback);

    INTERRUPT_GlobalInterruptEnable(); 
    INTERRUPT_PeripheralInterruptEnable(); 

    while(1){
        ;
    }    
}

Listing 1: Minimalist code demonstrating the function registration process.

Operation of the Function Registration

Our objective is to utilize the code contained with the mcc_generated_files (Figure 3) without making any changes to the MCC files—they would be clobbered if we did. The solution is to register My_1ms_Callback to the MCC generated code. This is like our previous example, we call Bob() but Alice() does the work.

The function registration is performed via this simple line of code:

    TMR0_OverflowCallbackRegister(My_1ms_Callback);

That’s it, that’s the key to worry-free linking of main() to the MC- generated code. The function registration processes the bridge to success.

Tech Tip: MCC is a great tool for the experienced programmer. However, the novice would benefit from manually configuring the registers. Spending a few weeks in the datasheet will strengthen their skills. IMO, it’s like learning to ride a bicycle. We build datasheet muscle memory that will serve us well when things go wrong. Also, studying one microcontroller such as the featured PIC16 is the gateway to other devices including the 32-bit cousins.

Too bad we don’t still have datasheets in book form. The bugs were always highlighted and double underlined. Like writing to the TRIS and not the port – that little bug cost me hours of troubleshooting.

Parting Thoughts

I hate to admit this, Like many of you I did not read the Microchip instructions. There was a time when I was frustrated with the tools. I trust this tip will save you some time.

Let’s end with a salute to Kernighan and Ritchie for the function pointer.

Best wishes,

APDahlen

Related Articles by this Author

If you enjoyed this article, you may also find these related articles helpful:

About This Author

Aaron Dahlen, LCDR USCG (Ret.), serves as an application engineer at DigiKey. He has a unique electronics and automation foundation built over a 27-year military career as a technician and engineer which was further enhanced by 12 years of teaching (interwoven). With an MSEE degree from Minnesota State University, Mankato, Dahlen has taught in an ABET-accredited EE program, served as the program coordinator for an EET program, and taught component-level repair to military electronics technicians.

Dahlen has returned to his Northern Minnesota home, completing a decades-long journey that began as a search for capacitors. Read his story here.