#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());
}