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] USART not receiving correct string value

Status
Not open for further replies.

Ranbeer Singh

Full Member level 5
Joined
Jul 30, 2015
Messages
259
Helped
22
Reputation
44
Reaction score
22
Trophy points
1,298
Location
Faridabad India
Activity points
3,266
Hello,

I am not able to read string value from USART and save it in variable. I am receiving string value from other controller at same settings of USART registers. I am saving received value in a string variable, but in variable save a garbage value.

My codes-:
Code:
#include <p18f2520.h>
#include <usart.h>
#include <stdio.h>

void write_to_USART(const rom char *data);
char *Receive_string_USART(void);
char *val = "Pre-text";

void main()
{
	TRISC = 0xFF;
	TRISB = 0xF0;
	PORTB = 0x00;
	ADCON1 = 0x0F;

	BAUDCON = 0x00;			// 8-bit Baud Rate Generator, 
	SPBRG = 64;			// 19200 baud rate at 20Mhz oscillator.  0.16% error.
  	TXSTA = 0x24;			// 8 bit mode, Transmit enable, Asynchronous mode, High speed mode
	RCSTA = 0x90;			// Sreial enable, Receiving enable
	write_to_USART(val);

	while(1)
	{
		if(DataRdyUSART())
		{
			val = Receive_string_USART();
			PORTBbits.RB0 = ~PORTBbits.RB0 ;
		}

		if(PORTCbits.RC4 == 1)		
		{
			while(PORTCbits.RC4);
			PORTBbits.RB1 = 1;
			write_to_USART(val);
		}
	}
}

void write_to_USART(const rom char *data)
{
	do
  	{
    		while(BusyUSART());
    		TXREG = *data;
  	} while(*data++ != 0);
}

char *Receive_string_USART()
{
	char buffer, *save_in;
	do
  	{                               // Receive a byte at a time
    	while(!DataRdyUSART());
    	buffer = RCREG;    		// Get a character from the USART
                           		// and save in the buffer
	*save_in = buffer;		// Save value in pointer
	save_in++;		        // Increment the pointer
		
	// If i put USART data sending code hare, it works fine. Exa- TXREG = buffer;.	

  	} while( buffer != 0);		// Untill NULL 
	return save_in;
}
 

Hi,

If I understand right, then sending from microcontroller is working, but not receiving.

If so:
* Show us your schematic
* what is the communication partner? PC? What is it's setup?
* what is your clock source? Xtal?
* send a known pattern. Maybe 0x30, and show us what the microcontroller receives.

Klaus
 

I try to shine it in deep....

Controller receiver and transmitter are working fine.

* Show us your schematic
It is just two points schametic. RX to TX, TX to RX & 20 mhz crystal with 15PF capacitors. 3 core twisted wire less then 0.5 mtr long.



* what is the communication partner? PC? What is it's setup?
I tried with controller (Pic18f2520) to controller (Pic18f2520) communication. But serial comm. was not working so now i am debugging it with PROTEUS.



* send a known pattern. Maybe 0x30, and show us what the microcontroller receives.
Please look one sight on codes.
When i receive single byte, it's work fine. I have tried it with many of different ways.
Please see "// If i put USART data sending code hare, it works fine. Exa- TXREG = buffer;. "

Code:
char *Receive_string_USART()
{
	char buffer, *save_in;
	do
  	{                               // Receive a byte at a time
    	while(!DataRdyUSART());
    	buffer = RCREG;    		// Get a character from the USART
                           		// and save in the buffer
	*save_in = buffer;		// Save value in pointer
	save_in++;		        // Increment the pointer
		
	// If i put USART data sending code hare, it works fine. Exa- TXREG = buffer;.	

  	} while( buffer != 0);		// Untill NULL 
	return save_in;
}


- - - Updated - - -

I think function codes working fine, but not saving value in variable val.
 

I think function codes working fine, but not saving value in variable val.
True, you are using an uninitialized pointer char *save_in without assigned char buffer.

A better way would be a function with receive buffer parameter

Code:
void Receive_string_USART(char *receive_to)

Good programming practice would also pass a maximum receive size to avoid writing out of bounds.

Code:
void Receive_string_USART(char *receive_to, unsigned char buffsize)
 
Good programming practice would also pass a maximum receive size to avoid writing out of bounds.

Code:
void Receive_string_USART(char *receive_to, unsigned char buffsize)
Thanks for your reply.

I do'not know what will the buffer size be.
 

I do'not know what will the buffer size be.
The size of the receiving buffer.

In your code, you have defined the buffer implicitely by specifying an initialized char buffer.
Code:
char *val = "Pre-text";

It would be better to use an independent, slightly larger receiver buffer.

Did you understand the first point of my post? The explanation why your code can't work?

By the way, array val is not rom char[], if it is, it can't receive data. I'm not sure how the compiler translates the assignment of a ram char pointer to the below function.
Code:
void write_to_USART(const rom char *data);
 
By the way, array val is not rom char[], if it is, it can't receive data. I'm not sure how the compiler translates the assignment of a ram char pointer to the below function.
Code:
void write_to_USART(const rom char *data);
It have a matter, When i try to make function without const rom char. PIC C18 compiler gives me a warning (Suspicious pointer conversion) when i send any string value like
Code:
 write_to_USART("This is text");
 

Save this text

"This is text"

as const rom char and then pass it to the function.
 
It have a matter, When i try to make function without const rom char. PIC C18 compiler gives me a warning (Suspicious pointer conversion) when i send any string value like
Code:
write_to_USART("This is text");

This is because you instructed the compiler to do so.

Code:
void write_to_USART(const rom char *data)

So the compiler waits from you to pass such an argument. You could follow Okada's suggestion, or change the function prototype instead, in order to pass a char* as argument.

Code:
void write_to_USART(char *data)
 
Thanks to all

#4
True, you are using an uninitialized pointer char *save_in without assigned char buffer.
It was correct answer of my problem. Golden viewpoint Mr. FvM.

I observed, it was implicit declaration of string pointers without assignment of buffer. The garbage value return due to mismatching of pointers memory address at writing and reading time. Changed my codes are given below and result is OK.

Code:
void Receive_string_USART( char save_in[])
{
	char buffer, a = 0;
	do
  	{                               // Receive a byte at a time
    	while(!DataRdyUSART());
    	buffer = RCREG;    		// Get a character from the USART
                                	// and save in the buffer
	    save_in[a] = buffer;	    // Save value in pointer
	    a++;
  	} while(buffer != 0);		// Untill NULL

and assigned char val[];
 
Last edited:

Try this


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
char uart_rd[];
 
void Receive_string_USART( char *ptr2Uart_rd)
{
    do
    {                               // Receive a byte at a time
        while(!DataRdyUSART());
        *ptr2Uart_rd = RCREG;           // Get a character from the USART
                                    // and save in the buffer
        
    } while(*ptr2Uart_rd++ != 0);       // Untill NULL
}
 
Receive_string_USART(&uart_rd);

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top