ADC¶
Analog to digital converters, typically used for measuring voltages for sensors, battery voltage, etc.
Defines¶
Name | Value | Description |
---|---|---|
MIRA_ADC_PIN_VDD |
0xFF00 | |
MIRA_ADC_PIN_DISABLED |
0xFF01 |
Types¶
Name | Type | Description |
---|---|---|
mira_adc_value_t |
int16_t |
Enums¶
mira_adc_reference_t¶
Name | Value | Description |
---|---|---|
MIRA_ADC_REF_VDD |
||
MIRA_ADC_REF_INT_3_6V |
Structs¶
mira_adc_context_t¶
Type | Name | Description |
---|---|---|
uint16_t |
gpio_pin_p | |
uint16_t |
gpio_pin_n | |
mira_adc_reference_t |
reference | |
mira_bool_t |
is_measuring | |
mira_adc_value_t |
value | |
process |
measuring_process |
Struct containing the context of an ADC channel.
Do not change this struct directly, use the modifier methods mira_adc_set_*().
Functions¶
mira_adc_init¶
mira_status_t mira_adc_init( mira_adc_context_t* context);
Initialize the ADC module and an ADC context with default values. Use this to set up the context block with default parameters and initialize the ADC module.
Parameters
Parameter | Description |
---|---|
context |
Configuration block for the ADC. |
Return
Status of the operation.
Value | Description |
---|---|
MIRA_SUCCESS |
The initialization was successful. |
mira_adc_uninit¶
mira_status_t mira_adc_uninit( mira_adc_context_t* context);
Uninitialize the ADC module. Uninitializes the ADC module and aborts on-going measurement.
Parameters
Parameter | Description |
---|---|
context |
Configuration block for the ADC. |
Return
Status of the operation.
Value | Description |
---|---|
MIRA_SUCCESS |
The initialization was successful. |
mira_adc_set_source_supply¶
mira_status_t mira_adc_set_source_supply( mira_adc_context_t* context);
Set the source of the ADC to the supply voltage.
Parameters
Parameter | Description |
---|---|
context |
Configuration block for the ADC. |
Return
Status of the operation.
Value | Description |
---|---|
MIRA_SUCCESS |
The operation was successful. |
mira_adc_set_source_single¶
mira_status_t mira_adc_set_source_single( mira_adc_context_t* context, mira_gpio_pin_t pin);
Set the source of the ADC to a single ended measurement.
Parameters
Parameter | Description |
---|---|
context |
Configuration block for the ADC. |
pin |
GPIO pin for the source (not analog pin number). |
Return
Status of the operation.
Value | Description |
---|---|
MIRA_SUCCESS |
The operation was successful. No error on wrong pin selection, mira_adc_measurement_start() detects that instead. |
mira_adc_set_source_diff¶
mira_status_t mira_adc_set_source_diff( mira_adc_context_t* context, mira_gpio_pin_t pin_p, mira_gpio_pin_t pin_n);
Set the source of the ADC to a differential measurement. If CPU doesn't support differential measurements, this will be emulated by two consecutive measurements.
Parameters
Parameter | Description |
---|---|
context |
Configuration block for the ADC. |
pin_p |
GPIO pin for the positive source (not analog pin number). |
pin_n |
GPIO pin for the negative soruce (not analog pin number). |
Return
Status of the operation.
Value | Description |
---|---|
MIRA_SUCCESS |
The operation was successful. No error on wrong pin selection, mira_adc_measurement_start() detects that instead. |
mira_adc_set_reference¶
mira_status_t mira_adc_set_reference( mira_adc_context_t* context, mira_adc_reference_t reference);
Set the reference voltage of the analog conversion. Sets the voltage that maps to the maximum voltage of the conversion.
Parameters
Parameter | Description |
---|---|
context |
Configuration block for the ADC. |
reference |
The reference for the conversion. |
Return
Status of the operation.
Value | Description |
---|---|
MIRA_SUCCESS |
The operation was successful. |
MIRA_ERROR_NOT_SUPPORTED |
The reference voltage is not supported by the device. |
mira_adc_measurement_start¶
mira_status_t mira_adc_measurement_start( mira_adc_context_t* context);
Start a measurement.
mira_adc_measurement_in_progress()
returns MIRA_FALSE when the measurement is finished.
Return
mira_status_t
Correct procedure for measurement:
mira_status_t status; mira_value_t value; status = mira_adc_measurement_start(&context); if(status == MIRA_SUCCESS) { PROCESS_WAIT_UNTIL(!mira_adc_measurement_in_progress(&context)); status = mira_adc_measurement_finish(&context, &value); ... measurement result and status ... } else { ... handle error ... }
Call mira_adc_measurement_finish()
for every successful mira_adc_measurement_start()
call.
Parameters
Parameter | Description |
---|---|
context |
Configuration block for the ADC. |
Return
Status of the operation.
Value | Description |
---|---|
MIRA_SUCCESS |
The operation was successful. |
MIRA_ERROR_NOT_INITIALIZED |
The ADC module is not initialized. |
MIRA_ADC_ERROR_INVALID_PIN |
A pin is invalid. |
MIRA_ADC_ERROR_CHANNEL_BUSY |
A measurement is already in progress. |
mira_adc_measurement_in_progress¶
mira_bool_t mira_adc_measurement_in_progress( mira_adc_context_t* context);
Check if the measurement is in progress.
Parameters
Parameter | Description |
---|---|
context |
Configuration block for the ADC. |
Return
Status of measurement.
Value | Description |
---|---|
MIRA_TRUE |
A measurement is in progress. |
MIRA_FALSE |
No measurement is in progress. |
mira_adc_measurement_finish¶
mira_status_t mira_adc_measurement_finish( mira_adc_context_t* context, mira_adc_value_t* value);
Finish the conversion and get the result.
Call this for each successful mira_adc_measurement_start()
call, in order to free the ADC for other measurements.
Parameters
Parameter | Description |
---|---|
context |
Configuration block for the ADC. |
value |
Storage of the measurement value. Only set if successful. |
Return
Status of the operation.
Value | Description |
---|---|
MIRA_SUCCESS |
The operation was successful. |
MIRA_ADC_ERROR_NO_MEASUREMENT |
Measurement failed to start. |