MCUXpresso SDK API Reference Manual  Rev. 0
NXP Semiconductors
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
ADC16: 16-bit SAR Analog-to-Digital Converter Driver

Overview

The MCUXpresso SDK provides a peripheral driver for the 16-bit SAR Analog-to-Digital Converter (ADC16) module of MCUXpresso SDK devices.

Typical use case

Polling Configuration

adc16_config_t adc16ConfigStruct;
adc16_channel_config_t adc16ChannelConfigStruct;
ADC16_Init(DEMO_ADC16_INSTANCE);
ADC16_GetDefaultConfig(&adc16ConfigStruct);
ADC16_Configure(DEMO_ADC16_INSTANCE, &adc16ConfigStruct);
ADC16_EnableHardwareTrigger(DEMO_ADC16_INSTANCE, false);
#if defined(FSL_FEATURE_ADC16_HAS_CALIBRATION) && FSL_FEATURE_ADC16_HAS_CALIBRATION
if (kStatus_Success == ADC16_DoAutoCalibration(DEMO_ADC16_INSTANCE))
{
PRINTF("ADC16_DoAutoCalibration() Done.\r\n");
}
else
{
PRINTF("ADC16_DoAutoCalibration() Failed.\r\n");
}
#endif // FSL_FEATURE_ADC16_HAS_CALIBRATION
adc16ChannelConfigStruct.channelNumber = DEMO_ADC16_USER_CHANNEL;
adc16ChannelConfigStruct.enableInterruptOnConversionCompleted = false;
#if defined(FSL_FEATURE_ADC16_HAS_DIFF_MODE) && FSL_FEATURE_ADC16_HAS_DIFF_MODE
adc16ChannelConfigStruct.enableDifferentialConversion = false;
#endif // FSL_FEATURE_ADC16_HAS_DIFF_MODE
while(1)
{
GETCHAR(); // Input any key in terminal console.
ADC16_ChannelConfigure(DEMO_ADC16_INSTANCE, DEMO_ADC16_CHANNEL_GROUP, &adc16ChannelConfigStruct);
while (kADC16_ChannelConversionDoneFlag != ADC16_ChannelGetStatusFlags(DEMO_ADC16_INSTANCE, DEMO_ADC16_CHANNEL_GROUP))
{
}
PRINTF("ADC Value: %d\r\n", ADC16_ChannelGetConversionValue(DEMO_ADC16_INSTANCE, DEMO_ADC16_CHANNEL_GROUP));
}

Interrupt Configuration

volatile bool g_Adc16ConversionDoneFlag = false;
volatile uint32_t g_Adc16ConversionValue;
volatile uint32_t g_Adc16InterruptCount = 0U;
// ...
adc16_config_t adc16ConfigStruct;
adc16_channel_config_t adc16ChannelConfigStruct;
ADC16_Init(DEMO_ADC16_INSTANCE);
ADC16_GetDefaultConfig(&adc16ConfigStruct);
ADC16_Configure(DEMO_ADC16_INSTANCE, &adc16ConfigStruct);
ADC16_EnableHardwareTrigger(DEMO_ADC16_INSTANCE, false);
#if defined(FSL_FEATURE_ADC16_HAS_CALIBRATION) && FSL_FEATURE_ADC16_HAS_CALIBRATION
if (ADC16_DoAutoCalibration(DEMO_ADC16_INSTANCE))
{
PRINTF("ADC16_DoAutoCalibration() Done.\r\n");
}
else
{
PRINTF("ADC16_DoAutoCalibration() Failed.\r\n");
}
#endif // FSL_FEATURE_ADC16_HAS_CALIBRATION
adc16ChannelConfigStruct.channelNumber = DEMO_ADC16_USER_CHANNEL;
adc16ChannelConfigStruct.enableInterruptOnConversionCompleted = true; // Enable the interrupt.
#if defined(FSL_FEATURE_ADC16_HAS_DIFF_MODE) && FSL_FEATURE_ADC16_HAS_DIFF_MODE
adc16ChannelConfigStruct.enableDifferentialConversion = false;
#endif // FSL_FEATURE_ADC16_HAS_DIFF_MODE
while(1)
{
GETCHAR(); // Input a key in the terminal console.
g_Adc16ConversionDoneFlag = false;
ADC16_ChannelConfigure(DEMO_ADC16_INSTANCE, DEMO_ADC16_CHANNEL_GROUP, &adc16ChannelConfigStruct);
while (!g_Adc16ConversionDoneFlag)
{
}
PRINTF("ADC Value: %d\r\n", g_Adc16ConversionValue);
PRINTF("ADC Interrupt Count: %d\r\n", g_Adc16InterruptCount);
}
// ...
void DEMO_ADC16_IRQHandler(void)
{
g_Adc16ConversionDoneFlag = true;
// Read the conversion result to clear the conversion completed flag.
g_Adc16ConversionValue = ADC16_ChannelConversionValue(DEMO_ADC16_INSTANCE, DEMO_ADC16_CHANNEL_GROUP);
g_Adc16InterruptCount++;
}

Data Structures

struct  adc16_config_t
 ADC16 converter configuration. More...
 
struct  adc16_hardware_compare_config_t
 ADC16 Hardware comparison configuration. More...
 
struct  adc16_channel_config_t
 ADC16 channel conversion configuration. More...
 

Enumerations

enum  _adc16_channel_status_flags { kADC16_ChannelConversionDoneFlag = ADC_SC1_COCO_MASK }
 Channel status flags. More...
 
enum  _adc16_status_flags {
  kADC16_ActiveFlag = ADC_SC2_ADACT_MASK,
  kADC16_CalibrationFailedFlag = ADC_SC3_CALF_MASK
}
 Converter status flags. More...
 
enum  adc16_channel_mux_mode_t {
  kADC16_ChannelMuxA = 0U,
  kADC16_ChannelMuxB = 1U
}
 Channel multiplexer mode for each channel. More...
 
enum  adc16_clock_divider_t {
  kADC16_ClockDivider1 = 0U,
  kADC16_ClockDivider2 = 1U,
  kADC16_ClockDivider4 = 2U,
  kADC16_ClockDivider8 = 3U
}
 Clock divider for the converter. More...
 
enum  adc16_resolution_t {
  kADC16_Resolution8or9Bit = 0U,
  kADC16_Resolution12or13Bit = 1U,
  kADC16_Resolution10or11Bit = 2U,
  kADC16_ResolutionSE8Bit = kADC16_Resolution8or9Bit,
  kADC16_ResolutionSE12Bit = kADC16_Resolution12or13Bit,
  kADC16_ResolutionSE10Bit = kADC16_Resolution10or11Bit
}
 Converter's resolution. More...
 
enum  adc16_clock_source_t {
  kADC16_ClockSourceAlt0 = 0U,
  kADC16_ClockSourceAlt1 = 1U,
  kADC16_ClockSourceAlt2 = 2U,
  kADC16_ClockSourceAlt3 = 3U,
  kADC16_ClockSourceAsynchronousClock = kADC16_ClockSourceAlt3
}
 Clock source. More...
 
enum  adc16_long_sample_mode_t {
  kADC16_LongSampleCycle24 = 0U,
  kADC16_LongSampleCycle16 = 1U,
  kADC16_LongSampleCycle10 = 2U,
  kADC16_LongSampleCycle6 = 3U,
  kADC16_LongSampleDisabled = 4U
}
 Long sample mode. More...
 
enum  adc16_reference_voltage_source_t {
  kADC16_ReferenceVoltageSourceVref = 0U,
  kADC16_ReferenceVoltageSourceValt = 1U
}
 Reference voltage source. More...
 
enum  adc16_hardware_average_mode_t {
  kADC16_HardwareAverageCount4 = 0U,
  kADC16_HardwareAverageCount8 = 1U,
  kADC16_HardwareAverageCount16 = 2U,
  kADC16_HardwareAverageCount32 = 3U,
  kADC16_HardwareAverageDisabled = 4U
}
 Hardware average mode. More...
 
enum  adc16_hardware_compare_mode_t {
  kADC16_HardwareCompareMode0 = 0U,
  kADC16_HardwareCompareMode1 = 1U,
  kADC16_HardwareCompareMode2 = 2U,
  kADC16_HardwareCompareMode3 = 3U
}
 Hardware compare mode. More...
 

Driver version

#define FSL_ADC16_DRIVER_VERSION   (MAKE_VERSION(2, 0, 0))
 ADC16 driver version 2.0.0. More...
 

Initialization

void ADC16_Init (ADC_Type *base, const adc16_config_t *config)
 Initializes the ADC16 module. More...
 
void ADC16_Deinit (ADC_Type *base)
 De-initializes the ADC16 module. More...
 
void ADC16_GetDefaultConfig (adc16_config_t *config)
 Gets an available pre-defined settings for the converter's configuration. More...
 
status_t ADC16_DoAutoCalibration (ADC_Type *base)
 Automates the hardware calibration. More...
 
static void ADC16_SetOffsetValue (ADC_Type *base, int16_t value)
 Sets the offset value for the conversion result. More...
 

Advanced Features

static void ADC16_EnableHardwareTrigger (ADC_Type *base, bool enable)
 Enables the hardware trigger mode. More...
 
void ADC16_SetChannelMuxMode (ADC_Type *base, adc16_channel_mux_mode_t mode)
 Sets the channel mux mode. More...
 
void ADC16_SetHardwareCompareConfig (ADC_Type *base, const adc16_hardware_compare_config_t *config)
 Configures the hardware compare mode. More...
 
void ADC16_SetHardwareAverage (ADC_Type *base, adc16_hardware_average_mode_t mode)
 Sets the hardware average mode. More...
 
uint32_t ADC16_GetStatusFlags (ADC_Type *base)
 Gets the status flags of the converter. More...
 
void ADC16_ClearStatusFlags (ADC_Type *base, uint32_t mask)
 Clears the status flags of the converter. More...
 

Conversion Channel

void ADC16_SetChannelConfig (ADC_Type *base, uint32_t channelGroup, const adc16_channel_config_t *config)
 Configures the conversion channel. More...
 
static uint32_t ADC16_GetChannelConversionValue (ADC_Type *base, uint32_t channelGroup)
 Gets the conversion value. More...
 
uint32_t ADC16_GetChannelStatusFlags (ADC_Type *base, uint32_t channelGroup)
 Gets the status flags of channel. More...
 

Data Structure Documentation

struct adc16_config_t

Data Fields

adc16_reference_voltage_source_t referenceVoltageSource
 Select the reference voltage source. More...
 
adc16_clock_source_t clockSource
 Select the input clock source to converter. More...
 
bool enableAsynchronousClock
 Enable the asynchronous clock output. More...
 
adc16_clock_divider_t clockDivider
 Select the divider of input clock source. More...
 
adc16_resolution_t resolution
 Select the sample resolution mode. More...
 
adc16_long_sample_mode_t longSampleMode
 Select the long sample mode. More...
 
bool enableHighSpeed
 Enable the high-speed mode. More...
 
bool enableLowPower
 Enable low power. More...
 
bool enableContinuousConversion
 Enable continuous conversion mode. More...
 

Field Documentation

adc16_reference_voltage_source_t adc16_config_t::referenceVoltageSource
adc16_clock_source_t adc16_config_t::clockSource
bool adc16_config_t::enableAsynchronousClock
adc16_clock_divider_t adc16_config_t::clockDivider
adc16_resolution_t adc16_config_t::resolution
adc16_long_sample_mode_t adc16_config_t::longSampleMode
bool adc16_config_t::enableHighSpeed
bool adc16_config_t::enableLowPower
bool adc16_config_t::enableContinuousConversion
struct adc16_hardware_compare_config_t

Data Fields

adc16_hardware_compare_mode_t hardwareCompareMode
 Select the hardware compare mode. More...
 
int16_t value1
 Setting value1 for hardware compare mode. More...
 
int16_t value2
 Setting value2 for hardware compare mode. More...
 

Field Documentation

adc16_hardware_compare_mode_t adc16_hardware_compare_config_t::hardwareCompareMode

See "adc16_hardware_compare_mode_t".

int16_t adc16_hardware_compare_config_t::value1
int16_t adc16_hardware_compare_config_t::value2
struct adc16_channel_config_t

Data Fields

uint32_t channelNumber
 Setting the conversion channel number. More...
 
bool enableInterruptOnConversionCompleted
 Generate an interrupt request once the conversion is completed. More...
 

Field Documentation

uint32_t adc16_channel_config_t::channelNumber

The available range is 0-31. See channel connection information for each chip in Reference Manual document.

bool adc16_channel_config_t::enableInterruptOnConversionCompleted

Macro Definition Documentation

#define FSL_ADC16_DRIVER_VERSION   (MAKE_VERSION(2, 0, 0))

Enumeration Type Documentation

Enumerator
kADC16_ChannelConversionDoneFlag 

Conversion done.

Enumerator
kADC16_ActiveFlag 

Converter is active.

kADC16_CalibrationFailedFlag 

Calibration is failed.

For some ADC16 channels, there are two pin selections in channel multiplexer. For example, ADC0_SE4a and ADC0_SE4b are the different channels that share the same channel number.

Enumerator
kADC16_ChannelMuxA 

For channel with channel mux a.

kADC16_ChannelMuxB 

For channel with channel mux b.

Enumerator
kADC16_ClockDivider1 

For divider 1 from the input clock to the module.

kADC16_ClockDivider2 

For divider 2 from the input clock to the module.

kADC16_ClockDivider4 

For divider 4 from the input clock to the module.

kADC16_ClockDivider8 

For divider 8 from the input clock to the module.

Enumerator
kADC16_Resolution8or9Bit 

Single End 8-bit or Differential Sample 9-bit.

kADC16_Resolution12or13Bit 

Single End 12-bit or Differential Sample 13-bit.

kADC16_Resolution10or11Bit 

Single End 10-bit or Differential Sample 11-bit.

kADC16_ResolutionSE8Bit 

Single End 8-bit.

kADC16_ResolutionSE12Bit 

Single End 12-bit.

kADC16_ResolutionSE10Bit 

Single End 10-bit.

Enumerator
kADC16_ClockSourceAlt0 

Selection 0 of the clock source.

kADC16_ClockSourceAlt1 

Selection 1 of the clock source.

kADC16_ClockSourceAlt2 

Selection 2 of the clock source.

kADC16_ClockSourceAlt3 

Selection 3 of the clock source.

kADC16_ClockSourceAsynchronousClock 

Using internal asynchronous clock.

Enumerator
kADC16_LongSampleCycle24 

20 extra ADCK cycles, 24 ADCK cycles total.

kADC16_LongSampleCycle16 

12 extra ADCK cycles, 16 ADCK cycles total.

kADC16_LongSampleCycle10 

6 extra ADCK cycles, 10 ADCK cycles total.

kADC16_LongSampleCycle6 

2 extra ADCK cycles, 6 ADCK cycles total.

kADC16_LongSampleDisabled 

Disable the long sample feature.

Enumerator
kADC16_ReferenceVoltageSourceVref 

For external pins pair of VrefH and VrefL.

kADC16_ReferenceVoltageSourceValt 

For alternate reference pair of ValtH and ValtL.

Enumerator
kADC16_HardwareAverageCount4 

For hardware average with 4 samples.

kADC16_HardwareAverageCount8 

For hardware average with 8 samples.

kADC16_HardwareAverageCount16 

For hardware average with 16 samples.

kADC16_HardwareAverageCount32 

For hardware average with 32 samples.

kADC16_HardwareAverageDisabled 

Disable the hardware average feature.

Enumerator
kADC16_HardwareCompareMode0 

x < value1.

kADC16_HardwareCompareMode1 

x > value1.

kADC16_HardwareCompareMode2 

if value1 <= value2, then x < value1 || x > value2; else, value1 > x > value2.

kADC16_HardwareCompareMode3 

if value1 <= value2, then value1 <= x <= value2; else x >= value1 || x <= value2.

Function Documentation

void ADC16_Init ( ADC_Type *  base,
const adc16_config_t config 
)
Parameters
baseADC16 peripheral base address.
configPointer to configuration structure. See "adc16_config_t".
void ADC16_Deinit ( ADC_Type *  base)
Parameters
baseADC16 peripheral base address.
void ADC16_GetDefaultConfig ( adc16_config_t config)

This function initializes the converter configuration structure with available settings. The default values are as follows.

* config->referenceVoltageSource = kADC16_ReferenceVoltageSourceVref;
* config->clockSource = kADC16_ClockSourceAsynchronousClock;
* config->enableAsynchronousClock = true;
* config->clockDivider = kADC16_ClockDivider8;
* config->resolution = kADC16_ResolutionSE12Bit;
* config->longSampleMode = kADC16_LongSampleDisabled;
* config->enableHighSpeed = false;
* config->enableLowPower = false;
* config->enableContinuousConversion = false;
*
Parameters
configPointer to the configuration structure.
status_t ADC16_DoAutoCalibration ( ADC_Type *  base)

This auto calibration helps to adjust the plus/minus side gain automatically. Execute the calibration before using the converter. Note that the hardware trigger should be used during the calibration.

Parameters
baseADC16 peripheral base address.
Returns
Execution status.
Return values
kStatus_SuccessCalibration is done successfully.
kStatus_FailCalibration has failed.
static void ADC16_SetOffsetValue ( ADC_Type *  base,
int16_t  value 
)
inlinestatic

This offset value takes effect on the conversion result. If the offset value is not zero, the reading result is subtracted by it. Note, the hardware calibration fills the offset value automatically.

Parameters
baseADC16 peripheral base address.
valueSetting offset value.
static void ADC16_EnableHardwareTrigger ( ADC_Type *  base,
bool  enable 
)
inlinestatic
Parameters
baseADC16 peripheral base address.
enableSwitcher of the hardware trigger feature. "true" means enabled, "false" means not enabled.
void ADC16_SetChannelMuxMode ( ADC_Type *  base,
adc16_channel_mux_mode_t  mode 
)

Some sample pins share the same channel index. The channel mux mode decides which pin is used for an indicated channel.

Parameters
baseADC16 peripheral base address.
modeSetting channel mux mode. See "adc16_channel_mux_mode_t".
void ADC16_SetHardwareCompareConfig ( ADC_Type *  base,
const adc16_hardware_compare_config_t config 
)

The hardware compare mode provides a way to process the conversion result automatically by using hardware. Only the result in the compare range is available. To compare the range, see "adc16_hardware_compare_mode_t" or the appopriate reference manual for more information.

Parameters
baseADC16 peripheral base address.
configPointer to the "adc16_hardware_compare_config_t" structure. Passing "NULL" disables the feature.
void ADC16_SetHardwareAverage ( ADC_Type *  base,
adc16_hardware_average_mode_t  mode 
)

The hardware average mode provides a way to process the conversion result automatically by using hardware. The multiple conversion results are accumulated and averaged internally making them easier to read.

Parameters
baseADC16 peripheral base address.
modeSetting the hardware average mode. See "adc16_hardware_average_mode_t".
uint32_t ADC16_GetStatusFlags ( ADC_Type *  base)
Parameters
baseADC16 peripheral base address.
Returns
Flags' mask if indicated flags are asserted. See "_adc16_status_flags".
void ADC16_ClearStatusFlags ( ADC_Type *  base,
uint32_t  mask 
)
Parameters
baseADC16 peripheral base address.
maskMask value for the cleared flags. See "_adc16_status_flags".
void ADC16_SetChannelConfig ( ADC_Type *  base,
uint32_t  channelGroup,
const adc16_channel_config_t config 
)

This operation triggers the conversion when in software trigger mode. When in hardware trigger mode, this API configures the channel while the external trigger source helps to trigger the conversion.

Note that the "Channel Group" has a detailed description. To allow sequential conversions of the ADC to be triggered by internal peripherals, the ADC has more than one group of status and control registers, one for each conversion. The channel group parameter indicates which group of registers are used, for example, channel group 0 is for Group A registers and channel group 1 is for Group B registers. The channel groups are used in a "ping-pong" approach to control the ADC operation. At any point, only one of the channel groups is actively controlling ADC conversions. The channel group 0 is used for both software and hardware trigger modes. Channel group 1 and greater indicates multiple channel group registers for use only in hardware trigger mode. See the chip configuration information in the appropriate MCU reference manual for the number of SC1n registers (channel groups) specific to this device. Channel group 1 or greater are not used for software trigger operation. Therefore, writing to these channel groups does not initiate a new conversion. Updating the channel group 0 while a different channel group is actively controlling a conversion is allowed and vice versa. Writing any of the channel group registers while that specific channel group is actively controlling a conversion aborts the current conversion.

Parameters
baseADC16 peripheral base address.
channelGroupChannel group index.
configPointer to the "adc16_channel_config_t" structure for the conversion channel.
static uint32_t ADC16_GetChannelConversionValue ( ADC_Type *  base,
uint32_t  channelGroup 
)
inlinestatic
Parameters
baseADC16 peripheral base address.
channelGroupChannel group index.
Returns
Conversion value.
uint32_t ADC16_GetChannelStatusFlags ( ADC_Type *  base,
uint32_t  channelGroup 
)
Parameters
baseADC16 peripheral base address.
channelGroupChannel group index.
Returns
Flags' mask if indicated flags are asserted. See "_adc16_channel_status_flags".