2.1 ADC Multicore Driver
Overview
The High Speed 12-bit Analog-to-Digital Converter (ADC) features low conversion latency, high resolution and oversampling capabilities to improve performance in AC/DC, DC/DC power converters.
Features
- Multiple single channel dedicated ADC cores (depending on the specific device implementation) which allows for simultaneous sampling.
- One shared (common) ADC core with sequential sampling.
- Voltage reference of AVdd for positive reference and AVss for negative reference is supported.
- Up to 3.5 Msps Conversion Rate per Channel at 12-Bit Resolution.
- Low-Latency Conversion.
- Up to 24 (Depending on the hardware capability) Analog Input Channels, with a Separate 16-Bit Conversion Result Register for each Input.
- Conversion Result Formatted as UnsignedData on a per Channel basis.
- Multiple Conversion Trigger Options for each Core.
- Up to Four Integrated Digital Comparators with Dedicated Interrupts and multiple comparison options assignable to specific analog inputs.
- The adc channels are configurable from the table in the GUI.
- Users can configure the comparators selected in the channel table via the Comparator Table.
- The Clock and Divider settings for ADC can be configured in the PLIB dependency of the driver module.
2.1.1 How to use the ADC Usecases
The ADC Module driver generates an API interface to support the features of the peripheral. Some use cases of are linked below. Click the links to view the code snippets associated with each.
Instructions common for all code snippets can be found here: 2.1.5.1.1 ADC Use Case Code Snippet Instructions
- 2.1.5.1.2 ADC Use Case 1: ADC in Basic_mode with Printf of ADC Result An ADC conversion is taken every 100 ms, on the selected analog channel. After each ADC conversion a LED is toggled and the ADC Result is sent to the PC terminal using printf.
- 2.1.5.1.3 ADC Use Case 2: ADC Samples Visualized with the Data Streamer An ADC conversion is taken, on the selected analog channel, every 100 ms. ADC converstions are triggerd by an timer overflow callback, where the Data Streamer variables, adcResult, and adcSampleCount are updated.
2.1.2 Module Documentation
2.1.2.1 ADC Multicore Driver
High-Speed, 12-Bit Multiple SARs Analog-to-Digital Converter driver using dsPIC MCUs.
2.1.2.1.1 Module description
High-Speed, 12-Bit Multiple SARs Analog-to-Digital Converter driver using dsPIC MCUs.
Data structures
struct ADC_INTERFACE
Structure containing the function pointers of ADC driver.
struct ADC_MULTICORE
Structure containing the function pointers of ADC driver.
Definitions
#define ADC1_SCAN_MODE_SELECTED true
Defines the scan option selection done for the shared channels.
#define ADC1_RESOLUTION 12
Defines the ADC Resolution.
#define ADC_POWER_ENABLE_FEATURE_AVAILABLE 1
This macro defines the ADC core power enable functionality.
#define ADC_CALIBRATION_FEATURE_AVAILABLE 1
This macro defines the macro associated with ADC core calibration.
#define ADC_INDIVIDUAL_CHANNEL_INTERRUPT_FEATURE_AVAILABLE 1
Defines the macro associated with ADC indvidual channel interrupts.
#define ADC_COMPARATOR_FEATURE_AVAILABLE 1
Defines the macro associated with ADC comparator feature.
Enumerations
enum ADC_CHANNEL { Channel_AN2, MAX_CHANNELS = 1, ADC_MAX_CHANNELS = 1 }
Defines the ADC channles that are selected from the MCC Melody User Interface for the ADC conversions.
enum ADC_DEDICATED_CORE { MAX_DEDICATED_CORES = 0, ADC_MAX_DEDICATED_CORES = 0 }
Defines the ADC cores that are available for the module to use.
enum ADC_RESOLUTION_TYPE { ADC_6_BIT_RESOLUTION = 0x0, ADC_8_BIT_RESOLUTION = 0x1, ADC_10_BIT_RESOLUTION = 0x2, ADC_12_BIT_RESOLUTION = 0x3 }
Defines the supported ADC resolution types.
enum ADC_CMP { MAX_CMPS = 0, ADC_MAX_CMPS = 0 }
Defines the ADC comparators that are available for the module to use.
enum ADC_PWM_INSTANCE { ADC_PWM_GENERATOR_1 = 1, ADC_PWM_GENERATOR_2 = 2, ADC_PWM_GENERATOR_3 = 3, ADC_PWM_GENERATOR_4 = 4, ADC_PWM_GENERATOR_5 = 5, ADC_PWM_GENERATOR_6 = 6, ADC_PWM_GENERATOR_7 = 7, ADC_PWM_GENERATOR_8 = 8 }
Defines the ADC PWM trigger sources that are available for the module to use.
enum ADC_PWM_TRIGGERS { ADC_PWM_TRIGGER_1 = 1, ADC_PWM_TRIGGER_2 = 2 }
Defines the PWM triggers that are available in each individual PWM.
Functions
void ADC1_Initialize (void)
Initializes ADC1 module, using the given initialization data This function must be called before any other ADC1 function is called.
void ADC1_Deinitialize (void)
Deinitializes the ADC1 to POR values.
static void ADC1_Enable (void)
This inline function enables the ADC1 module.
static void ADC1_Disable (void)
This inline function disables the ADC1 module.
static void ADC1_SoftwareTriggerEnable (void)
This inline function sets software common trigger.
static void ADC1_SoftwareTriggerDisable (void)
This inline function resets software common trigger.
static void ADC1_ChannelSelect (enum ADC_CHANNEL channel)
This inline function allows selection of a channel for conversion.
static uint16_t ADC1_ConversionResultGet (enum ADC_CHANNEL channel)
Returns the conversion value for the channel selected.
static bool ADC1_IsConversionComplete (enum ADC_CHANNEL channel)
This inline function returns the status of conversion.
static void ADC1_ResolutionSet (enum ADC_RESOLUTION_TYPE resolution)
This inline function helps to configure all cores with same resolution.
static void ADC1_InterruptEnable (void)
This inline function enables the ADC1 interrupt.
static void ADC1_InterruptDisable (void)
This inline function disables the ADC1 interrupt.
static void ADC1_InterruptFlagClear (void)
Clears interrupt flag manually.
static void ADC1_InterruptPrioritySet (uint16_t priorityValue)
This inline function allows selection of priority for interrupt.
void ADC1_CommonCallbackRegister (void(*callback)(void))
This function can be used to override default callback and to define custom callback for ADC1 Common event.
void ADC1_CommonCallback (void)
This is the default callback with weak attribute.
void ADC1_Tasks (void)
This function is used to implement the tasks for polled implementations.
static void ADC1_IndividualChannelInterruptEnable (enum ADC_CHANNEL channel)
This inline function enables individual channel interrupt.
static void ADC1_IndividualChannelInterruptDisable (enum ADC_CHANNEL channel)
This inline function disables individual channel interrupt.
static void ADC1_IndividualChannelInterruptFlagClear (enum ADC_CHANNEL channel)
This inline function clears individual channel interrupt flag.
static void ADC1_IndividualChannelInterruptPrioritySet (enum ADC_CHANNEL channel, enum INTERRUPT_PRIORITY priorityValue)
This inline function allows selection of priority for individual channel interrupt.
void ADC1_ChannelCallbackRegister (void(*callback)(enum ADC_CHANNEL channel, uint16_t adcVal))
This function can be used to override default callback ADC1_ChannelCallback and to define custom callback for ADC1 Channel event. Read the conversion result of the corresponding channel in the custom callback.
void ADC1_ChannelCallback (enum ADC_CHANNEL channel, uint16_t adcVal)
This is the default callback function for all the analog channels.
void ADC1_ComparatorCallbackRegister (void(*callback)(enum ADC_CMP comparator))
This function can be used to override default callback and to define custom callback for ADC1_Comparator event.
void ADC1_ComparatorCallback (enum ADC_CMP comparator)
Comparator callback function.
void ADC1_ChannelTasks (enum ADC_CHANNEL channel)
This function call used only in polling mode, if channel conversion is done for requested channel, the calls the respective callback function.
void ADC1_SharedCorePowerEnable (void)
Enables power for ADC1 shared Core This function is used to set the analog and digital power for ADC1 shared Core.
void ADC1_PWMTriggerSourceSet (enum ADC_CHANNEL channel, enum ADC_PWM_INSTANCE pwmInstance, enum ADC_PWM_TRIGGERS triggerNumber)
Sets PWM trigger source for corresponding analog input.
Variables
const struct ADC_INTERFACE ADC1
Structure object of type ADC_INTERFACE with the custom name given by the user in the Melody Driver User interface.
2.1.2.1.2 Definition Documentation
ADC1_RESOLUTION
#define ADC1_RESOLUTION 12
Defines the ADC Resolution.
ADC1_SCAN_MODE_SELECTED
#define ADC1_SCAN_MODE_SELECTED true
Defines the scan option selection done for the shared channels.
ADC_CALIBRATION_FEATURE_AVAILABLE
#define ADC_CALIBRATION_FEATURE_AVAILABLE 1
This macro defines the macro associated with ADC core calibration.
APIs Supported:
void ADCx_CoreCalibration (enum ADC_CHANNEL channel); void ADC_SharedCoreCalibration (void);x denotes instance of ADC in ADCx, channel denotes dedicated Core. Refer to device specific datasheet to check number of cores and ADC module instance. Refer driver header file for detailed description of the APIs.
ADC_COMPARATOR_FEATURE_AVAILABLE
#define ADC_COMPARATOR_FEATURE_AVAILABLE 1
Defines the macro associated with ADC comparator feature.
APIs Supported:
void ADCx_ComparatorEnable(void); void ADCx_ComparatorDisable(void); void ADCx_ComparatorLowThresholdSet(uint16_t threshold ); void ADCx_ComparatorHighThresholdSet(uint16_t threshold ); uint16_t ADCx_ComparatorEventStatusGet(void); uint16_t ADCx_ComparatorEventChannelGet(void);x denotes instance of ADC in ADCx. Refer to device specific datasheet to check number of comparators and ADC module instance. Refer driver header file for detailed description of the APIs.
ADC_INDIVIDUAL_CHANNEL_INTERRUPT_FEATURE_AVAILABLE
#define ADC_INDIVIDUAL_CHANNEL_INTERRUPT_FEATURE_AVAILABLE 1
Defines the macro associated with ADC indvidual channel interrupts.
APIs Supported:
void ADCx_IndividualChannelInterruptEnable (enum ADC_CHANNEL channel); void ADCx_IndividualChannelInterruptDisable (enum ADC_CHANNEL channel); void ADCx_IndividualChannelInterruptFlagClear (enum ADC_CHANNEL channel);x denotes instance of ADC, channel denotes dedicated ADC channel. Refer to device specific datasheet to check number of cores and ADC module instance. Refer driver header file for detailed description of the APIs.
ADC_POWER_ENABLE_FEATURE_AVAILABLE
#define ADC_POWER_ENABLE_FEATURE_AVAILABLE 1
This macro defines the ADC core power enable functionality.
APIs Supported:
void ADCx_CorePowerEnable (enum ADC_CHANNEL channel); void ADC_SharedCorePowerEnable (void);x denotes instance of ADC in ADCx, channel denotes dedicated Core. Refer to device specific datasheet to check number of cores and ADC module instance. Refer driver header file for detailed description of the APIs.
2.1.2.1.3 Function Documentation
ADC1_ChannelCallback()
void ADC1_ChannelCallback (enum ADC_CHANNEL channel, uint16_t adcVal)
This is the default callback function for all the analog channels.
This callback is triggered once the channel conversion is done for a channel and to read the conversion result of the corresponding channel
in | channel |
- conversion completed channel |
in | adcVal |
- conversion result of channel |
none |
ADC1_ChannelCallbackRegister()
void ADC1_ChannelCallbackRegister (void(*)(enum ADC_CHANNEL channel, uint16_t adcVal) callback)
This function can be used to override default callback ADC1_ChannelCallback and to define custom callback for ADC1 Channel event. Read the conversion result of the corresponding channel in the custom callback.
in | callback |
- Address of the callback function. |
none |
ADC1_ChannelSelect()
inline static void ADC1_ChannelSelect (enum ADC_CHANNEL channel)
This inline function allows selection of a channel for conversion.
ADC1_Initialize() function should have been called before calling this function. |
in | channel |
- Channel for conversion |
none |
ADC1_ChannelTasks()
void ADC1_ChannelTasks (enum ADC_CHANNEL channel)
This function call used only in polling mode, if channel conversion is done for requested channel, the calls the respective callback function.
ADC1_Initialize() function should have been |
called before calling this function.
in | channel |
- Selected channel. |
none |
This function has to be polled to notify channel callbacks and clear the channel interrupt flags in non-interrupt mode of ADC
ADC1_CommonCallback()
void ADC1_CommonCallback (void )
This is the default callback with weak attribute.
The user can override and implement the default callback without weak attribute or can register a custom callback function using ADC1_CommonCallbackRegister
none |
none |
ADC1_CommonCallbackRegister()
void ADC1_CommonCallbackRegister (void(*)(void) callback)
This function can be used to override default callback and to define custom callback for ADC1 Common event.
in | callback |
- Address of the callback function. |
none |
ADC1_ComparatorCallback()
void ADC1_ComparatorCallback (enum ADC_CMP comparator)
Comparator callback function.
in | comparator |
- comparator in which compare event occurred |
none |
ADC1_ComparatorCallbackRegister()
void ADC1_ComparatorCallbackRegister (void(*)(enum ADC_CMP comparator) callback)
This function can be used to override default callback and to define custom callback for ADC1_Comparator event.
in | callback |
- Address of the callback function. |
none |
ADC1_ConversionResultGet()
inline static uint16_t ADC1_ConversionResultGet (enum ADC_CHANNEL channel)
Returns the conversion value for the channel selected.
This inline function returns the conversion value only after the conversion is complete. Conversion completion status can be checked using ADC1_IsConversionComplete(channel) function. |
in | channel |
- Selected channel |
Returns the analog to digital converted value |
ADC1_Deinitialize()
void ADC1_Deinitialize (void )
Deinitializes the ADC1 to POR values.
none |
none |
ADC1_Disable()
inline static void ADC1_Disable (void )
This inline function disables the ADC1 module.
ADC1_Initialize function should have been called before calling this function. |
none |
none |
ADC1_Enable()
inline static void ADC1_Enable (void )
This inline function enables the ADC1 module.
ADC1_Initialize function should have been called before calling this function. |
none |
none |
ADC1_IndividualChannelInterruptDisable()
inline static void ADC1_IndividualChannelInterruptDisable (enum ADC_CHANNEL channel)
This inline function disables individual channel interrupt.
in | channel |
- Selected channel |
none |
ADC1_IndividualChannelInterruptEnable()
inline static void ADC1_IndividualChannelInterruptEnable (enum ADC_CHANNEL channel)
This inline function enables individual channel interrupt.
in | channel |
- Selected channel |
none |
ADC1_IndividualChannelInterruptFlagClear()
inline static void ADC1_IndividualChannelInterruptFlagClear (enum ADC_CHANNEL channel)
This inline function clears individual channel interrupt flag.
in | channel |
- Selected channel |
none |
ADC1_IndividualChannelInterruptPrioritySet()
inline static void ADC1_IndividualChannelInterruptPrioritySet (enum ADC_CHANNEL channel, enum INTERRUPT_PRIORITY priorityValue)
This inline function allows selection of priority for individual channel interrupt.
in | channel |
- Selected channel |
in | priorityValue |
- The numerical value of interrupt priority |
none |
ADC1_Initialize()
void ADC1_Initialize (void )
Initializes ADC1 module, using the given initialization data This function must be called before any other ADC1 function is called.
none |
none |
ADC1_InterruptDisable()
inline static void ADC1_InterruptDisable (void )
This inline function disables the ADC1 interrupt.
none |
none |
ADC1_InterruptEnable()
inline static void ADC1_InterruptEnable (void )
This inline function enables the ADC1 interrupt.
none |
none |
ADC1_InterruptFlagClear()
inline static void ADC1_InterruptFlagClear (void )
Clears interrupt flag manually.
none |
none |
ADC1_InterruptPrioritySet()
inline static void ADC1_InterruptPrioritySet (uint16_t priorityValue)
This inline function allows selection of priority for interrupt.
in | priorityValue |
- The numerical value of interrupt priority |
none |
ADC1_IsConversionComplete()
inline static bool ADC1_IsConversionComplete (enum ADC_CHANNEL channel)
This inline function returns the status of conversion.
This function is used to determine if conversion is completed. When conversion is complete the function returns true otherwise false.
ADC1_SoftwareTriggerEnable() function should have been called before calling this function. |
in | channel |
- Selected channel |
true - Conversion is complete. false - Conversion is not complete. |
ADC1_PWMTriggerSourceSet()
void ADC1_PWMTriggerSourceSet (enum ADC_CHANNEL channel, enum ADC_PWM_INSTANCE pwmInstance, enum ADC_PWM_TRIGGERS triggerNumber)
Sets PWM trigger source for corresponding analog input.
in | channel |
- Selected channel |
in | pwmInstance |
- PWM instance for the trigger source |
in | triggerNumber |
- 1, for PWMx Trigger 1 |
in | triggerNumber |
- 2, for PWMx Trigger 2 |
none |
Configure PWM trigger value using PWM_TriggerACompareValueSet, PWM_TriggerBCompareValueSet or PWM_TriggerCCompareValueSet before calling this funcion and enable corresponding PWM trigger using PWM_Trigger1Enable or PWM_Trigger2Enable post calling it.
ADC1_ResolutionSet()
inline static void ADC1_ResolutionSet (enum ADC_RESOLUTION_TYPE resolution)
This inline function helps to configure all cores with same resolution.
in | resolution |
- Resolution type |
none |
ADC1_SharedCorePowerEnable()
void ADC1_SharedCorePowerEnable (void )
Enables power for ADC1 shared Core This function is used to set the analog and digital power for ADC1 shared Core.
none |
none |
ADC1_SoftwareTriggerDisable()
inline static void ADC1_SoftwareTriggerDisable (void )
This inline function resets software common trigger.
ADC1_Initialize function should have been called before calling this function. |
none |
none |
ADC1_SoftwareTriggerEnable()
inline static void ADC1_SoftwareTriggerEnable (void )
This inline function sets software common trigger.
ADC1_Initialize function should have been called before calling this function. |
none |
none |
ADC1_Tasks()
void ADC1_Tasks (void )
This function is used to implement the tasks for polled implementations.
ADC1_Initialize() function should have been called before calling this function. |
none |
none |
This function has to be polled to notify callbacks and clear the interrupt flags in non-interrupt mode of ADC
2.1.2.1.4 Enumeration Type Documentation
ADC_CHANNEL
enum ADC_CHANNEL
Defines the ADC channles that are selected from the MCC Melody User Interface for the ADC conversions.
The enum list in the Help document might be just a reference to show the analog channel list. Generated enum list is based on the configuration done by user in the MCC Melody user interface.
Channel_AN2 |
Channel Name:AN2 Assigned to:Shared Channel |
MAX_CHANNELS |
MAX_CHANNELS will be removed in future MCC releases. Use ADC_MAX_CHANNELS instead |
ADC_MAX_CHANNELS |
Maximum number of channels configured by user in MCC Melody User Interface |
ADC_CMP
enum ADC_CMP
Defines the ADC comparators that are available for the module to use.
MAX_CMPS |
MAX_CMPS will be removed in future MCC releases. Use ADC_MAX_CMPS instead |
ADC_MAX_CMPS |
Maximum Comparators configured by user in MCC Melody User Interface |
ADC_DEDICATED_CORE
enum ADC_DEDICATED_CORE
Defines the ADC cores that are available for the module to use.
The enum list in the Help document might be just a reference to to show the dedicated core list. Generated enum list is based on the configuration done by user in the MCC Melody user interface.
MAX_DEDICATED_CORES |
MAX_DEDICATED_CORES will be removed in future MCC releases. Use ADC_MAX_DEDICATED_CORES instead |
ADC_MAX_DEDICATED_CORES |
Maximum cores configured by user in MCC Melody User Interface |
ADC_PWM_INSTANCE
enum ADC_PWM_INSTANCE
Defines the ADC PWM trigger sources that are available for the module to use.
Refer PWM_GENERATOR enum for mapping between custom name and instance
ADC_PWM_GENERATOR_1 |
PWM name:PWM_GENERATOR_1 |
ADC_PWM_GENERATOR_2 |
PWM name:PWM_GENERATOR_2 |
ADC_PWM_GENERATOR_3 |
PWM name:PWM_GENERATOR_3 |
ADC_PWM_GENERATOR_4 |
PWM name:PWM_GENERATOR_4 |
ADC_PWM_GENERATOR_5 |
PWM name:PWM_GENERATOR_5 |
ADC_PWM_GENERATOR_6 |
PWM name:PWM_GENERATOR_6 |
ADC_PWM_GENERATOR_7 |
PWM name:PWM_GENERATOR_7 |
ADC_PWM_GENERATOR_8 |
PWM name:PWM_GENERATOR_8 |
ADC_PWM_TRIGGERS
enum ADC_PWM_TRIGGERS
Defines the PWM triggers that are available in each individual PWM.
ADC_PWM_TRIGGER_1 |
PWM TRIGGER 1 |
ADC_PWM_TRIGGER_2 |
PWM TRIGGER 2 |
ADC_RESOLUTION_TYPE
enum ADC_RESOLUTION_TYPE
Defines the supported ADC resolution types.
ADC_6_BIT_RESOLUTION |
ADC Resolution of 6 bit |
ADC_8_BIT_RESOLUTION |
ADC Resolution of 8 bit |
ADC_10_BIT_RESOLUTION |
ADC Resolution of 10 bit |
ADC_12_BIT_RESOLUTION |
ADC Resolution of 12 bit |
2.1.2.1.5 Variable Documentation
ADC1
const struct ADC_INTERFACE ADC1
Structure object of type ADC_INTERFACE with the custom name given by the user in the Melody Driver User interface.
The default name e.g. ADC1 can be changed by the user in the ADC user interface. This allows defining a structure with application specific name using the 'Custom Name' field. Application specific name allows the API Portability.
2.1.3 Class Documentation
2.1.3.1 ADC_INTERFACE Struct Reference
Structure containing the function pointers of ADC driver.
2.1.3.1.1 Detailed Description
Structure containing the function pointers of ADC driver.
#include <adc_interface.h>
Public Attributes
void(* Initialize )(void)
Pointer to ADCx_Initialize e.g. ADC1_Initialize.
void(* Deinitialize )(void)
Pointer to ADCx_Deinitialize e.g. ADC1_Deinitialize.
void(* Enable )(void)
Pointer to ADCx_Enable e.g. ADC1_Enable.
void(* Disable )(void)
Pointer to ADCx_Disable e.g. ADC1_Disable.
void(* SoftwareTriggerEnable )(void)
Pointer to ADCx_SoftwareTriggerEnable e.g. ADC1_SoftwareTriggerEnable.
void(* SoftwareTriggerDisable )(void)
Pointer to ADCx_SoftwareTriggerDisable e.g. ADC1_SoftwareTriggerDisable.
void(* ChannelSelect )(enum ADC_CHANNEL channel)
Pointer to ADCx_ChannelSelect e.g. ADC1_ChannelSelect.
uint16_t(* ConversionResultGet )(enum ADC_CHANNEL channel)
Pointer to ADCx_ConversionResultGet e.g. ADC1_ConversionResultGet.
bool(* IsConversionComplete )(enum ADC_CHANNEL channel)
Pointer to ADCx_IsConversionComplete e.g. ADC1_IsConversionComplete.
void(* ResolutionSet )(enum ADC_RESOLUTION_TYPE resolution)
Pointer to ADCx_ResolutionSet e.g. ADC1_ResolutionSet.
void(* InterruptEnable )(void)
Pointer to ADCx_InterruptEnable e.g. ADC1_InterruptEnable.
void(* InterruptDisable )(void)
Pointer to ADCx_InterruptDisable e.g. ADC1_InterruptDisable.
void(* InterruptFlagClear )(void)
Pointer to ADCx_InterruptFlagClear e.g. ADC1_InterruptFlagClear.
void(* InterruptPrioritySet )(uint16_t priorityValue)
Pointer to ADCx_InterruptPrioritySet e.g. ADC1_InterruptPrioritySet.
void(* CommonCallbackRegister )(void(*callback)(void))
Pointer to ADCx_CommonCallbackRegister e.g. ADC1_CommonCallbackRegister.
void(* Tasks )(void)
Pointer to ADCx_Tasks e.g. ADC1_Tasks (Supported only in polling mode)
const struct ADC_MULTICORE * adcMulticoreInterface
Pointer to ADC_MULTICORE.
2.1.3.1.2 Member Data Documentation
adcMulticoreInterface
const struct ADC_MULTICORE* adcMulticoreInterface
Pointer to ADC_MULTICORE.
ChannelSelect
void(* ChannelSelect) (enum ADC_CHANNEL channel)
Pointer to ADCx_ChannelSelect e.g. ADC1_ChannelSelect.
CommonCallbackRegister
void(* CommonCallbackRegister) (void(*callback)(void))
Pointer to ADCx_CommonCallbackRegister e.g. ADC1_CommonCallbackRegister.
ConversionResultGet
uint16_t(* ConversionResultGet) (enum ADC_CHANNEL channel)
Pointer to ADCx_ConversionResultGet e.g. ADC1_ConversionResultGet.
Deinitialize
void(* Deinitialize) (void)
Pointer to ADCx_Deinitialize e.g. ADC1_Deinitialize.
Disable
void(* Disable) (void)
Pointer to ADCx_Disable e.g. ADC1_Disable.
Enable
void(* Enable) (void)
Pointer to ADCx_Enable e.g. ADC1_Enable.
Initialize
void(* Initialize) (void)
Pointer to ADCx_Initialize e.g. ADC1_Initialize.
InterruptDisable
void(* InterruptDisable) (void)
Pointer to ADCx_InterruptDisable e.g. ADC1_InterruptDisable.
InterruptEnable
void(* InterruptEnable) (void)
Pointer to ADCx_InterruptEnable e.g. ADC1_InterruptEnable.
InterruptFlagClear
void(* InterruptFlagClear) (void)
Pointer to ADCx_InterruptFlagClear e.g. ADC1_InterruptFlagClear.
InterruptPrioritySet
void(* InterruptPrioritySet) (uint16_t priorityValue)
Pointer to ADCx_InterruptPrioritySet e.g. ADC1_InterruptPrioritySet.
IsConversionComplete
bool(* IsConversionComplete) (enum ADC_CHANNEL channel)
Pointer to ADCx_IsConversionComplete e.g. ADC1_IsConversionComplete.
ResolutionSet
void(* ResolutionSet) (enum ADC_RESOLUTION_TYPE resolution)
Pointer to ADCx_ResolutionSet e.g. ADC1_ResolutionSet.
SoftwareTriggerDisable
void(* SoftwareTriggerDisable) (void)
Pointer to ADCx_SoftwareTriggerDisable e.g. ADC1_SoftwareTriggerDisable.
SoftwareTriggerEnable
void(* SoftwareTriggerEnable) (void)
Pointer to ADCx_SoftwareTriggerEnable e.g. ADC1_SoftwareTriggerEnable.
Tasks
void(* Tasks) (void)
Pointer to ADCx_Tasks e.g. ADC1_Tasks (Supported only in polling mode)
2.1.3.2 ADC_MULTICORE Struct Reference
Structure containing the function pointers of ADC driver.
2.1.3.2.1 Detailed Description
Structure containing the function pointers of ADC driver.
#include <adc_interface.h>
Public Attributes
void(* ChannelCallbackRegister )(void(*callback)(enum ADC_CHANNEL channel, uint16_t adcVal))
Pointer to ADCx_ChannelCallbackRegister e.g. ADC1_ChannelCallbackRegister.
void(* ComparatorCallbackRegister )(void(*callback)(enum ADC_CMP comparator))
Pointer to ADCx_ComparatorCallbackRegister e.g. ADC1_ComparatorCallbackRegister.
void(* ChannelTasks )(enum ADC_CHANNEL channel)
Pointer to ADCx_ChannelTasks e.g. ADC1_ChannelTasks.
void(* ComparatorTasks )(enum ADC_CMP comparator)
Pointer to ADCx_ComparatorTasks e.g. ADC1_ComparatorTasks.
void(* IndividualChannelInterruptEnable )(enum ADC_CHANNEL channel)
Pointer to ADCx_IndividualChannelInterruptEnable e.g. ADC1_IndividualChannelInterruptEnable.
void(* IndividualChannelInterruptDisable )(enum ADC_CHANNEL channel)
Pointer to ADCx_IndividualChannelInterruptDisable e.g. ADC1_IndividualChannelInterruptDisable.
void(* IndividualChannelInterruptFlagClear )(enum ADC_CHANNEL channel)
Pointer to ADCx_IndividualChannelInterruptFlagClear e.g. ADC1_IndividualChannelInterruptFlagClear.
void(* IndividualChannelInterruptPrioritySet )(enum ADC_CHANNEL channel, enum INTERRUPT_PRIORITY priorityValue)
Pointer to ADCx_IndividualChannelInterruptPrioritySet e.g. ADC1_IndividualChannelInterruptPrioritySet.
void(* CorePowerEnable )(enum ADC_DEDICATED_CORE core)
Pointer to ADCx_CorePowerEnable e.g. ADC1_CorePowerEnable.
void(* SharedCorePowerEnable )(void)
Pointer to ADCx_SharedCorePowerEnable e.g. ADC1_SharedCorePowerEnable.
void(* PWMTriggerSourceSet )(enum ADC_CHANNEL channel, enum ADC_PWM_INSTANCE pwmInstance, enum ADC_PWM_TRIGGERS triggerNumber)
Pointer to ADCx_PWMTriggerSourceSet e.g. ADC1_PWMTriggerSourceSet.
2.1.3.2.2 Member Data Documentation
ChannelCallbackRegister
void(* ChannelCallbackRegister) (void(*callback)(enum ADC_CHANNEL channel, uint16_t adcVal))
Pointer to ADCx_ChannelCallbackRegister e.g. ADC1_ChannelCallbackRegister.
ChannelTasks
void(* ChannelTasks) (enum ADC_CHANNEL channel)
Pointer to ADCx_ChannelTasks e.g. ADC1_ChannelTasks.
ComparatorCallbackRegister
void(* ComparatorCallbackRegister) (void(*callback)(enum ADC_CMP comparator))
Pointer to ADCx_ComparatorCallbackRegister e.g. ADC1_ComparatorCallbackRegister.
ComparatorTasks
void(* ComparatorTasks) (enum ADC_CMP comparator)
Pointer to ADCx_ComparatorTasks e.g. ADC1_ComparatorTasks.
CorePowerEnable
void(* CorePowerEnable) (enum ADC_DEDICATED_CORE core)
Pointer to ADCx_CorePowerEnable e.g. ADC1_CorePowerEnable.
IndividualChannelInterruptDisable
void(* IndividualChannelInterruptDisable) (enum ADC_CHANNEL channel)
Pointer to ADCx_IndividualChannelInterruptDisable e.g. ADC1_IndividualChannelInterruptDisable.
IndividualChannelInterruptEnable
void(* IndividualChannelInterruptEnable) (enum ADC_CHANNEL channel)
Pointer to ADCx_IndividualChannelInterruptEnable e.g. ADC1_IndividualChannelInterruptEnable.
IndividualChannelInterruptFlagClear
void(* IndividualChannelInterruptFlagClear) (enum ADC_CHANNEL channel)
Pointer to ADCx_IndividualChannelInterruptFlagClear e.g. ADC1_IndividualChannelInterruptFlagClear.
IndividualChannelInterruptPrioritySet
void(* IndividualChannelInterruptPrioritySet) (enum ADC_CHANNEL channel, enum INTERRUPT_PRIORITY priorityValue)
Pointer to ADCx_IndividualChannelInterruptPrioritySet e.g. ADC1_IndividualChannelInterruptPrioritySet.
PWMTriggerSourceSet
void(* PWMTriggerSourceSet) (enum ADC_CHANNEL channel, enum ADC_PWM_INSTANCE pwmInstance, enum ADC_PWM_TRIGGERS triggerNumber)
Pointer to ADCx_PWMTriggerSourceSet e.g. ADC1_PWMTriggerSourceSet.
SharedCorePowerEnable
void(* SharedCorePowerEnable) (void)
Pointer to ADCx_SharedCorePowerEnable e.g. ADC1_SharedCorePowerEnable.
2.1.4 File Documentation
2.1.4.1 source/adc1.h File Reference
This is the generated driver header file for the ADC1 driver.
#include <xc.h> #include <stdbool.h> #include <stdint.h> #include "adc_types.h" #include "adc_interface.h"
2.1.4.1.1 Functions
void ADC1_Initialize (void)
Initializes ADC1 module, using the given initialization data This function must be called before any other ADC1 function is called.
void ADC1_Deinitialize (void)
Deinitializes the ADC1 to POR values.
static void ADC1_Enable (void)
This inline function enables the ADC1 module.
static void ADC1_Disable (void)
This inline function disables the ADC1 module.
static void ADC1_SoftwareTriggerEnable (void)
This inline function sets software common trigger.
static void ADC1_SoftwareTriggerDisable (void)
This inline function resets software common trigger.
static void ADC1_ChannelSelect (enum ADC_CHANNEL channel)
This inline function allows selection of a channel for conversion.
static uint16_t ADC1_ConversionResultGet (enum ADC_CHANNEL channel)
Returns the conversion value for the channel selected.
static bool ADC1_IsConversionComplete (enum ADC_CHANNEL channel)
This inline function returns the status of conversion.
static void ADC1_ResolutionSet (enum ADC_RESOLUTION_TYPE resolution)
This inline function helps to configure all cores with same resolution.
static void ADC1_InterruptEnable (void)
This inline function enables the ADC1 interrupt.
static void ADC1_InterruptDisable (void)
This inline function disables the ADC1 interrupt.
static void ADC1_InterruptFlagClear (void)
Clears interrupt flag manually.
static void ADC1_InterruptPrioritySet (uint16_t priorityValue)
This inline function allows selection of priority for interrupt.
void ADC1_CommonCallbackRegister (void(*callback)(void))
This function can be used to override default callback and to define custom callback for ADC1 Common event.
void ADC1_CommonCallback (void)
This is the default callback with weak attribute.
void ADC1_Tasks (void)
This function is used to implement the tasks for polled implementations.
static void ADC1_IndividualChannelInterruptEnable (enum ADC_CHANNEL channel)
This inline function enables individual channel interrupt.
static void ADC1_IndividualChannelInterruptDisable (enum ADC_CHANNEL channel)
This inline function disables individual channel interrupt.
static void ADC1_IndividualChannelInterruptFlagClear (enum ADC_CHANNEL channel)
This inline function clears individual channel interrupt flag.
static void ADC1_IndividualChannelInterruptPrioritySet (enum ADC_CHANNEL channel, enum INTERRUPT_PRIORITY priorityValue)
This inline function allows selection of priority for individual channel interrupt.
void ADC1_ChannelCallbackRegister (void(*callback)(enum ADC_CHANNEL channel, uint16_t adcVal))
This function can be used to override default callback ADC1_ChannelCallback and to define custom callback for ADC1 Channel event. Read the conversion result of the corresponding channel in the custom callback.
void ADC1_ChannelCallback (enum ADC_CHANNEL channel, uint16_t adcVal)
This is the default callback function for all the analog channels.
void ADC1_ComparatorCallbackRegister (void(*callback)(enum ADC_CMP comparator))
This function can be used to override default callback and to define custom callback for ADC1_Comparator event.
void ADC1_ComparatorCallback (enum ADC_CMP comparator)
Comparator callback function.
void ADC1_ChannelTasks (enum ADC_CHANNEL channel)
This function call used only in polling mode, if channel conversion is done for requested channel, the calls the respective callback function.
void ADC1_SharedCorePowerEnable (void)
Enables power for ADC1 shared Core This function is used to set the analog and digital power for ADC1 shared Core.
void ADC1_PWMTriggerSourceSet (enum ADC_CHANNEL channel, enum ADC_PWM_INSTANCE pwmInstance, enum ADC_PWM_TRIGGERS triggerNumber)
Sets PWM trigger source for corresponding analog input.
2.1.4.1.2 Macros
#define ADC1_SCAN_MODE_SELECTED true
Defines the scan option selection done for the shared channels.
#define ADC1_RESOLUTION 12
Defines the ADC Resolution.
2.1.4.1.3 Variables
const struct ADC_INTERFACE ADC1
Structure object of type ADC_INTERFACE with the custom name given by the user in the Melody Driver User interface.
2.1.4.1.4 Detailed Description
This is the generated driver header file for the ADC1 driver.
ADC1 Generated Driver Header File
2.1.4.2 source/adc_features.h File Reference
This is the generated module feature header file for ADC driver.
2.1.4.2.1 Macros
#define ADC_POWER_ENABLE_FEATURE_AVAILABLE 1
This macro defines the ADC core power enable functionality.
#define ADC_CALIBRATION_FEATURE_AVAILABLE 1
This macro defines the macro associated with ADC core calibration.
#define ADC_INDIVIDUAL_CHANNEL_INTERRUPT_FEATURE_AVAILABLE 1
Defines the macro associated with ADC indvidual channel interrupts.
#define ADC_COMPARATOR_FEATURE_AVAILABLE 1
Defines the macro associated with ADC comparator feature.
2.1.4.2.2 Detailed Description
This is the generated module feature header file for ADC driver.
ADC Generated Feature Header File
This file provides module feature list available on the selected device. The macros defined in this file provides the flexibility to easily migrate the user application to other device which might have varied feature list. The content in this file is strictly "read only" and should not be altered.
2.1.4.3 source/adc_interface.h File Reference
#include <stdint.h> #include <stdbool.h> #include "adc_types.h" #include "../system/interrupt_types.h"
2.1.4.3.1 Data structures
struct ADC_INTERFACE
Structure containing the function pointers of ADC driver.
struct ADC_MULTICORE
Structure containing the function pointers of ADC driver.
2.1.4.3.2 Detailed Description
ADC Generated Driver Interface Header File
2.1.4.4 source/adc_types.h File Reference
This is the generated driver types header file for the ADC driver.
2.1.4.4.1 Enumerations
enum ADC_CHANNEL { Channel_AN2, MAX_CHANNELS = 1, ADC_MAX_CHANNELS = 1 }
Defines the ADC channles that are selected from the MCC Melody User Interface for the ADC conversions.
enum ADC_DEDICATED_CORE { MAX_DEDICATED_CORES = 0, ADC_MAX_DEDICATED_CORES = 0 }
Defines the ADC cores that are available for the module to use.
enum ADC_RESOLUTION_TYPE { ADC_6_BIT_RESOLUTION = 0x0, ADC_8_BIT_RESOLUTION = 0x1, ADC_10_BIT_RESOLUTION = 0x2, ADC_12_BIT_RESOLUTION = 0x3 }
Defines the supported ADC resolution types.
enum ADC_CMP { MAX_CMPS = 0, ADC_MAX_CMPS = 0 }
Defines the ADC comparators that are available for the module to use.
enum ADC_PWM_INSTANCE { ADC_PWM_GENERATOR_1 = 1, ADC_PWM_GENERATOR_2 = 2, ADC_PWM_GENERATOR_3 = 3, ADC_PWM_GENERATOR_4 = 4, ADC_PWM_GENERATOR_5 = 5, ADC_PWM_GENERATOR_6 = 6, ADC_PWM_GENERATOR_7 = 7, ADC_PWM_GENERATOR_8 = 8 }
Defines the ADC PWM trigger sources that are available for the module to use.
enum ADC_PWM_TRIGGERS { ADC_PWM_TRIGGER_1 = 1, ADC_PWM_TRIGGER_2 = 2 }
Defines the PWM triggers that are available in each individual PWM.
2.1.4.4.2 Detailed Description
This is the generated driver types header file for the ADC driver.
ADC Generated Driver Types Header File
2.1.5 Module Documentation
2.1.5.1 ADC Use Cases
2.1.5.1.1 ADC Use Case Code Snippet Instructions
For new users it is recommended to start with the Timer0, UART and Data Streamer, since these components are used to support other use cases.
-
Add ADC to the project
-
Configure:
-
The ADC as described in the example.
-
Any other peripherals or pins needed for the use case.
-
-
Generate the code
-
Add the code snippet(s) to the application code
-
Program the board
2.1.5.1.2 ADC Use Case 1: ADC in Basic_mode with Printf of ADC Result
An ADC conversion is taken every 100 ms, on the selected analog channel. After each ADC conversion a LED is toggled and the ADC Result is sent to PC terminal using printf.
Note: Before each conversion is started, the sample capacitor is first discharged, by connecting it to the ground potential of the microcontroller.
-
Add ADC
-
Add UART
-
Configuration Settings:
-
Enable an ANx channel
-
Select Trigger Source for the same channel as Common Software Trigger
-
-
Hardware Settings:
-
Operating Mode: Basic Mode (Default)
-
-
Configuration Settings:
-
Redirect Printf to UART: Yes.
-
-
Dependency Selector:
-
UART PLIB Selector: Select the UART that is connected to Serial/CDC port.
-
Redirect STDIO to UART (for printf support): Yes
-
-
Clock Configuration: Default (1 MHz)
-
Pins: (For all pins check board schematics)
-
Pin Grid View: Select an ANx.
-
Pin Grid View: Select pin for UART Transmit - Tx.
-
Pin Grid View: Select LED pin as output.
-
Pins: Rename Custom Name to "LED".
-
After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:
/* Required header files for ADC Example 1 */ #include "mcc_generated_files/system/system.h" #include "mcc_generated_files/system/clock.h" #define FCY CLOCK_PeripheralFrequencyGet() #include <libpic30.h> #include "mcc_generated_files/adc/adc1.h" #include "mcc_generated_files/uart/uart1.h" #include "mcc_generated_files/system/pins.h" const struct ADC_INTERFACE *adc = &ADC1; const struct UART_INTERFACE *UartSerial = &UART1_Drv;
int main(void) { SYSTEM_Initialize(); uint16_t volatile adcResult; while(1) { adc->SoftwareTriggerEnable(); //Trigger the ADC conversion via software __delay_ms(100); adcResult = adc->ConversionResultGet(Channel_AN0); //Get ADC converted result for Channel_AN0 printf("The ADC Result is: %d\r\n",adcResult); LED_Toggle(); } }
2.1.5.1.3 ADC Use Case 2: ADC Samples Visualized with the Data Streamer
-
ADC converstions are triggerd by an timer overflow callback, where the Data Streamer variables adcResult and adcSampleCount are updated.
-
After each ADC conversion a LED is toggled and SEND_FRAME is set to true, so that a frame is sent to the Data Visualizer from the main while loop.
-
Add ADC
-
Add Timer (Use Case: 100 ms Timer)
-
Add Data Streamer (Use Case: Send Frames at a Period Set by the Timer)
-
Configuration Settings:
-
Enable an ANx channel
-
Select Trigger Source for the same channel as Common Software Trigger
-
-
Hardware Settings:
-
Operating Mode: Basic Mode (Default)
-
-
Software Settings:
-
UART Dependency Selection: Select the UART that is connected to Serial/CDC port.
-
-
Data Streamer Table:
-
uint16_t adcResult
-
uint8_t adcSampleCount
-
-
Clock Configuration: Default (1 MHz)
-
Pins: (For all pins check board schematics)
-
Pin Grid View: Select an ANx.
-
Pin Grid View: Select pin for UART Transmit - Tx.
-
Pin Grid View: Select LED pin as output.
-
Pin Grid View: Select Debug GPIO pin.
-
Pins: Rename Custom Names to "LED" and "DebugIO" respectively.
-
After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:
D,1,1,adcResult B,1,1,adcSampleCount
#include "mcc_generated_files/system/system.h" #include "mcc_generated_files/system/clock.h" #define FCY CLOCK_PeripheralFrequencyGet() #include <libpic30.h> #include "mcc_generated_files/timer/tmr1.h" #include "mcc_generated_files/adc/adc1.h" #include "mcc_generated_files/data_streamer/data_streamer.h" #include "mcc_generated_files/system/pins.h" /* Create a pointer of type TIMER_INTERFACE and assign it to the address of the Timer1 TIMER_INTERFACE struct. This enables us to get access the portable API interface, which ensures that it's easy to change the peripheral instance the timer runs on. */ const struct TIMER_INTERFACE *timer = &Timer1; const struct ADC_INTERFACE *adc = &ADC1; volatile bool SEND_FRAME = false;
void Timer_Callback_100ms(void)
{
DataStreamer.adcResult = adc->ConversionResultGet(Channel_AN0);
DataStreamer.adcSampleCount++;
LED_Toggle();
SEND_FRAME = true;
adc->SoftwareTriggerEnable(); //Trigger the ADC conversion via software
}
int main(void) { SYSTEM_Initialize(); DataStreamer.adcResult = 0; DataStreamer.adcSampleCount = 0; timer->TimeoutCallbackRegister(Timer_Callback_100ms); //Timer Callback Registry while(1) { if(SEND_FRAME) { WriteFrame(); //DataStreamer write SEND_FRAME = false; } } }