I have written code to send data from controller to pc through serialport using interrupt but it echos garbage value exactly 3 times back.

    unsigned char index = UDR;

    UDR = index;        

void uartInit()
    UCSRB |= (1 << RXCIE); // Enable the USART Receive Complete interrupt (USART_RXC)

int main(void)


EDIT: Function used to set baud rate..

#define FOSC 16000000// Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1

void USART_Init( unsigned int baud )
/* Set baud rate */
UBRRH = (unsigned char)(baud>>8);
UBRRL = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 1stop bit */
UCSRC = (0<<USBS)|(3<<UCSZ0);
  • 1
    $\begingroup$ This frequently a result of the client baud rate being set incorrectly. $\endgroup$ – DaemonMaker Feb 27 '13 at 22:01
  • $\begingroup$ How exactly are you connecting your AVR to the PC? I assume you have a TTL-RS232 tranceiver setup with an IC such as the MAX232. Is that so? $\endgroup$ – georgebrindeiro Feb 28 '13 at 1:39
  • $\begingroup$ yes thats how it is connected. $\endgroup$ – amitkindre Feb 28 '13 at 9:35
  • 2
    $\begingroup$ @DaemonMaker - When there isn't enough information in the question to determine if a suggestion like this is an answer or not, it's probably best making a comment asking for clarification rather than posting a one line answer which may or may not be relevant. $\endgroup$ – Mark Booth Feb 28 '13 at 12:51
  • 1
    $\begingroup$ @DaemonMaker - I agree that there is a possibility that this is the reason and hopefully the edit will provide enough information for someone to answer the question authoritatively. $\endgroup$ – Mark Booth Feb 28 '13 at 19:43

You are setting the UBRR register incorrectly: it should not directly hold the desired baudrate, but a value that depends on your CPU speed (which you defined as the macro FOSC, but never used) and the baudrate. The exact calculation is described in the ATMega8535 datasheet (p.147).

See the AVR UBRR Calculator for easy calculation of the proper UBRR register values (note that the calculator assumes U2X = 0, as seen in Table 72, p.173).

EDIT: I just noticed your macro MY_UBRR, sorry about that. Are you calling USART_Init with baud = MY_UBRR? If so, I'll delete this answer.

  • $\begingroup$ I've just added your edit as a comment to the question to prompt the questioner to see if we can't resolve this. $\endgroup$ – Mark Booth May 30 '13 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.