7.1 Example-1
This example shows the code snippet for the setup of the DMA module to transfer data from the EEPROM to a buffer in the user RAM space. The transfer is initiated by setting the DGO bit and has no abort triggers. In this example, 512 bytes of data that has been preloaded into the EEPROM at program time is moved into a RAM buffer called “DMA_READ_BUF” for faster access. User firmware needs to set the DGO bit in the DMA1CON0 register to initiate the DMA message. Once completed, the DMA1DCNTIF bit is set and since the interrupt for destination counter reload is enabled, the program will jump to the ISR.
void DMA1_Initialize(void)
{
DMA1SSA = 0x000000; //set source start address
DMA1DSA = &DMA_READ_BUF; //set destination start address
DMA1CON1 = 0x7A; //DMODE = 01 | DSTP = 1 | SMR = 11 | SMODE = 01 | SSTP = 0
DMA1SSZ = 0x0200; //set source size = 512 bytes
DMA1DSZ = 0x0200; //set destination size = 512 bytes
DMA1SIRQ = 0x00; //set DMA Transfer Trigger Source
DMA1AIRQ = 0x00; //set DMA Transfer abort Source
PIR2bits.DMA1DCNTIF = 0; //clear Destination Count Interrupt Flag bit
PIR2bits.DMA1SCNTIF = 0; //clear Source Count Interrupt Flag bit
PIR2bits.DMA1AIF = 0; //clear abort Interrupt Flag bit
PIR2bits.DMA1ORIF = 0; //clear overrun Interrupt Flag bit
PIE2bits.DMA1DCNTIE = 1; //enable Destination Count 0 Interrupt
PIE2bits.DMA1SCNTIE = 0; //disable Source Count Interrupt
PIE2bits.DMA1AIE = 0; //disable abort Interrupt
PIE2bits.DMA1ORIE = 0; //disable overrun Interrupt
asm("BCF INTCON0,7"); //disable Global Interrupts
asm ("BANKSEL PRLOCK"); //
asm ("MOVLW 0x55"); //
asm ("MOVWF PRLOCK"); // Arbiter Priority lock
asm ("MOVLW 0xAA"); // sequence
asm ("MOVWF PRLOCK"); //
asm ("BSF PRLOCK, 0"); //
asm("BSF INTCON0,7"); // enable Global Interrupts
DMA1CON0 = 0x80; //EN = 1 | SIRQEN = 0 | DGO = 0 |xx| AIRQEN = 0 |x| XIP = 0
}