Consider the AVR code segment shown below that performs initialization and recei
ID: 3670375 • Letter: C
Question
Consider the AVR code segment shown below that performs initialization and receive routines for USART1 that continuously receives data from a transmitter. Write and explain the instructions in lines (1)-(16) necessary to make this code work properly. More specifically,
(a) Fill in lines 1-4 with the necessary code to initialize the stack pointer.
(b) Fill in lines 5-6 with the necessary code to configure RXD1 (Port D, pin 2).
(c) Fill in lines 7-10 with the necessary code to set the Baud rate at 9,600 bps using double data rate.
(d) Fill in lines 11-12 with the necessary code to enable the receiver and Receive Complete interrupt.
(e) Fill in lines 13-14 to set the frame format to be asynchronous with 8 data bits, 2 stop bits, and even parity.
(f) Fill in lines 15-16 to receive data and store it into a buffer.
.include "m128def.inc"
.def mpr = r16
.ORG $0000
RJMP initUSART1;
.ORG $003C
RCALL RECEIVE_DATA;
RETI .
ORG $0046
initUSART1:
;Initialize Stack Pointer
(1)
(2)
(3)
(4)
; Configure Port D, pin 2
(5)
(6)
; Set baud rate at 9600bps, double data rate
(7)
(8)
(9)
(10)
; Enable receiver and Receive Complete interrupt
(11)
(12)
; Set frame format: 8 data, 2 stop bits, asynchronous, even parity
(13)
(14)
MAIN:
LDI XH, high(Buffer)
LDI XL, low(Buffer)
LOOP:
RJMP LOOP ; Wait for data …
RECEIVE_DATA:
push mpr ; Save mpr
; Receive data and store in buffer
(15)
(16)
pop mpr ; Restore mpr
ret ; Return
Buffer:
.BYTE100 ; 100 byte buffer
Explanation / Answer
.include "m128def.inc"
.def mpr = r16
.ORG $0000
RJMP initUSART1;
.ORG $003C
RCALL RECEIVE_DATA;
RETI
.ORG $0046
initUSART1:
; Initialize Stack Pointer
LDI mpr, high(RAMEND)
OUT SPH, mpr
LDI mpr, low(RAMEND)
OUT SPL, mpr
; Configure Port D, pin 2
; Set baud rate at 9600bps, double data rate
Mov TMOD , #20H
MOV TH1,#-3 ;9600 BOUD RATE
MOV SCON, #50H
SETB TR1
; Enable both receiver and transmitter, and receive interrupt
ldi mpr, (1<<RXEN0 | 1<<TXEN0 | 1<<RXCIE0)
out UCSR0B, mpr ;
; Set frame format: 8 data, 2 stop bits, asynchronous
ldi mpr, (0<< (0<<UMSEL0 | 1<<USBS0 | 1<<UCSZ01 | 1<<UCSZ00)
sts UCSR0C, mpr ; UCSR0C in extended I/O space
MAIN:
LDI XH, high(Buffer)
LDI XL, low(Buffer)
LOOP:
RJMP LOOP ; Wait for data …
RECEIVE_DATA:
push mpr ; Save mpr
; Receive data and store in buffer
Push MPR
IN R17, UDR0
pop mpr ; Restore mpr
ret ; Return
Buffer:
.BYTE100 ; 100 byte buffer
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.