Electronic – How to send string in serial communication in AVR-C

avrccommunicationserialuart

So as the title states, instead of sending a byte of information at a time, I would like to be able to send a whole string. What I tried to do was that I tried to put the user input in an array and get each element of that array to be transmitted. However I was only able to get the first few letters to transmit not the whole array, here is my code:

//tx serial
#include <avr/io.h> 

#define F_CPU 16000000 
#define BUAD 9600 
#define BUAD_RATE_CALC ((F_CPU/16/BUAD) - 1)  


int main(void){  

    char ar[]= "hello";

    //High and low bits
    UBRR0H = (BUAD_RATE_CALC >> 8); 
    UBRR0L = BUAD_RATE_CALC; 
    //////////////// 
    //transimit and recieve enable
    UCSR0B = (1 << TXEN0)| (1 << TXCIE0) | (1 << RXEN0) | (1 << RXCIE0); 
    UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);  //8 bit data format
    ////////////////////////////////////////////////////////////////
    int i = 0;

    while (1){  

    while (( UCSR0A & (1<<UDRE0))  == 0){};
        for (i = 0; i < strlen(ar); i++){ 
            UDR0 = ar[i]; 
        }
    }
}

What would the problem be with this?

Best Answer

Not quite sure, just a fast guess. Maybe you should put the while(UDRE0 == 0){} inbetween sending characters. I believe it's to wait until one character has been sent.

for (i = 0; i < strlen(ar); i++){ 
   while (( UCSR0A & (1<<UDRE0))  == 0){};
   UDR0 = ar[i]; 
}