Hardware Abstraction Layer (HAL)
DAC (Digital to Analog Converter)

General Description

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:

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;
uint16_t dac_read;
// 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
dac_read = cyhal_dac_read(&my_dac_obj);
if (dac_read == 0xFFFF)
{
// Insert code
}
}
// Release DAC object after use by calling cyhal_dac_free, this de-initializes the DAC
// (including its output)
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 lo...
cy_rslt_t cyhal_dac_init(cyhal_dac_t *obj, cyhal_gpio_t pin)
Initialize the DAC peripheral.
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 ca...
DAC object.
Definition: cyhal_hw_types.h:646
@ P9_6
Port 9 Pin 6.
Definition: cyhal_psoc6_01_116_bga_ble.h:110
uint32_t cy_rslt_t
Provides the result of an operation as a structured bitfield.
Definition: cy_result.h:426
#define CY_RSLT_SUCCESS
cy_rslt_t return value indicating success
Definition: cy_result.h:453

API Reference

 DAC HAL Results
 DAC specific return codes.
 

Enumerations

enum  cyhal_dac_ref_t {
  CYHAL_DAC_REF_VDDA ,
  CYHAL_DAC_REF_VREF
}
 Reference choices for the DAC. More...
 

Functions

cy_rslt_t cyhal_dac_init (cyhal_dac_t *obj, cyhal_gpio_t pin)
 Initialize the DAC peripheral. More...
 
cy_rslt_t cyhal_dac_init_cfg (cyhal_dac_t *obj, const cyhal_dac_configurator_t *cfg)
 Initialize the DAC peripheral using a configurator generated configuration struct. More...
 
void cyhal_dac_free (cyhal_dac_t *obj)
 Release the dac object. More...
 
cy_rslt_t cyhal_dac_set_reference (cyhal_dac_t *obj, cyhal_dac_ref_t ref)
 Set the DAC voltage reference. More...
 
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) More...
 
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. More...
 
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). More...
 
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. More...
 

Enumeration Type Documentation

◆ cyhal_dac_ref_t

Reference choices for the DAC.

Enumerator
CYHAL_DAC_REF_VDDA 

Analog supply (default)

CYHAL_DAC_REF_VREF 

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

Function Documentation

◆ cyhal_dac_init()

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
[out]objPointer to a DAC object. The caller must allocate the memory for this object but the init function will initialize its contents.
[in]pinThe 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

◆ cyhal_dac_init_cfg()

cy_rslt_t cyhal_dac_init_cfg ( cyhal_dac_t obj,
const cyhal_dac_configurator_t cfg 
)

Initialize the DAC peripheral using a configurator generated configuration struct.

Parameters
[out]objPointer to a DAC object. The caller must allocate the memory for this object but the init function will initialize its contents.
[in]cfgConfiguration structure generated by a configurator.
Returns
The status of the init request

◆ cyhal_dac_free()

void cyhal_dac_free ( cyhal_dac_t obj)

Release the dac object.

Parameters
[in,out]objThe dac object

◆ cyhal_dac_set_reference()

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
objThe DAC object
refThe selected voltage reference.
Returns
The status of the reference selection request

◆ cyhal_dac_write()

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
[in]objThe dac object
[in]valueThe 16-bit output value to set

◆ cyhal_dac_write_mv()

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
[in]objThe dac object
[in]valueThe number of millivolts to output set.
Returns
The status of the write request.

◆ cyhal_dac_read()

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
[in]objThe dac object
Returns
The 16-bit output value

◆ cyhal_dac_set_power()

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
[in]objdac object
[in]powerThe power level to set
Returns
The status of the set power request