|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2012-5-27 03:35:17
|
显示全部楼层
完整使用代码如下- void DMA0_Initial(void)
- {
-
- // reset DMA controller
- DMA.CTRL = 0;
- DMA.CTRL = DMA_RESET_bm;
- while ((DMA.CTRL & DMA_RESET_bm) != 0);
- //DMA enable
- DMA.CTRL = DMA_ENABLE_bm;
- //--------------------------------------------------------------------
- // CH0
- // DMA source address register
- // Increment
- // DMA channel destination address
- // Fixed
- DMA.CH0.ADDRCTRL = DMA_CH_SRCRELOAD_BLOCK_gc | DMA_CH_SRCDIR_INC_gc | DMA_CH_DESTRELOAD_NONE_gc | DMA_CH_DESTDIR_FIXED_gc;
- // Trigger Source ,Software triggers +usatre0 为空
- DMA.CH0.TRIGSRC = DMA_CH_TRIGSRC_USARTE0_DRE_gc;
- // DMA.CH0.TRIGSRC = DMA_CH_TRIGSRC_OFF_gc;
- // // ADDR0 - ADDR2 24bit adresse 目标地址 UART(UARTE0.DATA)
- DMA.CH0.DESTADDR0 = ((uint32_t) &USART.DATA) & 0xFF;
- DMA.CH0.DESTADDR1 = ((uint32_t) &USART.DATA >> 8) & 0xFF;
- DMA.CH0.DESTADDR2 = ((uint32_t) &USART.DATA >> 16) & 0xFF;
- // Block transfer count
- DMA.CH0.TRFCNT =394;//UART_TX_BUFFER_SIZE;//所需传输的block大小
- // ADDR0 - ADDR2 24bit Quelladresse //被传输的数组的首地址
- DMA.CH0.SRCADDR0 = (((uint32_t) UART_TX_Buffer)) & 0xFF;
- DMA.CH0.SRCADDR1 = (((uint32_t) UART_TX_Buffer) >> 8) & 0xFF;
- DMA.CH0.SRCADDR2 = (((uint32_t) UART_TX_Buffer) >> 16) & 0xFF;
- DMA.CH0.CTRLA = DMA_CH_SINGLE_bm |DMA_CH_BURSTLEN_1BYTE_gc;
- DMA.CH0.CTRLB|=DMA_CH_TRNINTLVL_HI_gc; //iSR enable, transfer
- // DMA.CH0.CTRLA|=DMA_CH_TRFREQ_bm;//开始传输
- //--------------------------------------------------------------------
- }
复制代码 一旦需要dma的时候 使用这个语句- DMA.CH0.CTRLA |= DMA_CH_ENABLE_bm;//使能dma,本程序中即为开始发送
复制代码 在中断中处理、检查是因为发送完成产生中断还是发送错误产生中断,经测试,发送完成产生中断后,该channel dma就被禁用了- ISR(DMA_CH0_vect)
- {
- if (DMA.CH0.CTRLB & DMA_CH_TRNIF_bm) { //Transaction Complete Interrup Flag bit mask
- DMA.CH0.CTRLB |= DMA_CH_TRNIF_bm; // DMA 传输结束标志位 This flag is cleared by writing a one to the bit location
- }
- }
复制代码 |
|