本帖最后由 ddllxxrr 于 2016-1-4 20:48 编辑
DMA是可以省去CPU而在,外设与外设,外设与内存,内存与内存,内存与外设之间的数据传输。
首先,在没有加入DMA模块之前在ASF列表点开DMA,当然也可加入后在ASF EXPLORER中开打,找到快速指导
根据快速指导完成程序:
首先设置如下:
突发长度:1字节
传输记数:1024
源:在RAM中的缓冲
目地:在RAM中的缓冲
源和目地地址装载模式工:传输结束
源各目地地址方向模式:递增
#include <asf.h>
#define DMA_CHANNEL 0
#define DMA_BUFFER_SIZE 1024
static uint8_t source[DMA_BUFFER_SIZE];
static uint8_t destination[DMA_BUFFER_SIZE];
static void dma_init(void)
{
struct dma_channel_config dmach_conf;
memset(&dmach_conf, 0, sizeof(dmach_conf));
dma_channel_set_burst_length(&dmach_conf, DMA_CH_BURSTLEN_1BYTE_gc);
dma_channel_set_transfer_count(&dmach_conf, DMA_BUFFER_SIZE);
dma_channel_set_src_reload_mode(&dmach_conf,
DMA_CH_SRCRELOAD_TRANSACTION_gc);
dma_channel_set_dest_reload_mode(&dmach_conf,
DMA_CH_DESTRELOAD_TRANSACTION_gc);
dma_channel_set_src_dir_mode(&dmach_conf, DMA_CH_SRCDIR_INC_gc);
dma_channel_set_source_address(&dmach_conf,
(uint16_t)(uintptr_t)source);
dma_channel_set_dest_dir_mode(&dmach_conf, DMA_CH_DESTDIR_INC_gc);
dma_channel_set_destination_address(&dmach_conf,
(uint16_t)(uintptr_t)destination);
dma_enable();
dma_channel_write_config(DMA_CHANNEL, &dmach_conf);
dma_channel_enable(DMA_CHANNEL);
}
int main (void)
{
/* Insert system clock initialization code here (sysclk_init()). */
//sysclk_init();
sysclk_init();
dma_init();
dma_channel_trigger_block_transfer(DMA_CHANNEL);
do {} while (dma_get_channel_status(DMA_CHANNEL) != DMA_CH_TRANSFER_COMPLETED);
while(1);
}
为了看清运行结果又把源数组赋值:#include <asf.h>
#define DMA_CHANNEL 0
#define DMA_BUFFER_SIZE 1024
static uint8_t source[DMA_BUFFER_SIZE];
static uint8_t destination[DMA_BUFFER_SIZE];
static void dma_init(void)
{
struct dma_channel_config dmach_conf;
memset(&dmach_conf, 0, sizeof(dmach_conf));
dma_channel_set_burst_length(&dmach_conf, DMA_CH_BURSTLEN_1BYTE_gc);
dma_channel_set_transfer_count(&dmach_conf, DMA_BUFFER_SIZE);
dma_channel_set_src_reload_mode(&dmach_conf,
DMA_CH_SRCRELOAD_TRANSACTION_gc);
dma_channel_set_dest_reload_mode(&dmach_conf,
DMA_CH_DESTRELOAD_TRANSACTION_gc);
dma_channel_set_src_dir_mode(&dmach_conf, DMA_CH_SRCDIR_INC_gc);
dma_channel_set_source_address(&dmach_conf,
(uint16_t)(uintptr_t)source);
dma_channel_set_dest_dir_mode(&dmach_conf, DMA_CH_DESTDIR_INC_gc);
dma_channel_set_destination_address(&dmach_conf,
(uint16_t)(uintptr_t)destination);
dma_enable();
dma_channel_write_config(DMA_CHANNEL, &dmach_conf);
dma_channel_enable(DMA_CHANNEL);
}
int main (void)
{
/* Insert system clock initialization code here (sysclk_init()). */
//sysclk_init();
uint32_t i;
sysclk_init();
dma_init();
for(i=0;i<1024;i++)
{
source[i]=i;
}
dma_channel_trigger_block_transfer(DMA_CHANNEL);
do {} while (dma_get_channel_status(DMA_CHANNEL) != DMA_CH_TRANSFER_COMPLETED);
while(1);
}
运行结果:
|