Kinetis SDK v.2.0 API Reference Manual  Rev. 0
Freescale Semiconductor, Inc.
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
ADC16: 16-bit SAR Analog-to-Digital Converter Driver

Overview

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_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 any key in 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 conversion result to clear the conversion completed flag.
g_Adc16ConversionValue = ADC16_ChannelConversionValue(DEMO_ADC16_INSTANCE, DEMO_ADC16_CHANNEL_GROUP);
g_Adc16InterruptCount++;
}

Files

file  fsl_adc16.h
 

Data Structures

struct  adc16_config_t
 ADC16 converter configuration . More...
 
struct  adc16_hardware_compare_config_t
 ADC16 Hardware compare 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 }
 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...
 

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 converter's configuration. More...
 

Advanced Feature

static void ADC16_EnableHardwareTrigger (ADC_Type *base, bool enable)
 Enables the hardware trigger mode. More...
 
void ADC16_SetHardwareCompareConfig (ADC_Type *base, const adc16_hardware_compare_config_t *config)
 Configures the hardware compare 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 a 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.

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.

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 an available settings. The default values are:

config->referenceVoltageSource = kADC16_ReferenceVoltageSourceVref;
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 configuration structure.
static void ADC16_EnableHardwareTrigger ( ADC_Type *  base,
bool  enable 
)
inlinestatic
Parameters
baseADC16 peripheral base address.
enableSwitcher of hardware trigger feature. "true" means to enable, "false" means not.
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 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
baseADC16 peripheral base address.
configPointer to "adc16_hardware_compare_config_t" structure. Passing "NULL" is to disable the feature.
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 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
baseADC16 peripheral base address.
channelGroupChannel group index.
configPointer to "adc16_channel_config_t" structure for 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".