Intermediate
30 min

Achieve precise power analysis with MCP39F511A and PIC18F87J11

Upgrade your U/I monitoring

PWR Meter Click with UNI-DS v8

Published Sep 30, 2023

Click board™

PWR Meter Click

Dev Board

UNI-DS v8

Compiler

NECTO Studio

MCU

PIC18F87J11

Our cutting-edge power monitoring solution excels at measuring and monitoring voltage and current values with exceptional precision, empowering you to optimize energy usage and ensure the reliability of your electrical systems

A

A

Hardware Overview

How does it work?

PWR Meter Click is based on the MCP39F511A, a power monitoring IC with on-chip 16-bit data processing and energy accumulation features from Microchip. This IC is an advanced power monitoring IC, capable of calculating power characteristics, based on the measurements taken from the connected load and power supply. The IC is able to calculate active, reactive, and apparent power, current and voltage RMS, line frequency, and power factor. In addition, it features several useful functions, such as the programmable event reporting, with the onboard LED labeled as the EVENT. PWR Meter click uses the UART interface for the communication with the host MCU. The communication is based on the SSI (Simple Sensor Interface) protocol and it is fairly simple to use. This protocol is widely used for a point-to-point communication between the host MCU and other sensor devices, such as the MCP39F511A. The default rate of the protocol is 9600 bps, but it can be configured by the user. More information about the communication protocol itself and the commands that can be used can be found in the MCP39F511A datasheet. However, PWR Meter click comes with a library which is compatible with all the MikroElektronika compilers. It contains functions which make working with the PWR Meter click even simpler, saving a lot of development time. The MCP39F511A incorporates two internal 24-bit Analog to Digital Converters (ADC), used to sample the voltage values on their differential inputs. One channel is used to

measure the voltage drop across the shunt resistor with the value of 0.2Ω, while the second channel samples the voltage across the voltage divider on the input terminal. The voltage drop across the shunt resistor allows calculating the current through the connected load, while the voltage divider allows voltage measurement across the connected load, scaling it down so that it can be measured. A third, 10-bit ADC is used to measure the ambient temperature, needed for compensation. It is connected to an output of the MCP9700A, a low power linear active integrated thermistor, from Microchip. The load should be connected to the 3-pole input terminal, between the input labeled with the L letter and the input labeled as the V-. The power supply should be also connected to the Click board™. Its hot (positive) end should be connected to the V+ labeled input of the 3-pole terminal, while the negative end should be connected to the input labeled as V-. The power supply should not exceed 50V. To better understand connection scheme, please take a look at the picture below. The Click board™ features a complete galvanic isolation of the measured circuit. The power for the high voltage section is provided by the MCP1661, an efficient, integrated boost (step-up) DC/DC converter, from Microchip. The integrated boost converter is built using the flyback topology, allowing a complete galvanic isolation between the primary and secondary side, since it uses a transformer instead of a coil. The input voltage of the DC-DC converter

is selected by an SMD jumper, labeled as the VCC SEL. The boosted voltage on the secondary of the transformer is further conditioned by the MCP17545, an LDO regulator from Microchip, and it is fixed to 3V. The output of the LDO is now galvanically isolated from the rest of the circuit, and it is used to supply the MCP39F511A monitoring IC and the additional thermal sensor with power. Turning on or off the MCP1661 DC/DC converter controls the operation of the PWR Meter click itself. The CS pin of the mikroBUS™ is routed to the EN pin of the MCP1661 converter, allowing the user to turn off the power for the monitoring IC. Setting the CS pin to a HIGH logic level will disable the converter, allowing current to sink through the transistor, thus setting the EN pin to a LOW logic level. Otherwise, the EN pin is pulled up to a HIGH level with the resistor, and the converter is enabled by default (when the CS pin of the mikroBUS™ is left floating, or driven to a LOW logic level) Galvanic isolation of the MCP39F511A data lines is done by using a bi-directional logic gate optocoupler, labeled as FOD8012A from ON Semiconductors. UART RX and TX lines from the MCP39F511A IC run through the integrated optocouplers and are also completely isolated from the low voltage circuitry. The previously mentioned VCC SEL jumper also selects the voltage for the optocoupler, allowing both 3.3V and 5V tolerant MCUs to be interfaced with the Click board™.

PWR Meter Click hardware overview image

Features overview

Development board

UNI-DS v8 is a development board specially designed for the needs of rapid development of embedded applications. It supports a wide range of microcontrollers, such as different STM32, Kinetis, TIVA, CEC, MSP, PIC, dsPIC, PIC32, and AVR MCUs regardless of their number of pins, and a broad set of unique functions, such as the first-ever embedded debugger/programmer over WiFi. The development board is well organized and designed so that the end-user has all the necessary elements, such as switches, buttons, indicators, connectors, and others, in one place. Thanks to innovative manufacturing technology, UNI-DS v8 provides a fluid and immersive working experience, allowing access anywhere and under any

circumstances at any time. Each part of the UNI-DS v8 development board contains the components necessary for the most efficient operation of the same board. An advanced integrated CODEGRIP programmer/debugger module offers many valuable programming/debugging options, including support for JTAG, SWD, and SWO Trace (Single Wire Output)), and seamless integration with the Mikroe software environment. Besides, it also includes a clean and regulated power supply module for the development board. It can use a wide range of external power sources, including a battery, an external 12V power supply, and a power source via the USB Type-C (USB-C) connector. Communication options such as USB-UART, USB

HOST/DEVICE, CAN (on the MCU card, if supported), and Ethernet is also included. In addition, it also has the well-established mikroBUS™ standard, a standardized socket for the MCU card (SiBRAIN standard), and two display options for the TFT board line of products and character-based LCD. UNI-DS v8 is an integral part of the Mikroe ecosystem for rapid development. Natively supported by Mikroe software tools, it covers many aspects of prototyping and development thanks to a considerable number of different Click boards™ (over a thousand boards), the number of which is growing every day.

UNI-DS v8 horizontal image

Microcontroller Overview

MCU Card / MCU

default

Type

8th Generation

Architecture

PIC

MCU Memory (KB)

128

Silicon Vendor

Microchip

Pin count

80

RAM (Bytes)

3904

Used MCU Pins

mikroBUS™ mapper

NC
NC
AN
NC
NC
RST
Power Enable
PJ0
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
NC
NC
INT
UART TX
PG1
TX
UART RX
PG2
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

Take a closer look

Schematic

PWR Meter Click Schematic schematic

Step by step

Project assembly

Fusion for PIC v8 front image hardware assembly

Start by selecting your development board and Click board™. Begin with the UNI-DS v8 as your development board.

Fusion for PIC v8 front image hardware assembly
GNSS2 Click front image hardware assembly
SiBRAIN for PIC32MZ1024EFK144 front image hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
v8 SiBRAIN Access MB 1 - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
NECTO Compiler Selection Step Image hardware assembly
NECTO Output Selection Step Image hardware assembly
Necto image step 6 hardware assembly
Necto image step 7 hardware assembly
Necto image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Necto PreFlash Image hardware assembly

Track your results in real time

Application Output

After pressing the "FLASH" button on the left-side panel, it is necessary to open the UART terminal to display the achieved results. By clicking on the Tools icon in the right-hand panel, multiple different functions are displayed, among which is the UART Terminal. Click on the offered "UART Terminal" icon.

UART Application Output Step 1

Once the UART terminal is opened, the window takes on a new form. At the top of the tab are two buttons, one for adjusting the parameters of the UART terminal and the other for connecting the UART terminal. The tab's lower part is reserved for displaying the achieved results. Before connecting, the terminal has a Disconnected status, indicating that the terminal is not yet active. Before connecting, it is necessary to check the set parameters of the UART terminal. Click on the "OPTIONS" button.

UART Application Output Step 2

In the newly opened UART Terminal Options field, we check if the terminal settings are correct, such as the set port and the Baud rate of UART communication. If the data is not displayed properly, it is possible that the Baud rate value is not set correctly and needs to be adjusted to 115200. If all the parameters are set correctly, click on "CONFIGURE".

UART Application Output Step 3

The next step is to click on the "CONNECT" button, after which the terminal status changes from Disconnected to Connected in green, and the data is displayed in the Received data field.

UART Application Output Step 4

Software Support

Library Description

This library contains API for PWR Meter Click driver.

Key functions:

  • pwrmeter_read_reg_word - Function reads 16-bit data from the desired register

  • pwrmeter_read_reg_dword - Function reads 32-bit data from the desired register

  • pwrmeter_read_reg_signed - Function reads signed 16bit or 32bit data from the desired register.

Open Source

Code example

This example can be found in NECTO Studio. Feel free to download the code, or you can copy the code below.

/*!
 * \file 
 * \brief PwrMeter Click example
 * 
 * # Description
 * This click is capable of measuring voltage and current through the load, connected to either 
 * AC or DC power source. It is used to calculate all the measurement parameters, returning 
 * values of multiple power parameters directly, over the UART interface, reducing the processing 
 * load on the host MCU. These parameters include active, reactive, and apparent power, current 
 * and voltage RMS, line frequency, and power factor.
 *
 * The demo application is composed of two sections :
 * 
 * ## Application Init 
 * Initializes UART interface, puts output of regulator in active state and
 * configures gain channel and uart baud rate.
 * 
 * ## Application Task  
 * Reads voltage, current and power measurements from data registers, then converts this values
 * to determined units and logs all results on uart terminal each second.
 * 
 * ## Additional Function
 * - void check_response ( ) - Displays an appropriate message on USB UART 
 * if there's an error occurred in the last response from the module.
 * 
 * ## Note
 * Do not apply higher voltage than 60V to this board! This click is designed for lower voltage 
 * monitoring and evaluation of the MCP39F511A and its basic functionalities. 
 * 
 * \author MikroE Team
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "pwrmeter.h"
#include "string.h"
#include "math.h"

// ------------------------------------------------------------------ VARIABLES


static pwrmeter_t pwrmeter;
static log_t logger;

PWRMETER_RETVAL response_byte;
uint16_t voltage_rms;
uint32_t current_rms;
uint32_t active_power;
uint32_t reactive_power;
uint32_t apparent_power;
int32_t power_factor;
uint8_t status_byte;

float meas_data[ 6 ];

// ------------------------------------------------------- ADDITIONAL FUNCTIONS

void check_response ( )
{
    if ( response_byte != PWRMETER_SUCCESSFUL )
    {
        switch ( response_byte )
        {
            case PWRMETER_ADDRESS_FAIL :
            {
                log_printf( &logger, "Wrong address parameter\r\n" );
            break;
            }
            case PWRMETER_CHECKSUM_FAIL :
            {
                log_printf( &logger, "Checksum fail\r\n" );
            break;
            }
            case PWRMETER_COMMAND_FAIL :
            {
                log_printf( &logger, "Command can't be performed\r\n" );
            break;
            }
            case PWRMETER_NBYTES_FAIL :
            {
                log_printf( &logger, "Number of bytes is out of range\r\n" );
            break;
            }
            case PWRMETER_PAGE_NUM_FAIL :
            {
                log_printf( &logger, "Page number is out of range\r\n" );
            break;
            }
            default :
            {
            break;
            }
        }
    }
}

// ------------------------------------------------------ APPLICATION FUNCTIONS

void application_init ( void )
{
    log_cfg_t log_cfg;
    pwrmeter_cfg_t cfg;

    /** 
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX 
     * are defined as HAL_PIN_NC, you will 
     * need to define them manually for log to work. 
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info( &logger, "---- Application Init ----" );
    Delay_ms( 100 );

    //  Click initialization.

    pwrmeter_cfg_setup( &cfg );
    PWRMETER_MAP_MIKROBUS( cfg, MIKROBUS_1 );
    pwrmeter_init( &pwrmeter, &cfg );
    Delay_ms( 500 );
    
    pwrmeter_enable( &pwrmeter, PWRMETER_ENABLE );
    Delay_ms( 100 );
    
    response_byte = pwrmeter_write_reg_dword ( &pwrmeter, PWRMETER_SYS_CONFIG_REG, PWRMETER_VOLT_GAIN_1 | PWRMETER_CURR_GAIN_8 | PWRMETER_UART_BR_9600 );
    check_response( );
    response_byte = pwrmeter_send_command( &pwrmeter, PWRMETER_SAVE_TO_FLASH_COMM );
    check_response( );

    log_printf( &logger, "PWR Meter is initialized\r\n" );
    Delay_ms( 100 );
}

void application_task ( void )
{
    response_byte = pwrmeter_read_reg_word( &pwrmeter, PWRMETER_VOLT_RMS_REG, &voltage_rms );
    check_response( );
    response_byte = pwrmeter_read_reg_dword( &pwrmeter, PWRMETER_CURR_RMS_REG, &current_rms );
    check_response( );
    response_byte = pwrmeter_read_reg_dword( &pwrmeter, PWRMETER_ACTIVE_PWR_REG, &active_power );
    check_response( );
    response_byte = pwrmeter_read_reg_dword( &pwrmeter, PWRMETER_REACTIVE_PWR_REG, &reactive_power );
    check_response( );
    response_byte = pwrmeter_read_reg_dword( &pwrmeter, PWRMETER_APPARENT_PWR_REG, &apparent_power );
    check_response( );
    response_byte = pwrmeter_read_reg_signed( &pwrmeter, PWRMETER_PWR_FACTOR_REG, PWRMETER_16BIT_DATA, &power_factor );
    check_response( );
    
    meas_data[ 0 ] = ( float ) voltage_rms / 100;
    meas_data[ 1 ] = ( float ) current_rms / 1000;
    meas_data[ 2 ] = ( float ) active_power / 100000;
    meas_data[ 3 ] = ( float ) reactive_power / 100000;
    meas_data[ 4 ] = ( float ) apparent_power / 100000;
    meas_data[ 5 ] = ( float ) power_factor / 32767;
    
    response_byte = pwrmeter_get_status( &pwrmeter, &status_byte );
    check_response( );
    
    if ( ( status_byte & PWRMETER_DCMODE_MASK ) != 0 )
    {
        log_printf( &logger, "DC mode\r\n" );
    }
    else
    {
        log_printf( &logger, "AC mode\r\n" );
    }
    

    log_printf( &logger, "RMS voltage:  " );
    if ( ( ( status_byte & PWRMETER_DCMODE_MASK ) != 0) && ( ( status_byte & PWRMETER_DCVOLT_SIGN_MASK ) == 0 ) )
    {
        log_printf( &logger, "-" );
    }
    log_printf( &logger, "%.2f[ V ]\r\n", meas_data[ 0 ] );
    
    
    log_printf( &logger, "RMS current:  " );
    if ( ( ( status_byte & PWRMETER_DCMODE_MASK ) != 0 ) && ( ( status_byte & PWRMETER_DCCURR_SIGN_MASK ) == 0 ) )
    {
        log_printf( &logger, "-" );
    }
    log_printf( &logger, "%.2f[ mA ]\r\n", meas_data[ 1 ] );
    
    
    log_printf( &logger, "Active power:  " );
    if ( ( status_byte & PWRMETER_PA_SIGN_MASK ) == 0 )
    {
        log_printf( &logger, "-" );
    }
    log_printf( &logger, "%.2f[ W ]\r\n", meas_data[ 2 ] );
    
    
    log_printf( &logger, "Reactive power:  " );
    if ( ( status_byte & PWRMETER_PR_SIGN_MASK ) == 0 )
    {
        log_printf( &logger, "-" );
    }
    log_printf( &logger, "%.2f[ VAr ]\r\n", meas_data[ 3 ] );
    
   
    log_printf( &logger, "Apparent power:  " );
    log_printf( &logger, "%.2f[ VA ]\r\n", meas_data[ 4 ] );

    
    log_printf( &logger, "Power factor:  %.2f\r\n", meas_data[ 5 ] );
    log_printf( &logger, "-----------------------------------\r\n" );
    
    Delay_ms( 1000 );
}

void main ( void )
{
    application_init( );

    for ( ; ; )
    {
        application_task( );
    }
}


// ------------------------------------------------------------------------ END

Additional Support

Resources