init USART, AVRSTUDIO4 UCSRC&UBRRH wrong updated

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

I am using AVRSTUDIO4 togheter with ATMEGA16 device, include "m16def.inc"

When debugging following code with STUDIO4 i see that UCSRC & UBRRH are
always BOTH updated when one of them is changed.

Init USART
------------
clr R17
out UBRRH,R17
ldi R17,$47 ;9600Baud
out UBRRL,R17
@ ldi R17,(1<<RXEN)+(1<<TXEN)+(1<<RXCIE)
out UCSRB,R17
ldi R17,(1<<URSEL)+(3<<UCSZ0)
* out UCSRC,R17
sei

@ Receiver, Transmiter and Receive complete interrupt are enabled

* R17 contains 10000110
Most significant bit is 1, URSEL, so with should select UCSRC.
I See value 86H being filled in UCSRC and in UBRRH ??

Also, when i change the first line into ldi R17,$0A iso clr R17
Then also, on the out UBRRH,R17 instruction the UBRRH and the UCSRC are
Both updated with value 0A ??

Can anyone tell me what i am doing wrong

Remark: USART is NOT transmitting any data when program is loaded.

Thanks in advance,
Fabrizio

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

Hi,

Check out page 160 of the mega16 datasheet. Sorry, it is too involved to try to explain here (at least for me).

Regards,
Steve

admin's test signature
 

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

Steve,

I looked on page 160 and i don't see my error.

Let's resume,

UBRRH and UCSRC occupy the same I/O address $20

With URSEL bit, bit-7, the access difference is made.

URSEL Access
0 UBBRH
1 UCSRC

That is what i intended do do.

I clear R17, URSEL=0, and write to Address $20, so we address UBBRH

ldi R17,(1<<URSEL)+(3<<UCSZ0) ;Load R17 with $86
URSEL bit is set, writing to Address $20, we access UCSRC

I am suspecting The simulator. Both registers have the Same address.
And if you look well to the expanded view of URBBH you see that the upper nibble
is grayed out. The most significant bit however is the URSEL bit. (See page 165 in the datasheet)

Regards,
Fabi,

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

Hi,

The init USART assembler program, given in previous mail, IS working when loaded into the DEVICE. There was a little HW problem with the MAX232.

So the simulator is updating URBBH and UCRSC wrongly!

regards,
fabi,

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

Your suspicion is correct,

Due to the variations of the USART, the simulator implementation of USART is erronous on older parts (including mega32).

Newer parts does not have the feature of shared register addresses. For instance, the tiny2313 (http://www.avrfreaks.com/redirec...). Look at page 133, and you'll find the URSEL bit is reserved.

The following piece of code tests for this (and passes on the ICE50, for instance). It is based on your initial code sample.

The work-around for this is for the older parts to ignore the double-writing of registers. There will possibly be a fix for this on a future update of AVR Studio, no promises, though.

; This demonstrates erronous behaviour of the usart
; in (at least) mega32
; KEYWORD: Shared address for register UCSRC and UBRRH

.include "m32def.inc"

; Init test, clear registers
clr R17
out UBRRH,R17
out UBRRL,R17
out UCSRA,R17
out UCSRB,R17
out UCSRC,R17

; High data register empty
;clr R17
;out UBRRH,R17

; set data rate to 9600 baud
ldi R17,$47 ;9600Baud
out UBRRL,R17

; set receive and transmit enable +
; receive complete interrupt enabled
ldi R17,(1

regards,
- Jo

admin's test signature