Configure the DAC

  1. 1.
    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.
  2. 2.
    Configure the DAC module.
    1. 2.1.
      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;
      
    2. 2.2.
      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.
    3. 2.3.
      Set extra DAC configurations.
      #if (SAML21)
          config_dac.reference = DAC_REFERENCE_INTREF;
      #else
          config_dac.reference = DAC_REFERENCE_AVCC;
      #endif
      
    4. 2.4.
      Set DAC configurations to DAC instance.
      dac_init(&dac_instance, DAC, &config_dac);
      
    5. 2.5.
      Enable the DAC module so that channels can be configured.
      dac_enable(&dac_instance);
      
  3. 3.
    Configure the DAC channel.
    1. 3.1.
      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;
      
    2. 3.2.
      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.
    3. 3.3.
      Configure the DAC channel with the desired channel settings.
      dac_chan_set_config(&dac_instance, DAC_CHANNEL_0, &config_dac_chan);
      
    4. 3.4.
      Enable the DAC channel so that it can output a voltage.
      dac_chan_enable(&dac_instance, DAC_CHANNEL_0);