Electronic – problem in initating pic uart

picuart

This is the first question in stack exchange.Ok, Im trying to initiate UART of pic microcontroller(PIC24F family). I studied uart related registers in the datasheet and wrote the code to transmit a character 'K', after compile the code, hyperterminal doesn't show anything.

Here is my header file code:

void init_uart1()
{
 //initiate uart1
 RPOR4bits.RP8R = 3;   //pin8
 U1MODEbits.STSEL=0;
 U1MODEbits.PDSEL=0;
 U1BRG = 25;
 U1MODEbits.UARTEN=1;
 U1STAbits.UTXEN =1;

}

Here is my main file:

    TRISB=0x0000;  //0000 0000 0000 0000
    PORTBbits.RB8=0; //set outout PORTB 8th pin 

   init_uart1();
   while (1)
    { 
       while(U1STAbits.UTXBF == 1);
       U1TXREG='K';
    }

whats wrong in my code?

Thxns…

Best Answer

Microchip provides peripheral library functions which you can use directly in your program instead of putting values in registers. Go to c\Program Files\Microchip\(your compiler)\docs you will find many documentation for it or try searching for PIC18/PIC24/PIC32 peripheral library. I have not worked in PIC24 but the functions are same for all the MCU series like PIC18 PIC24 PIC32. For example, for UART:

OpenUART(): to initialize UART channel.
putsUART(): to send string to your UART
getsUART(): to receive string from UART

you can use these functions directly and save your time.!