Intermediate
30 min

Unleash the waveform symphony using AD9833 and PIC18F47K42TQFP

Sine/triangle/square waveform generator

Waveform Click with Curiosity Nano with PIC18F47K42

Published Feb 13, 2024

Click board™

Waveform Click

Dev.Board

Curiosity Nano with PIC18F47K42

Compiler

NECTO Studio

MCU

PIC18F47K42TQFP

Explore this comprehensive waveform generator and add seamless signal generation to your solution

A

A

Hardware Overview

How does it work?

Waveform Click is based on the AD9833, a low-power, programmable waveform generator from Analog Devices. This company is well-established in the high-quality Digital Signal Processing (DSP) solutions market. The AD9833 IC is based on Direct Digital Synthesis (DDS), producing a waveform with a programmable frequency and selectable wave shape at its output. The AD9833 and the AD5227, a digital potentiometer IC from the same company, are controlled over the SPI interface, allowing both the frequency and amplitude to be changed very quickly without additional latency. DDS produces an analog waveform by exploiting the fact that the signal's phase is changed linearly. For simple periodic functions such as the sine function, the phase changes linearly between 0 and 2π. This allows building the Numerically Controlled Oscillator (NCO) block, which outputs a numerical value that linearly changes over time, between 0 and 228 – 1 (since the AD9833 IC has a 28-bit phase accumulator). The continuously changing output of the NCO block is used as the index for the Lookup Table (LUT), which contains amplitudes of the output waveform. The faster the NCO output changes, the higher the output signal frequency,

which is a basis for DDS. The main advantage over other types of synthesis (PLL, for example) is its simplistic approach. The frequency can be changed in small steps (depending on the clock generator), while the maximum frequency can easily reach GHz. Besides the NCO and the LUT, the AD9833 contains other blocks necessary to produce the waveform at the output. It also features a 10-bit DAC, which translates the digital value into an analog voltage at the output. Since the ADC is only 10 bits wide, the LUT does not need to have too many elements. The resolution of the ADC is the bottleneck, so a slightly higher resolution is required for the LUT data. This further reduces the complexity and costs. The AD9833 can completely avoid using the LUT, producing a square wave (by using only the MSB of the DAC), with a frequency that can be further multiplied by 2, and a triangle wave (by redirecting the NCO directly to DAC instead using it for sweeping through the LUT). The operating modes of the AD9833 can be set up by using the config-register over the SPI interface. For more detailed information about the AD9833 IC, please refer to the datasheet of the AD9833. However, the mikroSDK compatible library contains functions

that simplify work with the AD9833 IC. The output of the AD9833 is routed to the AD5227 digital potentiometer, which is used to set the amplitude of the output signal. This potentiometer is used to scale down the amplitude between 0V and 3.3V. It is controlled over the SPI interface. The potentiometer is used since the AD9833 IC does not provide means to regulate the amplitude of the signal at the output. Waveform click uses the clock generator 25MHz, which allows changing the frequency in steps of 0.1Hz. The high speed of the clock allows very high frequencies to be produced so that this Click board™ can generate a very clean sine wave with a frequency of up to 5MHz and a square wave with a frequency of up to 12MHz. The integrated clock generator offers a STAND-BY pin, which turns the clock on or off. If this pin has a HIGH logic state, the clock generator will produce a 25MHz clock signal. This pin is pulled to VCC by a pull-up resistor, so the 25MHz clock generator is enabled by default. The output signal of the Click board™ is buffered by an ADA4891-1 low-noise op-amp, providing a constant impedance and limited protection to the whole circuit. It is available over the SMA connector, allowing the shielded coaxial cable to be used.

Waveform Click top side image
Waveform Click bottom side image

Features overview

Development board

PIC18F47K42 Curiosity Nano evaluation kit is a cutting-edge hardware platform designed to evaluate the PIC18F47K42 microcontroller (MCU). Central to its design is the inclusion of the powerful PIC18F47K42 microcontroller (MCU), offering advanced functionalities and robust performance. Key features of this evaluation kit include a yellow user LED and a responsive mechanical user switch

providing seamless interaction and testing. The provision for a 32.768kHz crystal footprint ensures precision timing capabilities. With an onboard debugger boasting a green power and status LED, programming and debugging become intuitive and efficient. Further enhancing its utility is the Virtual serial port (CDC) and a debug GPIO channel (DGI GPIO), offering extensive connectivity options.

Powered via USB, this kit boasts an adjustable target voltage feature facilitated by the MIC5353 LDO regulator, ensuring stable operation with an output voltage ranging from 2.3V to 5.1V (limited by USB input voltage), with a maximum output current of 500mA, subject to ambient temperature and voltage constraints.

PIC18F47K42 Curiosity Nano double side image

Microcontroller Overview

MCU Card / MCU

default

Architecture

PIC

MCU Memory (KB)

128

Silicon Vendor

Microchip

Pin count

40

RAM (Bytes)

8192

You complete me!

Accessories

Curiosity Nano Base for Click boards is a versatile hardware extension platform created to streamline the integration between Curiosity Nano kits and extension boards, tailored explicitly for the mikroBUS™-standardized Click boards and Xplained Pro extension boards. This innovative base board (shield) offers seamless connectivity and expansion possibilities, simplifying experimentation and development. Key features include USB power compatibility from the Curiosity Nano kit, alongside an alternative external power input option for enhanced flexibility. The onboard Li-Ion/LiPo charger and management circuit ensure smooth operation for battery-powered applications, simplifying usage and management. Moreover, the base incorporates a fixed 3.3V PSU dedicated to target and mikroBUS™ power rails, alongside a fixed 5.0V boost converter catering to 5V power rails of mikroBUS™ sockets, providing stable power delivery for various connected devices.

Curiosity Nano Base for Click boards accessories 1 image

Used MCU Pins

mikroBUS™ mapper

NC
NC
AN
Chip Select for AD9833
PC7
RST
Chip Select for AD5227
PD6
CS
SPI Clock
PC6
SCK
NC
NC
MISO
SPI Data IN
PC4
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Clock Generator Enable
PA4
PWM
NC
NC
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

Waveform Click Schematic schematic

Step by step

Project assembly

Curiosity Nano Base for Click boards accessories 1 image hardware assembly

Start by selecting your development board and Click board™. Begin with the Curiosity Nano with PIC18F47K42 as your development board.

Curiosity Nano Base for Click boards accessories 1 image hardware assembly
Charger 27 Click front image hardware assembly
PIC18F47K42 Curiosity Nano front image hardware assembly
Prog-cut hardware assembly
Charger 27 Click complete accessories setup image hardware assembly
Curiosity Nano with PIC18F47XXX 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 image step 5 hardware assembly
Necto image step 6 hardware assembly
PIC18F57Q43 Curiosity MCU Step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step hardware assembly

Track your results in real time

Application Output

After loading the code example, pressing the "DEBUG" button builds and programs it on the selected setup.

Application Output Step 1

After programming is completed, a header with buttons for various actions available in the IDE appears. By clicking the green "PLAY "button, we start reading the results achieved with Click board™.

Application Output Step 3

Upon completion of programming, the Application Output tab is automatically opened, where the achieved result can be read. In case of an inability to perform the Debug function, check if a proper connection between the MCU used by the setup and the CODEGRIP programmer has been established. A detailed explanation of the CODEGRIP-board connection can be found in the CODEGRIP User Manual. Please find it in the RESOURCES section.

Application Output Step 4

Software Support

Library Description

This library contains API for Waveform Click driver.

Key functions:

  • waveform_sine_output - Sinusoide output function

  • waveform_triangle_output - Triangle output function

  • waveform_square_output - Square output 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 
 * \brief Waveform Click example
 * 
 * # Description
 * This program outputs wave forms.
 *
 * The application is composed of two sections :
 * 
 * ## Application Init 
 * Initializes the communication interface and configures the click board.
 * 
 * ## Application Task  
 * Predefined characters are inputed from the serial port.
 * Changes the signal frequency, waveform or amplitude depending on the receiver character.
 * 
 * ## Additional Functions
 * uint32_t waveform_aprox_freqcalculation ( float freqency ) - This function is used
 * to calculate the aproximate value that will be written to the frequency set
 * register.
 * 
 * void output_waveform ( uint32_t frequency_temp, uint8_t output_mode ) - This function 
 * checks which wave form has been chosen and sets frequency of the wave.
 * 
 * void frequency_increment ( uint8_t output_mode ) - This function increases frequency 
 * of the wave.
 * 
 * void frequency_decrement ( uint8_t output_mode ) - This function reduces frequency
 * of the wave.
 * 
 * \author MikroE Team 
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "waveform.h"

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

static waveform_t waveform;
static log_t logger;

static uint32_t freq;
static char rx_data_buffer[ 2 ];

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

uint32_t waveform_aprox_freqcalculation ( float freqency )
{
    uint32_t calculation;
    float waveform_osc_freq = 25000000.0;
    float waveform_constant = 268435456.0; // 2^28
    
    calculation = freqency * ( waveform_constant / waveform_osc_freq );
    
    return calculation;
}

void output_waveform ( uint32_t frequency_temp, uint8_t output_mode )
{
    if ( output_mode == WAVEFORM_SINE_OUT )
    {
        waveform_sine_output( &waveform, frequency_temp );
    }
    else if ( output_mode == WAVEFORM_TRIANGLE_OUT )
    {
        waveform_triangle_output( &waveform, frequency_temp );
    }
    else if ( output_mode == WAVEFORM_SQUARE_OUT )
    {
        waveform_square_output( &waveform, frequency_temp );
    }
}

void frequency_increment ( uint8_t output_mode )
{
    uint32_t frequency_temp;
    freq += 1;
    frequency_temp = freq << 14;
    output_waveform( frequency_temp, output_mode );
}

void frequency_decrement ( uint8_t output_mode )
{
    uint32_t frequency_temp;
    if ( freq > 1 )
    {
        freq -= 1;
    }
    frequency_temp = freq << 14;
    output_waveform( frequency_temp, output_mode );
}

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

void application_init ( )
{
    log_cfg_t log_cfg;
    waveform_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 ----" );

    //  Click initialization.

    waveform_cfg_setup( &cfg );
    WAVEFORM_MAP_MIKROBUS( cfg, MIKROBUS_1 );
    waveform_init( &waveform, &cfg );

    freq = waveform_aprox_freqcalculation( 5000 );
    waveform_square_output( &waveform, freq );
    freq = 1;
}

void application_task ( )
{
    uint8_t rx_len = log_read ( &logger, rx_data_buffer, 1 );
    
    if ( rx_len > 0 ) 
    {
       switch( rx_data_buffer[ 0 ] )
       {
           case '+': {
                            waveform_digipot_inc( &waveform );
                            log_printf( &logger, "Increasing amplitude of the current wave.\r\n" );
                            break;
                        }
           case '-': {
                            waveform_digipot_dec( &waveform );
                            log_printf( &logger, "Decreasing amplitude of the current wave.\r\n" );
                            break;
                        }
           case 'S': {
                            frequency_increment( WAVEFORM_SINE_OUT );
                            log_printf( &logger, "Increasing frequency of the sine wave.\r\n" );
                            break;
                        }
           case 's': {
                            frequency_decrement( WAVEFORM_SINE_OUT );
                            log_printf( &logger, "Decreasing frequency of the sine wave.\r\n" );
                            break;
                        }
           case 'T': {
                            frequency_increment( WAVEFORM_TRIANGLE_OUT );
                            log_printf( &logger, "Increasing frequency of the triangle wave.\r\n" );
                            break;
                        }
           case 't': {
                            frequency_decrement( WAVEFORM_TRIANGLE_OUT );
                            log_printf( &logger, "Decreasing frequency of the triangle wave.\r\n" );
                            break;
                        }
           case 'Q': {
                            frequency_increment( WAVEFORM_SQUARE_OUT );
                            log_printf( &logger, "Increasing frequency of the square wave.\r\n" );
                            break;
                        }
           case 'q': {
                            frequency_decrement( WAVEFORM_SQUARE_OUT );
                            log_printf( &logger, "Decreasing frequency of the square wave.\r\n" );
                            break;
                        }
           default :{
                            break;
                        }
       }
       rx_data_buffer[ 0 ] = 0;
       rx_len = 0;
    }
}

void main ( )
{
    application_init( );

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

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

Additional Support

Resources