Unable to write to UDR1 on ATMega2561

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

I'm trying to write several values to UDR1 to send out the USART1 on an ATMega2561 using JTAG ICE MkII and AVR Studio 4.18, but they never show up in the register, even if I disable transmit. I've also tried reading UDR1 to a variable immediately after writing and it stays at 0, so it really doesn't look like anything's getting there. What might be causing this? Here's my initialization and operating code; thanks for your help:

//UART1 initialize
// desired baud rate:9600
// actual baud rate:9615 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart1_init(void)
{
 UCSR1B = 0x00; //disable while setting baud rate
 UCSR1A = 0x00;
 UCSR1C = 0x06;
 UBRR1L = 0x67; //set baud (9600) rate lo
 UBRR1H = 0x00; //set baud (9600) rate hi
 UDR1 = 0;  // Clear transmit register
 UCSR1B = 0x08;  // Enable transmit
// UCSR1B = 0;  // Disable transmit
}


		   while ((UCSR1A & (1 <<UDRE1)) == 0);  // Wait until UDR is empty
		   UDR1 = 0x80;	// Init byte
		   while ((UCSR1A & (1 <<UDRE1)) == 0);  // Wait until UDR is empty
		   UDR1 = 0;	// Servo number
		   while ((UCSR1A & (1 <<UDRE1)) == 0);  // Wait until UDR is empty
		   UDR1 = 4;	// Command:  Set position, absolute
		   while ((UCSR1A & (1 <<UDRE1)) == 0);  // Wait until UDR is empty
		   UDR1 = LEFT_0_D1;	// Send high bits of position
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When you store a byte in the UDR, it hops into the shift register and starts shifting out. The UDR looks empty if you read it back. This is probably the way they program the simulator to work. How about getting a Real AVR and actually sending the chars out the tx pin and looking at them on a scope?

Imagecraft compiler user

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

The scope is next, but since it's being emulated (not simulated), I figured it would be pretty accurate. Plus, since the problem occurs even when transmit is disabled, I figured something else was going on. Shouldn't the bits just stay there in that case? And, I thought UDRE1 is supposed to measure if UDR1, not the shift-register, is empty; if so, if UDR1 were cleared as soon as you loaded it, UDRE1 would always be set a clock cycle after loading UDRE, wouldn't it?

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

Reading UDR will always give you what was received by the USART, not what was sent. Though the input and output share the same address, they are different registers. You will never be able to read back what you wrote.

Regards,
Steve A.

The Board helps those that help themselves.

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

Well, what should I expect to see during emulation? How do I confirm a byte was entered into UDR and subsequently sent, short of using a scope?

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

Quote:
Well, what should I expect to see during emulation?
Who knows? How perfectly do emulators imitate the real silicon. Just build the hardware and do the real thing!

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

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

Quote:

Well, what should I expect to see during emulation? How do I confirm a byte was entered into UDR and subsequently sent, short of using a scope?


Use Hapsim? Or Vmlab.

But here's a mad idea - forget simulators and see what happens on a real AVR.

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

Quote:
How do I confirm a byte was entered into UDR and subsequently sent
Why would you expect it to fail?

Regards,
Steve A.

The Board helps those that help themselves.