CMSIS-Driver_PIC32CM-JH  
Peripheral Library (PLIB) Documentation
 
Loading...
Searching...
No Matches
ADC Interface

Driver API for ADC (plib_adc0.h) More...

Typedefs

typedef uint32_t ADC_PRESCALER
 ADC clock prescaler selection.
 
typedef uint32_t ADC_VOLTAGE_REF
 ADC voltage reference selection.
 
typedef uint32_t ADC_RESOLUTION
 ADC conversion result resolution.
 
typedef uint32_t ADC_WIN_MODE
 ADC window comparator mode selection.
 
typedef uint32_t ADC_SAMPLE_NUM
 ADC sample number accumulation selection.
 
typedef uint32_t ADC_POS_CHANNEL
 ADC positive channel selection.
 
typedef uint32_t ADC_NEG_CHANNEL
 ADC negative channel selection.
 
typedef uint32_t ADC_EVENT_OUTPUT
 ADC event output and input selection.
 
typedef uint32_t ADC_INTERRUPT_SOURCE
 ADC interrupt sources.
 
typedef void(* ADC_CALLBACK_HANDLER) (ADC_INTERRUPT_SOURCE source)
 ADC interrupt callback handler type.
 

Functions

void ADC0_Calibration (void)
 Calibrate the ADC0 using factory fuse values.
 
void ADC0_SetPrescaler (ADC_PRESCALER pre_scaler)
 Sets the ADC0 clock prescaler.
 
void ADC0_SetReferenceCompensation (bool enable_ref_comp)
 Enables or disables the reference buffer offset compensation.
 
void ADC0_SetVoltageReference (ADC_VOLTAGE_REF ref_sel)
 Sets the ADC0 voltage reference source.
 
void ADC0_SetOffsetCompensation (bool offset_comp)
 Enables or disables comparator offset compensation.
 
void ADC0_SetSampleLength (uint8_t sample_len)
 Sets the ADC0 sample length.
 
void ADC0_SetResolution (ADC_RESOLUTION resolution)
 Sets the ADC0 conversion result resolution.
 
void ADC0_SetWindowMode (ADC_WIN_MODE win_mode)
 Sets the ADC0 window monitor mode.
 
void ADC0_ConfigureWindowMode (uint16_t win_lt, uint16_t win_ut)
 Configures the ADC0 window monitor upper and lower thresholds.
 
void ADC0_SetRailToRail (bool rail_to_rail)
 Enables or disables rail-to-rail operation.
 
void ADC0_SetCorrection (bool correction)
 Enables or disables digital correction logic.
 
void ADC0_SetGainCorrection (uint16_t gain_corr)
 Sets the ADC0 gain correction value.
 
void ADC0_SetOffsetCorrection (uint16_t offset_corr)
 Sets the ADC0 offset correction value.
 
void ADC0_SetFreeRunMode (bool free_run)
 Enables or disables free running mode.
 
void ADC0_SetLeftAdjResult (bool left_adj)
 Sets result left adjustment.
 
void ADC0_SetAveragingResultAdjustment (uint8_t adjres)
 Sets averaging result adjustment.
 
void ADC0_SetSampleAccummulationCount (ADC_SAMPLE_NUM samp_acc_count)
 Sets the number of samples to accumulate for ADC0 conversion.
 
void ADC0_SetOnDemand (bool on_demand)
 Enables or disables on-demand operation.
 
void ADC0_SetRunStandby (bool run_standby)
 Enables or disables ADC0 operation in standby mode.
 
void ADC0_SetDebugRun (bool debug)
 Enables or disables ADC0 operation during debugging.
 
void ADC0_Enable (void)
 Enables the ADC0 module.
 
void ADC0_SelectChannel (ADC_POS_CHANNEL positive_input)
 Selects the ADC0 input channel (single-ended).
 
void ADC0_ConfigureSequence (uint32_t seqmask)
 Configures the ADC0 input sequence.
 
bool ADC0_SequenceIsComplete (void)
 Checks if the ADC0 input sequence is complete.
 
uint8_t ADC0_GetSequenceState (void)
 Gets the current ADC0 sequence state.
 
void ADC0_SetDifferentialMode (bool diff_mode)
 Enables or disables differential mode.
 
void ADC0_SelectDifferentialChannel (ADC_POS_CHANNEL positive_input, ADC_NEG_CHANNEL negative_input)
 Selects the ADC0 input channels for differential conversion.
 
void ADC0_StartConversion (void)
 Starts an ADC0 conversion or sequence.
 
uint16_t ADC0_GetConversionResult (void)
 Gets the ADC0 conversion result.
 
void ADC0_SetEventControl (ADC_EVENT_OUTPUT event, bool enable)
 Configures ADC0 event control.
 
void ADC0_EnableInterrupt (ADC_INTERRUPT_SOURCE interrupt)
 Enables an ADC0 interrupt source.
 
void ADC0_DisableInterrupt (ADC_INTERRUPT_SOURCE interrupt)
 Disables an ADC0 interrupt source.
 
void ADC0_ClearInterruptFlag (ADC_INTERRUPT_SOURCE interrupt)
 Clears an ADC0 interrupt flag.
 
bool ADC0_GetInterruptFlagStatus (ADC_INTERRUPT_SOURCE interrupt)
 Gets the status of an ADC0 interrupt flag.
 
void ADC0_Disable (void)
 Disables the ADC0 module.
 
void ADC0_Flush (void)
 Flushes the ADC0 pipeline.
 
void ADC0_ResetSoftware (void)
 Resets the ADC0 via software.
 
void ADC0_SetCallbackHandler (ADC_CALLBACK_HANDLER callback)
 Registers an ADC0 interrupt callback handler.
 

Description

Driver API for ADC (plib_adc0.h)

Usage Example

The following example demonstrates typical usage of the ADC peripheral:

#include "plib_adc0.h"
#include <stdbool.h>
#include <stdint.h>
static void ADC0_CallbackFunction(ADC_INTERRUPT_SOURCE source)
{
if (source == (uint32_t)ADC_INTFLAG_RESRDY_Pos)
{
/* Read ADC result inside interrupt */
}
}
int32_t configure_adc0(void)
{
const uint8_t ADC_CLOCK_CYCLES = 3U;
/* Reset ADC0 registers to default state */
/* Load calibration values for accurate conversion */
/* Set ADC clock prescaler (divides input clock by 8) */
ADC0_SetPrescaler(ADC_CTRLB_PRESCALER_DIV8_Val);
/* Configure sampling time */
ADC0_SetSampleLength(ADC_CLOCK_CYCLES);
/* Select internal voltage reference */
ADC0_SetVoltageReference(ADC_REFCTRL_REFSEL_INTVCC0_Val);
/* Set conversion resolution to 12 bits */
ADC0_SetResolution(ADC_CTRLC_RESSEL_12BIT_Val);
/* Enable continuous conversion mode */
/* Configure ADC in single-ended mode */
/* Select analog input channel AIN0 */
ADC0_SelectChannel(ADC_INPUTCTRL_MUXPOS_AIN0_Val);
/* Register callback for ADC interrupt events */
ADC0_SetCallbackHandler(&ADC0_CallbackFunction);
/* Enable interrupt when conversion result is ready */
ADC0_EnableInterrupt(ADC_INTENSET_RESRDY_Pos);
/* Enable ADC peripheral */
/* Start ADC conversion */
/* Poll until conversion is complete */
while (!ADC0_GetInterruptFlagStatus(ADC_INTFLAG_RESRDY_Pos))
{
/* Wait for conversion to complete */
}
/* Read converted digital value */
uint16_t adc_result = ADC0_GetConversionResult();
/* Disable ADC to save power */
(void)adc_result;
return 0;
}
void ADC0_SetSampleLength(uint8_t sample_len)
Sets the ADC0 sample length.
void ADC0_StartConversion(void)
Starts an ADC0 conversion or sequence.
void ADC0_SetResolution(ADC_RESOLUTION resolution)
Sets the ADC0 conversion result resolution.
void ADC0_SelectChannel(ADC_POS_CHANNEL positive_input)
Selects the ADC0 input channel (single-ended).
bool ADC0_GetInterruptFlagStatus(ADC_INTERRUPT_SOURCE interrupt)
Gets the status of an ADC0 interrupt flag.
uint16_t ADC0_GetConversionResult(void)
Gets the ADC0 conversion result.
void ADC0_EnableInterrupt(ADC_INTERRUPT_SOURCE interrupt)
Enables an ADC0 interrupt source.
void ADC0_SetDifferentialMode(bool diff_mode)
Enables or disables differential mode.
void ADC0_Disable(void)
Disables the ADC0 module.
void ADC0_SetFreeRunMode(bool free_run)
Enables or disables free running mode.
void ADC0_Enable(void)
Enables the ADC0 module.
void ADC0_ResetSoftware(void)
Resets the ADC0 via software.
void ADC0_SetPrescaler(ADC_PRESCALER pre_scaler)
Sets the ADC0 clock prescaler.
uint32_t ADC_INTERRUPT_SOURCE
ADC interrupt sources.
Definition plib_adc_common.h:128
void ADC0_SetCallbackHandler(ADC_CALLBACK_HANDLER callback)
Registers an ADC0 interrupt callback handler.
void ADC0_Calibration(void)
Calibrate the ADC0 using factory fuse values.
void ADC0_SetVoltageReference(ADC_VOLTAGE_REF ref_sel)
Sets the ADC0 voltage reference source.

Typedef Documentation

◆ ADC_CALLBACK_HANDLER

ADC_CALLBACK_HANDLER

ADC interrupt callback handler type.

Defines the prototype for ADC interrupt callback functions.

Parameters
sourceADC interrupt source that triggered the callback.

◆ ADC_EVENT_OUTPUT

ADC event output and input selection.

This type specifies ADC event input and output behavior.

◆ ADC_INTERRUPT_SOURCE

ADC interrupt sources.

This type identifies the ADC interrupt source passed to the callback.

◆ ADC_NEG_CHANNEL

ADC negative channel selection.

This type specifies the ADC negative input channel.

◆ ADC_POS_CHANNEL

ADC positive channel selection.

This type specifies the ADC positive input channel.

◆ ADC_PRESCALER

ADC clock prescaler selection.

This type represents the ADC clock prescaler configuration. Valid values are provided as macros defined by the device header

◆ ADC_RESOLUTION

ADC conversion result resolution.

This type specifies the resolution of the ADC conversion result.

◆ ADC_SAMPLE_NUM

ADC sample number accumulation selection.

This type specifies the number of samples accumulated before averaging.

◆ ADC_VOLTAGE_REF

ADC voltage reference selection.

This type specifies the voltage reference source used by the ADC. Valid values are provided as macros defined by the ADC.

◆ ADC_WIN_MODE

ADC window comparator mode selection.

This type specifies the window comparison mode used by the ADC.

Function Documentation

◆ ADC0_Calibration()

void ADC0_Calibration ( void )

Calibrate the ADC0 using factory fuse values.

Loads BIAS and LINEARITY calibration values from NVM software calibration row into the ADC0 CALIB register. This ensures specified accuracy for ADC0 conversions.

Parameters
None.
Returns
None.

◆ ADC0_ClearInterruptFlag()

void ADC0_ClearInterruptFlag ( ADC_INTERRUPT_SOURCE interrupt)

Clears an ADC0 interrupt flag.

This function clears the specified interrupt flag.

Parameters
interruptInterrupt flag to clear (see ADC_INTERRUPT_SOURCE for options).
Returns
None.

◆ ADC0_ConfigureSequence()

void ADC0_ConfigureSequence ( uint32_t seqmask)

Configures the ADC0 input sequence.

This function configures automatic sequencing of conversions for selected input channels.

Parameters
seqmaskBitmask of positive input channels to include in the sequence.
Returns
None.

◆ ADC0_ConfigureWindowMode()

void ADC0_ConfigureWindowMode ( uint16_t win_lt,
uint16_t win_ut )

Configures the ADC0 window monitor upper and lower thresholds.

This function sets the lower and upper threshold values for the window monitor.

Parameters
win_ltLower threshold value.
win_utUpper threshold value.
Returns
None.

◆ ADC0_Disable()

void ADC0_Disable ( void )

Disables the ADC0 module.

This function disables the ADC0 peripheral.

Parameters
None.
Returns
None.

◆ ADC0_DisableInterrupt()

void ADC0_DisableInterrupt ( ADC_INTERRUPT_SOURCE interrupt)

Disables an ADC0 interrupt source.

This function disables the specified ADC0 interrupt.

Parameters
interruptInterrupt source (see ADC_INTERRUPT_SOURCE for options).
Returns
None.

◆ ADC0_Enable()

void ADC0_Enable ( void )

Enables the ADC0 module.

This function enables the ADC0 peripheral, allowing conversions to be started.

Parameters
None.
Returns
None.

◆ ADC0_EnableInterrupt()

void ADC0_EnableInterrupt ( ADC_INTERRUPT_SOURCE interrupt)

Enables an ADC0 interrupt source.

This function enables the specified ADC0 interrupt.

Parameters
interruptInterrupt source (see ADC_INTERRUPT_SOURCE for options).
Returns
None.

◆ ADC0_Flush()

void ADC0_Flush ( void )

Flushes the ADC0 pipeline.

This function flushes the ADC0 pipeline which will Abort all conversions in progress and restart the ADC0 clock.

Parameters
None.
Returns
None.

◆ ADC0_GetConversionResult()

uint16_t ADC0_GetConversionResult ( void )

Gets the ADC0 conversion result.

This function returns the latest conversion result from the ADC0 conversion.

Parameters
None.
Returns
16-bit ADC0 conversion result.

◆ ADC0_GetInterruptFlagStatus()

bool ADC0_GetInterruptFlagStatus ( ADC_INTERRUPT_SOURCE interrupt)

Gets the status of an ADC0 interrupt flag.

This function checks if the specified interrupt flag is set.

Parameters
interruptInterrupt source (see ADC_INTERRUPT_SOURCE for options).
Returns
true if interrupt flag is set, false otherwise.

◆ ADC0_GetSequenceState()

uint8_t ADC0_GetSequenceState ( void )

Gets the current ADC0 sequence state.

Returns the input number of the last conversion done in the sequence.

Parameters
None.
Returns
Sequence state value.

◆ ADC0_ResetSoftware()

void ADC0_ResetSoftware ( void )

Resets the ADC0 via software.

This function resets the ADC0 to the default state and disables the ADC0.

Parameters
None.
Returns
None.

◆ ADC0_SelectChannel()

void ADC0_SelectChannel ( ADC_POS_CHANNEL positive_input)

Selects the ADC0 input channel (single-ended).

This function configures the positive input channel for single-ended conversion.

Parameters
positive_inputMUXPOS value for the desired input channel (see ADC_POS_CHANNEL for options).
Returns
None.

◆ ADC0_SelectDifferentialChannel()

void ADC0_SelectDifferentialChannel ( ADC_POS_CHANNEL positive_input,
ADC_NEG_CHANNEL negative_input )

Selects the ADC0 input channels for differential conversion.

This function configures the positive and negative input channels for differential conversion.

Parameters
positive_inputMUXPOS value for positive input (see ADC_POS_CHANNEL for options).
negative_inputMUXNEG value for negative input (see ADC_NEG_CHANNEL for options).
Returns
None.

◆ ADC0_SequenceIsComplete()

bool ADC0_SequenceIsComplete ( void )

Checks if the ADC0 input sequence is complete.

This function checks the current sequence status.

Parameters
None.
Returns
true if sequence is complete, false otherwise.

◆ ADC0_SetAveragingResultAdjustment()

void ADC0_SetAveragingResultAdjustment ( uint8_t adjres)

Sets averaging result adjustment.

This function configures the division coefficient for averaging results.

Parameters
adjresAdjusting result (division coefficient in 2n steps).
Returns
None.

◆ ADC0_SetCallbackHandler()

void ADC0_SetCallbackHandler ( ADC_CALLBACK_HANDLER callback)

Registers an ADC0 interrupt callback handler.

This function sets a user-defined callback function to be invoked when an ADC0 interrupt occurs.

Parameters
callbackPointer to the callback function.
Returns
None.

◆ ADC0_SetCorrection()

void ADC0_SetCorrection ( bool correction)

Enables or disables digital correction logic.

Enabling will correct the ADC0 result for gain and offset based on the gain and offset correction values. Conversion time will be increased when enabled.

Parameters
correctionTrue to enable, false to disable.
Returns
None.

◆ ADC0_SetDebugRun()

void ADC0_SetDebugRun ( bool debug)

Enables or disables ADC0 operation during debugging.

When enabled, ADC0 continues operation when CPU is halted by debugger.

Parameters
debugTrue to enable, false to disable.
Returns
None.

◆ ADC0_SetDifferentialMode()

void ADC0_SetDifferentialMode ( bool diff_mode)

Enables or disables differential mode.

This function configures the ADC0 for differential or single-ended conversion.

Parameters
diff_modeTrue for differential, false for single-ended.
Returns
None.

◆ ADC0_SetEventControl()

void ADC0_SetEventControl ( ADC_EVENT_OUTPUT event,
bool enable )

Configures ADC0 event control.

Enables or disables event input/output features for the ADC0.

Parameters
eventEvent selection (see ADC_EVENT_OUTPUT for options).
enableTrue to enable, false to disable.
Returns
None.

◆ ADC0_SetFreeRunMode()

void ADC0_SetFreeRunMode ( bool free_run)

Enables or disables free running mode.

When enabled, ADC0 continuously samples and converts the input channel.

Parameters
free_runTrue to enable, false to disable.
Returns
None.

◆ ADC0_SetGainCorrection()

void ADC0_SetGainCorrection ( uint16_t gain_corr)

Sets the ADC0 gain correction value.

This function sets the gain correction value to compensate for gain error in ADC0 results.

Parameters
gain_corrGain correction value (1-bit integer + 11-bit fraction).
Returns
None.

◆ ADC0_SetLeftAdjResult()

void ADC0_SetLeftAdjResult ( bool left_adj)

Sets result left adjustment.

This function configures whether the ADC0 result is left- or right-adjusted in the RESULT register.

Parameters
left_adjTrue for left-adjusted, false for right-adjusted.
Returns
None.

◆ ADC0_SetOffsetCompensation()

void ADC0_SetOffsetCompensation ( bool offset_comp)

Enables or disables comparator offset compensation.

Enabling can reduce temperature or voltage drift offset each sampling period. When enabled, sampling duration is fixed to 4 ADC clock cycles.

Parameters
offset_compTrue to enable, false to disable.
Returns
None.

◆ ADC0_SetOffsetCorrection()

void ADC0_SetOffsetCorrection ( uint16_t offset_corr)

Sets the ADC0 offset correction value.

This function sets the offset correction value to compensate for offset error in ADC0 results.

Parameters
offset_corrOffset correction value (two's complement format).
Returns
None.

◆ ADC0_SetOnDemand()

void ADC0_SetOnDemand ( bool on_demand)

Enables or disables on-demand operation.

When enabled, ADC0 runs only when requested by a peripheral.

Parameters
on_demandTrue to enable, false to disable.
Returns
None.

◆ ADC0_SetPrescaler()

void ADC0_SetPrescaler ( ADC_PRESCALER pre_scaler)

Sets the ADC0 clock prescaler.

This function configures the ADC0 clock division factor relative to the peripheral clock.

Parameters
pre_scalerPrescaler selection (see ADC_PRESCALER for options).
Returns
None.

◆ ADC0_SetRailToRail()

void ADC0_SetRailToRail ( bool rail_to_rail)

Enables or disables rail-to-rail operation.

Enabling will extend the allowable range of input common mode voltage. Requires offset compensation and a sampling period of four cycles.

Parameters
rail_to_railTrue to enable, false to disable.
Returns
None.

◆ ADC0_SetReferenceCompensation()

void ADC0_SetReferenceCompensation ( bool enable_ref_comp)

Enables or disables the reference buffer offset compensation.

Enabling can reduce the gain error but also increase reference startup time.

Parameters
enable_ref_compTrue to enable, false to disable.
Returns
None.

◆ ADC0_SetResolution()

void ADC0_SetResolution ( ADC_RESOLUTION resolution)

Sets the ADC0 conversion result resolution.

This function configures the number of bits in the ADC0 conversion result.

Parameters
resolutionResolution setting (see ADC_RESOLUTION for options)
Returns
None.

◆ ADC0_SetRunStandby()

void ADC0_SetRunStandby ( bool run_standby)

Enables or disables ADC0 operation in standby mode.

When enabled, ADC0 continues operation during standby sleep mode.

Parameters
standbyTrue to enable, false to disable.
Returns
None.

◆ ADC0_SetSampleAccummulationCount()

void ADC0_SetSampleAccummulationCount ( ADC_SAMPLE_NUM samp_acc_count)

Sets the number of samples to accumulate for ADC0 conversion.

This function configures the ADC0 to accumulate a specified number of samples for each conversion for averaging or oversampling.

Parameters
samp_acc_countNumber of samples to accumulate.
Returns
None.

◆ ADC0_SetSampleLength()

void ADC0_SetSampleLength ( uint8_t sample_len)

Sets the ADC0 sample length.

This function configures the number of ADC clock periods used for sampling. Sample length is only available when offset compensation is disabled.

Parameters
sample_lenNumber of ADC clock periods plus 1 for sampling.
Returns
None.

◆ ADC0_SetVoltageReference()

void ADC0_SetVoltageReference ( ADC_VOLTAGE_REF ref_sel)

Sets the ADC0 voltage reference source.

This function selects the voltage reference source for the ADC0.

Parameters
ref_selVoltage reference selection (see ADC_VOLTAGE_REF for options).
Returns
None.

◆ ADC0_SetWindowMode()

void ADC0_SetWindowMode ( ADC_WIN_MODE win_mode)

Sets the ADC0 window monitor mode.

This function enables or disables the window monitor and sets its comparison mode.

Parameters
win_modeWindow monitor mode. (see ADC_WIN_MODE for options)
Returns
None.

◆ ADC0_StartConversion()

void ADC0_StartConversion ( void )

Starts an ADC0 conversion or sequence.

This function initiates an ADC0 conversion or sequence

Parameters
None.
Returns
None.