cstringavruartavr-studio5

Receive a String


How to received string in uart. I am using avr studio 5 and brays terminal like this one

enter image description here

Same as this picture I am using baudrate of 9600. Went I try to type "abcdef", it only come out "abcf" .

My code are like this --->

#include <avr/io.h>

void serial_init(void)
{
    UBRRH = 0x00;
    UBRRL = 95;   //baudrate 9600 and F_CPU 14745600UL
    UCSRB =  (1 << RXEN) | (1 << TXEN) | (1<<RXCIE); 
    UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0)|(1 << UCSZ1);
}

unsigned long long Usart_Receive(void)          
{
    while((UCSRA & (1 << RXC)) == 0) {};
    return UDR;
}
void USART_Transmit(unsigned long c)   
{
    PORTD= 0b00000100;  //RTS Enable
    while ((UCSRA & (1 << UDRE)) ==0) {};
    UDR = c;
    PORTD= 0b00000000;  //RTS Disable
}

int main(void)
{
    unsigned char data;
    serial_init();
    while (1)
    {
        data = Usart_Receive();
        _delay_ms(100);
        USART_Transmit(data);
    }
    return 0;   
}

Simple as that, but I cannot find my problem why it only appear 4 letter at the terminal. I hope somebody can help about this. THANK.


Solution

  • Here are the answer that are work.

    int main(void)
     {
    
        char data[1][40];   
        int i1 = 0;
        int i2 = 0;
            serial_init();
    
            while (1)
        {
    
            for (i1=0;i1<1;i1++) 
                { 
                   for (i2=0;i2<40;i2++) 
                   { 
                      data[i1][i2] = Usart_Receive(); 
                   } 
                } 
    
               for (i1=0;i1<1;i1++) 
               { 
                   for (i2=0;i2<40;i2++) 
                   { 
                      Usart_Transmit(data[i1][i2]); 
                   } 
               }       
    
          }  
        return 0;   
    }
    

    Thank for those who are helping me before this.