Issue in UART config

Hello All,

I am trying to write a code for UART interrupt.
SO first i am write a code on polling bases. this will work fine if i did not use setup or loop function.

#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
 
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)
 
//Declaration of our functions
void USART_init(void);
unsigned char USART_receive(void);
void USART_send( unsigned char data);
void USART_Print(char* StringPtr);
 
char String[]="";    
char String1[]="hello";    
unsigned char c = 0;
 
int main(void){
USART_init();        
while(1){        
    USART_receive();
    
    USART_Print(String);    
 }
 
return 0;
}
 
void USART_init(void){
 
 UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8);
 UBRR0L = (uint8_t)(BAUD_PRESCALLER);
 UCSR0B = (1<<RXEN0)|(1<<TXEN0)| (1<<RXCIE0);
 //UCSR0C = (3<<UCSZ00);
 UCSR0C = ((1<<UCPOL0) | (1<<UCSZ01) | (1<<UCSZ00)); 
}
 
unsigned char USART_receive(void){
 int i = 0;
 while(!(UCSR0A & (1<<RXC0)));
     String[i] = UDR0;
     i++;
 return UDR0;
 
}

void USART_send( unsigned char data){
 
 while(!(UCSR0A & (1<<UDRE0)));
 UDR0 = data;
 
}
 
void USART_Print(char* StringPtr){
  while(*StringPtr != 0x00){
    USART_send(*StringPtr);
    StringPtr++;
  }
}

/*ISR(USART0_RX_vect)

{
  USART_Print(String1);
}
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
 
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)
 char String[]="";     

void setup()
{
  UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8);
  UBRR0L = (uint8_t)(BAUD_PRESCALLER);
  UCSR0B = (1<<RXEN0)|(1<<TXEN0)| (1<<RXCIE0);
  //UCSR0C = (3<<UCSZ00);
  UCSR0C = ((1<<UCPOL0) | (1<<UCSZ01) | (1<<UCSZ00)); 
}
void loop()
{
  USART_receive(); 
  USART_Print(String);  
}

unsigned char USART_receive(void){
 int i = 0;
 while(!(UCSR0A & (1<<RXC0)));
     String[i] = UDR0;
     i++;
 return UDR0;
 
}

void USART_send( unsigned char data){
 
 while(!(UCSR0A & (1<<UDRE0)));
 UDR0 = data;
 
}
 
void USART_Print(char* StringPtr){
  while(*StringPtr != 0x00){
    USART_send(*StringPtr);
    StringPtr++;
  }
}

anyone tell me why this second code is not working? any specific reasons ?

And how to make this code workable? (with interrupt or with ISR)

In the second one, the "Arduino framework" initializes a hardware Serial. This probably interferes. You can check if in the second Serial.println(String1); will work.

@robtillaart

You are right...it is working but not perfectly... But Something is better than nothing. :)

Any idea how to make this interrupt based ? or how to use ISR ?

Any idea how to make this interrupt based ?

The handling of serial data is already interrupt based.

or how to use ISR ?

Which ISR?

You need to start with explaining why the arrival of one byte on the serial port should cause your program to drop everything and deal with that one byte RIGHT NOW.

@PaulS

I am trying to interface with GSM modem. So I am trying to write my own code instead using any library. This will give me more hands on practice & also give me more understanding.

or how to use ISR ?

Here i want to know UART ISR?

A reasonable starting point would be to look at the HardwareSerial class (HardwareSerial.cpp and HardwareSerial.h) and see what they do. Then base your code on the ideas that gives you.

You will probably need use a main function (rather than setup or loop) if you want to avoid the default initialization.