1.8.2 Analog-to-Digital Converter (ADC)
-
12-bit Resolution with Enhanced Mode up to 16 bits
-
1 Msps Conversion Rate
-
Digital Averaging Function providing Enhanced Resolution Mode up to 16 bits
-
On-chip Temperature Sensor Management
-
Selectable Single-Ended or Differential Input Voltage
-
Digital Correction of Offset and Gain Errors
-
Individual Enable and Disable of Each Channel
-
Hardware or Software Trigger
-
Drive of PWM Fault Input
-
DMA Support
-
Two Sleep Modes
-
Channel Sequence Customizing
-
Automatic Window Comparison of Converted Values
Using The Library
Interrupt mode:
#include <stddef.h> // Defines NULL #include <stdbool.h> // Defines true #include <stdlib.h> // Defines EXIT_FAILURE #include "definitions.h" // SYS function prototypes #define ADC_VREF (3.3f) uint16_t adc_ch5_count, adc_ch6_count, adc_ch7_count; float adc_ch5_voltage, adc_ch6_voltage, adc_ch7_voltage; volatile bool result_ready = false; /* This function is called after conversion of last channel in the user sequence */ void ADC_EventHandler(uint32_t interruptStatus, uint32_t eocInterruptStatus, uintptr_t context) { /* Read the result of 3 channels*/ adc_ch6_count = ADC_ChannelResultGet(ADC_CH6); adc_ch7_count = ADC_ChannelResultGet(ADC_CH7); adc_ch5_count = ADC_ChannelResultGet(ADC_CH5); result_ready = true; } // ***************************************************************************** // ***************************************************************************** // Section: Main Entry Point // ***************************************************************************** // ***************************************************************************** int main ( void ) { /* Initialize all modules */ SYS_Initialize ( NULL ); /* Register callback function for ADC end of conversion interrupt*/ ADC_CallbackRegister(ADC_EventHandler, (uintptr_t)NULL); printf("\n\r---------------------------------------------------------"); printf("\n\r ADC User Sequence Demo "); printf("\n\r---------------------------------------------------------\n\r"); printf("CH5 Count CH5 Voltage CH6 Count CH6 Voltage CH7 Count CH7 Voltage \n\r"); TC0_CH0_CompareStart(); while ( true ) { /* Check if result is ready to be transmitted to console */ if (result_ready == true) { adc_ch6_voltage = (float)adc_ch6_count * ADC_VREF/4095U; adc_ch7_voltage = (float)adc_ch7_count * ADC_VREF/4095U; adc_ch5_voltage = (float)adc_ch5_count * ADC_VREF/4095U; printf("0x%03x %0.2f V 0x%03x %0.2f V 0x%03x %0.2f V \t\r", adc_ch5_count, adc_ch5_voltage, adc_ch6_count, adc_ch6_voltage, adc_ch7_count, adc_ch7_voltage); result_ready = false; } } /* Execution should not come here during normal operation */ return ( EXIT_FAILURE ); }
Polling mode:
#include <stddef.h> // Defines NULL #include <stdbool.h> // Defines true #include <stdlib.h> // Defines EXIT_FAILURE #include <stdio.h> #include "definitions.h" // SYS function prototypes /***************************************************** ADC CH0 - PD19 - Connect to Vcc or GND *****************************************************/ #define ADC_VREF (3.3f) uint16_t adc_count; float input_voltage; // ***************************************************************************** // ***************************************************************************** // Section: Main Entry Point // ***************************************************************************** // ***************************************************************************** int main ( void ) { /* Initialize all modules */ SYS_Initialize ( NULL ); printf("\n\r---------------------------------------------------------"); printf("\n\r ADC Demo "); printf("\n\r---------------------------------------------------------\n\r"); PIT64B0_TimerStart(); while (1) { /* Start ADC conversion */ ADC_ConversionStart(); /* Wait till ADC conversion result is available */ while(!ADC_ChannelResultIsReady(ADC_CH5)) { }; /* Read the ADC result */ adc_count = ADC_ChannelResultGet(ADC_CH5); input_voltage = (float)adc_count * ADC_VREF / 4095U; printf("ADC Count = 0x%03x, ADC Input Voltage = %0.2f V \r",adc_count, input_voltage); PIT64B0_DelayMs(500); } /* Execution should not come here during normal operation */ return ( EXIT_FAILURE ); }
Library Interface
Analog-to-Digital Converter peripheral library provides the following interfaces:
Functions
Name | Description |
---|---|
ADCx_Initialize | Initializes given instance of ADC peripheral |
ADC_ChannelsEnable | Enables the ADC channels |
ADC_ChannelsDisable | Disables the ADC channels |
ADC_ChannelsInterruptEnable | Enables the ADC interrupt sources |
ADC_ChannelsInterruptDisable | Disables the ADC interrupt sources |
ADC_InterruptEnable | This function enables the interrupts |
ADC_InterruptDisable | This function disables the interrupts |
ADC_InterruptStatusGet | Returns the status of ADC interrupt |
ADCx_ConversionStart | Starts the ADC conversion of all the enabled channels with the software trigger |
ADC_ChannelResultIsReady | Returns the status of the channel conversion |
ADC_ChannelResultGet | Reads the conversion result of the channel |
ADC_ConversionSequenceSet | Sets the user sequence of the channel conversion |
ADCx_ComparisonWindowSet | Sets Low threshold and High threshold in comparison window |
ADC_ComparisonEventResultIsReady | Returns the status of the Comparison event |
ADC_ComparisonRestart | Restart the comparison function |
ADC_SleepModeEnable | This function enables ADC sleep mode |
ADC_SleepModeDisable | This function disables ADC sleep mode |
ADC_FastWakeupEnable | This function enables ADC Fast Wakeup |
ADC_FastWakeupDisable | This function disables ADC Fast Wakeup |
ADCx_CallbackRegister | Registers the function to be called from interrupt |
Data types and constants
Name | Type | Description |
---|---|---|
ADC_CHANNEL_MASK | Enum | Identifies ADC channel mask |
ADC_CHANNEL_NUM | Enum | Identifies ADC channel number |
ADC_INTERRUPT_EOC_MASK | Enum | Identifies EOC interrupt sources number |
ADC_INTERRUPT_MASK | Enum | Identifies interrupt sources number |
ADC_CALLBACK | Typedef | Defines the data type and function signature for the ADC peripheral callback function |
ADC_CALLBACK_OBJECT | Struct | ADC Callback structure |