1

i´m trying to transfer data to computer via EUSART. I have already set all the configurate bits but serial communication still not working well. I receive only wrong data in my Pc. Here is my code for setting uart (i am using mplab compiler XC8):

TRISC6 = 1; \\ set in datasheet
TRISC7 = 1; \\ set in datasheet

SPBRG1=25;  \\ 4 MHz oscilator
SPBRGH1=0;  
TXSTA1bits.BRGH = 1;  \\ high speed uart
BAUDCON1bits.BRG16 = 0; \\ 8 bit data
TXSTA1bits.TX9 = 0;    
TXSTA1bits.SYNC = 0;  \\ asynchronus mode

RCSTA1bits.SPEN = 1;  \\ enable serial port
INTCONbits.GIE = 0;  \\ set off interrupt
TXSTA1bits.TXEN = 1; \\  transmitter is enabled
while (1) {
    TXSTA1bits.TXEN = 1;
    //UART_Write_Text("Bye pic \n");
       TXREG = 76;
    __delay_ms(100);
    TXSTA1bits.TXEN = 0;
}

Thanks for help

Nuke
  • 19
  • 1
  • 4

2 Answers2

0

The transmit buffer register must be empty before loading the next byte to send. From the data sheet: "Once the TXREGx register transfers the data to the TSR register (occurs in one TCY), the TXREGx register is empty and the TXxIF flag bit is set."

It appears the object is to repeatedly send 'L' at 9600 baud with a delay to allow the host to process the received data. So change to last lines to:

TXSTA1bits.TXEN = 1;
while(1){
  while (PIR1bits.TX1IF == 0);
  TXREG = 76;
  __delay_ms(100); 
}
jolati
  • 700
  • 11
  • 19
  • it read wrong letters or characters and the letters are changing during reading for example Tera Term reads Ř and character number 192 on the other hand NI MAX reads different characters but they also change during reading – Nuke Mar 21 '16 at 12:38
  • Are you sure about your FOSC frequency? Do you have the PLL enabled? Both affect baud rate. Can you confirm the baud rate with an oscilloscope? – jolati Mar 21 '16 at 16:15
0

This is my code with configurate bits my external oscilator is for 100% 4 MHz and i dont have oscilloscope :(

#include <xc.h>
#include <String.h>
#include <pic18f46k80.h>
#define _XTAL_FREQ 4000000

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG1L
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = LOW  // LF-INTOSC Low-power Enable bit (LF-INTOSC in Low-power mode during Sleep)
#pragma config SOSCSEL = DIG    // SOSC Power Selection and mode Configuration bits (Digital (SCLKI) mode)
#pragma config XINST = OFF       // Extended Instruction Set (Enabled)

// CONFIG1H
#pragma config FOSC = XT       // Oscillator (XT oscillator)
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)

// CONFIG2H
#pragma config WDTEN = SWDTDIS  // Watchdog Timer (WDT enabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)

// CONFIG3H                
#pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)

// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-03FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 04000-07FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 08000-0BFFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 0C000-0FFFF (Disabled)

// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)

// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-03FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 04000-07FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 08000-0BFFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 0C000-0FFFF (Disabled)

// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-03FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 04000-07FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 08000-0BFFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 0C000-0FFFF (Disabled)

// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)



void main(void) {

    TRISD = 0;
    ANCON0 = 0;
    ANCON1 = 0;
    TRISA = 0b00001111;
    TRISE = 0b00000000;
    TRISB = 0;


TRISCbits.TRISC6 = 1; // set in datasheet
TRISCbits.TRISC7 = 1; // set in datasheet

SPBRG1=25;  // 4 MHz oscilator
SPBRGH1=0;
TXSTA1bits.BRGH = 1;  // high speed uart
BAUDCON1bits.BRG16 = 0;// 8 bit data
TXSTA1bits.TX9 = 0;
TXSTA1bits.SYNC = 0;  // asynchronus mode

RCSTA1bits.SPEN = 1;  // enable serial port
INTCONbits.GIE = 0;  // set off interrupt
TXSTA1bits.TXEN = 1; //  transmitter is enabled

    while (1) {
      while (PIR1bits.TX1IF == 0);
     TXREG = 76;
      __delay_ms(100);
    }


    return;
}
Nuke
  • 19
  • 1
  • 4