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-:
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;
}