|
libeasymcp2221 2.0.3
|
Analog, clock-output, and interrupt-on-change helpers. More...
Go to the source code of this file.
Functions | |
| mcp2221_error_code_t | mcp2221_analog_set_vdd (mcp2221_t *dev, double volts) |
| Store the externally supplied MCP2221 supply voltage. More... | |
| mcp2221_error_code_t | mcp2221_analog_get_vdd (const mcp2221_t *dev, double *volts) |
| Return the configured MCP2221 supply voltage. More... | |
| mcp2221_error_code_t | mcp2221_adc_config (mcp2221_t *dev, const char *ref_str) |
| Configure the ADC voltage reference. More... | |
| mcp2221_error_code_t | mcp2221_adc_read_raw (mcp2221_t *dev, uint16_t out[3]) |
| Read the three MCP2221 ADC channels as raw 10-bit values. More... | |
| mcp2221_error_code_t | mcp2221_adc_read_normalized (mcp2221_t *dev, double out[3]) |
| Read the three ADC channels as normalized values. More... | |
| mcp2221_error_code_t | mcp2221_adc_read_volts (mcp2221_t *dev, double out[3]) |
| Read the three ADC channels as voltages. More... | |
| mcp2221_error_code_t | mcp2221_dac_config (mcp2221_t *dev, const char *ref_str) |
| Configure the DAC voltage reference while preserving its output code. More... | |
| mcp2221_error_code_t | mcp2221_dac_config_out (mcp2221_t *dev, const char *ref_str, int out_code) |
| Configure the DAC voltage reference and optionally its raw output code. More... | |
| mcp2221_error_code_t | mcp2221_dac_write_raw (mcp2221_t *dev, uint8_t code) |
| Write a raw 5-bit DAC output code. More... | |
| mcp2221_error_code_t | mcp2221_dac_write_normalized (mcp2221_t *dev, double normalized) |
| Write a normalized DAC output value. More... | |
| mcp2221_error_code_t | mcp2221_dac_write_volts (mcp2221_t *dev, double volts) |
| Write a DAC output voltage. More... | |
| mcp2221_error_code_t | mcp2221_clock_config (mcp2221_t *dev, int duty_percent, const char *freq_str) |
| Configure the MCP2221 clock output. More... | |
| mcp2221_error_code_t | mcp2221_ioc_read (mcp2221_t *dev, uint8_t *flag) |
| Read the interrupt-on-change flag. More... | |
| mcp2221_error_code_t | mcp2221_ioc_clear (mcp2221_t *dev) |
| Clear the interrupt-on-change flag. More... | |
| mcp2221_error_code_t | mcp2221_ioc_config (mcp2221_t *dev, const char *edge) |
| Configure interrupt-on-change edge detection. More... | |
Analog, clock-output, and interrupt-on-change helpers.
Definition in file mcp2221_analog.h.
| mcp2221_error_code_t mcp2221_adc_config | ( | mcp2221_t * | dev, |
| const char * | ref_str | ||
| ) |
Configure the ADC voltage reference.
Accepted reference strings are "OFF", "VDD", "1.024V", "2.048V", and "4.096V". Matching is case-insensitive.
| [in] | dev | Open MCP2221 device handle. |
| [in] | ref_str | ADC reference selection string. |
| mcp2221_error_code_t mcp2221_adc_read_normalized | ( | mcp2221_t * | dev, |
| double | out[3] | ||
| ) |
Read the three ADC channels as normalized values.
Each raw 10-bit result is divided by 1024.0 to match EasyMCP2221 behavior. The returned range is therefore 0.0 through 1023.0/1024.0 rather than reaching exactly 1.0.
Array index 0 through 2 corresponds to GP1 through GP3.
| [in] | dev | Open MCP2221 device handle. |
| [out] | out | Three-element array receiving normalized ADC values. |
| mcp2221_error_code_t mcp2221_adc_read_raw | ( | mcp2221_t * | dev, |
| uint16_t | out[3] | ||
| ) |
Read the three MCP2221 ADC channels as raw 10-bit values.
The returned array maps channels to pins as follows:
out[0]: ADC channel 0 on GP1out[1]: ADC channel 1 on GP2out[2]: ADC channel 2 on GP3| [in] | dev | Open MCP2221 device handle. |
| [out] | out | Three-element array receiving raw values from 0 through 1023. |
| mcp2221_error_code_t mcp2221_adc_read_volts | ( | mcp2221_t * | dev, |
| double | out[3] | ||
| ) |
Read the three ADC channels as voltages.
The current ADC reference is read from device SRAM. Fixed internal references are resolved automatically. If VDD is selected, a supply voltage must first have been provided with mcp2221_analog_set_vdd(). An OFF reference cannot be converted to volts.
Each raw result is converted as raw / 1024.0 * reference_voltage. Array index 0 through 2 corresponds to GP1 through GP3.
| [in] | dev | Open MCP2221 device handle. |
| [out] | out | Three-element array receiving ADC voltages. |
| mcp2221_error_code_t mcp2221_analog_get_vdd | ( | const mcp2221_t * | dev, |
| double * | volts | ||
| ) |
Return the configured MCP2221 supply voltage.
| [in] | dev | Open MCP2221 device handle. |
| [out] | volts | Receives the previously configured supply voltage. |
| mcp2221_error_code_t mcp2221_analog_set_vdd | ( | mcp2221_t * | dev, |
| double | volts | ||
| ) |
Store the externally supplied MCP2221 supply voltage.
The MCP2221 cannot provide the application with a sufficiently accurate VDD value for ADC/DAC conversion, so the caller supplies it explicitly. The value is stored in the device context and is used whenever a voltage conversion operates with VDD as its reference.
| [in] | dev | Open MCP2221 device handle. |
| [in] | volts | Supply voltage in volts. Must be within the supported MCP2221 supply-voltage range. |
| mcp2221_error_code_t mcp2221_clock_config | ( | mcp2221_t * | dev, |
| int | duty_percent, | ||
| const char * | freq_str | ||
| ) |
Configure the MCP2221 clock output.
Supported duty-cycle values are 0, 25, 50, and 75 percent. Supported frequency strings are "375kHz", "750kHz", "1.5MHz", "3MHz", "6MHz", "12MHz", and "24MHz". Frequency matching is case-insensitive.
| [in] | dev | Open MCP2221 device handle. |
| [in] | duty_percent | Clock duty cycle in percent. |
| [in] | freq_str | Clock frequency selection string. |
| mcp2221_error_code_t mcp2221_dac_config | ( | mcp2221_t * | dev, |
| const char * | ref_str | ||
| ) |
Configure the DAC voltage reference while preserving its output code.
Accepted reference strings are "OFF", "VDD", "1.024V", "2.048V", and "4.096V". Matching is case-insensitive.
| [in] | dev | Open MCP2221 device handle. |
| [in] | ref_str | DAC reference selection string. |
| mcp2221_error_code_t mcp2221_dac_config_out | ( | mcp2221_t * | dev, |
| const char * | ref_str, | ||
| int | out_code | ||
| ) |
Configure the DAC voltage reference and optionally its raw output code.
A negative out_code preserves the current 5-bit DAC value. Values from 0 through 31 set the new output code.
When the reference changes, the helper first turns the DAC reference module off and sets the output value to zero before applying the requested reference and value. This preserves the EasyMCP2221 workaround for the MCP2221 ADC/DAC reference-transition hardware quirk.
| [in] | dev | Open MCP2221 device handle. |
| [in] | ref_str | DAC reference selection string. Accepted values are "OFF", "VDD", "1.024V", "2.048V", and "4.096V" (case-insensitive). |
| [in] | out_code | Raw DAC code from 0 through 31, or any negative value to preserve the current code. |
| mcp2221_error_code_t mcp2221_dac_write_normalized | ( | mcp2221_t * | dev, |
| double | normalized | ||
| ) |
Write a normalized DAC output value.
The value is multiplied by 32 and converted to the 5-bit DAC code. Accepted inputs range from 0.0 through 31.0/32.0 inclusive. Values outside that range, including NaN, are rejected.
| [in] | dev | Open MCP2221 device handle. |
| [in] | normalized | Normalized DAC output value. |
| mcp2221_error_code_t mcp2221_dac_write_raw | ( | mcp2221_t * | dev, |
| uint8_t | code | ||
| ) |
Write a raw 5-bit DAC output code.
The currently configured DAC voltage reference is preserved.
| [in] | dev | Open MCP2221 device handle. |
| [in] | code | Raw DAC code from 0 through 31. |
| mcp2221_error_code_t mcp2221_dac_write_volts | ( | mcp2221_t * | dev, |
| double | volts | ||
| ) |
Write a DAC output voltage.
The current DAC reference is read from device SRAM. Fixed internal references are resolved automatically. If VDD is selected, a supply voltage must first have been provided with mcp2221_analog_set_vdd(). An OFF reference cannot be converted to a voltage.
The largest accepted voltage is 31.0/32.0 of the selected reference voltage. Values between two DAC steps are truncated to the lower raw output code.
| [in] | dev | Open MCP2221 device handle. |
| [in] | volts | Requested DAC output voltage. |
| mcp2221_error_code_t mcp2221_ioc_clear | ( | mcp2221_t * | dev | ) |
Clear the interrupt-on-change flag.
| [in] | dev | Open MCP2221 device handle. |
| mcp2221_error_code_t mcp2221_ioc_config | ( | mcp2221_t * | dev, |
| const char * | edge | ||
| ) |
Configure interrupt-on-change edge detection.
Accepted edge strings are "none", "rising", "falling", and "both". Matching is case-insensitive.
| [in] | dev | Open MCP2221 device handle. |
| [in] | edge | Requested edge-detection mode. |
| mcp2221_error_code_t mcp2221_ioc_read | ( | mcp2221_t * | dev, |
| uint8_t * | flag | ||
| ) |
Read the interrupt-on-change flag.
| [in] | dev | Open MCP2221 device handle. |
| [out] | flag | Receives the interrupt-on-change flag reported by the MCP2221. |