Created
November 14, 2014 20:45
-
-
Save BobBurns/d9f4a5e279864083b927 to your computer and use it in GitHub Desktop.
serialLoopback.c program from Elliot Williams AVR Programming written in avr assembly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;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.XXXXXXX -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 | |
.device atmega168 | |
.cseg | |
;inittialize Stack | |
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 char H | |
.def byte_tx = r24 | |
ldi byte_tx,'H' | |
rcall transmit | |
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