Intermediate
30 min

Experience the efficiency of our 4-20mA current loop solution with DAC161S997 and PIC18F47K42

Enhance your process automation

4-20mA T 2 Click with EasyPIC v8

Published Aug 09, 2023

Click board™

4-20mA T 2 Click

Dev Board

EasyPIC v8

Compiler

NECTO Studio

MCU

PIC18F47K42

Experience precise signal transmission with our advanced analog current loop transmitter, which provides seamless connectivity and compatibility with various industrial applications

A

A

Hardware Overview

How does it work?

4-20mA T 2 Click is based on the DAC161S997, a low-power 16-bit ΣΔ digital-to-analog converter (DAC) from Texas Instruments, realized as a ΣΔ modulator. Next to ΣΔ DAC, the DAC161S997 also contains an internal ultra-low power voltage reference and an internal oscillator to reduce power and component count in compact loop-powered applications. This architecture, where DAC's output current represents a multiplied copy of the filtered modulator output, ensures an excellent linearity performance while minimizing the device's power consumption. In addition to an industry-standard 4-20 mA current loop over the LOOP terminal, the DAC161S997 also has the possibility of a simple Highway Addressable

Remote Transducer (HART) modulator interfacing through an onboard HART TX terminal. It allows the injection of FSK-modulated digital data into the 4-20mA current loop. This Click board™ communicates with MCU using a 4-wire SPI serial interface with a maximum frequency of 10MHz, for data transfer and configuration of the DAC functions. The DAC161S997 supports both Mode 0 and Mode 3 of the SPI protocol. 4-20mA T 2 Click comes with an additional feature, as an interrupt, available on the ERR pin of the mikroBUS™ socket, the loop-error detection/reporting feature. By default, the DAC161S997 detects and reports several types of errors: loop error, SPI timeout error (channel error), frame error, and alarm current. In

the case of a fault condition or during the initial Power-Up sequence, the DAC161S997 will output current in either the upper or lower error current band. The band's choice is user-selectable via the appropriate position of an onboard jumper ERRL SEL, while the current error value is programmable through the SPI interface. This Click board™ can be operated only with a 3.3V logic voltage level. The board must perform appropriate logic voltage level conversion before using MCUs with different logic levels. Also, it comes equipped with a library containing functions and an example code that can be used, as a reference, for further development.

4-20mA T 2 Click hardware overview image

Features overview

Development board

EasyPIC v8 is a development board specially designed for the needs of rapid development of embedded applications. It supports many high pin count 8-bit PIC microcontrollers from Microchip, regardless of their number of pins, and a broad set of unique functions, such as the first-ever embedded debugger/programmer. 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, EasyPIC v8 provides a fluid and immersive working experience, allowing access anywhere and under any

circumstances at any time. Each part of the EasyPIC v8 development board contains the components necessary for the most efficient operation of the same board. In addition to the advanced integrated CODEGRIP programmer/debugger module, which offers many valuable programming/debugging options and seamless integration with the Mikroe software environment, the board 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 DEVICE, and CAN are also included, including the well-established mikroBUS™ standard, two display options (graphical and character-based LCD), and several different DIP sockets. These sockets cover a wide range of 8-bit PIC MCUs, from the smallest PIC MCU devices with only eight up to forty pins. EasyPIC 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.

EasyPIC v8 horizontal image

Microcontroller Overview

MCU Card / MCU

PIC18F47K42

Architecture

PIC

MCU Memory (KB)

128

Silicon Vendor

Microchip

Pin count

40

RAM (Bytes)

8192

Used MCU Pins

mikroBUS™ mapper

NC
NC
AN
NC
NC
RST
SPI Chip Select
RE0
CS
SPI Clock
RC3
SCK
SPI Data OUT
RC4
MISO
SPI Data IN
RC5
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
Loop-Error Interrupt
RB0
INT
NC
NC
TX
NC
NC
RX
NC
NC
SCL
NC
NC
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Schematic

4-20mA T 2 Click Schematic schematic

Step by step

Project assembly

EasyPIC v8 front image hardware assembly

Start by selecting your development board and Click board™. Begin with the EasyPIC v8 as your development board.

EasyPIC v8 front image hardware assembly
GNSS2 Click front image hardware assembly
MCU DIP 40 hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
EasyPIC v8 Access DIPMB 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 DIP 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 via UART Mode

1. Once the code example is loaded, pressing the "FLASH" button initiates the build process, and programs it on the created setup.

2. After the programming is completed, click on the Tools icon in the upper-right panel, and select the UART Terminal.

3. After opening the UART Terminal tab, first check the baud rate setting in the Options menu (default is 115200). If this parameter is correct, activate the terminal by clicking the "CONNECT" button.

4. Now terminal status changes from Disconnected to Connected in green, and the data is displayed in the Received data field.

UART_Application_Output

Software Support

Library Description

This library contains API for 4-20mA T 2 Click driver.

Key functions:

  • c420mat2_set_output_current - 4-20mA T 2 set output current function

  • c420mat2_get_status - 4-20mA T 2 set status function

  • c420mat2_set_lower_limit - 4-20mA T 2 set lower limit function

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 main.c
 * @brief 4-20mA T 2 Click example
 *
 * # Description
 * This example demonstrates the use of 4-20mA T 2 Click board™.
 * This driver provides functions to configure 
 * analog output current transfer over an industry standard 4-20mA current loop.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initialization of SPI module and log UART.
 * After driver initialization, default settings turn on the device.
 *
 * ## Application Task
 * This example demonstrates the use of the 4-20mA T 2 Click board™.
 * This example periodically changes the analog output current transfer 
 * from 4mA to 20mA and display status every 5 seconds.
 * Results are being sent to the UART Terminal, where you can track their changes.
 *
 * @author Nenad Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "c420mat2.h"

static c420mat2_t c420mat2;
static log_t logger;
static c420mat2_status_t status;

void display_status ( void )
{
    log_printf( &logger, " Status: \r\n" );
    if ( C420MAT2_STATUS_ERROR == status.ferr_sts )
    {
        log_printf( &logger, " - A frame error has occurred.\r\n" );
    }
    else
    {
        log_printf( &logger, " - No frame error occurred.\r\n" );
    }

    if ( C420MAT2_STATUS_ERROR == status.spi_timeout_err )
    {
        log_printf( &logger, " - The SPI interface has not received a valid command.\r\n" );
    }
    else
    {
        log_printf( &logger, " - The SPI interface has received a valid command.\r\n" );
    }

    if ( C420MAT2_STATUS_ERROR == status.loop_sts )
    {
        log_printf( &logger, " - A status loop error has occurred.\r\n" );
    }
    else
    {
        log_printf( &logger, " - No status loop error has occurred.\r\n" );
    }

    if ( C420MAT2_STATUS_ERROR == status.curr_loop_sts )
    {
        log_printf( &logger, " - A current loop error is occurring.\r\n" );
    }
    else
    {
        log_printf( &logger, " - No current loop error is occurring.\r\n" );
    }
    log_printf( &logger, " ----------------------------\r\n" );
}

void application_init ( void )
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    c420mat2_cfg_t c420mat2_cfg;  /**< Click config object. */

    /** 
     * 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 " );

    // Click initialization.
    c420mat2_cfg_setup( &c420mat2_cfg );
    C420MAT2_MAP_MIKROBUS( c420mat2_cfg, MIKROBUS_1 );
    if ( SPI_MASTER_ERROR == c420mat2_init( &c420mat2, &c420mat2_cfg ) )
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }

    if ( C420MAT2_ERROR == c420mat2_default_cfg ( &c420mat2 ) )
    {
        log_error( &logger, " Default configuration." );
        for ( ; ; );
    }

    log_info( &logger, " Application Task " );
    log_printf( &logger, " -----------------------------\r\n" );
    Delay_ms( 100 );
}

void application_task ( void )
{
    if ( C420MAT2_OK == c420mat2_set_output_current( &c420mat2, 4.0 ) )
    {
        log_printf( &logger, " Loop Current:  4.0 mA \r\n" );
        log_printf( &logger, " - - - - - - - - - - - - - - -\r\n" );
        if ( C420MAT2_OK == c420mat2_get_status ( &c420mat2, &status ) )
        {
            display_status( );    
        }
        Delay_ms( 5000 );
    }

    if ( C420MAT2_OK == c420mat2_set_output_current( &c420mat2, 10.0 ) )
    {
        log_printf( &logger, " Loop Current: 10.0 mA \r\n" );
        log_printf( &logger, " - - - - - - - - - - - - - - -\r\n" );
        if ( C420MAT2_OK == c420mat2_get_status ( &c420mat2, &status ) )
        {
            display_status( );    
        }
        Delay_ms( 5000 );
    }

    if ( C420MAT2_OK == c420mat2_set_output_current( &c420mat2, 15.0 ) )
    {
        log_printf( &logger, " Loop Current: 15.0 mA \r\n" );
        log_printf( &logger, " - - - - - - - - - - - - - - -\r\n" );
        if ( C420MAT2_OK == c420mat2_get_status ( &c420mat2, &status ) )
        {
            display_status( );    
        }
        Delay_ms( 5000 );
    }

    if ( C420MAT2_OK == c420mat2_set_output_current( &c420mat2, 20.0 ) )
    {
        log_printf( &logger, " Loop Current: 20.0 mA \r\n" );
        log_printf( &logger, " - - - - - - - - - - - - - - -\r\n" );
        if ( C420MAT2_OK == c420mat2_get_status ( &c420mat2, &status ) )
        {
            display_status( );    
        }
        Delay_ms( 5000 );
    }
}

void main ( void )
{
    application_init( );

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

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

Additional Support

Resources

Love this project?

'Buy This Kit' button takes you directly to the shopping cart where you can easily add or remove products.