Discussion in "PIC Microcontroller Discussion" started by    amit_ele15    Sep 25, 2015.
Fri Sep 25 2015, 11: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, 09:33 PM
#2
are you testing this on Proteus? or MplabX simulator?

Get Social

Information

Powered by e107 Forum System

Conversation

Sun Oct 13 2019, 02:02 AM
Aline Hutchinson
Hey there,
ExperimenterUK
Sat Oct 12 2019, 10:23 PM
Log in.. then post your code in the 8051 forum
Sat Oct 12 2019, 10:18 AM
Bhadra Sajikumar
i am doing a countdown timer with 8051 for my project, unfortunately our professor had to take a leave of absence before he could teach us most of it.. can someone explain the .asm code of it for me?
Mon Sep 23 2019, 07:37 AM
Nathaniel
I am doing a project for my schools wireless security system with cameras,sensors and spot lights using 8051 micro controller, could you help me out with the design and its source code.
Wed Sep 18 2019, 08:48 PM
Caryn Kover
Thank you for your time,
ExperimenterUK
Mon Aug 05 2019, 07:09 PM
@Ismail ..post in forum, give details
Mon Aug 05 2019, 04:33 PM
Ismail balghmi
Hello. Please I won't download
ExperimenterUK
Wed Jul 24 2019, 12:47 AM
@BUSTER123 Please post in the forum
Tue Jul 23 2019, 06:13 PM
BUSTER123
hello! newbie here. i am kind of a guy who hates using ready - made existing libraries, so this helped a lot as i was trying to control and write to a character lcd display. however, i can't figure out how to set the cursor position. for example, i want to set it to the 5th column in the first row, so position 5. so according to the table, i am sending 10000101 on the 8 data lines of the lcd (register select pulled low). the cursor does not move there however. and i cant move the cursor left or right too. is it something wrong that i am doing? thank you.
ExperimenterUK
Thu Jul 11 2019, 07:55 PM
@ramos https://www.theengineeringprojects.com/2017/09/l298-motor-driver-library-proteus.html

Downloads

Comments

ilylily07
Tue Oct 15 2019, 04:32 PM
hientran123
Tue Oct 15 2019, 02:27 AM
Sat Oct 05 2019, 02:38 PM
zexalo
Thu Oct 03 2019, 09:53 AM
thanhle12345
Thu Oct 03 2019, 07:37 AM
ramon_vsc
Fri Sep 20 2019, 04:53 PM
youssoufba
Thu Sep 19 2019, 11:57 AM
Taiphan000
Tue Sep 17 2019, 04:21 AM

Online

Guests: 41, Members: 0 ...

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

Members: 38097
Newest member: ilylily07
Forum Activity
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 03:09 PM
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 03:02 PM
sir, when i on display ,sometime 3rd display is no...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 02:53 PM
sir, when i on display ,sometime 3rd display is no...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sun Oct 20 2019, 12:21 AM
Your code seems okay on my simulationTry my code o...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sun Oct 20 2019, 12:21 AM
Your code seems okay on my simulationTry my code o...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sat Oct 19 2019, 07:13 PM
please find video attachment. 3rd & 4th displa...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sat Oct 19 2019, 07:05 PM
sir, 3rd and 4th dot matrix display flickering, ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Oct 18 2019, 09:18 PM
Hopefully you understand some of the program.Which...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Thu Oct 17 2019, 09:38 PM
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu Oct 17 2019, 08:35 PM
Do you understand how this code works?If so, addin...