4.1.2.3 12-bit ADCC

12-bit ADC with Computation

4.1.2.3.1 Introduction

This Analog-to-Digital Converter with Computation (ADCC) module converts an analog voltage value into a 12-bit numerical value. It offers different computational actions that can be performed on the result such as averaging or low-pass filtering.

4.1.2.3.2 Supported Device Families

PIC16F184xx PIC16F191xx PIC18F-K42 PIC18F-K83 PIC18F-Q4x

4.1.2.3.3 Required header files:


#include "mcc_generated_files/adc/adcc.h"

4.1.2.3.4 How to use the ADCC PLIB Driver

The ADCC 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.

Instructions common for all code snippets can be found here: 4.1.2.3.4.3.1.1 ADCC Use Case Code Snippet Instructions

Table 4-272. Running the ADCC Use-Cases
Videos Running the MCC Melody use case code snippets for PIC16F/18F:
Many of use cases build on the following components, so this is a good place to start if you are new to MCC Melody:

Module Documentation

ADCC

API Prototypes for the ADCC driver.

Module description

API Prototypes for the ADCC driver.

Version: ADCC Driver Version 2.1.4
Typedefs
  • typedef uint16_t adc_result_t

    Used for the result of the A/D conversion.

Functions
Typedef Documentation
adc_result_t

adc_result_t

Used for the result of the A/D conversion.

Section: Included Files

Function Documentation
ADCC_ClearAccumulator()

void ADCC_ClearAccumulator (void )[inline]

Clears the accumulator.

Parameters:
None.
Returns:

None.

ADCC_DefineSetPoint()

void ADCC_DefineSetPoint (uint16_t setPoint)

Sets the value of the ADC Threshold setpoint.

Parameters:
setPoint

- 16-bit value for set point.

Returns:

None.

ADCC_DisableContinuousConversion()

void ADCC_DisableContinuousConversion (void )[inline]

Disables continuous conversion.

Parameters:
None.
Returns:

None.

ADCC_DischargeSampleCapacitor()

void ADCC_DischargeSampleCapacitor (void )[inline]

Discharges the input sample capacitor by setting the channel to AVss.

Parameters:
None.
Returns:

None.

ADCC_EnableContinuousConversion()

void ADCC_EnableContinuousConversion (void )[inline]

Enables continuous conversion.

Parameters:
None.
Returns:

None.

ADCC_EnableDoubleSampling()

void ADCC_EnableDoubleSampling (void )[inline]

Enables the Double Sampling bit.

Parameters:
None.
Returns:

None.

ADCC_GetAccumulatorValue()

uint24_t ADCC_GetAccumulatorValue (void )

Retrieves the 18-bit value of the ADC accumulator.

Parameters:
None.
Returns:

Value of the ADC accumulator.

ADCC_GetConversionResult()

adc_result_t ADCC_GetConversionResult (void )

Retrieves the result of the latest A/D conversion.

Precondition:

Call ADCC_StartConversion() first before this function and check completion status using ADCC_IsConversionDone() routine.

Parameters:
None.
Returns:

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

ADCC_GetConversionStageStatus()

uint8_t ADCC_GetConversionStageStatus (void )

Retrieves the multi-stage status.

Precondition:

Call ADCC_StartConversion() first before this function.

Parameters:
None.
Returns:

Contents of the ADC STATUS register.

ADCC_GetCurrentCountofConversions()

uint8_t ADCC_GetCurrentCountofConversions (void )

Retrieves the current value of the ADC Repeat Count register.

Precondition:

Call ADCC_StartConversion() first before this function.

Parameters:
None.
Returns:

Current value of the ADC Repeat Count register

ADCC_GetErrorCalculation()

uint16_t ADCC_GetErrorCalculation (void )

Retrieves the value of the ADC Setpoint Error register.

Precondition:

Call ADCC_StartConversion() first before this function.

Parameters:
None.
Returns:

16-bit value obtained from ADERRH and ADERRL registers.

ADCC_GetFilterValue()

uint16_t ADCC_GetFilterValue (void )

Retrieves the value of the ADC Filter register.

Parameters:
None.
Returns:

16-bit value obtained from the ADFLTRH and ADFLTRL registers.

ADCC_GetPreviousResult()

uint16_t ADCC_GetPreviousResult (void )

Retrieves the value of the ADC Previous register.

Precondition:

Call ADCC_StartConversion() first before this function.

Parameters:
None.
Returns:

16-bit value obtained from the ADPREVH and ADPREVL registers.

ADCC_GetSingleConversion()

adc_result_t ADCC_GetSingleConversion (adcc_channel_t channel)

Retrieves the result of a single A/D conversion on the given channel.

Precondition:

Call the ADCC_DisableContinuousConversion() function first before this function.

Parameters:
channel

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

Returns:

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

ADCC_HasAccumulatorOverflowed()

bool ADCC_HasAccumulatorOverflowed (void )

Determines if the ADC accumulator has overflowed.

Precondition:

Call ADCC_StartConversion() first before this function.

Parameters:
None.
Return values:
true

- The ADC accumulator has overflowed

false

- The ADC accumulator has not overflowed

ADCC_HasErrorCrossedLowerThreshold()

bool ADCC_HasErrorCrossedLowerThreshold (void )

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

Precondition:

Call ADCC_StartConversion() first before this function.

Parameters:
None.
Return values:
true

- If ERR < LTH

false

- If ERR >= LTH

ADCC_HasErrorCrossedUpperThreshold()

bool ADCC_HasErrorCrossedUpperThreshold (void )

Determines if the ADC error has crossed the upper threshold.

Precondition:

Call ADCC_StartConversion() first before this function.

Parameters:
None.
Return values:
true

- If ERR > UTH

false

- If ERR <= UTH

ADCC_Initialize()

void ADCC_Initialize (void )

Initializes the ADC module. This routine will be called before any other ADC routine.

Section: ADCC Module APIs

Parameters:
None.
Returns:

None.

Section: Included FilesSection: ADCC Module APIs

ADCC_IsConversionDone()

bool ADCC_IsConversionDone (void )

Checks if the ongoing A/D conversion is complete.

Precondition:

Call ADCC_StartConversion() first before this function.

Parameters:
None.
Return values:
true

- The conversion is complete.

false

- The conversion is ongoing.

ADCC_LoadAcquisitionRegister()

void ADCC_LoadAcquisitionRegister (uint16_t acquisitionValue)

Loads the ADC Acquisition Time Control register with the specified value.

Parameters:
acquisitionValue

- Value to be loaded to the acquisition time control register.

Returns:

None.

ADCC_SetLowerThreshold()

void ADCC_SetLowerThreshold (uint16_t lowerThreshold)

Sets the value of the ADC Lower Threshold register.

Parameters:
lowerThreshold

- 16-bit value for the lower threshold.

Returns:

None.

ADCC_SetPrechargeTime()

void ADCC_SetPrechargeTime (uint16_t prechargeTime)

Loads the ADC Precharge Time Control register with the specified value.

Parameters:
prechargeTime

- Value to be loaded to the precharge time control register.

Returns:

None.

ADCC_SetRepeatCount()

void ADCC_SetRepeatCount (uint8_t repeatCount)

Loads the repeat counter with the specified value.

Parameters:
repeatCount

- Value to be loaded to the repeat counter.

Returns:

None.

ADCC_SetStopOnInterrupt()

void ADCC_SetStopOnInterrupt (void )[inline]

Enables the Stop On Interrupt bit.

Precondition:

Call ADCC_EnableContinuousConversion() before calling this function.

Parameters:
None.
Returns:

None.

ADCC_SetUpperThreshold()

void ADCC_SetUpperThreshold (uint16_t upperThreshold)

Sets the value of the ADC Upper Threshold register.

Parameters:
upperThreshold

- 16-bit value for the upper threshold.

Returns:

None.

ADCC_StartConversion()

void ADCC_StartConversion (adcc_channel_t channel)

Starts an A/D conversion on the selected channel.

Parameters:
channel

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

Returns:

None.

ADCC_StopConversion()

void ADCC_StopConversion (void )[inline]

Stops an ongoing A/D conversion.

Precondition:

Call ADCC_StartConversion() first before this function.

Parameters:
None.
Returns:

None.

Enumeration Type Documentation
adcc_channel_t

enum adcc_channel_t

Contains the available ADC channels.

channel_OPA1
channel_DAC2
channel_VSS
channel_Temp
channel_DAC1
channel_FVR_Buffer1
channel_FVR_Buffer2

File Documentation

source/adcc.c File Reference

API Implementations for the ADCC driver.

#include <xc.h>
#include "../adcc.h"
Functions
Detailed Description

API Implementations for the ADCC driver.

ADCC Generated Driver File

Version: ADCC Driver Version 2.1.4

source/adcc.h File Reference

#include <stdint.h>
#include <stdbool.h>
Functions
Typedefs
  • typedef uint16_t adc_result_t

    Used for the result of the A/D conversion.

  • typedef __uint24 uint24_t

Detailed Description

ADCC Generated Driver API Header File

Typedef Documentation

Module Documentation

ADCC Use Cases

ADCC Use Case Code Snippet Instructions

For new users it is recommended to start with the Timer0, UART and Data Streamer, since these components are used to support other use cases.

The use cases show example uses of the ADCC PLIB Driver, within an MCC Melody project:
  • Add ADCC to the project

  • Configure:
    1. The ADCC as described in the example.

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

  • Generate the code

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

  • Program the board

ADC Use Case 1: ADCC in Basic_mode with Printf of ADC Result

An ADC conversion is taken every 100 ms, on the selected analog channel. After each ADC conversion a LED is toggled and the ADC Result is sent to PC terminal using printf.

Note: Before each conversion is started, the sample capacitor is first discharged, by connecting it to the ground potential of the microcontroller.

Device Resources:
  1. Add ADCC

  2. Add UART

ADCC Configuration:
  • Hardware Settings:
    1. Operating Mode: Basic Mode (Default)

UART:
  • Configuration Settings:
    1. Redirect Printf to UART: Yes.

  • Dependency Selector:  

    1. UART PLIB Selector: Select the UART that is connected to Serial/CDC port.

    2. Redirect STDIO to UART (for printf support): Yes

System:
  • Clock Configuration: Default (1 MHz)

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

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

    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:

/* Required header files for UART Example 1 */
#include "mcc_generated_files/system/system.h"
int main(void)
{
    SYSTEM_Initialize();
    
    uint16_t volatile adcResult;

    while(1)
    {
        ADCC_DischargeSampleCapacitor();   //Discharge capacitor voltage by connecting the VSS channel
        adcResult = ADCC_GetSingleConversion(channel_ANC3); //Get the conversion result
        LED_Toggle();
        printf("The ADC Result is: %d\r\n",adcResult);

        __delay_ms(100);  
    }
}
ADC Use Case 2: ADCC Samples Visualized with the Data Streamer
An ADC conversion is taken, on the selected analog channel, every 100 ms.
  • ADC converstions are triggerd by an timer overflow callback, where the Data Streamer variables adcResult and adcSampleCount are updated.

  • After each ADC conversion a LED is toggled and SEND_FRAME is set to true, so that a frame is sent to the Data Visualizer from the main while loop.

Device Resources:
  1. Add ADCC

  2. Add Timer (Use Case: 100 ms Timer)

  3. Add Data Streamer (Use Case: Send Frames at a Period Set by the Timer)

ADCC:
  • Hardware Settings:
    1. Operating Mode: Basic Mode (Default)

Data Streamer:  

  • Software Settings:
    1. UART Dependency Selection: Select the UART that is connected to Serial/CDC port.

  • Data Streamer Table:
    1. uint16_t adcResult

    2. uint8_t adcSampleCount

System:
  • Clock Configuration: Default (1 MHz)

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

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

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

    4. Pin Grid View: Select Debug GPIO pin.

    5. Pins: Rename Custom Names to "LED" and "DebugIO" respectively.

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

D,1,1,adcResult
B,1,1,adcSampleCount
#include "mcc_generated_files/system/system.h"

/* Create a pointer of type TMR_INTERFACE and assign it to the address of the Timer0 TMR_INTERFACE struct.
This enables us to get access the portable API interface, which ensures that it's easy to change the peripheral instance the timer runs on. */
const struct TMR_INTERFACE *Timer = &Timer0;
volatile bool SEND_FRAME = false;
void Timer_Callback_100ms(void){
    ADCC_DischargeSampleCapacitor();   //Discharge capacitor voltage by connecting the VSS channel
    Data_Streamer.adcResult = ADCC_GetSingleConversion(channel_ANC3); //Get the conversion result
    Data_Streamer.adcSampleCount++;
    LED_Toggle();
    SEND_FRAME = true;
}
int main(void)
{
    SYSTEM_Initialize();
    
    Data_Streamer.adcSampleCount = 0;
    Data_Streamer.adcResult = 0;
    
    Timer->TimeoutCallbackRegister(Timer_Callback_100ms);
    
    INTERRUPT_GlobalInterruptEnable();

    while(1)
    {
        if(SEND_FRAME){
            WriteFrame();
            SEND_FRAME = false;
        }
    }
}
ADC Use Case 3: ADCC Spike Detection Visualized with the Data Streamer

A change (above a set threshold) in the ADC result between successive conversions, represents a "spike". When a spike is detected, an callback updates the adcResult_lastSpike and adcSpikeCount variables.

Tested using PIC18F16Q41 CNANO board and potentiometer. When quickly turning the potentiometer a spike is detected.

Device Resources:
  1. Add ADCC

  2. Add Data Streamer

  3. Add Timer (Use Case: 100 ms Timer)

ADCC:  

  • Hardware Settings:
    1. Set Operating Mode: Basic_mode (default)

  • Computation Feature:
    1. Threshold Interrupt Mode: ADERR < ADLTH or ADERR > ADUTH.

    2. Lower Threshold: -35

    3. Upper Threshold: 35

  • Interrupt Settings:
    1. ADC Threshold Interrupt: Yes

Data Streamer:  

  • Software Settings:
    1. UART Dependency Selection: Select the UART that is connected to Serial/CDC port.

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

    2. Add variable: uint16_t adcResult_lastSpike

    3. Add variable: uint8_t adcSpikeCount

System:
  • Clock Configuration: Default (1 MHz)

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

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

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

    4. Pin Grid View: Select Debug GPIO pin.

    5. Pins: Rename Custom Names to "LED" and "DebugIO" respectively.

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

D,1,1,adcResult
D,1,1,adcResult_lastSpike
B,1,1,adcSpikeCount
#include "mcc_generated_files/system/system.h"
const struct TMR_INTERFACE *Timer = &Timer0;
volatile bool SEND_FRAME = false;
void ADCC_Callback_SpikeThreashold(void){
    Data_Streamer.adcResult_lastSpike = ADCC_GetConversionResult();
    Data_Streamer.adcSpikeCount++;
    LED_Toggle();
    DebugIO_Toggle();
    SEND_FRAME = true;
}

void Timer_Callback_100ms(void){
    ADCC_DischargeSampleCapacitor();         //Discharge capacitor voltage by connecting the VSS channel
    Data_Streamer.adcResult =  ADCC_GetSingleConversion(channel_ANC3); //Replace with suitable analog input - consulting your schematic 
    FRAME_SEND = true;
}
int main(void)
{
    SYSTEM_Initialize();
    
    Data_Streamer.adcSpikeCount = 0;
    Data_Streamer.adcResult_lastSpike = 0;
    Data_Streamer.adcResult = 0;

    Timer->TimeoutCallbackRegister(Timer_Callback_100ms);
    ADCC_SetADTIInterruptHandler(ADCC_Callback_SpikeThreashold);
    INTERRUPT_GlobalInterruptEnable();

    while(1)
    {
        if(SEND_FRAME){
            WriteFrame();
            SEND_FRAME = false;
        }
    }
}
ADC Use Case 3.5: Modifying the Spike Detection Sensitivity:

Observe the change in sensitivity with the following settings...

ADCC:
  • Hardware Settings:
    1. Set Operating Mode : Average_mode

  • Computation Feature:
    1. Set Repeat: 4

    2. Set Accumulator Right Shift : 1