Configure the DAC
- Create a module software instance structure for the DAC module to store the DAC driver state while it is in use.
struct
dac_module dac_instance;
Note: This should never go out of scope as long as the module is in use. In most cases, this should be global. - Configure the DAC module.
- Create a DAC module configuration struct, which can be filled out to adjust the configuration of a physical DAC peripheral.
struct
dac_config config_dac;
- Initialize the DAC configuration struct with the module's default values.
dac_get_config_defaults(&config_dac);
Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings. - Set extra DAC configurations.
#if (SAML21)
config_dac.reference = DAC_REFERENCE_INTREF;
#else
config_dac.reference = DAC_REFERENCE_AVCC;
#endif
- Set DAC configurations to DAC instance.
dac_init(&dac_instance, DAC, &config_dac);
- Enable the DAC module so that channels can be configured.
dac_enable(&dac_instance);
- Configure the DAC channel.
- Create a DAC channel configuration struct, which can be filled out to adjust the configuration of a physical DAC output channel.
struct
dac_chan_config config_dac_chan;
- Initialize the DAC channel configuration struct with the module's default values.
dac_chan_get_config_defaults(&config_dac_chan);
Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings. - Configure the DAC channel with the desired channel settings.
dac_chan_set_config(&dac_instance, DAC_CHANNEL_0, &config_dac_chan);
- Enable the DAC channel so that it can output a voltage.
dac_chan_enable(&dac_instance, DAC_CHANNEL_0);