PIC24 USART Help - Page 1
Forum section: electronics / general

author: diyaudio
author_rank: Frequent Contributor
timestamp: April 21, 2014, 02:13:13 pm
content: Hi Guys.I've been playing around with all the major peripherals on the PIC24FJ128GB210, However I'm having trouble with the USART module. (I think) according to the datasheet I have all the set-up configuration settings see code below.I have a USB to serial FTDI breakout board see attachment, according the terminal the data isn't formatted correctly, the scope also tells me the data isn't present, (there is sign of communication though ,see attachment but the data isn't correct)Here is the code.Summary,Buad Rate: 9600Stop bit : Enabled.No Parity.I'm NOT using the DTR and CTXS wires. Only TX in this case.Code:/* Data to be transmitted using UART communication module */volatile char Txdata[] = {'H', 'E', 'L', 'L', 'O'};void usart_send(){/* Holds the value of baud register */unsigned int baudvalue;/* Holds the value of uart config reg */unsigned int U1MODEvalue;/* Holds the information regarding uartTX & RX interrupt modes */unsigned int U1STAvalue;/* Turn off UART1module */CloseUART1();/* Configure uart1 receive and transmit interrupt */// ConfigIntUART1(UART_TX_ENABLE & UART_RX_INT_EN & UART_RX_INT_PR6 & UART_TX_INT_DIS & UART_TX_INT_PR2);/* Configure UART1 module to transmit 8 bit data with one stopbit. Also Enable loopback mode */baudvalue = 51;U1MODEvalue = UART_1STOPBIT & UART_NO_PAR_8BIT & UART_BRGH_FOUR & UART_UXRX_IDLE_ZERO & UART_DIS_ABAUD & UART_DIS_LOOPBACK & UART_DIS_WAKE & UART_UEN_00 & UART_MODE_SIMPLEX & UART_IrDA_DISABLE & UART_IDLE_CON & UART_EN;U1STAvalue = UART_TX_ENABLE & UART_INT_TX_EACH_CHAR & UART_SYNC_BREAK_DISABLED & UART_UXRX_IDLE_ONE & UART_ADR_DETECT_DIS;OpenUART1(U1MODEvalue, U1STAvalue, baudvalue);/* Load transmit buffer and transmit the same till null character is encountered */putsUART1 ((unsigned int *)Txdata);// WriteUART1(10);/* Wait for transmission to complete */while(BusyUART1());/* Turn off UART1 module */CloseUART1();}

author: dannyf
author_rank: Super Contributor
timestamp: April 21, 2014, 03:20:32 pm
content: I don't use plib. Generally, you want to initialize the module, and then load up the transmission string.you should also look into pls to activate the pins.I would also look into using interrupt for the transmission - uart is slow.

author: diyaudio
author_rank: Frequent Contributor
timestamp: April 21, 2014, 04:09:24 pm
content: Quote from: dannyf on April 21, 2014, 03:20:32 pmNot sure what the issue is, seems like I need go the register route instead of the plib (for sanity sake), data transmission speed isn't a requirement at the moment.

author: mazurov
author_rank: Frequent Contributor
timestamp: April 21, 2014, 04:13:00 pm
content: This is my init which works well on GB206 (same as GB210 but 64 pins). If you use interrupts you need to enable them elsewhere, after the init.Code:uint8_t tmpdata;U2MODE = 0x0008; /* enable high baud rate */U2STAbits.UTXISEL1 = 1;U2BRG = ((FCY / 4 / BAUD_RATE_CONSOLE) - 1); /* baud rate generator */_U2TXIP = 1; //serial interrupt priority_U2RXIP = 1;_U2ERIP = 1;U2MODEbits.UARTEN = 1; //UART then Tx - in that order!U2STAbits.UTXEN = 1;tmpdata = U2RXREG; //clear RX errorsU2STAbits.FERR = 0;U2STAbits.OERR = 0;//Set serial console pinsPPSUnLock;PPSOutput( PPS_RP21, PPS_U2TX ); //hardcodedPPSInput( PPS_U2RX, PPS_RP26 ); //todo - make less crypticTRISGbits.TRISG7 = 1; //set RX to input. todo - make less hardcodedPPSLock;return;

author: hans
author_rank: Super Contributor
timestamp: April 21, 2014, 04:43:45 pm
content: I neither use the plib, but this seems strange:Code:/* Load transmit buffer and transmit the same till null character is encountered */putsUART1 ((unsigned int *)Txdata);// WriteUART1(10);/* Wait for transmission to complete */while(BusyUART1());Txdata isn't zero terminated, and the size thereby isn't known. Make Txbuf like so:Code:volatile char Txdata[] = {'H', 'E', 'L', 'L', 'O', '\0'};or just much more simply:Code:const char* Txdata = "HELLO";edit: oops, looked up putsUART1 and it does send strings, in any case the string must be zero terminated.

author: diyaudio
author_rank: Frequent Contributor
timestamp: April 21, 2014, 05:00:34 pm
content: Quote from: hans on April 21, 2014, 04:43:45 pm@hans Thanks for the help,Unfortunately it didn't work as I was reading the post I realized the '\0' is missing, so I tried that first no change. Then I took your advice as you presented and still nothing, the data is some what pumped out differently but it still yields garbage on the scope and the terminal.There is one issue im not sure but in the example code the plib examples uses the #define UART_TX_PIN_NORMAL 0xF7FF /* UART TX pin operates normally */ This is not present on the PIC24 "uart.h", I wonder if this may be the issue.

author: diyaudio
author_rank: Frequent Contributor
timestamp: April 28, 2014, 10:49:11 pm
content: Quote from: diyaudio on April 21, 2014, 05:00:34 pmHad a chance today to sit down with this usart issue and got it working.Code:void main(void){while(1){// usart_send testusart_send();__delay_ms(10);}}volatile const char* Txdata = "HELLO";void usart_send(){/* Holds the value of baud register */unsigned int baudvalue;/* Holds the value of uart config reg */unsigned int U1MODEvalue;/* Holds the information regarding uartTX & RX interrupt modes */unsigned int U1STAvalue;/* Turn off UART1module */CloseUART1();/* Configure UART1 module to transmit 8 bit data with one stopbit. Also Enable loopback mode */baudvalue = 51;ConfigIntUART1(UART_RX_INT_DIS & // Disable UART Receive InterruptsUART_RX_INT_PR6 & // Assign Priority 6 to RX interruptUART_TX_INT_EN & // Enable UART Transmit InterruptsUART_TX_INT_PR6); // Assign Priority 6 to TX interruptsU1MODEvalue = UART_EN & // Enable the UART moduleUART_IDLE_CON & // UART continues operation in debug modeUART_IrDA_DISABLE & // Disable IRDA modeUART_MODE_SIMPLEX & // Disable RTS/CTS handshakingUART_DIS_WAKE & // Disable sleep mode wake up on start bit receptionUART_UEN_00 & // tx / rx only. No cts or rtsUART_DIS_LOOPBACK & // Disable loopback modeUART_DIS_ABAUD & // Disable auto baud rate measurementUART_BRGH_SIXTEEN & // Use low speed BRG (high speed one is silicon bugged)UART_NO_PAR_8BIT & // 8-bit data, no parityUART_1STOPBIT; // 1-stop bitU1STAvalue = UART_INT_TX_LAST_CH & // Interrupt when shift-register and buffer are emptyUART_IrDA_POL_INV_ZERO & // TX idle state is zero (no inversion)UART_SYNC_BREAK_DISABLED & // Disable transmit break transmissionUART_TX_ENABLE & // Enable the UART Transmission blockUART_INT_RX_CHAR & // RX Interrupt on every character receivedUART_ADR_DETECT_DIS & // Disable address detect featureUART_RX_OVERRUN_CLEAR; // Clear any overun error stateOpenUART1(U1MODEvalue, U1STAvalue, baudvalue);putsUART1 ((unsigned int *)Txdata);while(BusyUART1());}