[TUT][CODE] USART receive & transmit interrupt routine,G

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

Attached is full code that runs on Arduino Mega2560.

1. Setup registers.

USART3 is wired to the Adafruit Ultimate GPS.
USART0 connects to my PC via USB

		cli ; disable interupts
								; Set baud rates
		ldi r16, 25				;38.4k baud to pc from ATmega2560 datasheet
		sts UBRR0L, r16			;connection to PC
		ldi r16, 103			;9.6k baud to GPS
		sts UBRR3L, r16			;connection to GPS
		clr r16
		clr r16
		sts UBRR0H, r16			;high baud rate
		sts UBRR3H, r16			;high baud rate
		sts UCSR0A, r16			; set to zero to be sure
		sts UCSR0A, r16			; set to zero to be sure

; Enable receiver and transmitter
		ldi r16,0b0000_1000   ; only transmitter to pc & no interrupt
		sts UCSR0B,r16
		ldi r16,0b1101_1000   ; rx&tx to gps and enable interrupt rx & tx
		sts UCSR3B,r16
; Set frame format: 8data, 1stop bit
		ldi r16, 0b0000_0110 ;
		sts UCSR0C,r16
		sts UCSR3C,r16
		nop
		clr r16
		out GPIOR2,r16			;set up counter for index into buffer
; finished setup of USART

2. Write interrupt routines.

2a. Receive, to go in USART3_RXC.

USART3_RXC:
			push r16
			in r16,SREG
			push r16
			push YH
			push YL
			push r18
;--------------------------------------------------------------
			in r16,GPIOR2		;get index into buffer
			mov r18,r16			;save for indexing
			inc r16				;for next position
			cpi r16,120			;limit input to 120 characters
			brne URX3a
			clr r16				;exceeded the limit so zero out
			sbi GPIOR0,7		;flag message complete
URX3a:
			ldi YH,high(inbuf)	;get address of the buffer
			ldi YL,low(inbuf)
			add YL,r18			;index into the buffer
			clr r18
			adc YH,r18			;might be a carry
			lds r18,UDR3		;get the character from the USART buffer
			sts UDR0,r18		;echo to pc
			st Y,r18			;put character into buffer
			cpi r18,10			;check if character = NL (10)
			brne URX3b			;no continue
			clr r16				;end of message, so zero index for next message
			sbi GPIOR0,7		;flag message complete
URX3b:
			out GPIOR2,r16		;save the index
;--------------------------------------------------------------
			pop r18				;make sure the pop = the pushes & reverse order
			pop YL
			pop YH
			pop r16
			out SREG,R16
			pop r16
			reti

2b. Transmit routine to go in USART3_TXC

USART3_TXC:
								;Transmit complete interrupt routine
								;the last character transmission is complete
								;can send the next character
			push r16
			in r16,SREG
			push r16
			push r17
			push YH
			push YL
;------------------------------------------------------------
			in r17,GPIOR1		;get index into message
			cpi r17,0			;if zero nothing to send (will happen on last character
								;transmitted
			breq endtxc1
			ldi YH,HIGH(outbuf)	;get pointer to outbuf
			ldi YL,LOW(outbuf)
			cpi r17,120			;check for too many characters sent
			breq endtxc
			clr r16				;need a zero byte to add to get the carry
			add YL,r17			;add the index to the Z register
			adc YH,r16			;might be a carry
			inc r17				;point the index to the next character
			ld r16,Y			;get the character to send from program memory
			sts UDR3,r16		;send it to the GPS
			cpi r16,10			;end if character = NL (10)
			brne endtxc1		;not end
endtxc:
			clr r17				;end, so the index is now zero, and next TX complete
								;interrupt can be ignored.
endtxc1:
			out GPIOR1,r17		;save the index
;------------------------------------------------------
			pop YL
			pop YH
			pop r17
			pop r16
			out SREG,R16
			pop r16
			reti

3. Enable

3a. Enable receive by turning on Global Interrupts. Flag - GPIOR0,7 is set when message is received (signaled by NL)

3b. Once Global Interrupts are on, send the first character in the buffer.

Attachment(s): 

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

Surely this is a "project" not a tutorial? If you want to turn it into a tutorial add some text to educate others as to how it works and why you have chosen specific methods to implement it.

Moderator

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

I tried to submit more text, but couldn't get it to upload. So more text is in the attached pdf