The KSDK provides a Peripheral driver for the 16-bit SAR Analog-to-Digital Converter (ADC16) module of Kinetis devices.
Typical use case
Polling Configuration
ADC16_Configure(DEMO_ADC16_INSTANCE, &adc16ConfigStruct);
#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;
#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
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;
#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
} |
| Converter status flags. 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_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.
|
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_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 an available settings. The default values are:
config->enableAsynchronousClock = true;
config->enableHighSpeed = false;
config->enableLowPower = false;
config->enableContinuousConversion = false;
- Parameters
-
config | Pointer to configuration structure. |
static void ADC16_EnableHardwareTrigger |
( |
ADC_Type * |
base, |
|
|
bool |
enable |
|
) |
| |
|
inlinestatic |
- Parameters
-
base | ADC16 peripheral base address. |
enable | Switcher of hardware trigger feature. "true" means to enable, "false" means not. |
The hardware compare mode provides a way to process the conversion result automatically by hardware. Only the result in compare range is available. To compare the range, see "adc16_hardware_compare_mode_t", or the reference manual document for more detailed information.
- Parameters
-
base | ADC16 peripheral base address. |
config | Pointer to "adc16_hardware_compare_config_t" structure. Passing "NULL" is to disable the feature. |
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 if 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 can have more than one group of status and control register, one for each conversion. The channel group parameter indicates which group of registers are used 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. Channel group 0 is used for both software and hardware trigger modes of operation. Channel groups 1 and greater indicate potentially multiple channel group registers for use only in hardware trigger mode. See the chip configuration information in the MCU reference manual about the number of SC1n registers (channel groups) specific to this device. None of the channel groups 1 or greater are used for software trigger operation and therefore writes to these channel groups do not initiate a new conversion. Updating 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 "adc16_channel_config_t" structure for 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".