In this use case, ADCA is configured for:
- sampling on two channels (0 and 1) with respective inputs:
- I/O pin as single-ended input (PA0)
- two I/O pins as differential input w/ 2x gain (PA1 and PA5)
- signed conversions
- 12-bit resolution
- internal 1V reference
- free-running conversions
- interrupt-based conversion handling
The ADC results are handled in an interrupt callback function which simply stores the result in one of two channel-specific, global variables.
- Note
- This use case assumes that the device has multiple ADC channels. Refer to the applicable device datasheet for information about the number of ADC channels.
Setup steps
Example code
Ensure that conf_adc.h contains:
#define CONFIG_ADC_CALLBACK_ENABLE
#define CONFIG_ADC_CALLBACK_TYPE int16_t
Add to application C-file:
#define MY_ADC ADCA
int16_t ch0_result;
int16_t ch1_result;
{
switch (ch_mask) {
ch0_result = result;
break;
ch1_result = result;
break;
default:
break;
}
}
static void adc_init(void)
{
}
Add to main()
:
Workflow
- Define a macro for the ADC to use, in case we want to change it later:
- Define global variables to contain the ADC result of each channel:
int16_t ch0_result;
int16_t ch1_result;
- Create an ADC interrupt callback function that stores the results in the channels' respective global variables:
{
switch (ch_mask) {
ch0_result = result;
break;
ch1_result = result;
break;
default:
break;
}
}
- Note
- Refer to adc_callback_t for documentation on the interrupt callback function type.
- Create a function
adc_init()
to intialize the ADC:
static void adc_init(void)
{
}
- Allocate configuration structs for ADC and channel, then initialize them:
- Set signed, 12-bit conversions with internal 1V voltage reference:
-
- Note
- With signed, 12-bit conversion, 1 bit is used to indicate sign/polarity, so the resolution is halved in terms of Volt per LSB.
- Set free-running conversions on the first two ADC channels:
-
- Note
- The base event channel (0) does not affect operation in this mode.
- Set ADC clock rate to maximum 5 KHz:
-
- Note
- In free-running mode, it is wise to reduce the ADC clock so that the device has time to handle the results, e.g., channel 0 does not complete a new conversion before channel 1's result has been handled.
- Set the interrupt callback function to use for the ADC:
- Write the configuration to the ADC:
- Enable interrupts for the ADC channels:
- Set up single-ended input from pin 0 on port A, then write the config to the first channel (0):
- Set up differential input from pins 1 and 5 on port A, with 2x gain, then write the config to the second channel (1):
-
- Note
- Not all input and gain combinations are valid. Refer to adcch_set_input() for documentation on the restrictions.
- Initialize the clock system, the ADC, and the PMIC since we will be using interrupts:
Usage steps
Example code
Add to main.c()
:
Workflow
- Enable interrupts globally to allow the ADC interrupts to be handled:
- Enable the ADC to start conversions:
-
- Note
- When configured for free-running conversions, the ADC will start doing conversions as soon as it is enabled, so we do not need to do it manually.
- Enter a busy-loop while interrupts handle the ADC results: