This is a general-purpose ADC with up to 14 input channels. The ADC contains an internal 1.2V reference, but can also be used with external reference or full scale range equal to VDD. It can be operated in a single step mode with sampling under software control, or a continuous conversion mode with a programmable sampling rate.
This module contains setup functions for configuring the ADC; operation functions for starting the ADC and reading a sampled value; and status functions for checking if the ADC is busy or ready, or has overflowed.
#define ADC_STARTUP_CNT 1; |
The following example show a typical usage of the ADC:
void main(void) { uint16_t adc_out; adc_init(); // Initialise ADC while(true) // Endless loop { hal_adc_start(); // Start ADconversion while(hal_adc_busy()); // Wait for data ready adc_out = hal_adc_read_LSB(); // Read LSB of output adc_out += ((uint16_t)hal_adc_read_MSB()) << 8; // Add MSB of output // Output from ADC is now stored in adc_out } } // Initialize ADC: Set input channel to AIN7 and VDD as reference. Use // single ended mode and single step conversion. Use 10 bits to represent // the analog input signal, set the output data to be right justified void adc_init(void) { hal_adc_set_input_channel(HAL_ADC_INP_AIN7); hal_adc_set_reference(HAL_ADC_REF_VDD); hal_adc_set_input_mode(HAL_ADC_SINGLE); hal_adc_set_conversion_mode(HAL_ADC_SINGLE_STEP); hal_adc_set_resolution(HAL_ADC_RES_10BIT); hal_adc_set_data_just(HAL_ADC_JUST_RIGHT); }
An enum describing the ADC's input channel.
Definition at line 98 of file hal_adc.h.
{ HAL_ADC_INP_AIN0 = HAL_INP_AIN0, HAL_ADC_INP_AIN1 = HAL_INP_AIN1, HAL_ADC_INP_AIN2 = HAL_INP_AIN2, HAL_ADC_INP_AIN3 = HAL_INP_AIN3, HAL_ADC_INP_AIN4 = HAL_INP_AIN4, HAL_ADC_INP_AIN5 = HAL_INP_AIN5, HAL_ADC_INP_AIN6 = HAL_INP_AIN6, HAL_ADC_INP_AIN7 = HAL_INP_AIN7, HAL_ADC_INP_AIN8 = HAL_INP_AIN8, HAL_ADC_INP_AIN9 = HAL_INP_AIN9, HAL_ADC_INP_AIN10 = HAL_INP_AIN10, HAL_ADC_INP_AIN11 = HAL_INP_AIN11, HAL_ADC_INP_AIN12 = HAL_INP_AIN12, HAL_ADC_INP_AIN13 = HAL_INP_AIN13, HAL_ADC_INP_VDD1_3 = HAL_INP_VDD1_3, HAL_ADC_INP_VDD2_3 = HAL_INP_VDD2_3 } hal_adc_input_channel_t;
enum hal_adc_reference_t |
An enum describing the ADC's reference.
HAL_ADC_REF_INT |
Internal 1.22V reference |
HAL_ADC_REF_VDD |
VDD as reference |
HAL_ADC_REF_AIN3 |
External reference on AIN3 |
HAL_ADC_REF_AIN9 |
External reference on AIN9 |
Definition at line 120 of file hal_adc.h.
{ HAL_ADC_REF_INT = 0x00, HAL_ADC_REF_VDD = 0x01, HAL_ADC_REF_AIN3 = 0x02, HAL_ADC_REF_AIN9 = 0x03 } hal_adc_reference_t;
enum hal_adc_input_mode_t |
An enum describing the ADC's single ended or differential mode.
HAL_ADC_SINGLE |
Single ended mode |
HAL_ADC_DIFF_AIN2 |
Differential with AIN2 as inv. input |
HAL_ADC_DIFF_AIN6 |
Differential with AIN6 as inv. input |
Definition at line 130 of file hal_adc.h.
{ HAL_ADC_SINGLE = 0x00, HAL_ADC_DIFF_AIN2 = 0x01, HAL_ADC_DIFF_AIN6 = 0x02 } hal_adc_input_mode_t;
An enum describing the ADC's conversion mode.
HAL_ADC_SINGLE_STEP |
Single step conversion |
HAL_ADC_CONTINOUS |
Cont. conversion, def. sampling rate |
Definition at line 139 of file hal_adc.h.
{ HAL_ADC_SINGLE_STEP = 0x00, HAL_ADC_CONTINOUS = 0x01 } hal_adc_conversion_mode_t;
An enum describing the ADC's sampling rate (Continuous conversion).
HAL_ADC_2KSPS |
ADC sampling rate = 2kbps |
HAL_ADC_4KSPS |
ADC sampling rate = 4kbps |
HAL_ADC_8KSPS |
ADC sampling rate = 8kbps |
HAL_ADC_16KSPS |
ADC sampling rate = 16kbps |
Definition at line 147 of file hal_adc.h.
{ HAL_ADC_2KSPS = 0x00, HAL_ADC_4KSPS = 0x01, HAL_ADC_8KSPS = 0x02, HAL_ADC_16KSPS = 0x03 } hal_adc_sampling_rate_t;
An enum describing the ADC's power down delay (Single step conversion).
HAL_ADC_PDD_0US |
ADC power down delay = 0us |
HAL_ADC_PDD_6US |
ADC power down delay = 6us |
HAL_ADC_PDD_24US |
ADC power down delay = 24us |
HAL_ADC_PDD_INF |
ADC power down delay = infinite |
Definition at line 157 of file hal_adc.h.
{ HAL_ADC_PDD_0US = 0x00, HAL_ADC_PDD_6US = 0x01, HAL_ADC_PDD_24US = 0x02, HAL_ADC_PDD_INF = 0x03 } hal_adc_power_down_delay_t;
enum hal_adc_acq_window_t |
An enum describing the ADC's input acquisition window.
HAL_ADC_AQW_075US |
Input acquisition window = 0.75us |
HAL_ADC_AQW_3US |
Input acquisition window = 3us |
HAL_ADC_AQW_12US |
Input acquisition window = 12us |
HAL_ADC_AQW_36US |
Input acquisition window = 36us |
Definition at line 167 of file hal_adc.h.
{ HAL_ADC_AQW_075US = 0x00, HAL_ADC_AQW_3US = 0x01, HAL_ADC_AQW_12US = 0x02, HAL_ADC_AQW_36US = 0x03 } hal_adc_acq_window_t;
enum hal_adc_resolution_t |
An enum describing the ADC's resolution.
HAL_ADC_RES_6BIT |
ADC resolution set to 6 bit |
HAL_ADC_RES_8BIT |
ADC resolution set to 8 bit |
HAL_ADC_RES_10BIT |
ADC resolution set to 10 bit |
HAL_ADC_RES_12BIT |
ADC resolution set to 12 bit |
Definition at line 177 of file hal_adc.h.
{ HAL_ADC_RES_6BIT = 0x00, HAL_ADC_RES_8BIT = 0x01, HAL_ADC_RES_10BIT = 0x02, HAL_ADC_RES_12BIT = 0x03 } hal_adc_resolution_t;
enum hal_adc_data_just_t |
An enum describing the position of output data.
Definition at line 187 of file hal_adc.h.
{ HAL_ADC_JUST_LEFT = 0x00, HAL_ADC_JUST_RIGHT = 0x01 } hal_adc_data_just_t;
enum hal_adc_overflow_t |
An enum describing the ADC's resolution.
HAL_ADC_FLOW_NONE |
No overflow or underflow |
HAL_ADC_FLOW_OVER |
Overflow |
HAL_ADC_FLOW_UNDER |
Underflow |
HAL_ADC_FLOW_BOTH |
Both overflow and underflow |
Definition at line 195 of file hal_adc.h.
{ HAL_ADC_FLOW_NONE = 0x00, HAL_ADC_FLOW_OVER = 0x01, HAL_ADC_FLOW_UNDER = 0x02, HAL_ADC_FLOW_BOTH = 0x03 } hal_adc_overflow_t;
void hal_adc_set_input_channel | ( | hal_adc_input_channel_t | chsel ) |
void hal_adc_set_reference | ( | hal_adc_reference_t | refsel ) |
void hal_adc_set_input_mode | ( | hal_adc_input_mode_t | input_mode ) |
Function to set the ADC single ended or differential mode. This function sets the single ended or differential mode of the ADC.
input_mode | Single ended or differential mode |
void hal_adc_set_conversion_mode | ( | hal_adc_conversion_mode_t | conv_mode ) |
void hal_adc_set_sampling_rate | ( | hal_adc_sampling_rate_t | rate ) |
void hal_adc_set_power_down_delay | ( | hal_adc_power_down_delay_t | pdd ) |
void hal_adc_set_acq_window | ( | hal_adc_acq_window_t | tacq ) |
Function to set the duration of the acquisition window for the ADC. This function sets the duration of the acquisition window.
tacq | Duration of the acquisition window |
void hal_adc_set_resolution | ( | hal_adc_resolution_t | res ) |
void hal_adc_set_data_just | ( | hal_adc_data_just_t | just ) |
void hal_adc_start | ( | void | ) |
uint8_t hal_adc_read_LSB | ( | void | ) |
uint8_t hal_adc_read_MSB | ( | void | ) |
_Bool hal_adc_busy | ( | void | ) |
Function that returns the status of the ADC. This function returns true if the ADC is busy.
FALSE | no conversion in progress |
TRUE | conversion in progress |
< The value of bit 6
hal_adc_overflow_t hal_adc_get_overflow_status | ( | void | ) |