#define F_CPU 16000000UL // Defining the CPU Frequency
#include <avr/io.h> // Contains all the I/O Register Macros
#include <util/delay.h> // Generates a Blocking Delay
#define USART_BAUDRATE 9600 // Desired Baud Rate
#define BAUD_PRESCALER (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
#define ASYNCHRONOUS (0<<UMSEL00) // USART Mode Selection
#define DISABLED (0<<UPM00)
#define EVEN_PARITY (2<<UPM00)
#define ODD_PARITY (3<<UPM00)
#define PARITY_MODE DISABLED // USART Parity Bit Selection
#define ONE_BIT (0<<USBS0)
#define TWO_BIT (1<<USBS0)
#define STOP_BIT ONE_BIT // USART Stop Bit Selection
#define FIVE_BIT (0<<UCSZ00)
#define SIX_BIT (1<<UCSZ00)
#define SEVEN_BIT (2<<UCSZ00)
#define EIGHT_BIT (3<<UCSZ00)
#define DATA_BIT EIGHT_BIT // USART Data Bit Selection
#define RX_COMPLETE_INTERRUPT (1<<RXCIE0)
#define DATA_REGISTER_EMPTY_INTERRUPT (1<<UDRIE0)
volatile uint8_t GlobalTransmit;
// uint8_t GlobalTransmit;
static uint8_t j=0;
//volatile int GlobalTransmit;
void USART_Init()
{
// Set Baud Rate
UBRR0H = BAUD_PRESCALER >> 8;
UBRR0L = BAUD_PRESCALER;
// Set Frame Format
UCSR0C = ASYNCHRONOUS | PARITY_MODE | STOP_BIT | DATA_BIT;
// Enable Receiver and Transmitter
UCSR0B = (1<<RXEN0) | (1<<TXEN0);
sei(); //Enabling Global Interrupt
}
void UART_transmit_interrupt(char *data)
{
while(*data!='\0')
{
GlobalTransmit=data[j];
UCSR0B |= DATA_REGISTER_EMPTY_INTERRUPT; // Enables the Interrupt
}
}
void USART_TransmitPolling(char * DataByte)
{
uint8_t i;
while(*DataByte!='\0')
{
while (( UCSR0A & (1<<UDRE0)) == 0) {}; // Do nothing until UDR is ready
UDR0 = DataByte[i];
DataByte++;
}
}
void setup()
{
USART_Init();
USART_TransmitPolling("Treel");
UCSR0B |= RX_COMPLETE_INTERRUPT;
}
void loop()
{
//uint8_t LocalData[15]="TreelPvtltd\n";
// UART_transmit_interrupt(LocalData);
}
ISR(USART_UDRE_vect)
{
UDR0 =GlobalTransmit;
j++;
if(GlobalTransmit=='\0'){j=0;}
UCSR0B &= ~DATA_REGISTER_EMPTY_INTERRUPT; // Disables the Interrupt, uncomment for one time transmission of data
}
ISR(USART_RX_vect)
{
GlobalTransmit= UDR0;
UDR0 = GlobalTransmit;
}
/* In this program,
@Receiver side
>Treel is sent to enable serial monitor
>We write some text on Serial monitor console
> data is received in UDR0(storage buffer of UART), we write 1 to RXC0 bit, after receiving is
complete RXC0 bit is cleared after recepetion complete.
Till reception is not completed one byte data(i.e is only one character) is stored in UDR0 buffer
>We copy this one byte data in a user created pointer data.
>Pointer data stores in itself the actual data and the actual data is returned to the address of buffer(local_data) indicated in main program.
>One-One byte data is received in buffer(local_data)indicated in main program.
how the receiving data is stored in next array without we incrementing the array it is unknown to me
>We are copying the buffer data(local_data) to other buffer(buffer_data).(This step of our prcatice)
@Transmitter side
>We writing to serial monitor by using the USART_TransmitPolling function, we are passing buffer base address(buffer_data).
>In USART_TransmitPolling function we have pointer which points to the address of buffer(buffer_data) and picking up the actual data.
>Data byte by byte is passed to UART Buffer(UDR0) while it is cleared and while the string we are passing reaches till '/0'.*/