Receiving serial on dsPIC

Status
Not open for further replies.

drkidd22

Member
Hello,

I have the bellow code to try and receive an 11-byte transmission on the dsPIC USART. I have not been able to yet successfully accomplish and wanted to see if someone here could give me a hand. Single byte transmission works fine. The far end sends "$123456789#" where $=Start byte and #=end byte of transmission. I'm using dsPIC33FJ256GP710A.


Code:
int main(void)
{

unsigned char inBytes[11];
unsigned int inBytesIndex;
  int tempInteger;
  unsigned char temp;

        // Fosc = (Fin * M)/(N1 * N2), Fcy = Fosc/2
        // Fosc = 8M *(40/(2*4)) = 40MIPS
        // Fcy = Fosc / 2 = 20MIPS
        // Tcy = 1/Fcy = 50ns
        PLLFBD = 0x0026;            /* M = PLLDIV = 40 (38 + 2) */
        CLKDIVbits.PLLPOST = 1;     /* N2 = 4, Default*/
        CLKDIVbits.PLLPRE =  0;     /* N1 = 2, Default*/

    while(OSCCONbits.LOCK != 1) {}; // Wait for PLL to lock

    InitUart1();

   while(1)
    {

    tempInteger = U1STAbits.URXDA;
    while (U1STAbits.URXDA != 0)
    {
      temp = U1RXREG;
      inBytes[inBytesIndex++] = temp;
    }

    delay(100);
        U1TXREG = inBytes[5];
    }
}

Code:
void InitUart1(void)
{
  U1MODEbits.UARTEN = 0;            //Disable UART1, Re-enable at end of function
  /*UART -1 Mode settings*/
  U1MODEbits.STSEL = 0;             // 1-stop bit
  U1MODEbits.PDSEL = 0;             // No Parity, 8-data bits
  U1MODEbits.BRGH = 0;              // BRG generates 16 clocks per bit period (16x baud clock, Standard mode)
  U1MODEbits.URXINV = 0;
  U1MODEbits.ABAUD = 0;             // Autobaud Disabled
  U1MODEbits.LPBACK = 0;            // Loop-Back Disabled
  U1MODEbits.WAKE = 0;              // No wake up
  U1MODEbits.UEN = 0;               // Enable UART Pins
  U1MODE = 0;
  U1BRG = BRGVAL;                   // Set for 9600

  /*UART -1 Status & Control Settings*/
  U1STA = 0;
  U1STAbits.UTXBRK = 0;
  U1STAbits.UTXISEL0 = 0;        //Interrupt when a character is transferred to the Transmit Shift Register
  U1STAbits.UTXISEL1 = 0;
  U1STAbits.URXISEL = 0;         // Interrupt is set when data is transferred from the shift register to RX register

  /*Interrupt Controller Settings*/
  IFS0bits.U1TXIF = 0;  //clear the tx interupt flag
  IEC0bits.U1TXIE = 0;  //enable transmit interupts 
  IFS0bits.U1RXIF = 0;  //clear the rx interupt flag
  IEC0bits.U1RXIE = 0;  //enable receive interupts
  IPC3bits.U1TXIP = 0;  //set the tx interupt level
  IPC2bits.U1RXIP = 0;  //set the rx interupt level

  U1MODEbits.UARTEN = 1; //Enable Uart
  U1STAbits.UTXEN = 1;   //Enable Transmission
}
 
I have had similar headache with PIC24's UART, Polling buffer flag does not seem to cope , so RX interrupt was the answer , with a circular FIFO , with write position and read position pointers, ( found code on www somewhere ) .

Edit.
( not found 'code' to click !)
C:
void __attribute__((__interrupt__, auto_psv))_U1RXInterrupt(void)
{
    while(U1STAbits.URXDA == 1)
   {
    KB_CB[KBW] = U1RXREG ;
      if ((KBW+1)% KB_size != KBR)
         KBW++;
          KBW %= KB_size;
    }
       IFS0bits.U1RXIF = 0;  //  clear flag.
      Ext_input = 1;            // flag buffer activity
}
 
Last edited by a moderator:
( not found 'code' to click !)
It's on the "Insert" header

When you click this select code from the dropdown menu.. I find it faster to just type the word "code" and "/code" in square braces...
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…