Sunday, April 15, 2018

Arduino UNO /AVR 328P Serial USART Transmission in Assembly




;
; Serial USART.asm
;
; Created: 4/15/2018 9:24:49 PM
;

.equ    UBRRNX0=103    ; 9600bps
.equ    UBRRNX1=207

// start code
start:
    rcall    USART_Init
    ; write your code here
    ldi        r16,0x24
    rcall USART_Transmit   

end:
    rjmp    end

; end of your code
; this is my part
;----------
USART_Init:
    push r16
    push r17
       
    ldi r16, low(UBRRNX0)            ; Set baud rate to UBRR0
    ldi r17, high(UBRRNX0)
    sts UBRR0H, r17
    sts UBRR0L, r16
       
    ldi r16, (1
<<RXEN0)|(1<<TXEN0)    ; Enable receiver and transmitter
    sts UCSR0B,r16
       
    ldi r16, (1
<<USBS0)|(3<<UCSZ00)    ; Set frame format: 8data, 2stop bit
    sts UCSR0C,r16
    pop r17
    pop r16
    ret

USART_Transmit:
    push r17
    lds r17, UCSR0A                    ; Wait for empty transmit buffer
    sbrs r17, UDRE0
    rjmp USART_Transmit
       
    sts UDR0,r16                    ; Put data (r16) into buffer, sends the data
    pop r17
    ret

2 comments:

  1. I am really glad I’ve found this info. Today bloggers publish just about gossip and internet stuff and this is really frustrating. A good website with interesting content, that’s what I need. Thanks for making this site, and I will be visiting again. Do you do newsletters by email? and please visit my website. prediksi mix parlay

    ReplyDelete
  2. Thanks a lot, very helpful, but there is a small a mistake in the code USART_Transmit: push r17
    You loop USART_Transmit so on every loop you push r17 on the stack, so you need to put a different loop label after the push r17, thanks.

    ReplyDelete