Microchip® Advanced Software Framework

Quick Start Guide for DAC - Basic

In this use case, the DAC will be configured with the following settings:

  • Analog VCC as reference
  • Internal output disabled
  • Drive the DAC output to the VOUT pin
  • Right adjust data
  • The output buffer is disabled when the chip enters STANDBY sleep mode

Quick Start

Prerequisites

There are no special setup requirements for this use-case.

Code

Add to the main application source file, outside of any functions:

struct dac_module dac_instance;

Copy-paste the following setup code to your user application:

void configure_dac(void)
{
struct dac_config config_dac;
dac_init(&dac_instance, DAC, &config_dac);
}
void configure_dac_channel(void)
{
struct dac_chan_config config_dac_chan;
dac_chan_get_config_defaults(&config_dac_chan);
dac_chan_set_config(&dac_instance, DAC_CHANNEL_0, &config_dac_chan);
dac_chan_enable(&dac_instance, DAC_CHANNEL_0);
}

Add to user application initialization (typically the start of main()):

configure_dac();
configure_dac_channel();

Workflow

  1. Create a module software instance structure for the DAC module to store the DAC driver state while 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. Configure the DAC module.
    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. Initialize the DAC configuration struct with the module's default values.
      Note
      This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
  3. Configure the DAC channel.
    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. 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. Configure the DAC channel with the desired channel settings.
      dac_chan_set_config(&dac_instance, DAC_CHANNEL_0, &config_dac_chan);
    4. Enable the DAC channel so that it can output a voltage.
      dac_chan_enable(&dac_instance, DAC_CHANNEL_0);
  4. Enable the DAC module.
    dac_enable(&dac_instance);

Use Case

Code

Copy-paste the following code to your user application:

uint16_t i = 0;
while (1) {
dac_chan_write(&dac_instance, DAC_CHANNEL_0, i);
if (++i == 0x3FF) {
i = 0;
}
}

Workflow

  1. Create a temporary variable to track the current DAC output value.
    uint16_t i = 0;
  2. Enter an infinite loop to continuously output new conversion values to the DAC.
    while (1) {
  3. Write the next conversion value to the DAC, so that it will be output on the device's DAC analog output pin.
    dac_chan_write(&dac_instance, DAC_CHANNEL_0, i);
  4. Increment and wrap the DAC output conversion value, so that a ramp pattern will be generated.
    if (++i == 0x3FF) {
    i = 0;
    }