这个例子是通过ADC AIN4输入信号,不通过CPU尔直接通过DMA传给,DAC然后通过PA2输出。
打开上次的工和根据快速指导,建立相应的程序。
形文件后一定要注意在编译之前,一定要加入DAC和DMAC两个模块,并且点应用。否则编译器找不到文件:
程序如下:
#include <asf.h>
/* AC module software instance (must not go out of scope while in use) */
struct dac_module dac_instance;
struct adc_module adc_instance;
struct dma_resource example_resource;
COMPILER_ALIGNED(16)
DmacDescriptor example_descriptor SECTION_DMAC_DESCRIPTOR;
void configure_adc(void);
void configure_dac(void);
void configure_dac_channel(void);
void configure_dma_resource(struct dma_resource *resource);
void setup_transfer_descriptor(DmacDescriptor *descriptor);
void configure_adc(void)
{
struct adc_config config_adc;
adc_get_config_defaults(&config_adc);
#if !(SAML21)
config_adc.gain_factor = ADC_GAIN_FACTOR_DIV2;
config_adc.resolution = ADC_RESOLUTION_10BIT;
#endif
config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV16;
config_adc.reference = ADC_REFERENCE_INTVCC1;
config_adc.positive_input = ADC_POSITIVE_INPUT_PIN4;
config_adc.freerunning = true;
config_adc.left_adjust = false;
adc_init(&adc_instance, ADC, &config_adc);
adc_enable(&adc_instance);
}
void configure_dac(void)
{
struct dac_config config_dac;
dac_get_config_defaults(&config_dac);
#if (SAML21)
config_dac.reference = DAC_REFERENCE_INTREF;
#else
config_dac.reference = DAC_REFERENCE_AVCC;
#endif
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);
}
void configure_dma_resource(struct dma_resource *resource)
{
struct dma_resource_config config;
dma_get_config_defaults(&config);
config.peripheral_trigger = ADC_DMAC_ID_RESRDY;
config.trigger_action = DMA_TRIGGER_ACTON_BEAT;
dma_allocate(resource, &config);
}
void setup_transfer_descriptor(DmacDescriptor *descriptor)
{
struct dma_descriptor_config descriptor_config;
dma_descriptor_get_config_defaults(&descriptor_config);
descriptor_config.beat_size = DMA_BEAT_SIZE_HWORD;
descriptor_config.dst_increment_enable = false;
descriptor_config.src_increment_enable = false;
descriptor_config.block_transfer_count = 1000;
descriptor_config.source_address = (uint32_t)(&adc_instance.hw->RESULT.reg);
#if (SAML21)
descriptor_config.destination_address = (uint32_t)(&dac_instance.hw->DATA[DAC_CHANNEL_0].reg);
#else
descriptor_config.destination_address = (uint32_t)(&dac_instance.hw->DATA.reg);
#endif
descriptor_config.next_descriptor_address = (uint32_t)descriptor;
dma_descriptor_create(descriptor, &descriptor_config);
}
int main (void)
{
system_init();
configure_adc();
configure_dac();
configure_dac_channel();
dac_enable(&dac_instance);
configure_dma_resource(&example_resource);
setup_transfer_descriptor(&example_descriptor);
dma_add_descriptor(&example_resource, &example_descriptor);
adc_start_conversion(&adc_instance);
dma_start_transfer_job(&example_resource);
while (true) {
}
}
|