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_Configure(DEMO_ADC16_INSTANCE, &adc16ConfigStruct);
#if defined(FSL_FEATURE_ADC16_HAS_CALIBRATION) && FSL_FEATURE_ADC16_HAS_CALIBRATION
{
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;
#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();
ADC16_ChannelConfigure(DEMO_ADC16_INSTANCE, DEMO_ADC16_CHANNEL_GROUP, &adc16ChannelConfigStruct);
{
}
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_Configure(DEMO_ADC16_INSTANCE, &adc16ConfigStruct);
#if defined(FSL_FEATURE_ADC16_HAS_CALIBRATION) && FSL_FEATURE_ADC16_HAS_CALIBRATION
{
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;
#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();
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;
g_Adc16ConversionValue = ADC16_ChannelConversionValue(DEMO_ADC16_INSTANCE, DEMO_ADC16_CHANNEL_GROUP);
g_Adc16InterruptCount++;
}
|
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...
|
|
bool adc16_config_t::enableAsynchronousClock |
bool adc16_config_t::enableHighSpeed |
bool adc16_config_t::enableLowPower |
bool adc16_config_t::enableContinuousConversion |
struct adc16_hardware_compare_config_t |
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 |
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 |
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.
|
- Parameters
-
base | ADC16 peripheral base address. |
config | Pointer to configuration structure. See "adc16_config_t". |
void ADC16_Deinit |
( |
ADC_Type * |
base | ) |
|
- Parameters
-
base | ADC16 peripheral base address. |
This function initializes the converter configuration structure with available settings. The default values are as follows.
* config->enableAsynchronousClock = true;
* config->enableHighSpeed = false;
* config->enableLowPower = false;
* config->enableContinuousConversion = false;
*
- Parameters
-
config | Pointer 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
-
base | ADC16 peripheral base address. |
- Returns
- Execution status.
- Return values
-
kStatus_Success | Calibration is done successfully. |
kStatus_Fail | Calibration 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
-
base | ADC16 peripheral base address. |
value | Setting offset value. |
static void ADC16_EnableHardwareTrigger |
( |
ADC_Type * |
base, |
|
|
bool |
enable |
|
) |
| |
|
inlinestatic |
- Parameters
-
base | ADC16 peripheral base address. |
enable | Switcher of the hardware trigger feature. "true" means enabled, "false" means not enabled. |
Some sample pins share the same channel index. The channel mux mode decides which pin is used for an indicated channel.
- Parameters
-
base | ADC16 peripheral base address. |
mode | Setting channel mux mode. See "adc16_channel_mux_mode_t". |
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
-
base | ADC16 peripheral base address. |
config | Pointer to the "adc16_hardware_compare_config_t" structure. Passing "NULL" disables the feature. |
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
-
base | ADC16 peripheral base address. |
mode | Setting the hardware average mode. See "adc16_hardware_average_mode_t". |
uint32_t ADC16_GetStatusFlags |
( |
ADC_Type * |
base | ) |
|
- Parameters
-
base | ADC16 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
-
base | ADC16 peripheral base address. |
mask | Mask value for the cleared flags. See "_adc16_status_flags". |
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
-
base | ADC16 peripheral base address. |
channelGroup | Channel group index. |
config | Pointer to the "adc16_channel_config_t" structure for the conversion channel. |
static uint32_t ADC16_GetChannelConversionValue |
( |
ADC_Type * |
base, |
|
|
uint32_t |
channelGroup |
|
) |
| |
|
inlinestatic |
- Parameters
-
base | ADC16 peripheral base address. |
channelGroup | Channel group index. |
- Returns
- Conversion value.
uint32_t ADC16_GetChannelStatusFlags |
( |
ADC_Type * |
base, |
|
|
uint32_t |
channelGroup |
|
) |
| |
- Parameters
-
base | ADC16 peripheral base address. |
channelGroup | Channel group index. |
- Returns
- Flags' mask if indicated flags are asserted. See "_adc16_channel_status_flags".