I started to work on PIC24FJ128GA202 microcontroller. Tried samples programs like LED blink and switch, controller works fine with the coding, then I started to work on UART configured all the parameters as per datasheet norms. But my PIC is not functioning to my code. I have connected my controller to PC to check the characters transferred . I have posted my code below. I don't receive any character on hyperterminal... Some times I received garbage values.

#include "p24FJ128GA202.h"              // Pic24FJ128GA202 header file
#define led PORTAbits.RA2 

void init_processor(void);

void init_processor(void)
    /* oscillator configuration is pending*/
    OSCCON = 0x0000;
    CLKDIV   =  0x0000;  //do not divide
    OSCTUN = 0; 
    RPINR18bits.U1RXR   = 7; //UART1 receive set to RB7
    RPOR4bits.RP8R  =   3;   //UART1 transmit set to RB8
    __builtin_write_OSCCONL(OSCCON & 0xbf); //clear the bit 6 of OSCCONL to
                                       //unlock Pin Re-map
   RPOR4bits.RP8R = 3;
   //__builtin_write_OSCCONL(OSCCON | 0x40); //set the bit 6 of OSCCONL to
                                        //lock Pin Re-map
    // configure digital pins
    ANSA = 0x0000;
    ANSB = 0x0000;
    //  assign  IO values for ports
    PORTA = 0x0000;
    TRISA = 0x0000;
    PORTB = 0x0000;
    TRISB = 0x0000;   // 7 as receive and 8 as transmitter

    // Init UART1
    U1MODE = 0x0000;
    U1STA = 0x0400;     //Enable Transmission, Clear all flags
    U1BRG = 25;
    U1MODEbits.UARTEN = 1;        // And turn the peripheral on

void uart1tx(char in_c)
    while(U1STAbits.UTXBF != 0);
    U1TXREG = in_c;

void uart1str(char *s)

char uart1rx()
    char c;
    while(U1STAbits.URXDA == 0); 
    c = U1RXREG;
    return c;   

int main()
    int i,j;
    led = 1;
        led = ~led;
    return 0;
  • \$\begingroup\$ If you're getting garbage, then your UART baudrate on both sides is not equal! They need to be. \$\endgroup\$ – Junior Aug 6 '15 at 12:24
  • \$\begingroup\$ Do you have 'scope? If yes, probe the serial data coming out of the PIC. Set up an infinite loop sending one character with a little delay. 'U' is nice because its 0x10101010 - easy to look at on the scope. Then check your bit widths to see whether your baud rate is correct. Then check number and presence of start/stop bits. \$\endgroup\$ – kabZX Aug 6 '15 at 12:24
  • \$\begingroup\$ Do you have an inverting driver between the chip and the PC? \$\endgroup\$ – Spehro Pefhany Aug 6 '15 at 13:36
  • \$\begingroup\$ ya, i have max 3232 converter between pic and pc. i just made a small change in a program. From hyperterminal i transmitted a character and received it on pic Uc. if received char is 'T' led will blink.. Else led is off... only problem is transmitting a character. \$\endgroup\$ – Pradeep kumark Aug 6 '15 at 13:51

You need to unlock the PPS prior to making changes. Specifically, the

__builtin_write_OSCCONL(OSCCON & 0xbf);

must be moved above

RPINR18bits.U1RXR   = 7;

Also, you probably want to use MC macros to do this. Here is an example of working UART init for similar micro -> https://github.com/felis/cdb/blob/master/fw/minimal/bsp.c#L185


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.