Discussion in "PIC Microcontroller Discussion" started by    amit_ele15    Sep 25, 2015.
Fri Sep 25 2015, 07:03 AM
#1
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. 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 and there might be problem in proteus simulation. please help..

#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()
{
unsigned int 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;

}
Tue Sep 29 2015, 05:33 PM
#2
are you testing this on Proteus? or MplabX simulator?

Get Social

Information

Powered by e107 Forum System

Conversation

scena
Wed May 20 2020, 03:59 AM
Does anyone have microcontroller programming software for STC15F104W (core 8051, 4 k, 128 byte, 6 I/O pin, UART ... DIL8), that works?
Sun May 03 2020, 01:08 AM
Tamu Gurung
Hi I am looking for a assembly language program for a digital decimal counter. Time counts max limit of 2 mins starting from 2.00 mins and ending at 0.00. time is to be displayed on 4 7-segment LED displays. Edsim51 simulator to be used. Thanks
Wed Mar 25 2020, 07:09 PM
Tajammul
Sir kindly send the code file i2c for 8051
ExperimenterUK
Sat Mar 14 2020, 04:06 AM
@ Rohitnani,ABEBE..which project ?
Fri Mar 13 2020, 06:25 PM
ABEBE
how can get hadwere of this project
Rohitnani
Sat Mar 07 2020, 09:41 PM
Sir, Could you please send me the zip file input password....
Wed Feb 26 2020, 09:28 PM
Peter Parker
Is it possible to send multiple messages using only 1 GSM module?
Sat Feb 01 2020, 02:42 PM
Pearl Olin
Hello,
Wed Jan 22 2020, 10:46 PM
MINU CHAUDHARY
I m getting errors while simulation process during interfacing socket output and lcd... Can i get the report of ur project which is same as us.
Tue Jan 14 2020, 11:38 AM
shubham kharade
when i am trying to simulate the program in Proteus 8 professional, I am getting some errors as cannot find module file dhtxx.md can u help me

Downloads

Comments

Davidthils
Thu May 21 2020, 08:44 AM
Jakeror
Wed May 20 2020, 09:28 PM
motorCar
Wed May 20 2020, 03:05 PM
Vordrync
Wed May 20 2020, 07:52 AM
JadenjiT
Wed May 20 2020, 03:15 AM
scena
Tue May 19 2020, 06:08 PM
CodySiz
Tue May 19 2020, 02:22 PM
ClaireSat
Mon May 18 2020, 09:50 PM

Online

Guests: 40, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 01:37 AM

Members: 38211
Newest member: Davidthils
Forum Activity
xrxx Avatar
Posted By xrxx
Thu Apr 30 2020, 06:59 AM
BCD MULTIPLICATIONAssume that you have to 2 BCD nu...
liqian Avatar
Posted By liqian
Tue Apr 28 2020, 05:26 AM
I happened to see your post and I got many useful ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Apr 28 2020, 04:21 AM
The code is hard to read.Please zip it and attach ...
rogermado Avatar
Posted By rogermado
Sun Apr 26 2020, 01:53 AM
@ExperimenterUK, thanks for your reply. You'll fin...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sat Apr 25 2020, 05:42 PM
Some simulators handle displays better than others...
rogermado Avatar
Posted By rogermado
Sat Apr 25 2020, 03:59 PM
Hello everyone, sorry in advance if i'm not in the...
PHANTOM28 Avatar
Posted By PHANTOM28
Fri Feb 28 2020, 10:25 AM
Hi.I hope you all can help me. I am relatively ne...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Feb 21 2020, 05:05 PM
ajay_bhargav Avatar
Posted By ajay_bhargav
Fri Feb 21 2020, 04:45 AM
Yes you can do that.1. Create a channel @Thingspea...
ananyagupta Avatar
Posted By ananyagupta
Thu Feb 20 2020, 03:58 AM
With regards to the particular characters sent to ...