Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] PIC16f877a with 4 mhz oscillator problem

Status
Not open for further replies.

Bijesh Kawan

Newbie level 5
Joined
Feb 26, 2015
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
103
hey friends
I have got and problem with the 4 Mhz oscillator with pic16f877a.I have programmed a code using mplab for sending string to labview and when i check the output value from the PuTTY software I got and output as in picture i attached instead of getting string.But in proteus simulation it gives and exact string written in code.
Please help me to figure out the problem.
Thanks in advance
Untitled.png
Code:
#include<pic.h>
void main()
{
	TRISD=0x00;

TXSTA=0b00100100;  //configure serial port 
RCSTA=0b10010000;  //configure serial port
 SPBRG=25; 
 	while(1)
	{
			for(int i=0;i<10;i++)
			{
			TXREG='F'; 
   			delay(100);
			TXREG='2';
	delay(100); 
			TXREG='0'; 
				delay(100);
TXREG='2'; 
	delay(100);
				TXREG='1'; 
		delay(100);
		TXREG='2'; 
	delay(100);
			TXREG='0'; 
	delay(100);
			TXREG='0'; 
	delay(100);
			TXREG='0'; 
	delay(100);
			TXREG='4';
	delay(100);
			TXREG='5';
	delay(100);
			TXREG='0';
	delay(100);
			TXREG='0';
			
	delay(100);
			TXREG='5';
	delay(100);
			TXREG='2';
	delay(100);
			TXREG='5';
	delay(100);
			TXREG='0';
	delay(100);
			TXREG='0';
	delay(1000);
			} 
}
}
 

The normal way of initialising a peripheral is to configure it completely and then enable it. In you code you actually enable when you set the TXSTA register.
You have not shown any of the configuration settings so I'll assume that the Fosc really is at 4MHz and further, working back from your SPBRG settings, that you are working at 9600 baud.
I don't know how to interpret the delay statements - are you intending to delay 100uSec???
At 9600 baud, it will take 104uSec for the hardware to send each bit and as ther are 10 bits per character (8 bits character plus start and stop bits) that means 1040uSec for each character.
However using delays is certainly not the correct way to send characters through the UART. You are better to write a short function that will take the character you want to send as a parameter. The function should then check that the UART buffer is free by spinning on the TRMT bit. Then write the character the the buffer. If you really want to you can also wait until the character has been sent completely before returning but as long as you are doing the 'buffer free' check at the start then that will be OK.
By the way, what you are seeing on the terminal is typical of a baud rate error. Therefore you should also make sure that your oscillator is at the correct frequency.
However I suspect that you are over-writing the transmit buffer because you are not waiting for the previous character to be completely sent and therefore you are sending a corrupted bit stream.
This is one of the reasons why I do not trust simulators - they do not properly simulate the real hardware and you can end up with really bad and error prone code that appears to work.
Susan
 
Thanks for your response.I finally managed to get the output..
Untitled.png
 

For the benefit of others who may come across this thread, what was your issue and how did you fix it?
I mentioned several things that it might be but which of them helped you?
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top