In this use case, the DAC will convert 16 samples using interrupt driven conversion.
When all samples have been sampled, a callback will be called that signals the main application that conversion is compete.
The DAC will be set up as follows:
- 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
- DAC conversion is started with RTC overflow event
Setup
Prerequisites
There are no special setup requirements for this use case.
Code
Add to the main application source file, outside of any functions:
struct rtc_module rtc_instance;
static volatile bool transfer_is_done = false;
static uint16_t dac_data[DATA_LENGTH];
Callback function:
{
transfer_is_done = true;
}
Copy-paste the following setup code to your user application:
void configure_rtc_count(void)
{
#ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED
config_rtc_count.continuously_update = true;
#endif
rtc_event.generate_event_on_overflow = true;
}
void configure_dac(void)
{
#if (SAML21)
#else
dac_instance.start_on_event = true;
#endif
dac_init(&dac_instance, DAC, &config_dac);
#if (SAML21)
#else
{ .on_event_start_conversion = true };
#endif
}
void configure_dac_channel(void)
{
&config_dac_chan);
}
Define a data length variables and add to user application (typically the start of main()
):
Add to user application initialization (typically the start of main()
):
configure_rtc_count();
configure_dac();
configure_dac_channel();
configure_event_resource();
for (i = 0;i < DATA_LENGTH;i++) {
dac_data[i] = 0x3f * i;
}
Workflow
- Create a module software instance structure for the DAC module to store the DAC driver state while in use.
- Note
- This should never go out of scope as long as the module is in use. In most cases, this should be global.
- RTC module is used as the event trigger for DAC in this case, create a module software instance structure for the RTC module to store the RTC driver state.
struct rtc_module rtc_instance;
- Note
- This should never go out of scope as long as the module is in use. In most cases, this should be global.
- Create a buffer for the DAC samples to be converted by the driver.
static uint16_t dac_data[DATA_LENGTH];
- Create a callback function that will be called when DAC completes convert job.
{
transfer_is_done = true;
}
- Configure the DAC module.
- Create a DAC module configuration struct, which can be filled out to adjust the configuration of a physical DAC peripheral.
- 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.
- Configure the DAC module with starting conversion on event.
#if (SAML21)
#else
dac_instance.start_on_event = true;
#endif
- Initialize the DAC module.
dac_init(&dac_instance, DAC, &config_dac);
- Enable DAC start on conversion mode.
#if (SAML21)
#else
{ .on_event_start_conversion = true };
#endif
- Enable DAC event.
- 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.
- Initialize the DAC channel 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.
- Configure the DAC channel with the desired channel settings.
- Enable the DAC channel so that it can output a voltage.
- Enable DAC module.
- Configure the RTC module.
- Create an RTC module event struct, which can be filled out to adjust the configuration of a physical RTC peripheral.
- Create an RTC module configuration struct, which can be filled out to adjust the configuration of a physical RTC peripheral.
- Initialize the RTC 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.
- Change the RTC module configuration to suit the application.
#ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED
config_rtc_count.continuously_update = true;
#endif
- Initialize the RTC module.
- Configure the RTC module with overflow event.
rtc_event.generate_event_on_overflow = true;
- Enable RTC module overflow event.
- Enable RTC module.
- Configure the Event resource.
- Create an event resource config struct, which can be filled out to adjust the configuration of a physical event peripheral.
- Initialize the event 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.
- Change the event module configuration to suit the application.
event_config.generator = EVSYS_ID_GEN_RTC_OVF;
- Allocate the event resource.
- Attach the event resource with user DAC start.
#if (SAML21)
#else
#endif
- Register and enable the DAC Write Buffer Complete callback handler.
- Register the user-provided Write Buffer Complete callback function with the driver, so that it will be run when an asynchronous buffer write job completes.
- Enable the Read Buffer Complete callback so that it will generate callbacks.
Use Case
Code
Copy-paste the following code to your user application:
dac_data, DATA_LENGTH);
while (!transfer_is_done) {
}
while (1) {
}
Workflow
- Start a DAC conversion and generate a callback when complete.
- Wait until the conversion is complete.
while (!transfer_is_done) {
}
- Enter an infinite loop once the conversion is complete.