atmega2560 usart test in assembly

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

Hi Guys,

 

Just trying out a new atmega2560 board I recently got from ebay.

(I think its an arduino R3)

It is wired to use USART0.

 

I didn't post in the arduino forum because I an not using the arduino IDE.

I only know how to code in assembly and I have no desire at my age to ever learn C.

 

I am using the code example from the datasheet, however I can't get the code to work. 

 

Can anyone see what the problem is?

It should work.

 

I have checked the fuses, all programmed correctly.

low: 0xff, high: 0xD8, ext: 0xFD

;
.nolist
.include "m2560def.inc"
.list
;
.def	temp		= r16	;
.def	temp1		= r17	; used for sending data to device/host
;
.cseg
.org	0
;
Reset:
	jmp   Start      ;
;****************************************************
Start:
	ldi	temp,LOW(RAMEND)	;set up stack pointer
	out	SPL,temp
	ldi	temp,HIGH(RAMEND)
	out	SPH,temp

	call	USART_Init
;----------------------------------------------------
MainLoop:
	RCALL	Usart_RX
	RCALL	Usart_TX
	rjmp	MainLoop
;-----------------------------------------------------
.equ   baud=9600      ;Baud rate
.equ   fosc=16000000   ;Crystal frequency
;-----------------------------------------------------
USART_Init:
	ldi temp1, high(fosc/(16*baud)-1)
	ldi temp, low(fosc/(16*baud)-1) 

	sts UBRR0H, temp1		; Set baud rate
	sts UBRR0L, temp

	ldi temp, (1<<RXEN0)|(1<<TXEN0)	; Enable receiver and transmitter
	sts UCSR0B,temp

;	ldi temp, (1<<USBS0)|(3<<UCSZ00)	; Set frame format: 8data, 2stop bit
	ldi temp, (0<<USBS0)|(3<<UCSZ00)	; Set frame format: 8data, 1stop bit
	sts UCSR0C,temp
	ret
;-----------------------------------------------------
Usart_TX:
	lds	temp,UCSR0A		;
	sbrs	temp,UDRE0		;Wait for empty transmit buffer
	rjmp	Usart_TX		;

	sts	UDR0,temp1		; Put data (temp1) into buffer, sends the data
	ret
;-----------------------------------------------------
Usart_RX:
	lds	temp, UCSR0A	; Wait for data to be received
	sbrs	temp, RXC0
	rjmp	Usart_RX

	lds temp1, UDR0		; Get and return received data from buffer
	ret
;-----------------------------------------------------
.exit

 

 

This topic has a solution.
Last Edited: Sat. Jan 12, 2019 - 09:57 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

On the surface that looks right. The key thing usually is the UBRR setting but 16MHZ sounds right for an Arduino

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

forget about rx now, just load 'q' into  temp1 & use tX....get that working first..once you see qqqq..... you will be on your way

 

you may have to set TX port pin as an output (I don't remember if that is true), but it won't hurt

When in the dark remember-the future looks brighter than ever.

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

TXEN also takes control of the pin as output (it's Xmega (and possibly derivatives?) where direction needs to set separately.
Capital 'U' is a good test character to send because if you have a scope/analyser /meter that can read period/frequency the 10101010.. constant pattern makes checking for 9600/104us easy

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

Default is 8N1, no need to mess with UCSR0C if that is what you want.

 

Send "U" in loop to test output, produces a square wave at frequency of your baud rate, makes checking your cpu frequency and baud rate calcs easy.

 

Once that works, then try receiving with a known working TX function.

 

 

Jim

 

 

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

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

newbie123 wrote:
I only know how to code in assembly and I have no desire at my age to ever learn C.

A C compiler is just a really nice macro assembler!  smiley

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

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

Greetings -

 

A couple of pertinent questions:

 

1. How are you observing the serial output?

 

2. What does "doesn't work" mean? In this context, it might mean (a) no detectable serial, or (b) serial that is incorrect

 

3. Do you have access to an oscilloscope or logic analyzer?

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

Thanks Jim,

 

I am observing the serial output from the terminal.

The chars typed in the terminal should echo back.

Nothing is displayed.

 

The exact same setup works with an atmega328.

Nothing has changed except the board & the code of course.

 

There are two TX and RX led's.

When I press a key in the terminal I can see the TX led go on.

The RX LED should go on when the byte is echoed back.

 

Yes I do have an oscilloscope & a logic analyzer.

I wanted to be sure the code was good before I start troubleshooting the hardware.

 

I will do some more troubleshooting and respond afterwards.

Thanks

 

 

 

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

Here is a screenshot with my saleae logic analyzer. (fantastic tool by the way!)

I am pressing the number "1" on the keyboard. 

9600 baud rate.

the file "keypress.jpg" is the screenshot of the non working atmega2560 board.

 

the file "screenshot.jpg" is what it should look like taken from the working atmega328 board.

 

the labels are wrong in the logic analyzer setup disregard.

(RX1 should be TX0)

 

 

Attachment(s): 

Last Edited: Fri. Jan 11, 2019 - 07:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

Default is 8N1, no need to mess with UCSR0C if that is what you want.

 

Send "U" in loop to test output, produces a square wave at frequency of your baud rate, makes checking your cpu frequency and baud rate calcs easy.

 

Once that works, then try receiving with a known working TX function.

Jim

 

 

tried that. Sending a "U" is working so I guess its the reception.

 

Could be the shitty CH340 USB to Serial chip. I am going to try and bypass it using a good USBtoTTL converter.

The bit duration is the same 104uS So I guess the baudrate is correct (besides it shows up in the terminal)

Attachment(s): 

Last Edited: Fri. Jan 11, 2019 - 07:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

newbie123 wrote:
Yes I do have an oscilloscope & a logic analyzer. I wanted to be sure the code was good before I start troubleshooting the hardware.

 

Normally one proves the h/w first, then works on the software!

 

Jim

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

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

Be sure you don't fall into the newbie technician trap..."its all wired up boss  .... I ohm'd out TX to TX & RX to RX"...NOPE!!!  TX goes to RX

When in the dark remember-the future looks brighter than ever.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

newbie123 wrote:
Yes I do have an oscilloscope & a logic analyzer. I wanted to be sure the code was good before I start troubleshooting the hardware.

 

Normally one proves the h/w first, then works on the software!

 

Jim

 

this software is a simple program to do just that (test the hardware)

 

I found the problem was this:

	ldi temp, (0<<USBS0)|(3<<UCSZ00)	; Set frame format: 8data, 1stop bit
	sts UCSR0C,temp

should be this:
 

    ldi temp, (2<<UMSEL00)|(3<<UCSZ00)    ; Set frame format: 8data, 1stop bit
    sts UCSR0C,temp

thanks to everyone who took the time to answer my question.

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

Why bother writing the C register anyway? The AVR power on default is 8N1 anyway.