AN2039 - Adding a Code Module

Adding an additional Module to the Existing Code:

The MPLAB® Code Configurator (MCC) will generate code for the user that has been made fairly modular so that the existing application can be expanded to include more modules. In this example, we will add one additional Module 4 to our existing three modules in the program.

Each module has two functions which are defined:

APP_M4_Initialize() – Function which initializes the Module #4 State Machine and Hardware/Firmware
APP_M4_Tasks() – Function which runs the State Machine for the Module #4

Also, there is a data structure that is defined as appmData[4].XXXXX. This data structure has several elements in its example: State, DACValue, PWMValue, etc. which are the data elements for each module. They are residents in an array structure and the fourth element of that array will correspond to Module 4.

In file app.h the structure is shown below already exists, but one extra element needs to be added. A constant "AllModules" has been defined in app.h and this needs to be changed to 5.


#define AllModules 5 // M1 to M4 + the Input voltage = 5

By redefining the constant “AllModules”, most parameters will appropriately be adjusted to accommodate the additional Module 4 added to the application. Below are the parameters which need to be modified by the user.

Note: As mentioned several times in this application note, Module 0, or the zero element of this array, is assigned to the input voltage. The user does not need to copy/paste the entire structure, however, the structure needs to be increased by one element from 4 to 5, as shown above.

Module 4 has three hardware elements associated with it:

  1. Enable signal I/O pin assigned by the user during MCC initialization.
  2. Pulse Width Modulation (PWM) output to drive the Trim line on the Module. This is assigned by the user during MCC Initialization.
  3. Analog-to-Digital Converter (ADC) channel to sample and convert the Module Output Voltage. This ADC channel is defined by the user during MCC Initialization.

These three hardware elements in this application note were assigned the following designations during the MCC Initialization process:

  1. EN4 for the Enable I/O pin
  2. PWM4 for the trim voltage PWM
  3. VO4 for the ADC channel number

These values should be noted as they are required to be changed as we copy and paste an existing module to re-create an additional module.

Apart from the hardware elements, some firmware elements are also uniquely defined for each module depending on the voltage of that new module. In our application note, Module 4 is the 1.8 V module and it has its unique default values defined in the file The user can copy an existing module’s parameters in that file and then change the value with the new module parameter. Let us use Module 1 values and copy and paste them just below the Module 3 values in the file:

In the file, copy and paste Module 1 defines just below Module 3 defines :


M3MarginUVL    EQU 10            ;(0.2/5.0)*256    

;Default voltage/time defs for M1 = 5 V module
M1NormalOVL    EQU 155     ;(5.5V*0.55/5.0V)*256; 0.55 = Res. Divide Factor
M1NormalUVL    EQU 127     ;(4.5V*0.55/5.0V)*256; 0.55 = Res. Divide Factor
PWM1DC    EQU 100     ;
M1OnTime    EQU 250
M1OffTime    EQU 2000
M1MarginOVL    EQU 51            ;(1.2V/5.0V)* 256
M1MarginUVL    EQU 10            ;(0.2/5.0)*256

Note: Module 1 is the 5.0 V module, so all the parameters associated with it are for the 5.0 V Voltage.

Then, the user can edit and modify the M1 parameter to M4 and the associated values for 18 V to 5.0 V.


;voltage/time defs for M4 = 1.8 V module    
M4NormalOVL    EQU 118     ;(3.8V/5.0V)*256; 
M4NormalUVL    EQU 67            ;(2.8V/5.0V)*256;
PWM4DC    EQU 52            ;
M4OnTime    EQU 2000
M4OffTime    EQU 500
M4MarginOVL    EQU 51            ;(1.2V/5.0V)* 256
M4MarginUVL    EQU 10            ;(0.2/5.0)*256

In file, app.h, the new module needs a nominal voltage output level defined as :


// Nominal voltage for M4 = 1.8 V module
#define VO4Nominal          93     // (1.8V/5.0V)*256

Also, the VO4Nominal needs to be added to the array VONominal[AllModules] as the 5th element.


unsigned char VONominal[AllModules] =
{0,VO1Nominal, VO2Nominal, VO3Nominal, VO4Nominal};

Additionally, the M4 parameter defaults defined in needs to be saved in the Buf[] array used during rd/wrt operations on the Flash PM. Each element has its unique Buffer Index(BI) and so the user can copy an existing list for say Module 1 and paste and edit it for Module 4.


// Buf Index (BI) for M4 parameters
#define M4NormalOVLBI       24
#define M4NormalUVLBI       25
#define PWM4DCBI            26
#define M4OnTimeBI          27
#define M4OffTimeBI         28
#define M4MarginOVLBI       29
#define M4MarginUVLBI       30

Note: The Buf [ ] array is a 32 element buffer of unsigned 16-bit values.

In the app.h file, prototype definitions are done for the APP_M4_Initialize() and APP_M4_Tasks() functions. The user can copy an existing prototype definition and then change the designator.

Once all the needed changes in the app.h file have been made, the user can switch to the app.c file to make the changes for the APP_M4_Initialize(), SetUVFault(), SetOVFault(), TurnOnMod(), and TurnOffMod() functions. It is very easy to just copy and paste an existing module and then make the required module designator changes. Therefore, copy and paste the APP_M1_Initialize() function. Then change the designators in the function from 1 to 4 to make it an APP_M4_Initialize() function. In the SetUVFault(), SetOVFault(), TurnOnMod(), and TurnOffMod() functions, cut and paste an existing case statement for Module 1, change the designator from 1 to 4 and create a new case statement for Module 4.

Finally, in file main.c, you will have to make a function call for the APP_M4_Initialize() routine.

With all these changes made and double-checked, build and program the part and check for proper operation. In case of an error, most likely a typo has occurred during the copy and paste operation, so it would be prudent to check the new module edits and see that all the parameters are as desired.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.