/*
#define F_CPU 8000000UL
#define myUBRR 51 // 9600 b/s
#define myU2X 0
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
void USART_Init( unsigned int speed)//initialization USART by given value of UBRR register, which should be calculated by BoudRate
/* if U2X=0
UBRR=Fosc/(16BaudRate)-1
BaudRate=Fosc/16(UBRR+1)
at U2X=1
UBRR=Fosc/(8BaudRate)-1
BaudRate=Fosc/8(UBRR+1)
*/
{
UBRRH = (unsigned char)(speed>>8);
UBRRL = (unsigned char)speed;
UCSRB=(1<<RXEN)|( 1<TXEN); //transmit and receive USART
#if (myU2X==1) //the following line will only be compiled if myU2X=1
UCSRA |= (1<<U2X); //transmit rate doublet
#endif
UCSRC = (1<<URSEL)|(1<USBS)|(1<<UCSZ1)|(1<UCSZ0);// 8-bit transmission, 1 stop bit
}
void USART_Transmit( unsigned char data ) //Transmit one byte to USART channel
{
while ( ((UCSRA & (1<<UDRE)) ); //Checking for data register clearing (readiness for the next byte loading)
UDR = data; //loading data into the register for transmitting
UCSRA |= (1<TXC); //Reset transmission end bit
}
int main (void)
{
char ch='O';
int i;
DDRC=0xFF;
USART_Init(myUBRR);
while (true) {
i=0;
do {
PORTC=i;//put loop number to C port, in order to prepare fixation of a new received symbol in the log-file
if (UCSRA & (1<RXC)){ //check USART channel information
ch=UDR;//read the receiver buffer to prepare receiving the next byte
if (UCSRA & ((1<FE)|(1<DOR))){ //check for a reception error
ch='.';//receive byte with error and replace with '.'
PORTC=0xFF;//for easy identification of the situation in the log file
}
//no reception errors
PORTC=ch;//write the received byte in the C port to be fixed in the log-file
USART_Transmit(ch);
i++;
}
}
while (i<10);
while ( ((UCSRA & (1<TXC)) ); //Check if the shift register is cleared (transfer completed)
//release and re-enable uart,
//to induce the necessity of a new receiving synchronization (search for the start bit)
UCSRB &= ~((1<<RXEN)|( 1<<TXEN));// disable UART receive-transfer, this resets the accumulated received bits in the buffer.
PORTC=0; // for fixing the moment of prohibiting UART in the log-file
for (i=0;i<(3*myUBRR);i++);//take the time equal to the time of transmission of some bits
UCSRB|=((1<<RXEN)|( 1<TXEN));//transmit/receive UART, start bit search
PORTC=1;// for fixing the moment of UART enable in the log file
}
}