UART frame inversion

cmicrocontrollerpicuart

I have a problem when I send some character from an hyperterminal through an uart frame, I receive the wrong character. I use a PIC 18F46K80. In some test I did, I got these results :

Test 1 :
Character sent : 'A' (=1000001)
Character received : '_' (=01011111)

Test 2 :
Character sent : 'B' (=1000010)
Character received : '/' (=101111)

Test 3 :
Character sent : 'C' (=1000011)
Character received : '^' (=1011110)

I don't know what is the logic of the transmission. You can find bellow my code:

#include <stdio.h>
#include <stdlib.h>
#include <usart.h>
#include <math.h>
#include <p18f46k80.h>

//CPU Configuration
#pragma config FOSC = INTIO1    // internal oscillitor
//#pragma config BOREN = ON       // Enable Brown-out Reset 
#pragma config WDTEN = OFF      // Watchdog timer is disabled
#pragma config XINST = OFF      // CPU Extended mode is disabled

typedef unsigned char bool;
#define true    1
#define false   0


unsigned char msgData[100];
unsigned char cTemp;
unsigned char cTemp_0;

int i=0;

void ports_IO_init();
void init_UART();
void FOSC_Config();
void start_UART();


void main()
{
    bool b=0;  

    int J=0;
    unsigned char len=0;


    start_UART();

    while(1)
    {
       if (PIR1bits.RC1IF==1)
       {
           cTemp = RCREG;
       }
    }
}

void ports_IO_init()
{
    TRISCbits.TRISC6 = 0; //Le port RC6 en sortie TX1
    TRISCbits.TRISC7 = 1; //Le port RC7 en entrée RX1
}

void init_UART()
{
    //Configuration of TX
    TXSTA1bits.TX9 = 0; //8-Bit Transmit Enable
    TXSTA1bits.TXEN = 1; //Transmit is enabled
    TXSTA1bits.SYNC = 0; // Asynchronous mode
    TXSTA1bits.SENDB = 1; //Sends Sync Break on next transmission
    TXSTA1bits.BRGH = 0; //Baud rate selected at high speed

    //Configuration of RX
    RCSTA1bits.SPEN = 1; //Serial port is enabled
    RCSTA1bits.RX9 = 0; // 8 bits reception
    RCSTA1bits.CREN = 1; //Enable receiving

    //Configuration du bauderate 
    //For a FOSC of 8 MHz, a desired baud rate of 9600, in Asynchronous mode, and 8-bit BRG
    // SBRG1 = ((FOSC/Desired Baude Rate)/64)-1 ((8000000/9600)/64)-1 = 12
     SPBRG1 = 12; // 12 = 0xC in Hex
}

void FOSC_Config()
{
    // Configuration for a frequency of 8Mhz
    OSCCONbits.IRCF = 110;
    OSCCONbits.SCS1 = 1;

    OSCCON2bits.SOSCRUN=1;
    OSCCON2bits.SOSCDRV=1;
    OSCCON2bits.SOSCGO=1;

    OSCTUNEbits.INTSRC= 1;
    OSCTUNEbits.PLLEN = 1;
}

void start_UART(void)
{
    FOSC_Config();
    ports_IO_init();
    init_UART();
}

Best Answer

It looks like your oscillator is not set up right. Your OSCCON:IRCF bits are 110, so they are giving you a clock of 8MHz, and your baud rate calculation is based on 8MHz. However, you have PLLEN set. This turns on the phase lock loop, producing an effective FOSC frequency of four times 8MHz = 32MHz.
Try

SPBRG1=51