UDR register cleared before reading data

1.9k views Asked by At

I am trying to simulate the uart using ATmega128. I have written this code in AVR STUDIO 4.

The PORTB0 is for used switch so that when it is pressed it is connected to 5v dc and it sends 'a' to uart1. at other times it is connected to ground. the reception of data is by interrupt. Using debugger, when there is data in UDR1 and RXC1 is set, program jumps to ISR, and then UDR register is immediately cleared and nothing is retrieved. Can any one tell me why this happens?

Here is the code.

volatile unsigned char rxdata;

void uart_init(void)
{
    UCSR1A = 0x00;
    UCSR1B |= (1<<RXCIE1)|(1<<RXEN1)|(1<<TXEN1);    //0b10011000;
    UCSR1C |= (1<<7)|(1<<UCSZ11)|(UCSZ10);  //0b10000110;
    UBRR1H = 0;
    UBRR1L = 103; //9600 baud rate
}

ISR(USART1_RX_vect)
{
    rxdata = UDR1;
    PORTC = rxdata;
}

void putch(char data)
{
    while(!(UCSR1A & 0x20));
    UDR1 = data;
}

And the main program is

void port_init(void)
{
    DDRC = 0xFF;        
}

int main(void)
{
    port_init();
    uart_init();    
    sei();

    while(1)
    {
        if (PINB & 0x01){
            putch('a');         
        }
    }
}
2

There are 2 answers

1
Rev On

I took a look at the AVR Studio 4 help section. Regarding known simulator issues with respect to UART functions it states:

The UART/USART UDR register can only be modified from the application. Input via stimuli files or by modifying the I/O view etc is not possible.

0
ibschreiber On

I had this once. In my case, setting the breakpoint before the flag was evaluated in the code cleared it, because The AVR Studio "read" the flag (as I had the flag register open). Setting the breakpoint AFTER the line where the flag was read, helped. In your case, set the breakpoint on line PORTC = rxdata;

To get a better debug feeling, I read the flag into a variable right at the beginning of the ISR and set the breakpoint right after this.

It's been some years since this happened and I'm not even sure if this was really the case. So, maybe you can verify this ;)