Measuring Power Consumption with PAC1934 Step 3: Configure MCU Resources with MCC
Launch MCC
Open the MCC plugin by clicking Tools > Embedded > MPLAB Code Configurator v4: Open/Close.
Or, you may click the MCC logo in the toolbar.
Use the same method to close MCC.
Configure System Module
Now you can start configuring the settings.
Click System Module in the upper left window.
Click on the Prescaler drop-down menu and select 4:1. The Fosc value should be 32 MHz.
Scroll down the System Module and go to the ICD section. Select Communicate on PGEC2 and PGED2 for the Emulator Pin Placement. These are the ICSP pins used in the PKOB on-board debugger/programmer.
Configure GPIO
GPIO needs to be configured to drive LEDs and read switch values on the demonstration board.
Select Pin Manager: Grid View in the bottom right window. Click each pin with a green lock as either an input or output as shown in the figure below.
2 In the Pin Module window, at the top right corner, select the Easy Setup tab and configure the selected GPIO as follows.
Pin Name | Function | Custom Name | Start High | Direction | Interrupt-on-change |
---|---|---|---|---|---|
RA8 | GPIO | LED_01 | No | Output | None |
RA9 | GPIO | LED_02 | No | Output | None |
RA10 | GPIO | OLED_CS | No | Output | None |
RA11 | GPIO | SWITCH_CH | No | Input | Negative Edge |
RA12 | GPIO | SWITCH_DEBUG | No | Input | Negative Edge |
RA13 | GPIO | METER_RST | Yes | Output | None |
RA14 | GPIO | OLED_RST | No | Output | None |
RC4 | GPIO | OLED_DC | No | Output | None |
RC8 | GPIO | METER_SLOW | Yes | Output | None |
The Pin Module settings should look like the image below:
Configure UART
Now we will add the peripherals needed for the project and configure the PAC1934 MCC library. The UART will be configured to send information to the MCP2221A Breakout Module and display data on Tera Term.
Add UART1 by opening the UART folder in the Device Resources window and clicking the + icon for UART1 [PIC24/dsPIC33/PIC32MM MCUs by Microchip Technology, Inc.].
In the UART1 configuration window, click the checkbox next to Redirect Printf to UART. This is used to display information to Tera Term.
Configure Timer2
Timer2 is used to maintain state machine timing.
Add TMR2 by opening the Timer folder in the Device Resources window and clicking the + icon for TMR2.
Update the TMR2 configuration window as follows:
- Disable the TMR, this is needed for the customer callback in the application
- Change the Prescaler to 1:64 to increase the Timer Period value
- Change Timer Period value to 200 ms
- Enable Timer Interrupt
Configure SPI
SPI is used to transfer information to be displayed on the OLED W Click.
Add SPI1 by opening the SPI folder in the Device Resources windows and clicking the + icon for SPI1 [PIC24/dsPIC33/PIC32MM MCUs by Microchip Technology, Inc.].
Verify Clock Edge is set to “Idle to Active” in the SPI1 configuration window.
Configure I2C
The I2C peripheral is used to communicate with the PAC1934 Click board. Both measurement data and control signals will be shared between the PIC24FJ256GA705 MCU and the PAC1934 metering IC using this communication channel.
Add I2C2 by opening the I2C folder in the Device Resources and clicking the + icon for I2C2 [PIC24/dsPIC33/PIC32MM MCUs by Microchip Technology, Inc.].
Change the baud rate to 400000 Hz (400 kHz) to match the I2C2 configuration window below.
Configure Interrupt Priorities
It is necessary to set up the interrupt priorities to ensure concurrent resource requests are resolved in the correct order.
Select the Interrupt Module in the Project Resource window.
Disable I2C Slave Events and change the priority of Timer2 interrupt to 4 to match the example below.
Configure Peripheral Pins
These assignments ensure the peripherals communicate using the correct pins on the demonstration board.
In the Pin Manager: Grid View window, in the lower right window tab, lock the following Modules to their assigned pins by clicking on the blue unlock symbol to turn it to a green lock:
- SPI1: SCK1OUT to RC6
- SPI1: SDI1 to RB9
- SPI1: SDO1 to RC2
- UART1: U1RX to RB8
- UART1: U1TX to RB7
Once complete, the Pin Manager: Grid View window will look like this:
Click Pin Module in the Project Resources window to check for consistency.