4.1.3.1 12-bit ADCC with Context

12-bit Analog-to-Digital Converter with Computation and Context (ADC3)

4.1.3.1.1 Introduction

The Analog-to-Digital Converter with Computation and Context (ADC3) conversion of both single-ended and differential analog input signals to a 12-bit binary representation of that signal. This device uses analog inputs, which are multiplexed into a single sample and hold circuit. The output of the sample and hold is connected to the input of the converter. The converter generates a 12-bit binary result via successive approximation and stores the conversion result into the ADC result registers.

4.1.3.1.2 Supported Device Families

PIC18F-Q71 PIC18F-Q8x

4.1.3.1.3 Required Header Files:

#include "mcc_generated_files/adc/adc.h"

4.1.3.1.4 How to use the ADC3 PLIB Driver

The ADC3 Module driver generates an API interface to support the features of the peripheral. Some use cases of are linked below. Click the links to view the code snippets associated with each.

4.1.3.1.5 Module Documentation

ADC

This is the generated header file for the ADC driver.

Module description

This is the generated header file for the ADC driver.

Version: ADC Driver Version 1.0.1
Typedefs
  • typedef uint16_t adc_result_t

    Displays the result of the Analog-to-Digital (A/D) conversion.

Definitions
Enumerations
Functions

Definition Documentation

ADC_ClearAccumulator

#define ADC_ClearAccumulator ADC_ClearAccumulator

Defines the Custom Name for the ADC_ClearAccumulator API.

ADC_DefineSetPoint

#define ADC_DefineSetPoint ADC_DefineSetPoint

Defines the Custom Name for the ADC_DefineSetPoint API.

ADC_DisableChannelScan

#define ADC_DisableChannelScan ADC_DisableChannelScan

Defines the Custom Name for the ADC_DisableChannelScan API.

ADC_DisableChannelSequencer

#define ADC_DisableChannelSequencer ADC_DisableChannelSequencer

Defines the Custom Name for the ADC_DisableChannelSequencer API.

ADC_DisableContinuousConversion

#define ADC_DisableContinuousConversion ADC_DisableContinuousConversion

Defines the Custom Name for the ADC_DisableContinuousConversion API.

ADC_DischargeSampleCapacitor

#define ADC_DischargeSampleCapacitor ADC_DischargeSampleCapacitor

Defines the Custom Name for the ADC_DischargeSampleCapacitor API.

ADC_EnableChannelScan

#define ADC_EnableChannelScan ADC_EnableChannelScan

Defines the Custom Name for the ADC_EnableChannelScan API.

ADC_EnableChannelSequencer

#define ADC_EnableChannelSequencer ADC_EnableChannelSequencer

Defines the Custom Name for the ADC_EnableChannelSequencer API.

ADC_EnableContinuousConversion

#define ADC_EnableContinuousConversion ADC_EnableContinuousConversion

Defines the Custom Name for the ADC_EnableContinuousConversion API.

ADC_EnableDoubleSampling

#define ADC_EnableDoubleSampling ADC_EnableDoubleSampling

Defines the Custom Name for the ADC_EnableDoubleSampling API.

ADC_GetAccumulatorValue

#define ADC_GetAccumulatorValue ADC_GetAccumulatorValue

Defines the Custom Name for the ADC_GetAccumulatorValue API.

ADC_GetConversionResult

#define ADC_GetConversionResult ADC_GetConversionResult

Defines the Custom Name for the ADC_GetConversionResult API.

ADC_GetConversionStageStatus

#define ADC_GetConversionStageStatus ADC_GetConversionStageStatus

Defines the Custom Name for the ADC_GetConversionStageStatus API.

ADC_GetCurrentCountofConversions

#define ADC_GetCurrentCountofConversions ADC_GetCurrentCountofConversions

Defines the Custom Name for the ADC_GetCurrentCountofConversions API.

ADC_GetErrorCalculation

#define ADC_GetErrorCalculation ADC_GetErrorCalculation

Defines the Custom Name for the ADC_GetErrorCalculation API.

ADC_GetFilterValue

#define ADC_GetFilterValue ADC_GetFilterValue

Defines the Custom Name for the ADC_GetFilterValue API.

ADC_GetPreviousResult

#define ADC_GetPreviousResult ADC_GetPreviousResult

Defines the Custom Name for the ADC_GetPreviousResult API.

ADC_GetSingleConversion

#define ADC_GetSingleConversion ADC_GetSingleConversion

Defines the Custom Name for the ADC_GetSingleConversion API.

ADC_HasAccumulatorOverflowed

#define ADC_HasAccumulatorOverflowed ADC_HasAccumulatorOverflowed

Defines the Custom Name for the ADC_HasAccumulatorOverflowed API.

ADC_HasErrorCrossedLowerThreshold

#define ADC_HasErrorCrossedLowerThreshold ADC_HasErrorCrossedLowerThreshold

Defines the Custom Name for the ADC_HasErrorCrossedLowerThreshold API.

ADC_HasErrorCrossedUpperThreshold

#define ADC_HasErrorCrossedUpperThreshold ADC_HasErrorCrossedUpperThreshold

Defines the Custom Name for the ADC_HasErrorCrossedUpperThreshold API.

ADC_Initialize

#define ADC_Initialize ADC_Initialize

Defines the Custom Name for the ADC_Initialize API.

ADC_IsConversionDone

#define ADC_IsConversionDone ADC_IsConversionDone

Defines the Custom Name for the ADC_IsConversionDone API.

ADC_SelectContext

#define ADC_SelectContext ADC_SelectContext

Defines the Custom Name for the ADC_SelectContext API.

ADC_SetAcquisitionRegister

#define ADC_SetAcquisitionRegister ADC_SetAcquisitionRegister

Defines the Custom Name for the ADC_SetAcquisitionRegister API.

ADC_SetADIInterruptHandler

#define ADC_SetADIInterruptHandler ADC_SetADIInterruptHandler

Defines the Custom Name for the ADC_SetADIInterruptHandler API.

ADC_SetChannel

#define ADC_SetChannel ADC_SetChannel

Defines the Custom Name for the ADC_SetChannel API.

ADC_SetContext1ThresholdInterruptHandler

#define ADC_SetContext1ThresholdInterruptHandler ADC_SetContext1ThresholdInterruptHandler

Defines the Custom Name for the ADC_SetContext1ThresholdInterruptHandler API.

ADC_SetContext2ThresholdInterruptHandler

#define ADC_SetContext2ThresholdInterruptHandler ADC_SetContext2ThresholdInterruptHandler

Defines the Custom Name for the ADC_SetContext2ThresholdInterruptHandler API.

ADC_SetContext3ThresholdInterruptHandler

#define ADC_SetContext3ThresholdInterruptHandler ADC_SetContext3ThresholdInterruptHandler

Defines the Custom Name for the ADC_SetContext3ThresholdInterruptHandler API.

ADC_SetContext4ThresholdInterruptHandler

#define ADC_SetContext4ThresholdInterruptHandler ADC_SetContext4ThresholdInterruptHandler

Defines the Custom Name for the ADC_SetContext4ThresholdInterruptHandler API.

ADC_SetLowerThreshold

#define ADC_SetLowerThreshold ADC_SetLowerThreshold

Defines the Custom Name for the ADC_SetLowerThreshold API.

ADC_SetPrechargeRegister

#define ADC_SetPrechargeRegister ADC_SetPrechargeRegister

Defines the Custom Name for the ADC_SetPrechargeRegister API.

ADC_SetRepeatCount

#define ADC_SetRepeatCount ADC_SetRepeatCount

Defines the Custom Name for the ADC_SetRepeatCount API.

ADC_SetStopOnInterrupt

#define ADC_SetStopOnInterrupt ADC_SetStopOnInterrupt

Defines the Custom Name for the ADC_SetStopOnInterrupt API.

ADC_SetUpperThreshold

#define ADC_SetUpperThreshold ADC_SetUpperThreshold

Defines the Custom Name for the ADC_SetUpperThreshold API.

ADC_StartConversion

#define ADC_StartConversion ADC_StartConversion

Defines the Custom Name for the ADC_StartConversion API.

ADC_StartConversionOnChannel

#define ADC_StartConversionOnChannel ADC_StartConversionOnChannel

Defines the Custom Name for the ADC_StartConversionOnChannel API.

ADC_StopConversion

#define ADC_StopConversion ADC_StopConversion

Defines the Custom Name for the ADC_StopConversion API.

Typedef Documentation

adc_result_t

adc_result_t

Displays the result of the Analog-to-Digital (A/D) conversion.

Function Documentation

ADC_ADCH1_ISR()

void ADC_ADCH1_ISR (void )

Implements the ADCH1 ISR for the interrupt-driven implementations.

Parameters:
None.
Returns:

None.

ADC_ADCH2_ISR()

void ADC_ADCH2_ISR (void )

Implements the ADCH2 ISR for the interrupt-driven implementations.

Parameters:
None.
Returns:

None.

ADC_ADCH3_ISR()

void ADC_ADCH3_ISR (void )

Implements the ADCH3 ISR for the interrupt-driven implementations.

Parameters:
None.
Returns:

None.

ADC_ADCH4_ISR()

void ADC_ADCH4_ISR (void )

Implements the ADCH4 ISR for the interrupt-driven implementations.

Parameters:
None.
Returns:

None.

ADC_ADI_ISR()

void ADC_ADI_ISR (void )

Implements the ADC Interrupt (ADI) ISR for the interrupt-driven implementations.

Parameters:
None.
Returns:

None.

ADC_ClearAccumulator()

void ADC_ClearAccumulator (void )[inline]

Clears the accumulator.

Parameters:
None.
Returns:

None.

ADC_DefineSetPoint()

void ADC_DefineSetPoint (uint16_t setPoint)

Sets the value of the ADC Threshold Setpoint (ADSTPT) register.

Parameters:
setPoint

- 16-bit value for the ADC Threshold Setpoint register.

Returns:

None.

ADC_DisableChannelScan()

void ADC_DisableChannelScan (adc_context_t context)

Excludes a channel context from the scan sequence. Select the correct context using ADC_SelectContext before calling this API.

Parameters:
context

- Context which needs to be excluded from the scan. Refer to the adc_context_t enum for the list of available contexts.

Returns:

None.

ADC_DisableChannelSequencer()

void ADC_DisableChannelSequencer (void )[inline]

Disables the ADC channel sequencer. Initialize the ADC module using ADC_Initialize before calling this API.

Parameters:
None.
Returns:

None.

ADC_DisableContinuousConversion()

void ADC_DisableContinuousConversion (void )[inline]

Disables continuous conversion.

Parameters:
None.
Returns:

None.

ADC_DischargeSampleCapacitor()

void ADC_DischargeSampleCapacitor (void )[inline]

Discharges the input sample capacitor for the selected context by setting the channel to AVss. Use ADC_SelectContext to select the correct ADC context before calling this API.

Parameters:
None.
Returns:

None.

ADC_EnableChannelScan()

void ADC_EnableChannelScan (adc_context_t context)

Includes a channel context in the scan sequence. Select the correct context using ADC_SelectContext before calling this API.

Parameters:
context

- Context which needs to be included in the scan. Refer to the adc_context_t enum for the list of available contexts.

Returns:

None.

ADC_EnableChannelSequencer()

void ADC_EnableChannelSequencer (void )[inline]

Enables the ADC channel sequencer. Initialize the ADC module using ADC_Initialize before calling this API.

Parameters:
None.
Returns:

None.

ADC_EnableContinuousConversion()

void ADC_EnableContinuousConversion (void )[inline]

Enables continuous conversion.

Parameters:
None.
Returns:

None.

ADC_EnableDoubleSampling()

void ADC_EnableDoubleSampling (void )[inline]

Enables the Double-Sample Enable bit.

Parameters:
None.
Returns:

None.

ADC_GetAccumulatorValue()

uint24_t ADC_GetAccumulatorValue (void )[inline]

Retrieves a 24-bit value from the ADC accumulator.

Parameters:
None.
Returns:

Value of the ADC accumulator.

ADC_GetConversionResult()

adc_result_t ADC_GetConversionResult (void )[inline]

Retrieves the result of the latest A/D conversion.

Precondition:

Call ADC_StartConversion() before calling this function and check the completion status using the ADC_IsConversionDone() API.

Parameters:
None.
Returns:

The result of the A/D conversion. Refer to adc_result_t.

ADC_GetConversionStageStatus()

uint8_t ADC_GetConversionStageStatus (void )[inline]

Retrieves the ADC module cycle multi-stage status.

Precondition:

Call ADC_StartConversion() before calling this function.

Parameters:
None.
Returns:

Contents of the ADC Status (ADSTAT) register.

ADC_GetCurrentCountofConversions()

uint8_t ADC_GetCurrentCountofConversions (void )[inline]

Retrieves the current value of the ADC Repeat Counter (ADCNT) register.

Precondition:

Call ADC_StartConversion() before calling this function.

Parameters:
None.
Returns:

Current value of the ADC Repeat Counter register.

ADC_GetErrorCalculation()

int16_t ADC_GetErrorCalculation (void )[inline]

Retrieves the value of the ADC Setpoint Error (ADERR) register.

Precondition:

Call ADC_StartConversion() before calling this function.

Parameters:
None.
Returns:

16-bit value obtained from the high byte ADERR (ADERRH) and low byte ADERR (ADERRL) registers.

ADC_GetFilterValue()

uint16_t ADC_GetFilterValue (void )[inline]

Retrieves the value of the ADC Filter (ADFLTR) register.

Parameters:
None.
Returns:

16-bit value obtained from the high byte ADFLTR (ADFLTRH) and low byte ADFLTR (ADFLTRL) registers.

ADC_GetPreviousResult()

uint16_t ADC_GetPreviousResult (void )[inline]

Retrieves the value of the ADC Previous Result (ADPREV) register.

Precondition:

Call ADC_StartConversion() before calling this function.

Parameters:
None.
Returns:

16-bit value obtained from the high byte ADPREV (ADPREVH) and low byte ADPREV (ADPREVL) registers.

ADC_GetSingleConversion()

adc_result_t ADC_GetSingleConversion (adc_channel_t channel)

Retrieves the result of a single A/D conversion on the given channel in Single-Ended mode.

Precondition:

Call ADC_DisableContinuousConversion() before calling this function.

Parameters:
channel

- Analog channel number on which the A/D conversion has to be applied. Refer to adc_channel_t for the available channels.

Returns:

The result of the A/D conversion. Refer to adc_result_t.

ADC_HasAccumulatorOverflowed()

bool ADC_HasAccumulatorOverflowed (void )[inline]

Determines if the ADC accumulator has overflowed.

Precondition:

Call ADC_StartConversion() before calling this function.

Parameters:
None.
Return values:
True

- ADC accumulator has overflowed.

False

- ADC accumulator has not overflowed.

ADC_HasErrorCrossedLowerThreshold()

bool ADC_HasErrorCrossedLowerThreshold (void )[inline]

Determines if the ADC error is less than the lower threshold.

Precondition:

Call ADC_StartConversion() before calling this function.

Parameters:
None.
Return values:
True

- ADC error is less than the lower threshold.

False

- ADC error is higher than the lower threshold.

ADC_HasErrorCrossedUpperThreshold()

bool ADC_HasErrorCrossedUpperThreshold (void )[inline]

Determines if the ADC error is higher than the upper threshold.

Precondition:

Call ADC_StartConversion() before calling this function.

Parameters:
None.
Return values:
True

- ADC error has crossed the upper threshold.

False

- ADC error has not crossed the upper threshold.

ADC_Initialize()

void ADC_Initialize (void )

Initializes the ADC module. This routine must be called before other ADC APIs.

Parameters:
None.
Returns:

None.

ADC_IsConversionDone()

bool ADC_IsConversionDone (void )[inline]

Checks if the ongoing A/D conversion is complete.

Precondition:

Call ADC_StartConversion() before calling this function.

Parameters:
None.
Return values:
True

- Conversion is complete.

False

- Conversion is ongoing.

ADC_SelectContext()

void ADC_SelectContext (adc_context_t context)[inline]

Selects an ADC context to perform read/write operations on context-specific registers.

Parameters:
context

- Context to be selected. Refer to the adc_context_t enum for the list of available contexts.

Returns:

None.

ADC_SetAcquisitionRegister()

void ADC_SetAcquisitionRegister (uint16_t acquisitionValue)

Loads the ADC Acquisition Time Control (ADACQ) register with the specified value.

Parameters:
acquisitionValue

- Value to be loaded in the ADC Acquisition Time Control register.

Returns:

None.

ADC_SetADIInterruptHandler()

void ADC_SetADIInterruptHandler (void(*)(void) InterruptHandler)

Sets the callback for the ADC Interrupt Service Routine (ISR).

Parameters:
InterruptHandler

- Callback function to be called.

Returns:

None.

ADC_SetChannel()

void ADC_SetChannel (adc_channel_t channel)[inline]

Sets the positive channel as per user selection.

Precondition:

Initialize the ADC with ADC_Initialize() before calling this API.

Parameters:
channel

- Desired analog channel. Refer to the adc_channel_t enum for the list of available analog channels.

Returns:

None.

ADC_SetContext1ThresholdInterruptHandler()

void ADC_SetContext1ThresholdInterruptHandler (void(*)(void) InterruptHandler)

Sets the callback for the ADC Context 1 (ADCH1) interrupt.

Parameters:
InterruptHandler

- Callback function to be called.

Returns:

None.

ADC_SetContext2ThresholdInterruptHandler()

void ADC_SetContext2ThresholdInterruptHandler (void(*)(void) InterruptHandler)

Sets the callback for the ADC Context 2 (ADCH2) interrupt.

Parameters:
InterruptHandler

- Callback function to be called.

Returns:

None.

ADC_SetContext3ThresholdInterruptHandler()

void ADC_SetContext3ThresholdInterruptHandler (void(*)(void) InterruptHandler)

Sets the callback for the ADC Context 3 (ADCH3) interrupt.

Parameters:
InterruptHandler

- Callback function to be called.

Returns:

None.

ADC_SetContext4ThresholdInterruptHandler()

void ADC_SetContext4ThresholdInterruptHandler (void(*)(void) InterruptHandler)

Sets the callback for the ADC Context 4 (ADCH4) interrupt.

Parameters:
InterruptHandler

- Callback function to be called.

Returns:

None.

ADC_SetLowerThreshold()

void ADC_SetLowerThreshold (uint16_t lowerThreshold)

Sets the value of the ADC Lower Threshold (ADLTH) register.

Parameters:
lowerThreshold

- 16-bit value for the ADC Lower Threshold register.

Returns:

None.

ADC_SetPrechargeRegister()

void ADC_SetPrechargeRegister (uint16_t prechargeTime)

Loads the ADC Precharge Time Control (ADPRE) register with the specified value.

Parameters:
prechargeTime

- Value to be loaded in the ADC Precharge Time Control register.

Returns:

None.

ADC_SetRepeatCount()

void ADC_SetRepeatCount (uint8_t repeatCount)[inline]

Loads the ADC Repeat Counter (ADCNT) with the specified value.

Parameters:
repeatCount

- Value to be loaded to the ADC Repeat Counter register.

Returns:

None.

ADC_SetStopOnInterrupt()

void ADC_SetStopOnInterrupt (void )[inline]

Enables the Stop-on-Interrupt bit.

Precondition:

Call ADC_EnableContinuousConversion() before calling this function.

Parameters:
None.
Returns:

None.

ADC_SetUpperThreshold()

void ADC_SetUpperThreshold (uint16_t upperThreshold)

Sets the value of the ADC Upper Threshold (ADUTH) register.

Parameters:
upperThreshold

- 16-bit value for the ADC Upper Threshold register.

Returns:

None.

ADC_StartConversion()

void ADC_StartConversion (void )[inline]

Starts the A/D conversion on the selected channel.

Parameters:
None.
Returns:

None.

ADC_StartConversionOnChannel()

void ADC_StartConversionOnChannel (adc_channel_t channel)

Starts the A/D conversion on the selected channel.

Parameters:
channel

- Analog channel number on which the A/D conversion has to be applied. Refer to adc_channel_t for the list of available channels.

Returns:

None.

ADC_StopConversion()

void ADC_StopConversion (void )[inline]

Stops the ongoing A/D conversion.

Precondition:

Call ADC_StartConversion() before calling this function.

Parameters:
None.
Returns:

None.

Enumeration Type Documentation

adc_channel_t

enum adc_channel_t

Contains the available ADC channels.

channel_ADCG1
channel_OPA1IN_pos
channel_OPA2IN_pos
channel_DAC2
channel_DAC3
channel_VSS
channel_Temp
channel_DAC1
channel_FVR_Buffer1
channel_FVR_Buffer2
channel_OPA1IN0_pos
channel_OPA1IN1_pos
channel_OPA1IN2_pos
channel_OPA1IN3_pos
channel_OPA2IN0_pos
channel_OPA2IN1_pos
channel_OPA2IN2_pos
channel_OPA2IN3_pos

4.1.3.1.6 File Documentation

source/adc.c File Reference

This is the generated driver implementation file for the ADC driver.

#include <xc.h>
#include "../adc.h"

Functions

Detailed Description

This is the generated driver implementation file for the ADC driver.

ADC Generated Driver File

Version: ADC Driver Version 1.0.1

Function Documentation

ADC_DefaultADI_ISR()

static void ADC_DefaultADI_ISR (void )[static]

ADC_DefaultContext1Threshold_ISR()

static void ADC_DefaultContext1Threshold_ISR (void )[static]

ADC_DefaultContext2Threshold_ISR()

static void ADC_DefaultContext2Threshold_ISR (void )[static]

ADC_DefaultContext3Threshold_ISR()

static void ADC_DefaultContext3Threshold_ISR (void )[static]

ADC_DefaultContext4Threshold_ISR()

static void ADC_DefaultContext4Threshold_ISR (void )[static]

Variable Documentation

ADC_Context1ThresholdHandler

void(* ADC_Context1ThresholdHandler) (void)[static]

ADC_Context2ThresholdHandler

void(* ADC_Context2ThresholdHandler) (void)[static]

ADC_Context3ThresholdHandler

void(* ADC_Context3ThresholdHandler) (void)[static]

ADC_Context4ThresholdHandler

void(* ADC_Context4ThresholdHandler) (void)[static]

ADC_ConversionCompleteHandler

void(* ADC_ConversionCompleteHandler) (void)[static]

Section: Included Files

source/adc.h File Reference

#include <stdint.h>
#include <stdbool.h>

Functions

Macros

Typedefs

  • typedef __uint24 uint24_t

  • typedef uint16_t adc_result_t

    Displays the result of the Analog-to-Digital (A/D) conversion.

Enumerations

Detailed Description

ADC Generated Driver API Header File

Typedef Documentation

Enumeration Type Documentation

adc_context_t

enum adc_context_t

context_1
context_2
context_3
context_4

4.1.3.1.7 Melody ADCCC Use Cases

Melody ADCCC Use Case 1 - Configure ADC Basic Mode and Data Streamer

For new users it is recommended to start with a Single-Ended ADC read to make the learning curve easier. The output will be displayed using Data Streamer.

The use cases show example uses of the ADCCC Driver, within an MCC Melody project:
  • Add the following drivers/libraries to the project:
    1. Data Streamer

    2. ADC

  • DataStreamer Configuration:
    1. UART Dependency: UART2

    2. Variable type: uint16_t

    3. Variable name: adc_result  

  • ADC Easy View Configuration:
    1. Single-ended mode

    2. Right-justified

    3. VDD 3.3V

    4. Clock Selection ADCRC

  • ADC Context 1 Configuration:
    1. Positive Channel: ANA2

    2. Positive Reference: VDD -Other:

    3. Any other peripherals or pins needed for the use case.

  • Configure Pin Grid View:
    1. PORTA Pin 2 as ANPx input

  • Generate the code

  • Add the code snippet(s) to the application code

  • Program the board

#include "mcc_generated_files/system/system.h"
volatile bool SEND_FRAME = false;
void DS_Frame_Update(void){
    DataStreamer.adc_result = ADC_GetSingleConversion(channel_ANA2);
    SEND_FRAME = true;
}
int main(void)
{
    SYSTEM_Initialize();
    
    DataStreamer.adc_result = 0; 

    while(1)
    {
        DS_Frame_Update();      // write the result of an ADC read to the Data Streamer struct
        if(SEND_FRAME)  {       // if the Data Streamer frame has been updated
            WriteFrame();       // Write the new frame 
            SEND_FRAME = false; // Set the boolean back to false for next time
            LED_Toggle();       // Toggle the LED as a visual indicator
        }
    }    
}

Melody ADCCC Use Case 2: ADCCC in Burst_Average_mode with DataStreamer of ADC Result

The ADC is configured to take 16 samples and then average. This is useful for mitigating random noise. The averaged ADC result is sent to DataStreamer, for display in Data Visualizer. After each ADC conversion is completed and sent to DataStreamer the onboard LED is toggled.

Device Resources:
  1. Add ADC

  2. Add DataStreamer

ADC Easy View Configuration:
  1. Single-ended mode

  2. Right-justified

  3. VDD 3.3V

  4. External Positive Reference Voltage: 3.3V

  5. Clock Selection ADCRC ADC Context 1 Configuration:

  6. Positive Channel: ANA2

  7. Positive Reference: VDD

  8. Operating Mode Selection: Burst_Average_mode

  9. Error Calculation Mode: First derivative of filtered value

  10. Upper Threshold: 3.3V

  11. Lower Threshold: 0V

  12. Threshold Setpoint: 0V

  13. Repeat Threshold: 64

  14. Accumulator Right Shift: 6

  15. Acquisition Count: 1 (minimum, dependent on analog circuit output impedance)

DataStreamer:
  • Configuration Settings:
    1. Variable type: uint16_t

    2. Variable name: adc_result

  • Dependency Selector:  

    1. UART Dependency: UART2.

System:
  • Clock Configuration: Default (1 MHz)

  • Pins: (For all pins check board schematics)
    1. Pin Grid View: Select an ANx (ANA2).

    2. Pin Grid View: Select pin for UART Transmit - Tx (RB4).

    3. Pin Grid View: Select LED pin as output.  

    4. Pins: Rename Custom Name to "LED".

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"
volatile bool SEND_FRAME = false;
void DS_Frame_Update(void){
    ADC_SetChannel(channel_ANA2);
    ADC_StartConversion();
    while(!(ADC_IsConversionDone()));
    DataStreamer.adc_result = ADC_GetFilterValue();
    SEND_FRAME = true;
}
int main(void)
{
    SYSTEM_Initialize();
    
    DataStreamer.adc_result = 0;

    while(1)
    {
        DS_Frame_Update();
        if(SEND_FRAME)  {
            WriteFrame();
            SEND_FRAME = false;
            LED_Toggle();
        }
    }    
}

Melody ADCCC Use Case 3 - Leveraging ADCCC Functionality to Mitigate Periodic Noise and Displaying using DataStreamer.

This Use Case uses the Signal and Noise Generator board, along with the Curiosity Nano Base and Q71 Curiosity Nano development board. The ADC is configured to use the System Clock prescaled as its base clock. The user can then tune away from periodic noise by modifying the number of samples accumulated and thus the acquisition count/time. The filtered ADC result is sent to DataStreamer, for display in Data Visualizer. After each ADC conversion is completed and sent to DataStreamer the onboard LED is toggled.

Set the Signal and Noise Generator to output a DC voltage, then add Periodic Noise with frequency 100kHz and amplitude 0.1Vpp.

Device Resources:
  1. Add ADC

  2. Add DataStreamer

ADC Easy View Configuration:
  1. Single-ended mode

  2. Right-justified

  3. VDD 3.3V

  4. External Positive Reference Voltage: 3.3V

  5. Clock Selection: FOSC

  6. Clock Divider: FOSC/4 ADC Context 1 Configuration:

  7. Positive Channel: ANA2

  8. Positive Reference: VDD

  9. Operating Mode Selection: Burst_Average_mode

  10. Error Calculation Mode: Filtered Value vs Setpoint

  11. Upper Threshold: 32767

  12. Lower Threshold: -32768

  13. Threshold Setpoint: 0

  14. Repeat Threshold: 64

  15. Accumulator Right Shift: 6

  16. Acquisition Count: 8191 (Note Acquistion Time of 2.048ms)

  17. Previous Sample Input: FLTR

DataStreamer:
  • Configuration Settings:
    1. Variable type: uint16_t

    2. Variable name: adc_result

  • Dependency Selector:  

    1. UART Dependency: UART2.

System:
  • Clock Configuration: 4 MHz
    1. Clock Source: HFINTOSC

    2. HF Internal Clock: 4 MHz

    3. Clock Divider: 1

  • Configure Pin Grid View:
    1. PORTA Pin 2 as ANPx input

    2. Set UART Tx Pin as Output (RB4 on Q71 Curiosity Nano)

    3. Select LED pin as output (RC7 on Q71 Curiosity Nano)

    4. Rename Custom Name to "LED"

Other:
  • Generate the code

  • Add the code snippet(s) to the application code

  • Program the board

  • Open Data Visualizer, Click New Variable Streamer, Import, mcc_generated_files -> data_streamer -> data_streamer.ds. Click Next, then Finish.

#include "mcc_generated_files/system/system.h"
volatile bool SEND_FRAME = false;
uint16_t adcResult;
void DS_Frame_Update(void){
    ADC_StartConversion();
    while(!(ADC_IsConversionDone()));
    DataStreamer.adc_result = ADC_GetFilterValue();
    LED_Toggle();
    SEND_FRAME = true;
}
int main(void)
{
    SYSTEM_Initialize();
    
    ADC_SetChannel(channel_ANA2);
    
    DataStreamer.adc_result = 0;

    // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts 
    // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts 
    // Use the following macros to: 

    // Enable the Global Interrupts 
    //INTERRUPT_GlobalInterruptEnable(); 

    // Disable the Global Interrupts 
    //INTERRUPT_GlobalInterruptDisable(); 


    while(1)
    {           
        // Start ADC conversion and wait for result
        ADC_StartConversion();
        while(!(ADC_IsConversionDone()));
        
        // Return Filtered value
        adcResult = ADC_GetFilterValue();
        
        DS_Frame_Update();          // write the result of an ADC read to the Data Streamer struct
        if(SEND_FRAME)  {           // if the Data Streamer frame has been updated
            WriteFrame();           // Write the new frame
            SEND_FRAME = false;     // Set the boolean back to false for next time
            LED_Toggle();           // Toggle the LED as a visual indicator
        }
    }    
}
Build the project and program the device. Open Data Visualizer and view the noise on the ADC signal. The Data Axis can be adjusted to zoom in on the noise.

Now change the following MCC Melody settings:

ADC Context 1 Configuration:
  1. Acquisition Count: 130 (Note Acquistion Time of 32.5us) After changing the ADC configuration, click 'Generate' to generate the code. Build the project and program the device. Open Data Visualizer and observe the noise is mitigated on the ADC signal. The Data Axis can be adjusted to zoom in on the noise.

Melody ADCCC Use Case 4 - Window Detection

Create an ADC window detector with upper and lower thresholds. Lower bound 1.0V and upper bound 2.0V. Take a single ADC read and compare to window thresholds. If outside of user defined window (above 2.0V or below 1.0V), turn on LED.

Device Resources:
  1. Add ADC3

  2. Add Data Streamer

ADC3:  

  • Hardware Settings:
    1. Set Input Configuration: Single Ended Mode

    2. Set Result Format: Right Justified

    3. VDD (V): 3.3

  • Clock Selection: ADCRC

  • Context 1 Channel Selection
    1. Positive Channel Selection: ANA2 (if using Signal and Noise Generator)

  • Computation and Threshold Comparison:
    1. Operating Mode Selection: Basic Mode

    2. Error Calculation Mode: Actual Result vs Setpoint

    3. Threshold Interrupt Mode: ADERR < ADLTH or ADERR > ADUTH

    4. Upper Threshold (V): 2

    5. Lower Threshold (V): 1

    6. Threshold Setpoint (V): 0

  • Advanced Hardware Settings:
    1. Acquisition Count: 1 (if using Signal and Noise Generator or other device with buffered output)

    2. Previous Sample Input: RES

  • Interrupt Settings:
    1. ADCH1 Interrupt Enable

Data Streamer:  

  • Software Settings:
    1. UART Dependency Selection: Select the UART that is connected to Serial/CDC port. On Q71 Curiosity Nano, this is UART2.

  • Data Streamer Table:
    1. Add variable: uint16_t adc_result

    2. Add variable: uint8_t LED_state

System:
  • Clock Configuration: Default (1 MHz)

  • Configure Pin Grid View:
    1. PORTA Pin 2 as ANPx input

    2. Set UART Tx Pin as Output (RB4 on Q71 Curiosity Nano)

    3. Select LED pin as output (RC7 on Q71 Curiosity Nano)

    4. Rename Custom Name to "LED"

Other:
  • Generate the code

  • Add the code snippet(s) to the application code

  • Program the board

  • Open Data Visualizer, Click New Variable Streamer, Import, mcc_generated_files -> data_streamer -> data_streamer.ds. Click Next, then Finish.

#include "mcc_generated_files/system/system.h"
volatile bool SEND_FRAME = false;
uint16_t adc_result;
volatile uint8_t outside_threshold = 0;
void DS_Frame_Update(void){
    ADC_StartConversion();
    while(!(ADC_IsConversionDone()));
    DataStreamer.adc_result = ADC_GetFilterValue();
    LED_Toggle();
    SEND_FRAME = true;
}
void ADC_Interrupt_Threshold_ISR(void)   {
    // Clear the ADC interrupt flag
    PIR1bits.ADCH1IF = 0;
    // Set software flag to use in while(1) loop
    outside_threshold = 1;
}
int main(void)
{
    SYSTEM_Initialize();
    
    // Point interrupt Handler to our custom ISR
    ADC_SetContext1ThresholdInterruptHandler(&ADC_Interrupt_Threshold_ISR);
    
    // If using Signal and Noise Generator board, use pin RA2
    ADC_SetChannel(channel_ANA2);
    
    // Initialize Data Streamer ADC variable
    DataStreamer.adc_result = 0;

    // Enable the Global Interrupts 
    INTERRUPT_GlobalInterruptEnable(); 

    while(1)
    {           
        // Check if outside user defined threshold
        // If so turn on LED (active LOW)
        {
        if (outside_threshold)  {
            LED_SetLow();           // turn on LED
            outside_threshold = 0;  // reset so it can be set again by ISR
        }
        else LED_SetHigh();         // otherwise turn off LED
        
        DS_Frame_Update();
        if(SEND_FRAME)  {           // if the Data Streamer frame has been updated
            WriteFrame();           // Write the new frame
            SEND_FRAME = false;     // Set the boolean back to false for next time
        }
    }    
}

Melody ADCCC Use Case 5 - Using FVR as ADC voltage reference to scale ADC reads

This is useful when working with a small single-ended analog signal that does not use full range from GND to VDD. This Use Case narrows the ADC window of interest using FVR (Fixed Voltage Reference) predefined voltage references: 1.024V, 2.048V, 4.096V

Device Resources:
  1. Add ADC3

  2. Add FVR

  3. Add Data Streamer

Data Streamer:  

  • Software Settings:
    1. UART Dependency Selection: Select the UART that is connected to Serial/CDC port. On Q71 Curiosity Nano, this is UART2.

  • Data Streamer Table:
    1. Add variable: uint16_t adc_result

FVR:
  • Hardware Settings:
    1. Enable FVR

    2. FVR Buffer Gain to ADC: 2x (note the voltage 2.048V is listed when this option is selected)

    3. FVR Buffer Gain to other Peripherals: OFF

ADC3:  

  • Hardware Settings:
    1. Set Input Configuration: Single Ended Mode

    2. Set Result Format: Right Justified

    3. VDD (V): 3.3

  • Clock Selection: ADCRC

  • Context 1 Channel Selection
    1. Positive Channel Selection: ANA2 (if using Signal and Noise Generator)

    2. Positive Voltage Reference: FVR

  • Computation and Threshold Comparison:
    1. Operating Mode Selection: Basic Mode

  • Advanced Hardware Settings:
    1. Acquisition Count: 1 (if using Signal and Noise Generator or other device with buffered output)

    2. Previous Sample Input: RES

  • Configure Pin Grid View:
    1. PORTA Pin 2 as ANPx input

    2. Set UART Tx Pin as Output (RB4 on Q71 Curiosity Nano)

Other:
  • Generate the code

  • Add the code snippet(s) to the application code

  • Program the board

  • Open Data Visualizer, Click New Variable Streamer, Import, mcc_generated_files -> data_streamer -> data_streamer.ds. Click Next, then Finish.

  • If using Signal and Noise Generator board, select either Sine Wave or Square Wave and adjust signal Amplitude and Offset to fit within ADCCC reference voltage.

#include "mcc_generated_files/system/system.h"
volatile bool SEND_FRAME = false;
uint16_t adc_result;
void DS_Frame_Update(void){
    ADC_StartConversion();
    while(!(ADC_IsConversionDone()));
    DataStreamer.adc_result = ADC_GetFilterValue();
    LED_Toggle();
    SEND_FRAME = true;
}
int main(void)
{
    SYSTEM_Initialize();

    // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts 
    // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts 
    // Use the following macros to: 

    // Enable the Global Interrupts 
    //INTERRUPT_GlobalInterruptEnable(); 

    // Disable the Global Interrupts 
    //INTERRUPT_GlobalInterruptDisable(); 

    while(1)
    {
        DS_Frame_Update();
        if(SEND_FRAME)  {           // if the Data Streamer frame has been updated
            WriteFrame();           // Write the new frame
            SEND_FRAME = false;     // Set the boolean back to false for next time
        }
    }    
}

Melody ADCCC Use Case 6 - Using DAC as ADC voltage reference

This is useful when working with a small analog signal that does not range from GND to VDD. This Use Case narrows the range of the ADC window using the DAC as a reference, which is useful when one of the FVR internal references (see ADCCC Use Case 5) is not close enough to the ADC range of interest. The user can set a specific reference voltage in order to maximize the ADC resolution over the ADC input range of interest.

Device Resources:
  1. Add ADC3

  2. Add DAC

  3. Add Data Streamer

Data Streamer:  

  • Software Settings:
    1. UART Dependency Selection: Select the UART that is connected to Serial/CDC port. On Q71 Curiosity Nano, this is UART2.

  • Data Streamer Table:
    1. Add variable: uint16_t adc_result

DAC:
  • Software Settings:
    1. VDD: Enter microcontroller VDD. This example uses 3.3V

    2. Required Ref: Enter reference voltage (between 1.8V and VDD). This example uses 1.8V

  • Hardware Settings:
    1. DAC Enable: Turn ON

    2. DAC Positive Reference Selection: VDD

    3. DAC Negative Reference Selection: VSS

    4. DAC Output Enable Selection: DACOUT2 Enabled and DACOUT1 Disabled (DACOUT1 on pin RA2, DACOUT2 on pin RB7)

ADC3:  

  • Hardware Settings:
    1. Set Input Configuration: Single Ended Mode

    2. Set Result Format: Right Justified

    3. VDD (V): Enter microcontroller VDD. This example uses 3.3V

  • Clock Selection: ADCRC

  • Context 1 Channel Selection
    1. Positive Channel Selection: ANA2 (if using Signal and Noise Generator)

    2. Positive Voltage Reference: External (The VREF field fill be automatically populated. If it is incorrect, check correct VDD value has been entered for both DAC and ADC.)

  • Computation and Threshold Comparison:
    1. Operating Mode Selection: Basic Mode

  • Advanced Hardware Settings:
    1. Acquisition Count: 1 (if using Signal and Noise Generator or other device with buffered output)

    2. Previous Sample Input: RES  

  • Configure Pin Grid View:
    1. PORTA Pin 2 as ANPx input

    2. Set UART Tx Pin as Output (RB4 on Q71 Curiosity Nano)

IMPORTANT NOTE: Place a jumper wire between DAC output (RB7) and ADC VREF+ (RA3).

Other:
  • Generate the code

  • Add the code snippet(s) to the application code

  • Program the board

  • Open Data Visualizer, Click New Variable Streamer, Import, mcc_generated_files -> data_streamer -> data_streamer.ds. Click Next, then Finish.

  • If using Signal and Noise Generator board, select either Sine Wave or Square Wave and adjust signal Amplitude and Offset to fit within ADCCC reference voltage.

#include "mcc_generated_files/system/system.h"
volatile bool SEND_FRAME = false;
void DS_Frame_Update(void){
    ADC_StartConversion();
    while(!(ADC_IsConversionDone()));
    DataStreamer.adc_result = ADC_GetConversionResult();
    SEND_FRAME = true;
}
int main(void)
{
    SYSTEM_Initialize();

    while(1)
    {
        DS_Frame_Update();      // write the result of an ADC read to the Data Streamer struct
        if(SEND_FRAME)  {       // if the Data Streamer frame has been updated
            WriteFrame();       // Write the new frame 
            SEND_FRAME = false; // Set the boolean back to false for next time
        }
    }    
}