Objective
This tutorial shows how to interface the PAC1934 Quad DC Power Monitor to a PIC microcontroller in an energy metering application using Microchip’s MPLAB Code Configurator (MCC). MCC is used to configure peripheral drivers such as GPIO, SPI, I2C, UART, and the PAC1934 driver.
Overview
The tutorial makes use of MikroElektronika’s Click Board™ and Microchip’s Curiosity Board to simplify hardware integration.
The tutorial showcases the capability of the PAC1934 to provide the instantaneous voltage, current, and power as well as the accumulated power. Together with the PAC1934 driver, generated using MCC, this tutorial shows you how to measure and display these parameters.
Microchip’s PAC1934 is a four-channel power and energy monitoring device. A high-voltage multiplexer sequentially connects the inputs to a bus voltage monitor and current sense amplifier that feed high-resolution ADCs. Digital circuitry performs power calculations and energy accumulation.
Energy monitoring intervals can range from 1 ms up to 36 hours or longer. Bus voltage, sense resistor voltage, and accumulated proportional power are stored in registers for retrieval by the system master or embedded controller. The sampling rate and energy integration period can be controlled over SMBus or I2C. Active channel selection, one-shot measurements, and other controls are also available.
The PAC1934 device uses real-time calibration to minimize offset and gain errors. No input filters are required for this device.
For more details about the features of the PAC1934 device family, refer to the PAC1931/PAC1932/PAC1933/PAC1934 Data Sheet.
The MPLAB® Code Configurator (MCC) is a free graphical programming environment that generates seamless, easy-to-understand C code that is inserted into your project. It enables and configures a rich set of peripherals and functions using an intuitive interface. It is integrated into MPLAB X IDE to provide a powerful and easy-to-use development platform.
This training makes use of the PIC24FJ256GA7 Curiosity Board, OLED W click, PAC1934 click and the MCP2221A Breakout Module, which are available from Microchip Purchasing and Client Services.
The application initializes the hardware and remains in an idle state. Every 200 ms, the metering data is read and displayed. Error states are available in case the meter read or display update is unsuccessful.
The S1 button selects which power channel to display. S2 enables or disables the serial display.
This application utilizes:
- SPI1 for display communication
- UART1 to transfer information to the MCP2221A Breakout Module for USB communication to PC
- I2C2 for PAC1934 click communication
- General Purpose I/O (GPIO) to control 2 LEDs and read two switches
In the process, the lab also demonstrates the use of a callback function triggered by Timer2 to read and display measurements.
Two Ways to Use This Tutorial
- Create the project from scratch:
- Use the provided source files and step-by-step instructions.
- Use the solution project as an example:
- Build the solution project and program it to the PIC24FJ256GA7 Curiosity Board to observe the expected behavior.
Lab Objectives
- Create an MPLAB X IDE MCC project for a PIC24FJ256GA705 microcontroller from scratch
- Use MCC to configure and generate peripheral libraries (PLIB) code for the following peripherals:
- SPI
- UART
- I2C
- Port pins
- Use MCC Peripheral Library (PLIB) Application Programming Interfaces (APIs) to implement the application
Materials
Hardware Tools
PIC24FJ256GA7 Curiosity Development Board
The PIC24FJ256GA7 Curiosity Development Board is a cost-effective, fully integrated development board featuring the low-cost and low-power 16-bit PIC24FJ256GA705 microcontroller. This board also features a fully integrated programmer/debugger, two MikroElektronika mikroBUS™ interfaces that give the user access to over 250+ add-on Click boards™, user LEDs, RGB LED, pushbuttons, potentiometer, and a prototyping area for developing a wide variety of general-purpose applications.
MCP2221A USB to UART/I2C/SMBus
The MCP2221A Breakout Module board demonstrates the capabilities of the MCP2221A device as a USB-to-UART/I2C/SMBus protocol converter.
MikroElectronika PAC1934 Click
The PAC1934 click board from MikroElectronika carries the PAC1934 four channel DC power/energy monitor from Microchip. The click is designed to run on either 3.3 V or 5 V power supply. It communicates with the target microcontroller over an I2C interface.
MikroElectronika OLED W Click
The OLED W click board from MikroElectronika carries a 96 x 39 px white monochrome passive matrix OLED display. The display is bright, has a wide viewing angle, and low power consumption.
Note: The Curiosity series evaluation kits include an on-board, embedded debugger. No external tools are necessary to program or debug the PIC24FJ256GA705. For programming/debugging, the debugger connects to the host PC through the USB Micro-B connector on the PIC24FJ256GA7 Curiosity Board.
Hardware Connection Setup
Apart from the hardware tools listed above, the following items are required:
- USB Type-A male to Micro-B male cable for programming and debugging.
- USB Type-A male to Micro-B male cable for USB data logging from MCP2221 module.
- Wire to connect UART TX pin to MCCP2221 RX pin for data logging.
To simulate a multiple channel load, use a 5 V power supply and four 150 Ω resistors. The power supply can be turned off at this time. Refer to the following diagram for connections to the PAC1934 click:
Connection Diagram
The application has the OLED W click connected to the PIC24FJ256GA705 over SPI, PAC1934 over I2C, and UART to USB converter over UART.
Software Tools
Note:
This project has been verified to work with the following versions of software tools:
MPLAB X IDE v5.45 (How to install MPLAB X IDE)
MPLAB XC16 Compiler v1.50 (How to install MPLAB XC16)
MPLAB Code Configurator (MCC) v4.0.2 (How to install MCC)
PAC1934 MCC Library v2.2 (Installation covered in this tutorial)
Tera Term v4.105
File extraction utility such as 7-Zip that supports ZIP files
Because we regularly update our tools, occasionally you may discover an issue while using the newer versions. If you suspect that to be the case, we recommend that you double-check and use the same versions that the project was tested with.
Lab Source Files and Solutions
This ZIP file contains the completed solution project for this lab. It also contains the source files needed to perform the lab as per the following step-by-step instructions.
The contents of this ZIP file need to be placed in a folder of your choice.
Note: Because MCC generates source and header files and libraries under the project folder, the contents of this ZIP file can be placed in any folder of your choice.
Extracting the ZIP file creates the following folders:
- DisplayDriver contains the display driver for the OLED W Click board
- MeterDrivers contains the driver for the PAC1934 Click board
- TestApp contains software to test the application (not used in this tutorial)
- UserInterface configures the user interface
- Pac1934-app-pic24fjxxxga7.X.zip contains the solution project. It can be built directly and programmed on the hardware to observe the expected behavior
Lab Index
Note: Steps 1 - 5 must be completed before you are ready to build, download, and run the application.
- Step 1.1 - Attach PAC1934 Click board
- Step 1.2 - Attach OLED W Click
- Step 1.3 - Connect USB from PIC24FJ256GA7 Curiosity board to computer
- Step 1.4 - Connect USB from MCP2221A Breakout Module to computer
- Step 1.5 - Attach a jumper wire from UART TX to MCP2221A Breakout Module f
- Step 1.6 - Connect 5 V power supply to PAC1934 Click board
- Step 1.7 - Attach four 150 Ω resistors from PAC1934 Click board to the ground
Step 2: Create and Setup MPLAB X IDE project
- Step 2.1 - Create project
- Step 2.2 - Download example code
- Step 2.3 - Copy PAC1934 library files from demo project
- Step 2.4 - Add content to MPLAB X IDE project
- Step 2.5 - Install MCC
- Step 2.6 - Install PAC1934 MCC Library
Step 3: Configure MCU resources with MCC
- Step 3.1 - Launch MCC
- Step 3.2 - System Module
- Step 3.3 - GPIO
- Step 3.4 - UART
- Step 3.5 - Timer2
- Step 3.6 - SPI
- Step 3.7 - I2C
Step 4: Configure PAC193x MCC Library
- Step 4.1 - Add PAC193x library to MCC
- Step 4.2 - Configure I2C communication
- Step 4.3 - Update main.c
Step 5: Include C Directories, Build and Program
- Step 5.1 - Set project properties
- Step 5.2 - Build project
- Step 5.3 - Program MCU
- Step 6.1 - Open, configure Tera Term
- Step 6.2 - Select displayed parameters
- Step 6.3 - Verify correct operation
- Step 6.4 - Results
- Step 6.5 - Analysis
- Step 6.6 - Conclusions