Saturday, March 17, 2012

Receive string through UART

In this tutorial I am going to show you how to receive a string through UART. In this program we are going to make use of the previous function which were written for receiving the character through UART. Don't forget to put NULL character at the end of the string.....
#include<avr/io.h>

void UART1_Init()
{
//set baud rate = 9600 bps
UBRR1H = 0x00;
UBRR1L = 103;         //0x67

//set 1 stop bit, no parity bit and 8 bit charachter size
UCSR1C = 0x06;         //(1 << UCSZ1)|(1 << UCSZ0)  

//enable transmission and reception
UCSR1B = 0x18;         //(1 << RXEN)|(1 << TXEN)
}

//Transmit character through UART
void UART1_Tx_Char(unsigned char data)
{

//put the data to be transmitted into the UDR register
UDR1 = data;

//wait until the transmission is completed
 while(!(UCSR1A&(1<<UDRE)));
}



//Transmit string through UART
void UART1_Tx_Str(unsigned char * str)
{

while(*str)
{
UDR1 = *str++;
while(!(UCSR1A&(1<<UDRE);

}

//Receive a character through UART
unsigned char UART1_Rx_Char()
{
//wait for the charater
while(!(UCSR1A & (1<<RXC)));

//return the received charater
return(UDR1);
}

//Receive string through UART
unsigned char * UART1_Rx_Str()
{
 unsigned char string[20], x, i = 0;

//receive the characters until ENTER is pressed (ASCII for ENTER = 13)
while((x = UART1_Rx_Char()) != 13)
{
 //and store the received characters into the array string[] one-by-one
string[i++] = x;
}

//insert NULL to terminate the string
string[i] = '\0';

//return the received string
return(string);
}

void main()
{
//initialize the UART circuitry
UART1_Init();

//Transmit the Received string onto the UART again
UART1_Tx_Str( UART1_Rx_Str());
}

12 comments:

Anonymous said...

Thank you so much. I just don't understand this line:

return (password);

the data is stored in strung[i], why you didn't return string in this case.

Unknown said...

Your code definitely helped me, Thank you very much..
I was stuck in receiving strings through uart.. but this code piece saved me..

Anonymous said...

buggy code...

Ashish Gaur said...

Hi Anonymous....It wud be better if you cud tell me what caused a problem. That way i wud be able to rectify it as well.

Unknown said...

Hi Ashish, Your code has helped me with the receiving of strings.

Thank You
However, it is not always possible for me to know the length of the string to be received.
Sometimes may be more than 20 characters received, sometimes less.
How to work around this?

Ashish Gaur said...

In that case, you can have a global pointer and receive the string in that pointer only....you can store as many bytes as you want.

Unknown said...

Thank you for helping us...

أحمد شعبان حلوة said...

the pointer string must be guaranteed to be either a global one or defined as static to extend its life time. Otherwise the in-function array string[20] won't be accessible to any other routine or the main function; respectively.

Unknown said...

thank you very much

Anonymous said...

Am grateful. I didn't even know how to receive a sting from virtual terminal...thank u so much

yyouuseef said...

love you man xD

Ouss said...

Love you so much :D

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews