UCSR0C not getting set

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm trying to use the UART0 on a Mega162 but I'm having problems setting UCSR0C. Here is my UART initialization function:

void uart_init(void) {
   uint8_t tmp;
   
   rx_buf_head = 0;
   rx_buf_tail = 0;
   tx_buf_head = 0;
   tx_buf_tail = 0;
   
   tmp = SREG;
   cli();
   
   UBRR0H = UBRRH_VALUE;
   UBRR0L = UBRRL_VALUE;
#if USE_2X
   UCSR0A = _BV(U2X0);
#else
   UCSR0A &= ~_BV(U2X0);
#endif
   UCSR0B = _BV(RXCIE0)| _BV(RXEN0) | _BV(TXEN0);
   UCSR0C = _BV(URSEL0) | _BV(UPM01) | _BV(UCSZ01);
   
   SREG = tmp;
   
   return;
}

When writing to UCSR0C I set the URSEL0 bit as the datasheet states I should. Running through the code with my JTAGICE, I can see that OUT instruction does nothing. If I set the bits manually in the IO Viewer window, they are immediately cleared the next cycle. What is going on here?!

Thanks,
Jevin

Math is cool.
jevinskie.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You haven't read the datasheet about reading/writing to that shared register have you?

(you need to use 2 back to back opcodes to read it twice in 4 cycles THEN stop the JTAG at a breakpoint and look at what's in the register - this almost certainly means (if using C) that you MUST have optimisation enabled)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I did read that part of the datasheet but I thought that Studio would take care of the double reads for me. Reading it into a register twice showed that it is indeed getting set. Thanks for the bonk on the head!

-Jevin

Math is cool.
jevinskie.com