#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());
}
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<
//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:
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.
Your code definitely helped me, Thank you very much..
I was stuck in receiving strings through uart.. but this code piece saved me..
buggy code...
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.
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?
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.
Thank you for helping us...
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.
thank you very much
Am grateful. I didn't even know how to receive a sting from virtual terminal...thank u so much
love you man xD
Love you so much :D
Post a Comment