0
\$\begingroup\$

I'm new to pic and uart. Just trying out simple uart transmission. I'm using mplab IDE and coolterm for serial monitoring. I'm getting serial framing error. I also used PuTTY and it was printing junk value. Below's the code that i'm using and i want to print hello world on desktop.

config.h

#ifndef CONFIG_H
#define CONFIG_H

// PIC16F886 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = ON        // Internal External Switchover bit (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)

// CONFIG2
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

#endif  /* CONFIG_H */

uart.c

#include <xc.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include "config.h"

#define _XTAL_FREQ 20000000
#define baud 9600

void usart_init();
void usart_send_byte(char);
void usart_send_string(char*);


void usart_init(){
    TRISC6=0;
    TRISC7=1;
//    SPBRG=_XTAL_FREQ/(64*baud) -1;
    SPBRG=31;
    TXSTAbits.BRGH=1;
    TXSTAbits.SYNC=0;
    RCSTAbits.SPEN=1;
    TXSTAbits.TXEN=1;
    RCSTAbits.CREN=1;
    TXSTAbits.TX9=0;
    RCSTAbits.RX9=0;
    RCSTAbits.FERR = 0;     // Disable framing error
    RCSTAbits.OERR = 0;     // Disable overrun error
}

void usart_send_byte(char bt){
    while(TXIF==0);
    TXREG=bt;
}


void usart_send_string(char* str) {
    while(*str!= '\0')
        usart_send_byte(*str++);
}


void main(void) {
    char* str = "Hello world!\n\r";
    usart_init();

    while (1) {
        usart_send_string(str);
        __delay_ms(500);
    }

    return;
}
\$\endgroup\$
  • \$\begingroup\$ This is typical of a baudrate mismatch. Did you find out what baudrate you are transmitting at? \$\endgroup\$ – Dmitry Grigoryev Jan 26 at 7:14
  • \$\begingroup\$ @DmitryGrigoryev : Baud rate is 9600. \$\endgroup\$ – Captain aaloo reportin' in Jan 26 at 7:16
  • 2
    \$\begingroup\$ how do you know that it is 9600? \$\endgroup\$ – jsotola Jan 26 at 8:40
  • 1
    \$\begingroup\$ well, you can set putty and coolterm to any baud rate you like .... it will not affect the baud rate of the incoming data ........ so you do not actually know if the baud rate is 9600 ... do you have access to an oscilloscope? \$\endgroup\$ – jsotola Jan 26 at 9:03
  • 2
    \$\begingroup\$ @Captainaalooreportin'in Great! Then take a shot of your UART_TX line and post it in your question. Or analyze it yourself and see if the baudrate is really 9600 \$\endgroup\$ – Dmitry Grigoryev Jan 26 at 9:23
2
\$\begingroup\$

Your actual baud rate with those settings is 38400. If TXSTAbits.BRGH == 1, then the multiplier is 16, not 64. If you want 9600, then either set TXSTAbits.BRGH = 0 or SPBRG = 129 (20MHz / (16 * 9600))-1.

See the EUSART chapter of the datasheet for more details.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.