Pic24fj256gb110 uart help

Discussion in 'Embedded Systems and Microcontrollers' started by tokkalosh, Sep 16, 2012.

  1. tokkalosh

    Thread Starter New Member

    Sep 16, 2012
    1
    0
    Hi, I am new to PIC programming. I am using an explorer 16 board and I am able to use UART2 to send data from the PC to the PIC and back perfectly. E.G. Use hyperterminal to send keyboard commands to the PIC and then use these commands for UART1. I need to set up a second UART in order to communicate to a USB through a VNC1L chip using these commands. I am using a pictail plus to connect the USB board to the explorer board. I used an oscilloscope and saw the commands can be sent correctly to the VNC1L chip and the correct responses were coming back (I probed the actual PIC pin and they were there)..... However I am struggling to read these commands.... basically the buffer is saying it is empty. I know the correct commands are being sent as I checked them against another board. Initially I thought it was the baud rate however I don't think the commands could have even been sent back correctly if the baud rate was wrong. Both UART board rates are 19200. Code shown below. Clock rate is 8Mhz... Just let me know if I forgot any important information.



    Thanks.




    ------------------------------------------------------------------------------


    Code ( (Unknown Language)):
    1.  
    2.  
    3. #define SYSCLK          8000000
    4. #define BOARD_VERSION4
    5. #include <P24FJ256GB110.h>
    6. #include "delay.h"
    7. #include <stdio.h>
    8. #pragma udata
    9.  
    10. _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2 )
    11. _CONFIG2( FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_XT & FNOSC_PRI & DISUVREG_OFF & PLL_96MHZ_ON & IOL1WAY_ON)
    12.  
    13. void Init_Uart(void);
    14. void UART2PutChar(char Ch);
    15.  
    16. int i=0;
    17. char data,data2;
    18.  
    19. int main(void)
    20. {    
    21.  
    22. Init_Uart();
    23.    
    24.     while (1) // repeat continuously
    25.     {
    26.  
    27.         while(U1STAbits.URXDA==1)            //If input detected from USB
    28.         {
    29.         UART2PutChar('R');                //Print results to hyperterminal
    30.         data = U1RXREG;
    31.         UART2PutChar(data);
    32.         UART2PutChar('\n');
    33.         }
    34.  
    35.  
    36.         while(U2STAbits.URXDA==1)  // Keyboard command automatically sent to USB
    37.         {        
    38.             data2 = U2RXREG;           //Take from buffer
    39.             UART2PutChar(data2);
    40.             U1TXREG = data2;
    41.  
    42.         if(!U1STAbits.UTXBF)
    43.             {
    44.             U1TXREG = data2;            //Send to USB
    45.             }
    46.  
    47.        
    48.         }
    49.        
    50.     }
    51.  
    52. }
    53.  
    54. void Init_Uart(void)
    55. {
    56.  
    57. //PIC TO PC UART INITIALISATION
    58.     TRISFbits.TRISF5 = 0;
    59.     TRISFbits.TRISF4 = 1;
    60.     U2BRG = 12;
    61.     U2MODE = 0;
    62.     U2STA = 0;
    63.     U2MODEbits.UARTEN = 1;
    64.     U2STAbits.UTXEN = 1;
    65.       // reset RX flag
    66.      IFS1bits.U2RXIF = 0;
    67.  
    68.  
    69. // UART TO USB INITIALISATION
    70.     TRISBbits.TRISB2 = 0;
    71.     TRISAbits.TRISA15 = 1;
    72.     TRISDbits.TRISD14 = 1;
    73.     TRISDbits.TRISD15 = 0;
    74.    
    75.     U1BRG = 12;                                                                                                                        
    76.     U1MODE = 0;
    77.     U1STA = 0;
    78.     U1MODEbits.UARTEN = 1;
    79.     U1STAbits.UTXEN = 1;
    80.  
    81.     U1MODEbits.RXINV=1;
    82.     U1MODEbits.UEN1 = 1;
    83.     U1MODEbits.UEN0 = 0;
    84.  
    85. //  U4STAbits.UTXINV=1;
    86.  
    87.       // reset RX flag
    88.      IFS0bits.U1RXIF = 0;
    89.  
    90. //RS-232 FROM COMPUTER TO PIC
    91.  
    92. RPINR19bits.U2RXR = 10;           //U2RX = RP19
    93. RPOR8bits.RP17R = 5;            //RP25 = U2TX  
    94.  
    95.  
    96. //USB UART I/O PINS
    97.  
    98. RPINR18bits.U1RXR = 35;          //U1RX
    99. RPOR6bits.RP13R = 3;               //U1TX
    100.  
    101. //RPINR18bits.U1RXR = 30;          //U1RX
    102. //RPOR8bits.RP16R = 3;               //U1TX          
    103. RPINR18bits.U1CTSR = 43;         // U1CTS
    104. RPOR2bits.RP5R = 4;              //U1RTS      
    105.    
    106. }
    107.  
    108. void UART2PutChar(char Ch)
    109. {
    110.     // wait for empty buffer  
    111.    while(U2STAbits.UTXBF == 1);
    112.       U2TXREG = Ch;
    113. }
    114.  
     
    Last edited by a moderator: Sep 16, 2012
Loading...