# DAC (Digital to Analog Converter)¶

group group_hal_dac

High level interface for interacting with the digital to analog converter (DAC).

This block drives a pin with a firmware configurable voltage. See the device datasheet for details on which pins support DAC output.

Features

The DAC driver provides APIs to:

• Configure and work with the DAC hardware

• Update the DAC output value

• Read the DAC output voltage setting

note

The cyhal_dac_write and cyhal_dac_read APIs are defined as 16-bit, which may not match the underlying hardware. The functions will linearly scale the 16-bit value to and from a resolution that hardware is capable of outputting. For instance, if the device supports 12-bit resolution, only the 12 MSBs of the 16-bit value will be used. Refer to BSP Settings section in the kit's BSP API Reference Manual for details on the DAC's output range.

Quick Start

Call cyhal_dac_init to initialize a DAC instance by providing the DAC object ( obj ) and an output pin ( pin ).

See Use case 1: Simple DAC initialization.

Code Snippets

Use case 1: Simple DAC initialization

The following snippet initializes a DAC resource and assigns the output to the specified pin using cyhal_dac_init. cyhal_dac_write is used to set the DAC output value. cyhal_dac_read is used to read back DAC register.

    cyhal_dac_t my_dac_obj;
cy_rslt_t   dac_result;

// Full Scale value for the DAC
uint16_t dac_value = 0xFFFF;

// Initialize DAC, set Pin 9[6] as the DAC output
dac_result = cyhal_dac_init(&my_dac_obj, P9_6);

// Check the return of cyhal_dac_init to confirm initialization was successful
if (dac_result == CY_RSLT_SUCCESS)
{
// Write the 16 bit value as DAC input
cyhal_dac_write(&my_dac_obj, dac_value);

// Return the 16 bit DAC register value

{
// Insert code
}
}

// Release DAC object after use by calling cyhal_dac_free, this de-initializes the DAC
// (including its output)


Enums

enum cyhal_dac_ref_t

cyhal_dac_ref_t: Reference choices for the DAC.

Values:

enumerator CYHAL_DAC_REF_VDDA

Analog supply (default)

enumerator CYHAL_DAC_REF_VREF

Internal reference. See the BSP documentation for the reference value.

Functions

cy_rslt_t cyhal_dac_init(cyhal_dac_t *obj, cyhal_gpio_t pin)

Initialize the DAC peripheral.

By default, the reference will be set to CYHAL_DAC_REF_VDDA.

Parameters
• obj[out] Pointer to a DAC object. The caller must allocate the memory for this object but the init function will initialize its contents.

• pin[in] The dac pin name

Returns

The status of the init request. On success returns CY_RSLT_SUCCESS

.

On failure, a problem specific error code will be returned. This error could be from the HAL or lower level driver.

For all other return codes, please refer to device driver documentation available in the BSP landing page

void cyhal_dac_free(cyhal_dac_t *obj)

Release the dac object.

Parameters

obj[inout] The dac object

cy_rslt_t cyhal_dac_set_reference(cyhal_dac_t *obj, cyhal_dac_ref_t ref)

Set the DAC voltage reference.

This determines the highest value that the DAC can output.

Parameters
• obj – The DAC object

• ref – The selected voltage reference.

Returns

The status of the reference selection request

void cyhal_dac_write(const cyhal_dac_t *obj, uint16_t value)

Set the output voltage, as a normalized unsigned 16-bit value (where 0 is the lowest value the DAC can output and 0xFFFF is the highest)

note

While the input value is 16 bits, the actual resolution that can be achieved is dependent on what the underlying hardware supports. See the device datasheet for details.

Parameters
• obj[in] The dac object

• value[in] The 16-bit output value to set

cy_rslt_t cyhal_dac_write_mv(const cyhal_dac_t *obj, uint16_t value)

Set the output voltage, as an unsigned number of millivolts.

note

Depending on the resolution of the underlying hardware, it may not be possible to achieve the precise voltage requested. See the device datasheet for more details about the DAC resolution.

It is an error to specify a value that is outside of the DAC's operating range.

Parameters
• obj[in] The dac object

• value[in] The number of millivolts to output set.

Returns

The status of the write request.

uint16_t cyhal_dac_read(cyhal_dac_t *obj)

Read the current DAC output voltage setting, as a normalized unsigned 16-bit value (where 0 is the lowest value the DAC can output and 0xFFFF is the highest).

Note: Depending on the precision of the underlying hardware, this may not precisely match the most recent value passed in to cyhal_dac_write.

Parameters

obj[in] The dac object

Returns

The 16-bit output value

cy_rslt_t cyhal_dac_set_power(cyhal_dac_t *obj, cyhal_power_level_t power)

Changes the current operating power level of the DAC.

If the power level is set to CYHAL_POWER_LEVEL_OFF, the DAC will be powered-off but it will retain its configuration, so it is not necessary to reconfigure it when changing the power level from CYHAL_POWER_LEVEL_OFF to any other value.

Parameters
• obj[in] dac object

• power[in] The power level to set

Returns

The status of the set power request
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.160707026720047, "perplexity": 9713.391945600843}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2022-21/segments/1652662531352.50/warc/CC-MAIN-20220520030533-20220520060533-00619.warc.gz"}