; Name: usart_echo.asm ; Description: Echo back characters on serial port ; (c) by Thomas Hoehn, October 2007 .ifndef _TN2313DEF_INC_ .include "../inc/tn2313def.avra.inc" .endif ; CPU oscillator frequency .set F_OSC = 8000000 ; Baud rate for USART .set BAUD = 9600 ; UBRR value for Baud rate .set UBRR_VAL = (F_OSC/(BAUD*16)-1) ; Real Baud rate .set BAUD_REAL = (F_OSC/(16*(UBRR_VAL+1))) ; Error rate for fosc=8Mhz, differrent baud rates ; 9600 Baud, U2X=0, Error 0.2% ; 19200 Baud, U2X=0, Error 0.2% .equ BAUD_ERROR = ((BAUD_REAL*1000)/BAUD-1000) ; max. +/-10 promille, error > 1% ? .if ((BAUD_ERROR>10) || (BAUD_ERROR<-10)) .error "Baud rate error to high!" .endif ; define CR (Carriage Return) and LF (Line Feed) .set ASCII_CR = 0x0d .set ASCII_LF = 0x0a ; ; Macro definitions ; ; switch LED1 on .MACRO LED1_ON sbi PORTD,PORTD5 .ENDMACRO ; switch LED1 off .MACRO LED1_OFF cbi PORTD,PORTD5 .ENDMACRO ; toggle LED1 status .MACRO LED1_TOGGLE sbi PIND,PORTD5 .ENDMACRO ; switch LED2 on .MACRO LED2_ON sbi PORTD,PORTD6 .ENDMACRO ; switch LED2 off .MACRO LED2_OFF cbi PORTD,PORTD6 .ENDMACRO ; toggle LED2 status .MACRO LED2_TOGGLE sbi PIND,PORTD6 .ENDMACRO .cseg ; ; Interrupt vectors definitions ; .org 0x0 rjmp MAIN ; External interrupt request 0, INT0 .org INT0addr reti ; External interrupt request 1, INT1 .org INT1addr reti ; Timer/Counter1 capture event, TIMER1 CAPT .org ICP1addr reti ; Timer/Counter1 compare match A, TIMER1 COMPA .org OC1Aaddr reti ; Timer/Counter1 overflow, TIMER1 OVF .org OVF1addr reti ; Timer/Counter0 overflow, TIMER0 OVF .org OVF0addr reti ; USART0 Rx complete, USART0 RX .org URXCaddr rjmp USART_Receive ; USART0 Data register empty, USART0 UDRE .org UDREaddr reti ; USART0 Tx complete, USART0 TX .org UTXCaddr reti ; Analog Comparator, ANALOG COMP .org ACIaddr reti ; Pin change interrupt, PCINT .org PCIaddr reti ; Timer/Counter1 Compare Match B, TIMER1 COMPB .org OC1Baddr reti ; Timer/Counter0 Compare Match A, TIMER0 COMPA .org OC0Aaddr reti ; Timer/Counter0 Compare Match B, TIMER0 COMPB .org OC0Baddr reti ; USI Start condition, USI START .org USI_STARTaddr reti ; USI Overflow .org USI_OVFaddr reti ; EEPROM Ready .org ERDYaddr reti ; Watchdog timer overflow, WDT OVERFLOW .org WDTaddr reti ; programm code after interrupt vector table .org INT_VECTORS_SIZE MAIN: ; disable all interrupts cli ; set Stack Pointer to RAMEND ldi r16,low(RAMEND) out SPL,r16 ; ; init LED1 and LED2 ; ; configure PORTD:5 (LED1) as output sbi DDRD,PORTD5 cbi PORTD,PORTD5 ; configure PORTD:6 (LED2) as output sbi DDRD,PORTD6 cbi PORTD,PORTD6 ; ; init USART ; ; set Baud rate in UBRR ldi r16,high(UBRR_VAL) out UBRRH,r16 ldi r16,low(UBRR_VAL) out UBRRL,r16 ; (UCSR - USART control and status register) ; normal divider of baud rate divisor, U2X=0 ; disable multi-processor communication mode clr r16 out UCSRA,r16 ; enable receiver and transmitter and Rx interrupt ldi r16,(1<