Skip to content

Instantly share code, notes, and snippets.

@BobBurns
Created November 15, 2014 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BobBurns/5e54fa94b25ce4e61055 to your computer and use it in GitHub Desktop.
Save BobBurns/5e54fa94b25ce4e61055 to your computer and use it in GitHub Desktop.
Hello World - even better version of serial_assembly.asm
;serial io echo program from AVR Programming p.81 by Elliot Williams
;help from T. Margush Some Assembly Required
;compile with gavrasm serial_assembly.asm
;flash with avrdude -c avrisp -p m168 -P /dev/tty.usbmodem1411 -b 19200 -U flash:w:serial_assembly.hex
;
; cpu_f = 1,000,000 baud = 9600 use 2x(USX0 = 1)
;Programmer: Me
;2 mhz clock speed, 9600 baud UBBR = 12
.equ UBBRvalue = 12
.equ UCSR0C = 0xc2 ; SRAM address of UCSR0C
.def temp = r16
.def count = r17
.def zL = r30
.def zH = r31
.device atmega168
rjmp main
.cseg
input_string:
.db "Hello World"
;inittialize Stack
main: ldi temp,low(RAMEND)
out SPL,temp
ldi temp,high(RAMEND)
out SPH,temp
;leds display RXD counter
ldi temp,0xff
out DDRB,temp ;ddr output
ldi temp,0x00
out PORTB,temp
;
;initialize USART
ldi temp,high(UBBRvalue) ;baud rate param
sts UBRR0H,temp
ldi temp,low(UBBRvalue)
sts UBRR0L,temp
lds temp,UCSR0A
ori temp,(1 << U2X0) ;set use 2x because %error actual baud > .5
sts UCSR0A,temp
;--- USART register values
ldi temp,(1 << TXEN0) | (1 << RXEN0) ;enable transmit and receive
sts UCSR0B,temp
ldi temp,(1 << UCSZ01) | (1 << UCSZ00) ;8 data bits, 1 stop bit
sts UCSR0C,temp
;--- set up leds to count input
.def rxCount = r0
clr rxCount
com rxCount
;--- send a message
.def byte_tx = r24
ldi zH, high(input_string)
ldi zL, low(input_string)
ldi count,11 ; 11 characters in string
for1: lpm byte_tx,Z+
rcall transmit
dec count
brne for1
lp: rcall receive
brcc lp
dec rxCount
out PORTB,rxCount
rcall transmit
rjmp lp
;--- subroutines
receive:
clc
lds temp,UCSR0A
sbis temp,RXC0 ;is byte in rx buffer?
ret ;not yet
lds byte_tx,UDR0;
sec ;to indicate a byte was received
ret
transmit:
lds temp,UCSR0A
sbis temp,UDRE0 ;wait for Tx buffer to be empty
rjmp transmit ;not ready
sts UDR0,byte_tx;
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment