Intermediate
20 min

Receive and process RF signals with MICRF220 and PIC18F45K40

ASK/OOK 315MHz RF receiver with RSSI and squelch capabilities

MICRF RX Click with EasyPIC v8

Published Apr 23, 2024

Click board™

MICRF RX Click

Dev. board

EasyPIC v8

Compiler

NECTO Studio

MCU

PIC18F45K40

Develop solutions that enable the remote control of various devices, such as home automation systems or industrial machinery.

A

A

Hardware Overview

How does it work?

MICRF RX Click is based on the MICRF220, an ASK/OOK receiver with RSSI and squelch from Microchip. This super-heterodyne, image-reject RF receiver is engineered to require only a crystal, in this case, an onboard 9.81563MHz, and a minimal set of external components for operation. Its suitability spans various applications, such as low-power Remote Keyless Entry (RKE), Tire Pressure Monitoring Systems (TPMS), and remote actuation systems. In terms of performance, the MICRF220 distinguishes itself with a sensitivity of -110dBm at 1kbps and a 0.1% Bit Error Rate (BER), complemented by four selectable demodulator filter bandwidths ranging from 1170Hz to 9400Hz. Filter bandwidth can be selected by placing BW SEL

jumpers in a corresponding position, 0 or 1, choosing the corresponding frequency based on the truth table from the attached MICRF220 datasheet (Table 2.). This feature permits the device to accommodate bit rates as high as 14.4kbps (from 1.8kbps up to 14.4kbps). Operating on a 3.3V supply from the mikroBUS™ power rail, it is optimized for 315MHz operation, drawing a typical supply current of 4.3mA. Additionally, the MICRF220 has a low-power shutdown mode controllable through the SH pin, reducing the supply current to an impressive 0.1µA. It also has a squelch function accessed via the SQ pin that minimizes data output activity on the DO pin until valid bits are detected without compromising

receiver sensitivity. The board also incorporates an RSI pin to indicate received signal strength. For antenna configurations, the board allows for the use of an onboard PCB antenna specifically tuned to 315MHz or an external antenna via an SMA connector, with the selection made possible by adjusting the capacitor from position A to B near the SMA connector. 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.

MICRF RX 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

PIC18F45K40

Architecture

PIC

MCU Memory (KB)

32

Silicon Vendor

Microchip

Pin count

40

RAM (Bytes)

2048

You complete me!

Accessories

Right angle 433MHz rubber antenna boasts a frequency range of 433MHz, ensuring optimal performance within this spectrum. With a 50Ohm impedance, it facilitates efficient signal transmission. The antenna's vertical polarization enhances signal reception in a specific orientation. Featuring a 1.5dB gain, it can improve signal strength to some extent. The antenna can handle a maximum input power of 50W, making it suitable for various applications. Its compact 50mm length minimizes spatial requirements. Equipped with an SMA male connector, it easily interfaces with compatible devices. This antenna is an adaptable solution for wireless communication needs, particularly when vertical polarization is crucial.

MICRF RX Click accessories 1 image

Used MCU Pins

mikroBUS™ mapper

Received Signal Strength Monitor
RA2
AN
Shutdown
RE1
RST
ID COMM
RE0
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Squelch Function
RC0
PWM
Receiver Data Output
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

Click board™ Schematic

MICRF RX 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

1. Application Output - In Debug mode, the 'Application Output' window enables real-time data monitoring, offering direct insight into execution results. Ensure proper data display by configuring the environment correctly using the provided tutorial.

2. UART Terminal - Use the UART Terminal to monitor data transmission via a USB to UART converter, allowing direct communication between the Click board™ and your development system. Configure the baud rate and other serial settings according to your project's requirements to ensure proper functionality. For step-by-step setup instructions, refer to the provided tutorial.

3. Plot Output - The Plot feature offers a powerful way to visualize real-time sensor data, enabling trend analysis, debugging, and comparison of multiple data points. To set it up correctly, follow the provided tutorial, which includes a step-by-step example of using the Plot feature to display Click board™ readings. To use the Plot feature in your code, use the function: plot(*insert_graph_name*, variable_name);. This is a general format, and it is up to the user to replace 'insert_graph_name' with the actual graph name and 'variable_name' with the parameter to be displayed.

Software Support

Library Description

This library contains API for MICRF RX Click driver.

Key functions:

  • micrfrx_enable_device - This function enables device by setting the SHD pin to low logic state

  • micrfrx_wait_ready - This function waits for all training bytes to arrive which indicates data ready

  • micrfrx_read_packet - This function reads data packet and stores it in a packet_buf only if the MICRFRX_PREAMBLE bytes are received successfully

Open Source

Code example

The complete application code and a ready-to-use project are available through the NECTO Studio Package Manager for direct installation in the NECTO Studio. The application code can also be found on the MIKROE GitHub account.

/*!
 * @file main.c
 * @brief MICRF RX Click Example.
 *
 * # Description
 * This example demonstrates the use of MICRF RX click board by reading and parsing
 * packet messages received from the transmitter.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and enables the device and squelch mode. 
 *
 * ## Application Task
 * Waits for a data ready indication, then reads all packet data, verifies the CRC
 * bytes in a packet, and displays its data as well as the RSSI value on the USB UART.
 *
 * @note
 * The MICRF TX click board is a compatible transmitter for the MICRF RX click.
 * Here are a few steps for troubleshooting if you are experiencing issues running
 * this example:
 *  - Make sure the MICRF TX click is set to ASK mode with on-board jumpers.
 *  - Check the MCU clock configuration, use an external oscillator instead of the MCU's
 * internal one for better accuracy on manchester data rate delay.
 *  - Measure the actual data rate on the data line and adjust the MICRFRX_MAN_BIT_LEN_US
 * value accordingly.
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "micrfrx.h"

#define MICRFRX_PREAMBLE    0x5AA5  /**< Packet preamble word. */

static micrfrx_t micrfrx;   /**< MICRF RX Click driver object. */
static log_t logger;        /**< Logger object. */
static uint8_t packet_buf[ MICRFRX_MAX_DATA_LEN + 5 ] = { 0 };  /**< Packet buffer. */

/** 
 * @brief MICRF RX wait ready function.
 * @details This function waits for all training bytes to arrive which indicates data ready.
 * @param[in] ctx : Click context object.
 * See #micrfrx_t object definition for detailed explanation.
 * @return None.
 * @note None.
 */
static void micrfrx_wait_ready ( micrfrx_t *ctx );

/** 
 * @brief MICRF RX read packet function.
 * @details This function reads data packet and stores it in a packet_buf only if
 * the MICRFRX_PREAMBLE bytes are received successfully.
 * @param[in] ctx : Click context object.
 * See #micrfrx_t object definition for detailed explanation.
 * @return Number of data bytes stored in a packet_buf.
 * @note The expected packet format is as follows (MSB first, manchester IEEE 802.3):
 * TRAINING_BYTES (at least two bytes of 0xAA), PREABMLE, LEN, DATA_IN, CRC16 (calculated
 * from whole packet excluding training bytes). Training bytes are excluded from packet_buf.
 * This function must be called immediately after calling micrfrx_wait_ready.
 */
static uint8_t micrfrx_read_packet ( micrfrx_t *ctx );

/** 
 * @brief Reflect bits.
 * @details This function reflects a desired number of bits in data.
 * @return Reflected data.
 * @note None.
 */
static uint16_t micrfrx_reflect_bits( uint16_t data_in, uint8_t len );

/** 
 * @brief CRC-16/MAXIM calculation for CRC16 function.
 * @details This function calculates CRC16 with parameteres: 
 * @li @c  Width 16 bit
 * @li @c  Polynomial 0x8005 ( x16 + x15 + x2 + x0 )
 * @li @c  Initialization 0x0000
 * @li @c  Reflect input True
 * @li @c  Reflect output True
 * @li @c  Final Xor 0xFFFF
 * @li @c  Example { 69, 00 } - 0xAFD1
 * @param[in] data_buf : Array of bytes to calculate crc from.
 * @param[in] len : Number of bytes to calculate crc from.
 * @return Calculated CRC.
 * @note None.
 */
static uint16_t micrfrx_calculate_crc16 ( uint8_t *data_buf, uint16_t len );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    micrfrx_cfg_t micrfrx_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.
    micrfrx_cfg_setup( &micrfrx_cfg );
    MICRFRX_MAP_MIKROBUS( micrfrx_cfg, MIKROBUS_1 );
    if ( DIGITAL_OUT_UNSUPPORTED_PIN == micrfrx_init( &micrfrx, &micrfrx_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    micrfrx_enable_squelch ( &micrfrx );
    micrfrx_enable_device ( &micrfrx );

    log_info( &logger, " Application Task " );
}

void application_task ( void ) 
{
    static float rssi_v = 0;
    static uint8_t packet_len = 0;
    static uint8_t msg_cnt = 0;
    static uint16_t crc = 0;

    log_printf( &logger, "\r\n Waiting for data ready...\r\n" );
    micrfrx_wait_ready ( &micrfrx );
    packet_len = micrfrx_read_packet ( &micrfrx );
    if ( packet_len )
    {
        micrfrx_read_rssi_voltage ( &micrfrx, &rssi_v );
        crc = ( ( uint16_t ) packet_buf[ packet_len - 2 ] << 8 ) | packet_buf[ packet_len - 1 ];
        if ( crc == micrfrx_calculate_crc16 ( packet_buf, packet_len - 2 ) )
        {
            log_printf( &logger, " Received message: " );
            for ( msg_cnt = 0; msg_cnt < packet_buf[ 2 ]; msg_cnt++ )
            {
                log_printf( &logger, "%c", ( uint16_t ) packet_buf[ msg_cnt + 3 ] );
            }
            log_printf( &logger, "\r\n RSSI: %.1f dBm\r\n", MICRFRX_RSSI_V_TO_DBM ( rssi_v ) );
        }
    }
    Delay_ms ( 100 );
}

void main ( void ) 
{
    application_init( );

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

static void micrfrx_wait_ready ( micrfrx_t *ctx )
{
    uint16_t time_cnt = 0;
    uint16_t training_high_cnt = 0;
    uint16_t training_low_cnt = 0;
    // Loop until at least two 0xAA bytes of training data is detected
    for ( ; ; )
    {
        // Measure time in steps of MICRFRX_MAN_BIT_LEN_US / 40 for high signal
        for ( time_cnt = 0; micrfrx_get_data_pin ( ctx ); time_cnt++ )
        {
            Delay_us ( MICRFRX_MAN_BIT_LEN_US / 40 );
        }
        if ( time_cnt > 30 )
        {
            // Increment training_high_cnt if signal width is at least 75% of MICRFRX_MAN_BIT_LEN_US
            training_high_cnt++;
        }
        else
        {
            // Reset counters if any invalid signal is detected
            training_high_cnt = 0;
            training_low_cnt = 0;
        }

        // Measure time in steps of MICRFRX_MAN_BIT_LEN_US / 40 for low signal
        for ( time_cnt = 0; ( training_high_cnt > 0 ) && !micrfrx_get_data_pin ( ctx ); time_cnt++ )
        {
            Delay_us ( MICRFRX_MAN_BIT_LEN_US / 40 );
        }
        if ( time_cnt > 30 )
        {
            // Increment training_low_cnt if signal width is at least 75% of MICRFRX_MAN_BIT_LEN_US
            training_low_cnt++;
        }
        else if ( ( training_high_cnt >= 8 ) && ( training_high_cnt == ( training_low_cnt + 1 ) ) )
        {
            // At least two 0xAA bytes are detected ending with shorter low signal, so break the loop here.
            // Waiting for 12.5% of MICRFRX_MAN_BIT_LEN_US as an offset for packet reading.
            Delay_us ( MICRFRX_MAN_BIT_LEN_US / 8 );
            break;
        }
    }
}

static uint8_t micrfrx_read_packet ( micrfrx_t *ctx )
{
    uint8_t byte_cnt = 0;
    uint8_t bit_cnt = 0;
    // Loop until all data bytes are received or a bad PREAMBLE word is detected
    for ( ; ; )
    {
        if ( !micrfrx_get_data_pin ( ctx ) )
        {
            // Store data in packet_buf in manchester IEEE 802.3 format, MSB first
            packet_buf[ byte_cnt ] |= ( 0x80 >> bit_cnt );
        }
        
        if ( 8 == ++bit_cnt )
        {
            // Reset bit counter and increment byte counter if 8 bits are received
            bit_cnt = 0;
            byte_cnt++;
        }

        if ( 2 == byte_cnt )
        {
            // Two bytes are received, check PREAMBLE word
            if ( MICRFRX_PREAMBLE != ( ( ( uint16_t ) packet_buf[ 0 ] << 8 ) | packet_buf[ 1 ] ) )
            {
                byte_cnt = 0;
                break;
            }
        }
        else if ( ( packet_buf[ 2 ] + 5 ) == byte_cnt )
        {
            // Break the loop if all packet data are received (PREAMBLE + DATA_LEN + DATA + CRC)
            break;
        }

        // Move to the next manchester clock high state by delaying for MICRFRX_MAN_BIT_LEN_US
        Delay_us ( MICRFRX_MAN_BIT_LEN_US );
    }
    return byte_cnt;
}

static uint16_t micrfrx_reflect_bits( uint16_t data_in, uint8_t len )
{
    uint16_t data_out = 0;
    for ( uint16_t cnt = 0; cnt < len; cnt++ )
    {
        data_out |= ( ( data_in >> cnt ) & 1 ) << ( len - cnt - 1 );
    }
    return data_out;
}

static uint16_t micrfrx_calculate_crc16( uint8_t *data_buf, uint16_t len )
{
    uint16_t crc16 = 0x0000;
    for ( uint16_t cnt = 0; cnt < len; cnt++ )
    {
        crc16 ^= ( micrfrx_reflect_bits( data_buf[ cnt ], 8 ) << 8 );
        for ( uint8_t bit_cnt = 0; bit_cnt < 8; bit_cnt++ )
        {
            if ( crc16 & 0x8000 )
            {
                crc16 = ( crc16 << 1 ) ^ 0x8005;
            }
            else
            {
                crc16 <<= 1;
            }
        }
    }
    return micrfrx_reflect_bits( crc16, 16 ) ^ 0xFFFF;
}

// ------------------------------------------------------------------------ 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.