Microchip® Advanced Software Framework

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Digital to Analog Converter (DAC)

See Quick start guide for XMEGA DAC.

This is a driver for versions 1 and 2, introduced with the XMEGA AU family, of the AVR XMEGA DAC. It provides functions for enabling, disabling and configuring the DAC and its channels.

Note that functions for configuring refresh interval and conversion timing are not present for version 2 of the DAC, since it does not need those settings. The symbols XMEGA_DAC_VERSION_1 and XMEGA_DAC_VERSION_2 can be used to tell, at compile time, which DAC the driver is compiled for.

The API functions and definitions can be divided in two groups:

The API makes use of a structure that contains the configuration. This structure must be set up before the configuration is written to a DAC module.

After the DAC has been configured, it must be enabled before any conversions can be done or any of its channel values be changed.

Precondition
The functions for creating/changing configurations are not protected against interrupts. The functions that read from or write to the DAC's registers are protected.

Dependencies

This driver depends on the following modules:

Note
If both DAC channels are active, and one of them is run at maximum conversion rate, automatic refresh can fail since the triggered conversions have higher priority than the refresh.

Modules

 
 Quick Start Guide(s)
 In this section you can find a list of all Quick Start guides related to the Digital to Analog Converter (DAC).
 

Data Structures

struct  dac_config
 DAC configuration. More...
 

Macros

#define XMEGA_DAC_VERSION_1   true
 

Enumerations

enum  dac_adjust {
  DAC_ADJ_RIGHT,
  DAC_ADJ_LEFT = DAC_LEFTADJ_bm
}
 DAC value adjust settings. More...
 
enum  dac_reference {
  DAC_REF_BANDGAP = DAC_REFSEL_INT1V_gc,
  DAC_REF_AVCC = DAC_REFSEL_AVCC_gc,
  DAC_REF_AREFA = DAC_REFSEL_AREFA_gc,
  DAC_REF_AREFB = DAC_REFSEL_AREFB_gc
}
 DAC reference settings. More...
 
static void dac_enable_clock (DAC_t *dac)
 Enable peripheral clock for DAC. More...
 
static void dac_disable_clock (DAC_t *dac)
 Disable peripheral clock for DAC. More...
 

DAC channel masks

#define DAC_CH0   (1 << 0)
 DAC channel 0. More...
 
#define DAC_CH1   (1 << 1)
 DAC channel 1. More...
 

DAC module and channel management

void dac_enable (DAC_t *dac)
 Enable DAC. More...
 
void dac_disable (DAC_t *dac)
 Disable DAC. More...
 
bool dac_is_enabled (DAC_t *dac)
 Check if DAC is enabled. More...
 
static __always_inline uint8_t dac_get_calibration_data (enum dac_calibration_data cal)
 Get calibration data. More...
 
static __always_inline void dac_set_channel_value (DAC_t *dac, uint8_t ch_mask, uint16_t val)
 Set value of DAC channel. More...
 
static __always_inline uint16_t dac_get_channel_value (DAC_t *dac, uint8_t ch_mask)
 Get value of DAC channel. More...
 
static __always_inline bool dac_channel_is_ready (DAC_t *dac, uint8_t ch_mask)
 Check whether DAC channel(s) are ready for data. More...
 
static __always_inline void dac_wait_for_channel_ready (DAC_t *dac, uint8_t ch_mask)
 Wait for DAC channel(s) to get ready for data. More...
 

DAC module and channel configuration

void dac_write_configuration (DAC_t *dac, struct dac_config *conf)
 Write configuration to DAC. More...
 
void dac_read_configuration (DAC_t *dac, struct dac_config *conf)
 Read configuration from DAC. More...
 
static __always_inline void dac_set_active_channel (struct dac_config *conf, uint8_t ch_mask, uint8_t int_out_ch_mask)
 Set active DAC channel(s) and output(s) More...
 
static __always_inline void dac_set_refresh_interval (struct dac_config *conf, uint8_t us)
 Set automatic refresh timing interval. More...
 
static __always_inline void dac_set_conversion_interval (struct dac_config *conf, uint8_t us)
 Set conversion timing interval. More...
 
static __always_inline void dac_set_conversion_parameters (struct dac_config *conf, enum dac_reference ref, enum dac_adjust adj)
 Set DA conversion parameters. More...
 
static __always_inline void dac_set_conversion_trigger (struct dac_config *conf, uint8_t ch_mask, uint8_t ev_ch)
 Set DA conversions triggers. More...
 

#define XMEGA_DAC_VERSION_1   true

enum dac_adjust

DAC value adjust settings.

Enumerator
DAC_ADJ_RIGHT 

Right-adjusted value.

DAC_ADJ_LEFT 

Left-adjusted value.

DAC reference settings.

Enumerator
DAC_REF_BANDGAP 

1 V from internal bandgap reference.

DAC_REF_AVCC 

Analog VCC as reference.

DAC_REF_AREFA 

External reference on AREF pin on port A.

DAC_REF_AREFB 

External reference on AREF pin on port B.

static __always_inline bool dac_channel_is_ready ( DAC_t *  dac,
uint8_t  ch_mask 
)
static

Check whether DAC channel(s) are ready for data.

Checks if the data registers for all of the masked DAC channels are ready for a new value. This indicates that a conversion with the current value has been completed.

Parameters
dacPointer to DAC module.
ch_maskMask of DAC channel(s) to check whether are ready:
  • DAC_CHn , where n specifies the channel. (These can be OR'ed together.)
Return values
trueif DAC channels are ready.
falseif a DAC channel is not ready.

References Assert.

Referenced by dac_wait_for_channel_ready().

void dac_disable ( DAC_t *  dac)

Disable DAC.

Disables the DAC, stopping all conversions.

Parameters
dacPointer to DAC module.

References cpu_irq_restore(), cpu_irq_save(), dac_disable_clock(), SLEEPMGR_IDLE, and sleepmgr_unlock_mode().

static void dac_disable_clock ( DAC_t *  dac)
static

Disable peripheral clock for DAC.

Disables the peripheral clock for the specified DAC, unless the enable count indicates that it should be left enabled.

Parameters
dacPointer to DAC module.

References Assert, SYSCLK_DAC, sysclk_disable_module(), SYSCLK_PORT_A, and SYSCLK_PORT_B.

Referenced by dac_disable(), dac_read_configuration(), and dac_write_configuration().

static void dac_enable_clock ( DAC_t *  dac)
static

Enable peripheral clock for DAC.

Enables the peripheral clock for the specified DAC, unless it is already enabled.

Parameters
dacPointer to DAC module.

References Assert, SYSCLK_DAC, sysclk_enable_module(), SYSCLK_PORT_A, and SYSCLK_PORT_B.

Referenced by dac_enable(), dac_read_configuration(), and dac_write_configuration().

static __always_inline uint8_t dac_get_calibration_data ( enum dac_calibration_data  cal)
static

Get calibration data.

Returns the specified calibration data from the production signature row.

Parameters
calIdentifier for calibration data to get.
Returns
Byte with specified calibration data.

References nvm_read_production_signature_row().

Referenced by dac_write_configuration().

static __always_inline uint16_t dac_get_channel_value ( DAC_t *  dac,
uint8_t  ch_mask 
)
static

Get value of DAC channel.

Parameters
dacPointer to DAC module.
ch_maskMask of DAC channel from which to retrieve the value:
  • DAC_CHn , where n specifies the channel.
Returns
Current 12-bit value of the DAC channel.

References Assert, cpu_irq_restore(), cpu_irq_save(), and ilog2().

bool dac_is_enabled ( DAC_t *  dac)

Check if DAC is enabled.

Parameters
dacPointer to DAC module.

References Assert.

static __always_inline void dac_set_active_channel ( struct dac_config conf,
uint8_t  ch_mask,
uint8_t  int_out_ch_mask 
)
static

Set active DAC channel(s) and output(s)

Enables the masked DAC channels and configures their output. Either of the channels may be configured for internal output, but not both. Implicitly, channels that are not masked for internal output are configured for output on their respective pins.

Parameters
confPointer to DAC configuration.
ch_maskMask of DAC channel(s) to enable:
  • DAC_CHn , where n specifies the channel. (These can be OR'ed together.)
int_out_ch_maskMask of DAC channel to connect to internal output:
  • 0 to connect all channels to output pin.
  • DAC_CHn , where n specifies the channel.
Note
If both channels of a version 1 DAC are enabled, the refresh rate must also be configured with dac_set_refresh_interval().

References Assert, dac_config::ctrla, dac_config::ctrlb, DAC_CH0, DAC_CH1, and ilog2().

Referenced by main(), run_differential_12bit_conversion_test(), run_differential_12bit_with_gain_conversion_test(), run_dual_channel_12bit_conversion_test(), run_dual_channel_8bit_conversion_test(), run_single_channel_12bit_conversion_test(), run_single_channel_8bit_conversion_test(), run_single_ended_12bit_conversion_test(), run_single_ended_8bit_conversion_test(), and sound_init().

static __always_inline void dac_set_channel_value ( DAC_t *  dac,
uint8_t  ch_mask,
uint16_t  val 
)
static

Set value of DAC channel.

Parameters
dacPointer to DAC module.
ch_maskMask of DAC channel for which to set the value:
  • DAC_CHn , where n specifies the channel.
val12-bit value to set. (Must be adjusted as configured in the conversion parameters with dac_set_conversion_parameters.)
Note
This function has no effect if the DAC is not enabled.

References Assert, cpu_irq_restore(), cpu_irq_save(), and ilog2().

Referenced by main(), run_differential_12bit_conversion_test(), run_differential_12bit_with_gain_conversion_test(), run_dual_channel_12bit_conversion_test(), run_dual_channel_8bit_conversion_test(), run_single_channel_12bit_conversion_test(), run_single_channel_8bit_conversion_test(), run_single_ended_12bit_conversion_test(), run_single_ended_8bit_conversion_test(), and sample_callback().

static __always_inline void dac_set_conversion_interval ( struct dac_config conf,
uint8_t  us 
)
static

Set conversion timing interval.

Sets the timing interval from a conversion is completed until a new one can be started. This settings depends on whether single- or dual-channel mode is used, and so the channels should be configured before this function is called to ensure the proper configuration.

This interval is rounded downward to the closest setting.

Parameters
confPointer to DAC configuration.
usMicroseconds between conversions:
  • 1 to 127 for single-channel.
  • 2 to 127 for dual-channel.
Note
The conversion interval should not exceed 127.

References Assert, dac_config::ctrlb, ilog2(), and sysclk_get_per_hz().

Referenced by main(), run_differential_12bit_conversion_test(), run_differential_12bit_with_gain_conversion_test(), run_dual_channel_12bit_conversion_test(), run_dual_channel_8bit_conversion_test(), run_single_channel_12bit_conversion_test(), run_single_channel_8bit_conversion_test(), run_single_ended_12bit_conversion_test(), run_single_ended_8bit_conversion_test(), and sound_init().

static __always_inline void dac_set_conversion_parameters ( struct dac_config conf,
enum dac_reference  ref,
enum dac_adjust  adj 
)
static

Set DA conversion parameters.

Sets up the voltage reference and channel value adjust, i.e., whether the 12-bit value is shifted to the most significant (left) or least significant (right) bits in the channels' 16-bit data registers.

Parameters
confPointer to DAC configuration.
refDAC voltage reference setting.
adjDAC value adjust setting.

References dac_config::ctrlc.

Referenced by main(), run_differential_12bit_conversion_test(), run_differential_12bit_with_gain_conversion_test(), run_dual_channel_12bit_conversion_test(), run_dual_channel_8bit_conversion_test(), run_single_channel_12bit_conversion_test(), run_single_channel_8bit_conversion_test(), run_single_ended_12bit_conversion_test(), run_single_ended_8bit_conversion_test(), and sound_init().

static __always_inline void dac_set_conversion_trigger ( struct dac_config conf,
uint8_t  ch_mask,
uint8_t  ev_ch 
)
static

Set DA conversions triggers.

Sets up event-triggering of conversions on the masked channels, and manual triggering on the others. Conversions are manually triggered by setting new channel values, while events only trigger new conversions if a new channel value was set since the last conversion.

Parameters
confPointer to DAC configuration.
ev_chEvent channel to use.
ch_maskMask of DAC channel(s) to use event-triggering on:
  • DAC_CHn , where n specifies the channel. (These can be OR'ed together.)
  • 0 to disable.
Note
The specified event channel is common to all DAC channels on which event-triggering of conversions is configured.

References Assert, dac_config::ctrlb, and dac_config::evctrl.

Referenced by main(), run_differential_12bit_conversion_test(), run_differential_12bit_with_gain_conversion_test(), run_dual_channel_12bit_conversion_test(), run_dual_channel_8bit_conversion_test(), run_single_channel_12bit_conversion_test(), run_single_channel_8bit_conversion_test(), run_single_ended_12bit_conversion_test(), run_single_ended_8bit_conversion_test(), and sound_init().

static __always_inline void dac_set_refresh_interval ( struct dac_config conf,
uint8_t  us 
)
static

Set automatic refresh timing interval.

Sets or disables the automatic refresh timing interval for dual-channel mode of the DAC. If it is disabled, the DAC channels must be manually refreshed by updating the channel values and triggering new conversions.

This interval is rounded downward to the closest setting.

Parameters
confPointer to DAC configuration.
usMicroseconds between channel refresh:
  • 1 to 30 to enable.
  • 0 to disable.
Note
The refresh interval should not exceed 30 us.

References Assert, ilog2(), and sysclk_get_per_hz().

Referenced by main(), run_differential_12bit_conversion_test(), run_differential_12bit_with_gain_conversion_test(), run_dual_channel_12bit_conversion_test(), run_dual_channel_8bit_conversion_test(), run_single_channel_12bit_conversion_test(), run_single_channel_8bit_conversion_test(), run_single_ended_12bit_conversion_test(), and run_single_ended_8bit_conversion_test().

static __always_inline void dac_wait_for_channel_ready ( DAC_t *  dac,
uint8_t  ch_mask 
)
static

Wait for DAC channel(s) to get ready for data.

Busy-waits for all of the masked DAC channels' data registers to become ready for new values.

Parameters
dacPointer to DAC module.
ch_maskMask of DAC channel(s) to wait for data ready:
  • DAC_CHn , where n specifies the channel. (These can be OR'ed together.)

References Assert, and dac_channel_is_ready().

Referenced by main(), run_differential_12bit_conversion_test(), run_differential_12bit_with_gain_conversion_test(), run_dual_channel_12bit_conversion_test(), run_dual_channel_8bit_conversion_test(), run_single_channel_12bit_conversion_test(), run_single_channel_8bit_conversion_test(), run_single_ended_12bit_conversion_test(), and run_single_ended_8bit_conversion_test().

void dac_write_configuration ( DAC_t *  dac,
struct dac_config conf 
)

Write configuration to DAC.

Disables the DAC before writing the specified configuration and factory calibration values to it. If the DAC was enabled upon entry of the function, it is enabled upon function return.

Parameters
dacPointer to DAC module.
confPointer to DAC configuration.

References Assert, cpu_irq_restore(), cpu_irq_save(), dac_config::ctrla, dac_config::ctrlb, dac_config::ctrlc, dac_disable_clock(), dac_enable_clock(), dac_get_calibration_data(), dac_config::evctrl, and UNUSED.

Referenced by main(), run_differential_12bit_conversion_test(), run_differential_12bit_with_gain_conversion_test(), run_dual_channel_12bit_conversion_test(), run_dual_channel_8bit_conversion_test(), run_single_channel_12bit_conversion_test(), run_single_channel_8bit_conversion_test(), run_single_ended_12bit_conversion_test(), run_single_ended_8bit_conversion_test(), and sound_init().