Hi all,
I'm working with a Atmega2561.
I want to receive a character from rx uart and transmit it to tx. like here (lesson 5): http://brittonkerin.com/cduino/lessons.html
My code:
unsigned char message; void USART_init( void ) { int baudrate=12; // 4800 Bauds by default /* Set baud rate */ UBRR0L = (unsigned char)baudrate; UBRR0H = (unsigned char)(baudrate>>8); /* Enable Receiver and Transmitter */ UCSR0B = (1<<RXEN0)|(1<<TXEN0); /* Set frame format: 8data, 1stop bit */ UCSR0C = (1<<UCSZ01)|(1<<UCSZ00); } void USART_Flush( void ) { unsigned char dummy; while ( UCSR0A & (1<<RXC0) ) dummy = UDR0; } unsigned char USART_rx( void ) { while( ! (UCSR0A & (1<<RXC0)) ); return UDR0; } void USART_tx( unsigned char data ) { while( ! (UCSR0A & (1<<UDRE0)) ); UDR0 = data; } void USART_putstring( const char *StringPtr ) { int cmp = 0; while ( strlen( StringPtr ) != cmp ) { USART_tx( StringPtr[cmp] ); cmp++; } } /* Other */ void delay( uint32_t twait ) { while( twait-- ) asm volatile( "nop" ); } int main(void) { USART_init(); // Call the USART initialization code while(1) { message = USART_rx(); delay(50000L); USART_tx(message); } }
My problem is when i read the UDR0 register, the RXC0 bit does not fall down.
I found a dirty workaround (disable and re-enable the rx):
UCSR0B = (0<<RXEN0)|(1<<TXEN0); UCSR0B = (1<<RXEN0)|(1<<TXEN0);
I there something i do wrong?
Regards