Sign In

Trouble Configuring USART To Send Right Character

hamzeh smhhkh@gmail.com United States

Hardware: Pic16f1829 Running Xtal = 11.0592 MHz Software: C Project Using XC8 And Mplabx

its working with mikroe uart function , but with my initialization I'm getting garbage data on TX pin(RB7).

Images

Program

USART initial configuration
void Config_serialPort(void)
{
    TRISA = 0;
    SSP1CON1bits.SSPEN = 0;
    SSP2CON1bits.SSPEN = 0;
    ANSELB = 0 ;   
    ANSELBbits.ANSB4 = 0; //disable analoge mode
    ANSELBbits.ANSB5 = 0; //disable analoge mode

    PIE1bits.TXIE = 0;  /* Disable transmiter interrupt */
    PIE1bits.RCIE  = 0; /* Disable receiver interrupt */
    TRISBbits.TRISB5 = 1; /* Make RXD an input */
    TRISBbits.TRISB7 = 0;
      SPBRG = 17 ;// setting for 9600 baud rate
     //SPBRG = ((_XTAL_FREQ/Baud_rate)/64 - 1);
    BAUDCONbits.BRG16 = 0; //using 16bit baud rate generator
    TXSTAbits.TX9=0;  //8 bit transmission
    TXSTAbits.TXEN=1; //Transmit enable
    TXSTAbits.SYNC=0; //Async mode
    TXSTAbits.BRGH=0; //High speed baud rate
    //RCSTA
    RCSTAbits.SPEN=1;   //Serial port enabled
    PIR1bits.TXIF = 0; //clear transmit interrupt flag
    RCSTAbits.RX9=0;    //8 bit mode
    RCSTAbits.CREN=1;   //Enable receive
    RCSTAbits.ADDEN=0;  //Disable address detection
}
// putchar function
void putch(unsigned char byte) 
{
    /* output one byte */
    
    while(!PIR1bits.TXIF)    /* set when register is empty */
        continue;
    TXREG = byte;
    
}
and here the  main function 
void main(void)
{
    INTCONbits.T0IF = 0; 
    Config_serialPort();
    while(1)
    {
        for(i=65;i<80;i++)
        {
            putch(i);
            __delay_ms(250);
        }
    }
    return;
}

Post a reply to hamzeh

Think you can help hamzeh ? Then post your thoughts that might help hamzeh . You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.