Search code examples
atmelatmelstudioasfsamd21

Using External Vref for SAMD21 DAC


I use SAMD21 Xplained board. I run a DAC example provided by Atmel Studio: DAC_QUICKSTART1

In header file that comes with the example (dac_feature.h), I have following enum:

enum dac_reference {
    /** 1V from the internal band-gap reference*/
    DAC_REFERENCE_INT1V = DAC_CTRLB_REFSEL(0),
    /** Analog V<SUB>CC</SUB> as reference */
    DAC_REFERENCE_AVCC  = DAC_CTRLB_REFSEL(1),
    /** External reference on AREF */
    DAC_REFERENCE_AREF  = DAC_CTRLB_REFSEL(2),
};

Originally, reference voltage is internal 1V voltage. I want to use external reference, so that DAC output can vary from 0V to 5V or so.

My question is: How do I actually set these settings?

In the same file - dac_feature.h, there is a following struct:

struct dac_config {
    /** Reference voltage */
    enum dac_reference reference;
    /** Select DAC output */
    enum dac_output output;
    /** Left adjusted data */
    bool left_adjust;
    /** GCLK generator used to clock the peripheral */
    enum gclk_generator clock_source;
#ifdef FEATURE_DAC_DATABUF_WRITE_PROTECTION
    /** Bypass DATABUF write protection */
    bool databuf_protection_bypass;
#endif
    /** Voltage pump disable */
    bool voltage_pump_disable;
    /**
     * The DAC behaves as in normal mode when the chip enters STANDBY sleep
     * mode
     */
    bool run_in_standby;
#if (SAMC21)
    /** Dither mode enable data */
    bool dither_mode;
#endif
};

Here there is created instance of dac_reference called reference. I assume that this is where it is done, but I am still not sure how.

Any help appreciated.


Solution

  • The DAC's configuration can be defined in a struct dac_config which will then be used as a parameter to dac_init(). enum dac_reference defines the possible values to which dac_config.reference can be set.

    // DAC abstraction struct
    struct dac_module dac_instance;
    
    // DAC parameter struct
    struct dac_config config_dac;
    
    // initialize to defaults
    dac_get_config_defaults(&config_dac);
    
    // set DAC reference to AREF
    config_dac.reference = DAC_REFERENCE_AREF;
    
    // use parameters set above to initialize DAC hardware
    dac_init(&dac_instance, DAC, &config_dac);
    

    The lines above - along with a lot more detail - can be found in the application note AT03244 in chapter 9.1.