Author Topic: PIC24 USART Help  (Read 5768 times)

0 Members and 1 Guest are viewing this topic.

Offline diyaudioTopic starter

  • Frequent Contributor
  • **
  • !
  • Posts: 683
  • Country: za
PIC24 USART Help
« on: April 21, 2014, 02:13:13 pm »
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: 9600
Stop bit : Enabled.
No Parity.
I'm NOT using the DTR and CTXS wires. Only TX in this case.

Code: [Select]

 /* 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 uart
    TX & 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();
}




« Last Edit: April 21, 2014, 02:20:20 pm by diyaudio »
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: PIC24 USART Help
« Reply #1 on: April 21, 2014, 03:20:32 pm »
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.
================================
https://dannyelectronics.wordpress.com/
 

Offline diyaudioTopic starter

  • Frequent Contributor
  • **
  • !
  • Posts: 683
  • Country: za
Re: PIC24 USART Help
« Reply #2 on: April 21, 2014, 04:09:24 pm »
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.

Not 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.
 

Offline mazurov

  • Frequent Contributor
  • **
  • Posts: 524
  • Country: us
Re: PIC24 USART Help
« Reply #3 on: April 21, 2014, 04:13:00 pm »
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: [Select]
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 errors
   U2STAbits.FERR = 0;
   U2STAbits.OERR = 0;

    //Set serial console pins
    PPSUnLock;
    PPSOutput( PPS_RP21, PPS_U2TX );    //hardcoded
    PPSInput( PPS_U2RX, PPS_RP26 );     //todo - make less cryptic
 
    TRISGbits.TRISG7 = 1;    //set RX to input. todo - make less hardcoded

    PPSLock;


    return;
With sufficient thrust, pigs fly just fine - RFC1925
 

Offline hans

  • Super Contributor
  • ***
  • Posts: 1626
  • Country: nl
Re: PIC24 USART Help
« Reply #4 on: April 21, 2014, 04:43:45 pm »
I neither use the plib, but this seems strange:

Code: [Select]
    /* 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: [Select]
volatile   char Txdata[] = {'H', 'E', 'L', 'L', 'O', '\0'};
or just much more simply:

Code: [Select]
const char* Txdata = "HELLO";
edit: oops, looked up putsUART1 and it does send strings, in any case the string must be zero terminated.
« Last Edit: April 21, 2014, 04:46:17 pm by hans »
 

Offline diyaudioTopic starter

  • Frequent Contributor
  • **
  • !
  • Posts: 683
  • Country: za
Re: PIC24 USART Help
« Reply #5 on: April 21, 2014, 05:00:34 pm »
I neither use the plib, but this seems strange:

Code: [Select]
    /* 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: [Select]
volatile   char Txdata[] = {'H', 'E', 'L', 'L', 'O', '\0'};
or just much more simply:

Code: [Select]
const char* Txdata = "HELLO";
edit: oops, looked up putsUART1 and it does send strings, in any case the string must be zero terminated.


@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.

 

 

Offline diyaudioTopic starter

  • Frequent Contributor
  • **
  • !
  • Posts: 683
  • Country: za
Re: PIC24 USART Help
« Reply #6 on: April 28, 2014, 10:49:11 pm »
I neither use the plib, but this seems strange:

Code: [Select]
    /* 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: [Select]
volatile   char Txdata[] = {'H', 'E', 'L', 'L', 'O', '\0'};
or just much more simply:

Code: [Select]
const char* Txdata = "HELLO";
edit: oops, looked up putsUART1 and it does send strings, in any case the string must be zero terminated.


@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.


Had a chance today to sit down with this usart issue and got it working.  :)

Code: [Select]

void main(void)
{
    while(1)
    {
         // usart_send test
          usart_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 uart
    TX & 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 Interrupts
                 UART_RX_INT_PR6 &                                      // Assign Priority 6 to RX interrupt
                 UART_TX_INT_EN &                                       // Enable UART Transmit Interrupts
                 UART_TX_INT_PR6);                                      // Assign Priority 6 to TX interrupts
     
   
   U1MODEvalue = UART_EN &                                       // Enable the UART module
                 UART_IDLE_CON &                                        // UART continues operation in debug mode
                 UART_IrDA_DISABLE &                                  // Disable IRDA mode
                 UART_MODE_SIMPLEX &                               // Disable RTS/CTS handshaking
                 UART_DIS_WAKE &                                       // Disable sleep mode wake up on start bit reception
                 UART_UEN_00 &                                           // tx / rx only. No cts or rts
                 UART_DIS_LOOPBACK &                               // Disable loopback mode
                 UART_DIS_ABAUD &                                      // Disable auto baud rate measurement
                 UART_BRGH_SIXTEEN &                                // Use low speed BRG (high speed one is silicon bugged)
                 UART_NO_PAR_8BIT &                                 // 8-bit data, no parity
                 UART_1STOPBIT;                                          // 1-stop bit


   U1STAvalue = UART_INT_TX_LAST_CH &                    // Interrupt when shift-register and buffer are empty
                UART_IrDA_POL_INV_ZERO &                       // TX idle state is zero (no inversion)
                UART_SYNC_BREAK_DISABLED &                   // Disable transmit break transmission
                UART_TX_ENABLE &                                        // Enable the UART Transmission block
                UART_INT_RX_CHAR &                                    // RX Interrupt on every character received
                UART_ADR_DETECT_DIS &                               // Disable address detect feature
                UART_RX_OVERRUN_CLEAR;                            // Clear any overun error state

    OpenUART1(U1MODEvalue, U1STAvalue, baudvalue);

     putsUART1 ((unsigned int *)Txdata);
     while(BusyUART1());
}


 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf