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

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

Typedefs

typedef void(* ADC_Callback_t) (ADC_INTERRUPT_SOURCE intr_src)
 ADC callback.
 

Enumerations

enum  ADC_START_MODE { ADC_START_STOP = 0 , ADC_START_IMMEDIATE = 1 , ADC_START_INPUT = 2 , ADC_START_EVENT = 4 }
 ADC start-conversion mode selection. More...
 
enum  ADC_COMPUTATION_MODE {
  ADC_MODE_NONE = 0 , ADC_MODE_SINGLE = 1 , ADC_MODE_SERIES = 2 , ADC_MODE_BURST = 3 ,
  ADC_MODE_ACCTEST = 7
}
 ADC computation mode selection. More...
 
enum  ADC_WINMODE {
  ADC_WINMODE_NONE = 0 , ADC_WINMODE_BELOW = 1 , ADC_WINMODE_ABOVE = 2 , ADC_WINMODE_INSIDE = 3 ,
  ADC_WINMODE_OUTSIDE = 4
}
 ADC window comparator mode. More...
 
enum  ADC_WINSRC { ADC_WINSRC_RESULT = 0 , ADC_WINSRC_SAMPLE = 1 }
 ADC window comparator source. More...
 
enum  ADC_RES_SCALING { ADC_RES_SCALING_NORMAL = 0 , ADC_RES_SCALING_LEFTADJ = 1 , ADC_RES_SCALING_AVERAGE = 2 }
 ADC result scaling options. More...
 
enum  ADC_EVENT_OUTPUT {
  ADC_EVCTRL_STARTEI = ADC_EVCTRL_STARTEI_Msk , ADC_EVCTRL_STARTINV = ADC_EVCTRL_STARTINV_Msk , ADC_EVCTRL_RESRDYEO = ADC_EVCTRL_RESRDYEO_Msk , ADC_EVCTRL_SAMPRDYEO = ADC_EVCTRL_SAMPRDYEO_Msk ,
  ADC_EVCTRL_WCMPEO = ADC_EVCTRL_WCMPEO_Msk
}
 ADC event output. More...
 
enum  ADC_INTERRUPT_SOURCE {
  ADC_INT_RESRDY = 0 , ADC_INT_SAMPRDY = 1 , ADC_INT_WCMP = 2 , ADC_INT_RESOVR = 3 ,
  ADC_INT_SAMPOVR = 4 , ADC_INT_TRIGOVR = 5
}
 ADC interrupt sources. More...
 

Functions

void ADC_SetPrescaler (uint8_t pre_scaler)
 Sets the ADC clock prescaler value.
 
void ADC_SetSampleLength (uint8_t sample_len)
 Sets the ADC sample length.
 
void ADC_SetVoltageReference (uint8_t ref_sel)
 Sets the ADC voltage reference source.
 
void ADC_SetResolution (uint32_t resolution)
 Sets the ADC resolution.
 
void ADC_SetTimebase (uint8_t timebase)
 Sets the ADC Timebase field.
 
void ADC_SetFreeRunMode (bool free_run)
 Enables or disables the ADC free run mode.
 
void ADC_SetSampleAccummulationCount (uint32_t samp_acc_count)
 Sets the number of samples to accumulate for ADC conversion.
 
void ADC_SetDifferentialMode (bool diff_mode)
 Enables or disables ADC differential mode.
 
void ADC_SetComputationMode (ADC_COMPUTATION_MODE computation_mode)
 Sets the ADC computation mode.
 
void ADC_Enable (void)
 Enables the ADC module.
 
void ADC_Disable (void)
 Disables the ADC module.
 
uint32_t ADC_Get32bitConversionResult (void)
 Retrieves the 32-bit ADC conversion result.
 
uint16_t ADC_Get16bitConversionResult (void)
 Retrieves the 16-bit ADC conversion result.
 
void ADC_SelectDifferentialChannel (uint32_t positive_input, uint32_t negative_input)
 Selects the ADC differential input channels.
 
bool ADC_ConversionIsComplete (void)
 Checks if the ADC conversion is complete.
 
void ADC_StartConversion (ADC_START_MODE start_mode)
 Starts an ADC conversion with the specified start mode.
 
void ADC_AbortConversion (void)
 Aborts the ongoing ADC conversion.
 
void ADC_ResetSoftware (void)
 Performs a software reset and resets the ADC module.
 
bool ADC_EnableInterrupt (ADC_INTERRUPT_SOURCE interrupt)
 Enables the specified ADC interrupt(s).
 
void ADC_DisableInterrupt (ADC_INTERRUPT_SOURCE interrupt)
 Disables the specified ADC interrupt(s).
 
void ADC_ClearInterruptFlag (ADC_INTERRUPT_SOURCE intr_flag)
 Clears the specified ADC interrupt flag(s).
 
bool ADC_GetInterruptFlagStatus (ADC_INTERRUPT_SOURCE interrupt)
 Gets the current ADC interrupt flag status.
 
void ADC_ConfigureWindowMode (ADC_WINSRC win_src, ADC_WINMODE win_mode, uint32_t win_lt, uint32_t win_ht)
 Configures the ADC window comparator source, mode, and threshold values.
 
void ADC_SetEventControl (ADC_EVENT_OUTPUT event, bool enable)
 Configures the ADC event control settings.
 
void ADC_EnableWriteProtection (void)
 Enables write protection for ADC registers.
 
void ADC_DisableWriteProtection (void)
 Disables write protection for ADC registers.
 
void ADC_WriteProtectionLock (void)
 Locks write protection configuration until reset.
 
bool ADC_WriteProtectionIsEnabled (void)
 Queries if write protection is enabled.
 
bool ADC_WriteProtectionIsLocked (void)
 Queries if write protection is locked.
 
void ADC_SetResultScaling (ADC_RES_SCALING scale)
 Sets the ADC result scaling mode.
 
void ADC_SetLowPassFilter (bool low_pass_filter)
 Enables or disables the ADC low-pass filter.
 
void ADC_SetCallbackHandler (ADC_Callback_t callback)
 Sets a callback function for ADC interrupts.
 

Description

Driver API for ADC (plib_adc.h)

Usage Example

The following example demonstrates typical usage of the ADC peripheral:

#include "plib_adc.h"
#include <stdint.h>
#include <stdbool.h>
static void ADC_CallbackFunction(ADC_INTERRUPT_SOURCE int_src);
int configure_adc(void)
{
/* Reset ADC */
/* Set ADC prescaler to divide clock by 8 */
ADC_SetPrescaler((uint8_t)ADC_CTRLB_PRESCALER_DIV8);
/* Set sample length to 3 ADC clock cycles */
ADC_SetSampleLength((uint8_t)3UL);
/* Select internal voltage reference (1.024V) */
ADC_SetVoltageReference((uint8_t)ADC_CTRLC_REFSEL_1V024);
/* Set ADC resolution to 12 bits */
/* Enable free run mode for continuous conversions */
/* Set single-ended mode (differential mode disabled) */
/* Select positive input channel AIN2, negative input 0 (single-ended) */
ADC_SelectDifferentialChannel(ADC_INPUTCTRL_MUXPOS_AIN2, 0);
/* Set computation mode to single conversion */
/* Register the callback for ADC interrupts */
ADC_SetCallbackHandler(&ADC_CallbackFunction);
/* Enable Result Ready interrupt */
/* Enable the ADC module */
/* Start ADC conversion immediately */
/* Wait for conversion to complete (polling) */
{
/* Wait for conversion to complete */
}
/* Read the conversion result */
uint32_t adc_result = ADC_Get32bitConversionResult();
/* Disable the ADC module after use */
return adc_result;
}
static void ADC_CallbackFunction(ADC_INTERRUPT_SOURCE int_src)
{
if (int_src == ADC_INT_RESRDY)
{
/*uint32_t result = ADC_Get32bitConversionResult();*/
}
}
void ADC_SetResolution(uint32_t resolution)
Sets the ADC resolution.
void ADC_SetDifferentialMode(bool diff_mode)
Enables or disables ADC differential mode.
void ADC_SelectDifferentialChannel(uint32_t positive_input, uint32_t negative_input)
Selects the ADC differential input channels.
void ADC_Enable(void)
Enables the ADC module.
void ADC_SetFreeRunMode(bool free_run)
Enables or disables the ADC free run mode.
void ADC_SetVoltageReference(uint8_t ref_sel)
Sets the ADC voltage reference source.
void ADC_StartConversion(ADC_START_MODE start_mode)
Starts an ADC conversion with the specified start mode.
void ADC_SetPrescaler(uint8_t pre_scaler)
Sets the ADC clock prescaler value.
uint32_t ADC_Get32bitConversionResult(void)
Retrieves the 32-bit ADC conversion result.
void ADC_SetCallbackHandler(ADC_Callback_t callback)
Sets a callback function for ADC interrupts.
void ADC_ResetSoftware(void)
Performs a software reset and resets the ADC module.
void ADC_SetComputationMode(ADC_COMPUTATION_MODE computation_mode)
Sets the ADC computation mode.
void ADC_SetSampleLength(uint8_t sample_len)
Sets the ADC sample length.
void ADC_Disable(void)
Disables the ADC module.
bool ADC_EnableInterrupt(ADC_INTERRUPT_SOURCE interrupt)
Enables the specified ADC interrupt(s).
ADC_INTERRUPT_SOURCE
ADC interrupt sources.
Definition plib_adc.h:150
bool ADC_ConversionIsComplete(void)
Checks if the ADC conversion is complete.
@ ADC_MODE_NONE
Definition plib_adc.h:81
@ ADC_START_IMMEDIATE
Definition plib_adc.h:68
@ ADC_INT_RESRDY
Definition plib_adc.h:151

Typedef Documentation

◆ ADC_Callback_t

ADC_Callback_t

ADC callback.

This type defines a callback function that is called upon ADC events.

Parameters
intr_srcThe source of the ADC interrupt that triggered the callback.

Enumeration Type Documentation

◆ ADC_COMPUTATION_MODE

ADC computation mode selection.

Enumerates the available modes for ADC computation.

Enumerator
ADC_MODE_NONE 

None

ADC_MODE_SINGLE 

Single Conversion

ADC_MODE_SERIES 

Series with accumulation, a separate trigger for conversion

ADC_MODE_BURST 

Burst with accumulation. One trigger will run SAMPNUM conversions in one sequence

ADC_MODE_ACCTEST 

Accumulator diagnostics mode

◆ ADC_EVENT_OUTPUT

ADC event output.

Enumerates the available ADC event outputs.

Enumerator
ADC_EVCTRL_STARTEI 

Start event input

ADC_EVCTRL_STARTINV 

Start event input inverted

ADC_EVCTRL_RESRDYEO 

Result ready event output

ADC_EVCTRL_SAMPRDYEO 

Sample ready event output

ADC_EVCTRL_WCMPEO 

Window comparator event output

◆ ADC_INTERRUPT_SOURCE

ADC interrupt sources.

Enumerates the available interrupt sources for the ADC.

Enumerator
ADC_INT_RESRDY 

Result ready interrupt

ADC_INT_SAMPRDY 

Sample ready interrupt

ADC_INT_WCMP 

Window comparator interrupt

ADC_INT_RESOVR 

Result overrun interrupt

ADC_INT_SAMPOVR 

Sample overrun interrupt

ADC_INT_TRIGOVR 

Trigger overrun interrupt

◆ ADC_RES_SCALING

ADC result scaling options.

This enumeration defines the available scaling modes for the ADC result.

Enumerator
ADC_RES_SCALING_NORMAL 
ADC_RES_SCALING_LEFTADJ 
ADC_RES_SCALING_AVERAGE 

◆ ADC_START_MODE

ADC start-conversion mode selection.

Enumerates the available modes for starting an ADC conversion.

Enumerator
ADC_START_STOP 

Stop ADC conversion

ADC_START_IMMEDIATE 

Start ADC conversion immediately

ADC_START_INPUT 

Start ADC conversion on input trigger

ADC_START_EVENT 

Start ADC conversion on event trigger

◆ ADC_WINMODE

ADC window comparator mode.

Enumerates the available window comparator modes for the ADC.

Enumerator
ADC_WINMODE_NONE 

No window comparison

ADC_WINMODE_BELOW 

Window comparison: below threshold

ADC_WINMODE_ABOVE 

Window comparison: above threshold

ADC_WINMODE_INSIDE 

Window comparison: inside window

ADC_WINMODE_OUTSIDE 

Window comparison: outside window

◆ ADC_WINSRC

enum ADC_WINSRC

ADC window comparator source.

Enumerates the possible sources for the ADC window comparator.

Enumerator
ADC_WINSRC_RESULT 

Window comparator source: result register

ADC_WINSRC_SAMPLE 

Window comparator source: sample register

Function Documentation

◆ ADC_AbortConversion()

void ADC_AbortConversion ( void )

Aborts the ongoing ADC conversion.

This function stops any ongoing ADC conversion.

Parameters
None.
Returns
None.

◆ ADC_ClearInterruptFlag()

void ADC_ClearInterruptFlag ( ADC_INTERRUPT_SOURCE intr_flag)

Clears the specified ADC interrupt flag(s).

This function clears the specified ADC interrupt flags.

Parameters
intr_flagInterrupt flag mask to clear.
Returns
None.

◆ ADC_ConfigureWindowMode()

void ADC_ConfigureWindowMode ( ADC_WINSRC win_src,
ADC_WINMODE win_mode,
uint32_t win_lt,
uint32_t win_ht )

Configures the ADC window comparator source, mode, and threshold values.

Sets up the ADC window comparator with the specified source, mode, and threshold values.

Parameters
win_srcWindow comparator source selection (RESULT or SAMPLE).
win_modeWindow comparison mode (NONE, BELOW, ABOVE, INSIDE, OUTSIDE).
win_ltLow threshold value for window comparison.
win_htHigh threshold value for window comparison.
Returns
None.

◆ ADC_ConversionIsComplete()

bool ADC_ConversionIsComplete ( void )

Checks if the ADC conversion is complete.

This function checks whether the ADC has finished its current conversion.

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

◆ ADC_Disable()

void ADC_Disable ( void )

Disables the ADC module.

This function disables the ADC peripheral, stopping all conversions and reducing power consumption.

Parameters
None.
Returns
None.

◆ ADC_DisableInterrupt()

void ADC_DisableInterrupt ( ADC_INTERRUPT_SOURCE interrupt)

Disables the specified ADC interrupt(s).

This function disables the given ADC interrupt source(s).

Parameters
interruptInterrupt mask to disable.
Returns
None.

◆ ADC_DisableWriteProtection()

void ADC_DisableWriteProtection ( void )

Disables write protection for ADC registers.

Parameters
None.
Returns
None.

◆ ADC_Enable()

void ADC_Enable ( void )

Enables the ADC module.

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

Parameters
None.
Returns
None.

◆ ADC_EnableInterrupt()

bool ADC_EnableInterrupt ( ADC_INTERRUPT_SOURCE interrupt)

Enables the specified ADC interrupt(s).

This function enables the given ADC interrupt source(s).

Parameters
interruptInterrupt mask to enable.
Returns
true if the interrupt was enabled, false otherwise.

◆ ADC_EnableWriteProtection()

void ADC_EnableWriteProtection ( void )

Enables write protection for ADC registers.

Parameters
None.
Returns
None.

◆ ADC_Get16bitConversionResult()

uint16_t ADC_Get16bitConversionResult ( void )

Retrieves the 16-bit ADC conversion result.

This function returns the latest 16-bit result from the ADC conversion.

Returns
16-bit conversion result.

◆ ADC_Get32bitConversionResult()

uint32_t ADC_Get32bitConversionResult ( void )

Retrieves the 32-bit ADC conversion result.

This function returns the latest 32-bit result from the ADC conversion.

Parameters
None.
Returns
32-bit conversion result.

◆ ADC_GetInterruptFlagStatus()

bool ADC_GetInterruptFlagStatus ( ADC_INTERRUPT_SOURCE interrupt)

Gets the current ADC interrupt flag status.

Checks if the specified ADC interrupt flag is set.

Parameters
interruptInterrupt source to check.
Returns
true if the specified interrupt flag is set, false otherwise.

◆ ADC_ResetSoftware()

void ADC_ResetSoftware ( void )

Performs a software reset and resets the ADC module.

This function resets the ADC peripheral to its default state.

Parameters
None.
Returns
None.

◆ ADC_SelectDifferentialChannel()

void ADC_SelectDifferentialChannel ( uint32_t positive_input,
uint32_t negative_input )

Selects the ADC differential input channels.

Configures the ADC to use the specified positive and negative input channels for differential conversion.

Parameters
positive_inputThe channel to use as the positive input.
negative_inputThe channel to use as the negative input.
Returns
None.

◆ ADC_SetCallbackHandler()

void ADC_SetCallbackHandler ( ADC_Callback_t callback)

Sets a callback function for ADC interrupts.

Sets a user-defined callback function to be invoked when an ADC interrupt occurs.

Parameters
callbackThe callback function to be set.
Returns
None.

◆ ADC_SetComputationMode()

void ADC_SetComputationMode ( ADC_COMPUTATION_MODE computation_mode)

Sets the ADC computation mode.

This function selects the computation mode for the ADC, such as single, series, or burst conversion.

Parameters
computation_modeComputation mode selection value.
Returns
None.

◆ ADC_SetDifferentialMode()

void ADC_SetDifferentialMode ( bool diff_mode)

Enables or disables ADC differential mode.

Configures the ADC to operate in differential or single-ended mode.

Parameters
diff_modetrue to enable differential mode, false for single-ended.
Returns
None.

◆ ADC_SetEventControl()

void ADC_SetEventControl ( ADC_EVENT_OUTPUT event,
bool enable )

Configures the ADC event control settings.

Enables or disables specific ADC event outputs.

Parameters
eventADC event output to configure.
enableSet to true to enable the event, false to disable.
Returns
None.

◆ ADC_SetFreeRunMode()

void ADC_SetFreeRunMode ( bool free_run)

Enables or disables the ADC free run mode.

Configures the ADC to operate in free run mode, where it continuously performs conversions without requiring a new start command.

Parameters
free_runSet to true to enable free run mode, or false to disable it.
Returns
None.

◆ ADC_SetLowPassFilter()

void ADC_SetLowPassFilter ( bool low_pass_filter)

Enables or disables the ADC low-pass filter.

Configures the ADC to use its built-in low-pass filter to reduce high-frequency noise.

Parameters
low_pass_filterIf true, enables the low-pass filter; if false, disables it.
Returns
None.

◆ ADC_SetPrescaler()

void ADC_SetPrescaler ( uint8_t pre_scaler)

Sets the ADC clock prescaler value.

This function configures the ADC clock prescaler.

Parameters
pre_scalerPrescaler value to set for the ADC clock.
Returns
None.

◆ ADC_SetResolution()

void ADC_SetResolution ( uint32_t resolution)

Sets the ADC resolution.

This function configures the resolution of the ADC conversion result.

Parameters
resolutionResolution setting (e.g., 8, 10, 12 bits).
Returns
None.

◆ ADC_SetResultScaling()

void ADC_SetResultScaling ( ADC_RES_SCALING scale)

Sets the ADC result scaling mode.

Configures the ADC to use the specified result scaling mode (e.g., normal, left-adjusted, or averaged).

Parameters
scaleThe result scaling mode to set. See adc_res_scaling_t_enum for available options.
Returns
None.

◆ ADC_SetSampleAccummulationCount()

void ADC_SetSampleAccummulationCount ( uint32_t samp_acc_count)

Sets the number of samples to accumulate for ADC conversion.

This function configures the ADC to accumulate a specified number of samples for each conversion.

Parameters
samp_acc_countNumber of samples to accumulate.
Returns
None.

◆ ADC_SetSampleLength()

void ADC_SetSampleLength ( uint8_t sample_len)

Sets the ADC sample length.

This function configures the number of ADC clock cycles used for sampling.

Parameters
sample_lenNumber of ADC clock cycles for sampling.
Returns
None.

◆ ADC_SetTimebase()

void ADC_SetTimebase ( uint8_t timebase)

Sets the ADC Timebase field.

This function configures the TIMEBASE value in the ADC control register.

Parameters
timebaseTIMEBASE value to set.
Returns
None.

◆ ADC_SetVoltageReference()

void ADC_SetVoltageReference ( uint8_t ref_sel)

Sets the ADC voltage reference source.

This function selects the voltage reference source for the ADC.

Parameters
ref_selVoltage reference selection value.
Returns
None.

◆ ADC_StartConversion()

void ADC_StartConversion ( ADC_START_MODE start_mode)

Starts an ADC conversion with the specified start mode.

Initiates an ADC conversion using the provided start mode.

Parameters
start_modeStart mode selection value.
Returns
None.

◆ ADC_WriteProtectionIsEnabled()

bool ADC_WriteProtectionIsEnabled ( void )

Queries if write protection is enabled.

Parameters
None.
Returns
true if enabled, false otherwise.

◆ ADC_WriteProtectionIsLocked()

bool ADC_WriteProtectionIsLocked ( void )

Queries if write protection is locked.

Parameters
None.
Returns
true if locked, false otherwise.

◆ ADC_WriteProtectionLock()

void ADC_WriteProtectionLock ( void )

Locks write protection configuration until reset.

Parameters
None.
Returns
None.