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] problem in receiving data using PIC16F873A

Status
Not open for further replies.

abhishekdixit

Full Member level 2
Joined
Dec 30, 2011
Messages
124
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Location
India
Activity points
2,182
hello,
i am usning PIC16F873A. and I want to communicate UART communication. i have successfully completed transmission of data serially. but their is a problem in receiving data. i am posting my code. please someone help me..
Code:
#include<htc.h>
#define _XTAL_FREQ 12000000
//unsigned char arr[]="ABHISHEK DIXIT\n\r";
void uart_trans();
void uart_receive();

unsigned char a;
void main()
{  
	 TRISC=0x80;
//	unsigned int i;
while(1)
{
 uart_trans();
//for(i=0;i<16;i++)
{
	//TXREG=arr[i];
	TXREG='A';
	TXREG='\n';
	TXREG='\r';
	__delay_ms(500);
	uart_receive();
	uart_trans();
	TXREG=a;
	while(1);

	
}
}
}

void uart_trans()
{
	SYNC=0;
	TX9=0;
	SPBRG=0X4D;
	BRGH=1;
	SPEN=1;
	TXEN=1;
}

void uart_receive()
{	
	SPBRG=0X4D;
	BRGH=1;
	SYNC=0;
	SPEN=1;
	RCIE=0;
	RX9=0;
	CREN=1;
	REN=0;
	while(RCIF==0);
	a=RCREG;
     __delay_ms(100);
        RCIF=0;
	CREN=0;*/
}

please help me.

Thank you
with regards,
ABHISHEK DIXIT
 

First of all people are not going to check every thing of your code,so you should provide proper information ,anyway I am doing it for you…

As SYNC bit is 0, you are using asynchronous mode. You are using 12 MHz for clock and your baud rate is 9600 as you set BRGH = 1 and SPBRG = 77 or 0x4D.

Now about your problem, you can’t receive anything, and it’s normal you will never be able to receive anything as per your code. In your code you didn’t used any interrupts so you must continuously check for receiving data ,other wise you will miss it ,you used a for loop and couple of times you called the function to receive data ,you cant predict when the data will arrive you have to check it continuously. Even though you check it continuously you wouldn’t receive any data as your receiving logic is incorrect. You have to keep CREN bit always on to receive data at any moment.

The code should be something like this, the program will receive a char and increment it and send it back. Clock 12Mhz,baud rate 9600,one stop bit and no 9th bit and no flow control ,full duplex communication

Normally I use 18F , it have EUSART which have an additional register BAUDCON for controlling the baud generator, it’s a little better ,their more things to consider while doing asynchronous serial communication, like framing error and overrun error.

Code:
void main()
{
 char temp = 0x00;
 
 PORTC = 0x00;
 TRISC = 0xFF;
 
 SPBRG = 0x4D;
 //Setting the TXSTA Register
 TX9 = 0;  // 6th bit - Selects 8-bit transmission
 TXEN = 1; // 5th bit - Transmit enabled, any write to TXREG will cause transmission.
 SYNC = 0; // 4th bit - Asynchronous mode
 BRGH = 1; // 2nd bit - Low speed ,depends on baud rate calculation
 
 //Setting the RXSTA Register
 SPEN = 1;  // 7th bit - Serial port enabled (configures RX/DT and TX/CK pins as serial port pins)
 RX9 = 0;   // 6th bit - Selects 8-bit reception
 CREN = 1;  // 4th bit - Enables receiver
 ADDEN = 0; // 3rd bit - Disables address detection

 while(1)
 {
  //Receive data
  while(!RCIF){};
  temp = RCREG; 

  //Transmit back data, increment the char and send it back
  temp++;
  while(!TXIF){};
  TXREG = temp;

 }
   
}

Good Luck

EDIT : I think EDA is having some kind problem ,it posted my post twice so now I have to edit it,and their is a huge problem with scrolling too.
 
Last edited:
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top