Problem with Atmega16U2 USB2Serial code

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

Refer to the last 20 or so comments to:

 

For an updated version of this tutorial in PDF format, please see this page of my website.[/b...">[TUT] [SOFT] Using the USART - Interrupt driven serial comms 

 

For example:

115200 baud fails on an Arduino UNO, Nano and Mega2560 which all use the ATMega16U2.  This is tested with some sample codes posted in the comments of the Tutorial.

 

76800 baud works on all three.

 

115200 baud works on Chinese VISduino UNO R3 and a Chinese Nano style board which use the CH340.

 

1000000 baud works on an Arduino UNO and Mega2560 (I don't have an Arduino Nano) and also on the Chinese VISduino and Nano style boards.

 

 

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

Further testing with an Adafruit FTDI Friend usb2serial adapter connected to serial port1 on my Arduino Mega2560.

 

Works at 115,200; 230,400; 1,000,000.  also tested 230,400 and 2,000,000 in double speed mode.

 

Here is the code for the test of 115,200.

 

/*
 * s12560.asm
 *
 *  Created: 1/6/2015 10:22:49 PM
 *   Author: john
 */ 

 /*
 * u2560.asm
 *
 *  Created: 12/31/2014 11:09:19 PM
 *   Author: john
 */ 

;
.cseg

.org 0x0000 ;Places the following code from address 0x0000
        jmp RESET        ; Reset Handler
        jmp EXT_INT0    ; IRQ0 Handler
        jmp EXT_INT1    ; IRQ1 Handler
        jmp EXT_INT2    ; IRQ2 Handler
        jmp EXT_INT3    ; IRQ3 Handler
        jmp EXT_INT4    ; IRQ4 Handler
        jmp EXT_INT5    ; IRQ5 Handler
        jmp EXT_INT6    ; IRQ6 Handler
        jmp EXT_INT7    ; IRQ7 Handler
        jmp PCINT0L        ; PCINT0 Handler
        jmp PCINT1L        ; PCINT1 Handler
        jmp PCINT2L        ; PCINT2 Handler
        jmp WDT            ; Watchdog Timer Handler
        jmp TIM2_COMPA    ; Timer2 Compare A Handler
        jmp TIM2_COMPB    ; Timer2 Compare B Handler
        jmp TIM2_OVF    ; Timer2 Overflow Handler
        jmp TIM1_CAPT    ; Timer1 Capture Handler
        jmp TIM1_COMPA    ; Timer1 Compare A Handler
        jmp TIM1_COMPB    ; Timer1 Compare B Handler
        jmp TIM1_COMPC    ; Timer1 Compare c Handler
        jmp TIM1_OVF    ; Timer1 Overflow Handler
        jmp TIM0_COMPA    ; Timer0 Compare A Handler
        jmp TIM0_COMPB    ; Timer0 Compare B Handler
        jmp TIM0_OVF    ; Timer0 Overflow Handler
        jmp SPI_STC        ; SPI Transfer Complete Handler
        jmp USART0_RXC    ; USART0, RX Complete Handler
        jmp USART0_UDRE ; USART0, UDR Empty Handler
        jmp USART0_TXC    ; USART0, TX Complete Handler
        jmp ANA_COMP    ; Analog Comparator Handler
        jmp ADCR        ; ADC Conversion Complete Handler
        jmp EE_RDY        ; EEPROM Ready Handler
        jmp TIM3_CAPT    ; Timer3 Capture Handler
        jmp TIM3_COMPA    ; Timer3 Compare A Handler
        jmp TIM3_COMPB    ; Timer3 Compare B Handler
        jmp TIM3_COMPC    ; Timer3 Compare c Handler
        jmp TIM3_OVF    ; Timer3 Overflow Handler
        jmp USART1_RXC    ; USART1, RX Complete Handler
        jmp USART1_UDRE ; USART1, UDR Empty Handler
        jmp USART1_TXC    ; USART1, TX Complete Handler
        jmp TWI            ; 2-wire Serial Interface Handler
        jmp SPM_RDY        ; Store Program Memory Ready Handler
        jmp TIM4_CAPT    ; Timer4 Capture Handler
        jmp TIM4_COMPA    ; Timer4 Compare A Handler
        jmp TIM4_COMPB    ; Timer4 Compare B Handler
        jmp TIM4_COMPC    ; Timer4 Compare c Handler
        jmp TIM4_OVF    ; Timer4 Overflow Handler
        jmp TIM5_CAPT    ; Timer5 Capture Handler
        jmp TIM5_COMPA    ; Timer5 Compare A Handler
        jmp TIM5_COMPB    ; Timer5 Compare B Handler
        jmp TIM5_COMPC    ; Timer5 Compare c Handler
        jmp TIM5_OVF    ; Timer5 Overflow Handler
        jmp USART2_RXC    ; USART2, RX Complete Handler
        jmp USART2_UDRE ; USART2, UDR Empty Handler
        jmp USART2_TXC    ; USART2, TX Complete Handler
        jmp USART3_RXC    ; USART3, RX Complete Handler
        jmp USART3_UDRE ; USART3, UDR Empty Handler
        jmp USART3_TXC    ; USART3, TX Complete Handler
;
        nop

RESET:    ldi r16, high(RAMEND); Main program start
        out SPH,r16 ; Set Stack Pointer to top of RAM
        ldi r16, low(RAMEND)
        out SPL,r16
        cli ; disable interupts
                                ; Set baud rates
        ldi r16, 8               ; 8 = 115,200
        
        sts UBRR1L, r16            ;connection to PC

        clr r16
        sts UBRR1H, r16            ;high baud rate
        sts UCSR1A, r16            ; 

; Enable receiver and transmitter
        ldi r16,0b0001_1000   ; rx&tx to gps and enable interrupt rx
        sts UCSR1B,r16
; Set frame format: 8data, 1stop bit
        ldi r16, 0b0000_0110 ;
        sts UCSR1C,r16

; finished setup of USART
        ;sei                        ;turn on global interrupts

again:
        lds r18, UCSR1A
        andi r18,0b1000_0000
        breq again
        lds r19,UDR1
        sts UDR1,r19
        rjmp again

;ISR

EXT_INT0:
EXT_INT1:
EXT_INT2:
EXT_INT3:
EXT_INT4:
EXT_INT5:
EXT_INT6:
EXT_INT7:
PCINT0L:
PCINT1L:
PCINT2L:
WDT:
TIM2_COMPA:
TIM2_COMPB:
TIM2_OVF:
TIM1_CAPT:
TIM1_COMPA:
TIM1_COMPB:
TIM1_COMPC:
TIM1_OVF:
TIM0_COMPA:
TIM0_COMPB:
TIM0_OVF:
SPI_STC:
USART0_RXC:

USART0_UDRE:

USART0_TXC:
ANA_COMP:
ADCR:
EE_RDY:
TIM3_CAPT:
TIM3_COMPA:
TIM3_COMPB:
TIM3_COMPC:
TIM3_OVF:
USART1_RXC:
USART1_UDRE:
USART1_TXC:
TWI:
SPM_RDY:
TIM4_CAPT:
TIM4_COMPA:
TIM4_COMPB:
TIM4_COMPC:
TIM4_OVF:
TIM5_CAPT:
TIM5_COMPA:
TIM5_COMPB:
TIM5_COMPC:
TIM5_OVF:
USART2_RXC:
USART2_UDRE:
USART2_TXC:
USART3_RXC:
USART3_UDRE:
USART3_TXC:
;