# UART frame inversion

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();
}

• When posting code, please use the code markup - otherwise your question will look extremely bad. (Should have been visible in the rendered preview on the bottom of your question asking screen) – Arsenal Oct 8 '15 at 15:52
• First question with serial transmission issues: have you shared the ground between PC and microcontroller? Are you using the same voltage levels? (i.e. not RS232 mixed with TTL UART) I guess you set the baudrate correctly on the PC. – Arsenal Oct 8 '15 at 15:56
• In Asynchronous mode TXSTA1bits.BRGH = 1; is High Speed – Tinez Oct 8 '15 at 16:45

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

• I don't think that there is a problem of the clock configuration: in the PIC datasheet OSCCONbits.IRCF=110 corresponds to frequency of 8MHz. I tried the second solution with setting the clock at 4 MHz and SPBRG at 6, but it still doesn't work. – Bicha_95 Oct 9 '15 at 8:04
• You're right, I was looking at the wrong datasheet. Updated the answer... I still think it's your oscillator speed – Kane Anderson Oct 9 '15 at 14:29

It was a problem of configuration for the baudrate, I set SPBRG1 at 51 and RXDTP (register BAUDCONx) at 1. In any case, thank you all for your help, I which you a nice week :) Aymane