Using MPLAB X 1.70 with the dsPIC33FJ128GP802 microcontroller.
I have an application that collects data from two sensors with different sampling rates (one at 50 Hz, the other at 1000 Hz), both sensor packets also have different sizes (one at 5 bytes, the other at 21 bytes), Iām still used manual UART transmission as shown below:
void UART_send(char *txbuf, char size) {
char i;
for (i = 0; i < size; i++) {
while (U1STAbits.UTXBF);
U1TXREG = *txbuf++;
}
}
Masses of various sizes (5 or 21 bytes) were sent to this function with their size and simple loops laid out by each byte and displayed through the UART tx U1TXREG register.
DMA, . DMA UART ADC, . - , , , 21- , .
DMA, .
void UART_TX_DMA_init() {
DMA2CONbits.SIZE = 0;
DMA2CONbits.DIR = 1;
DMA2CONbits.AMODE = 0b00;
DMA2CONbits.MODE = 1;
DMA2PAD = (volatile unsigned int) &U1TXREG;
DMA2REQ = 12;
IFS1bits.DMA2IF = 0;
IEC1bits.DMA2IE = 1;
}
DMA . DMA, :
char TXBufferADC[5] __attribute__((space(dma)));
char TXBufferIMU[21] __attribute__((space(dma)));
void UART_send(char *txbuf, char size) {
int i;
DMA2CNT = size - 1;
if (size == ADCPACKETSIZE) {
DMA2STA = __builtin_dmaoffset(TXBufferADC);
for (i = 0; i < size; i++) {
TXBufferADC[i] = *txbuf++;
}
} else if (size == IMUPACKETSIZE) {
DMA2STA = __builtin_dmaoffset(TXBufferIMU);
for (i = 0; i < size; i++) {
TXBufferIMU[i] = *txbuf++;
}
} else {
NOTIFICATIONLED ^= 1;
}
DMA2CONbits.CHEN = 1;
DMA2REQbits.FORCE = 1;
}
-. DMA2STA, , . , DMA, (DMA2CNT), , , for, .
, , DMA / . , , . , ; ...
.