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.
EXAMPLE D-1: RE-DEFINE ALLMODULES
#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:
- Enable signal I/O pin assigned by the user during MCC initialization.
- Pulse Width Modulation (PWM) output to drive the Trim line on the Module. This is assigned by the user during MCC Initialization.
- 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:
- EN4 for the Enable I/O pin
- PWM4 for the trim voltage PWM
- 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 flash.as. 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 flash.as file:
In the flash.as file, copy and paste Module 1 defines just below Module 3 defines :
EXAMPLE D-2: COPY/PASTE MODULE 1 PARAMETERS
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.
EXAMPLE D-3: MODULE 1 TO MODULE 4 PARAMETER CHANGE
;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 :
EXAMPLE D-4: DEFINE V04NOMINAL
// 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.
EXAMPLE D-5: ADD V04NOMINAL TO ARRAY
unsigned char VONominal[AllModules] =
{0,VO1Nominal, VO2Nominal, VO3Nominal, VO4Nominal};
Additionally, the M4 parameter defaults defined in flash.as 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.
EXAMPLE D-6: DEFINE MODULE 4 BUFFER INDEX PARAMETERS
// 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.