Microchip® Advanced Software Framework

DA conversions on two channels

In this use case, the DAC module is configured for:

  • AVCC as reference, right adjusted channel value
  • Both DAC channels active, no internal output
  • Manually triggered conversions on both channels
  • 2 us conversion intervals
  • 10 us refresh intervals

This use case configures the DAC to perform DA conversions on both channels, with output to their respective pins every time it receives an event on event channel 0.

Setup steps

Prerequisites

  1. System Clock Management
  2. Timer/Counter Driver

Example code

  1. The following configuration must be added to the project (typically to a conf_dac.h file, but it can also be added to your main application file.):
    #define RATE_OF_CONVERSION 500
    #define OUTPUT_DAC DACA
  2. Add to application initialization:
    void tc_init(void)
    {
    tc_enable(&TCC0);
    tc_write_clock_source(&TCC0, TC_CLKSEL_DIV1_gc);
    }
    void dac_init(void)
    {
    struct dac_config conf;
    dac_read_configuration(&OUTPUT_DAC, &conf);
    #ifdef XMEGA_DAC_VERSION_1
    #endif
    dac_write_configuration(&OUTPUT_DAC, &conf);
    dac_enable(&OUTPUT_DAC);
    }

Add to main():

dac_init();

Workflow

  1. Create a function tc_init() to intialize the DAC trigger timer:
    • static void tc_init(void)
      {
      // ...
      }
  2. Enable the clock to the DAC trigger timer:
  3. Configure the DAC trigger timer in normal Waveform Generation mode:
  4. Configure the DAC trigger timer period to overflow at at the specified rate of conversion in Hz:
  5. Configure the DAC trigger timer clock source to use the peripheral bus frequency:
  6. Create a function dac_init() to intialize the DAC:
    • static void dac_init(void)
      {
      // ...
      }
  7. Config struct for the DAC:
  8. Initialize system clock:
  9. Initialize the dac configuration by reading back the configuration from the peripheral:
  10. Create and set configuration:
  11. Enable DAC:
  12. Initialize the clock system, event system, DAC trigger timer, and the DAC:

Usage steps

Example code

Add to, e.g., main loop in application C-file:

while (!(tc_is_overflow(&TCC0))) {};
dac_set_channel_value(&OUTPUT_DAC, DAC_CH0, 410);
dac_set_channel_value(&OUTPUT_DAC, DAC_CH1, 3686);
while (!(tc_is_overflow(&TCC0))) {};
dac_set_channel_value(&OUTPUT_DAC, DAC_CH0, 3686);
dac_set_channel_value(&OUTPUT_DAC, DAC_CH1, 410);

Workflow

  1. Wait for channels to get ready for new values, by waiting for the sample timer to overflow and then clearing the flag:
  2. Set the value of channel 0 to 10% and the other to 90% of maximum:
  3. Wait for channels to get ready for new values, by waiting for the sample timer to overflow and then clearing the flag:
  4. Set the value of channel 0 to 90% and the other to 10% of maximum: