Search code examples
stringavruart

AVR UART String transmission (not every char is transmited)


I program an AtMega 32 UART module. I try to send strings with my function, but it works in very special way. In general it sends some chars from that string. Some, not all. I wrote that function in three different ways, every time with the same result. I use RS232/USB converter to check how is it works.

This is my program, and below are results of transmission.

#define F_CPU 8000000
#include <avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>
#include<string.h>

#define BAUD_RATE 19200
#define BAUD_PRESCALER (((F_CPU / (BAUD_RATE * 16UL))) - 1) 

static void BAUD_SETTINGS(){
    UBRRH=(BAUD_PRESCALER>>8);
    UBRRL=BAUD_PRESCALER;
}

static void TRANSMIT_SETTINGS(){
    UCSRB=(1<<TXEN) | (1<<RXEN) | (1<<RXCIE);
    UCSRC=(1<<UCSZ0) | (1<<UCSZ1) | (1<<URSEL);
}
   
void UART_TX(unsigned char znak);
void UART_String_TX(char *command);

//MAIN//
int main(void)
{
    
    BAUD_SETTINGS();
    TRANSMIT_SETTINGS();
        
    DDRA=0xff;
    PORTA=0xff;
    PORTD=0x02;
    sei();
    
    _delay_ms(1500);
    for(;;){
        PORTA=0xff;
        UART_String_TX("Hello World!");
        _delay_ms(1000);
    }
}

void UART_TX(unsigned char znak){
    while(!(UCSRA & (1<<UDRE)));
    UDR = znak;
}

void UART_String_TX(char *command){
    
while(*command != 0x00){
    UART_TX(*command);
command++;}
}
    
ISR(USART_RXC_vect){
    char RXC_byte;
    RXC_byte=UDR;
    UDR=RXC_byte;
    PORTA=0x00;
}

There are two other variants of sending function, but it works the same.

/*
    void UART_TX(char *command){
        
        while(*command){            / *!='\0'* /
            if(UCSRA&(1<<UDRE)){
                UDR=command;
                command++;
            }
        }
    }*/
    /*
    void UART_TX(char *command){
        int i=0;
            while(command[i]){          / *!='\0'* /
                while(!(UCSRA&(1<<UDRE)));
                UDR=command[i];
                while(!(UCSRA&(1<<UDRE)));
                i++;
                        
        }
    }*/

Result of sending "Hello World!" few times (with 1s delay) is:

loWrd!

loWrd!

elë]Wrd!

el od!

el ol!

Hl ol!

HloWrd!

Hl ol!

HloWr!

HloWo#‘!

Memory adresses of that chars are (from other text):

97 99 101 103 104 106 108 110 112 113 115 117

96 98 100 102 104 106 107 109 111 113 114 116 117

96 98 99 101 103 105 107 108 110 111 113 115 117

There is no problem when i use "echo" function with RXC interrupt.

What's going on?


Solution

  • Usually, the problem is with data trasnfer baud rate. First of all check the settings on your PC (it should be the same as on MCU). If you use internall oscillator it might be defective, try to use external.