UART Communication in PIC16F877A

UART Communication in PIC16F877A

Postby amitele_15 » Sun Sep 27, 2015 12:36 pm

Hi everyone.. i have written UART code in MPLAB using "xc8" compiler for PIC16F877A Microcontroller..code build was successful but while i am simulation code using proteus i am not getting the output in virtual terminal as expected.i am getting some charater like

C0 00 C0 C0 C0 00 C0 00 00 C0 00 C0 C0 C0 C0 C0
00 C0 00 00 C0 C0 00 00 C0 00 C0 00 00 C0 C0 00
C0 C0 00 00 00 C0 C0 C0 C0 00 C0 00 00 C0 C0 C0
C0 C0 C0 C0 C0 00 C0


Frequency used for 9600 baud rate is 4.9152 MHZ so that baud rate error is ZERO. i have set frequency in proteus as same 4.9152 MHZ and baud rate as 9600 in virtual terminal. can anyone suggest weather i am missing something in the code or code is correct .

NOTE: PIC16F877A DOES NOT HAVE ANY INTERNAL OSCILLATOR AS VERIFIED FROM DATASHEET

Code: Select all
#include<xc.h>
#include<stdio.h>
#include<stdlib.h>
 
 
// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
 
#define _XTAL_FREQ 4915200
#define baud_rate 9600
 
void serial_init(void);
void serial_char(unsigned char);     
void serial_string(unsigned char *);
char serial_read(void);
 
char data_string[16] ="ENTER ANY VALUE";
unsigned char data_buff = 0,data_read =0;
 
void main()
{
  unsigned int i=0;
  serial_init();
  serial_char('A');
  serial_string(data_string);
  TRISB =0X00; 
  PORTB = 0X00;
  while(1)
  {
      if(PIR1bits.RCIF)
      {
         
          data_buff = serial_read();
         
          switch(data_buff)
          {
              case 0x31:
              {
                  PORTB = 0X01;
                  break;
              }
              case 0x32:
              {
                  PORTB = 0X02;
                  break;
              }
              case ' ':
              {
                  PORTB = 0X03;
                  break;
              }
              default:
                  PORTB = 0XAA;
          }
      }
  }
}
 
 
void serial_init()
{
      char speed = (_XTAL_FREQ - (baud_rate*16))/(baud_rate*16);
      TRISCbits.TRISC6 = 0;
      TRISCbits.TRISC7 = 1;
      SPBRG = speed;
      TXSTAbits.BRGH = 1;  //REQUIRED FOR HIGH BAUD RATE
      TXSTAbits.SYNC = 0;  //for Asynchronous communication
      RCSTAbits.SPEN = 1;  // enable UART
      RCSTAbits.CREN = 1;  // enable continuous receive enable
      TXSTAbits.TXEN = 1;  // enable transmission
 }
 
void serial_char(unsigned char data)
{
    while(!TXSTAbits.TRMT); //wait for previous transmission to over
    TXREG = data;
}
 
void serial_string(unsigned char *str)
{
    while(*str != 0)
    {
        serial_char(*str++);
    }
}
 
char serial_read()
{
    while(!PIR1bits.RCIF);  //check weather reception is complete
    data_read = RCREG;      //read the RCREG register
        if(RCSTAbits.OERR == 1)  // check for any over run error
        {
            RCSTAbits.CREN = 0;
            RCSTAbits.CREN = 1;
        }
    return data_read;
   
}
amitele_15
 
Posts: 2
Joined: Sun Sep 27, 2015 12:01 pm

Re: UART Communication in PIC16F877A

Postby Entropy » Sun Sep 27, 2015 1:29 pm

I dont think you need to hit 9600 spot on, my only program with usart I ever did my calculated baund rate is actualy 9541 if you apply that formula and is working
Entropy
 
Posts: 24
Joined: Fri Sep 04, 2015 7:04 am
Location: Timisoara, Romania

Re: UART Communication in PIC16F877A

Postby amitele_15 » Sun Sep 27, 2015 4:03 pm

#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
CAN anyone please explain when to use FOSC = XT mode in configuration bits
amitele_15
 
Posts: 2
Joined: Sun Sep 27, 2015 12:01 pm

Re: UART Communication in PIC16F877A

Postby jtemples » Sun Sep 27, 2015 8:34 pm

XT and HS are used for different frequency ranges of crystals. Which one to use depends on your crystal.
jtemples
Verified identity
 
Posts: 195
Joined: Sun May 25, 2014 2:23 am
Location: The 805
PIC experience: Professional 5+ years with MCHP products

Re: UART Communication in PIC16F877A

Postby ric » Mon Sep 28, 2015 2:25 am

Do you have any real hardware to test this on?
My first guess would be a problem with Proteus...
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: UART Communication in PIC16F877A

Postby vloki » Mon Sep 28, 2015 8:33 am

amitele_15 wrote:
Code: Select all
void serial_init()
{
      char speed = (_XTAL_FREQ - (baud_rate*16))/(baud_rate*16);
      TRISCbits.TRISC6 = 0;
      TRISCbits.TRISC7 = 1;
... }
 

I have no clue if it makes any difference in Proteus, but according to the datasheet you should set both tris bits.
Bit SPEN (RCSTA<7>) and bits TRISC<7:6> have to be
set in order to configure pins RC6/TX/CK and RC7/RX/DT
as the Universal Synchronous Asynchronous Receiver
Transmitter.
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products


Return to SCI/USART/EUSART

Who is online

Users browsing this forum: No registered users and 9 guests

cron