Starting from:

$30

Homework 03 Solution

Note: I strongly recommend that you utilize the ATmega128 datasheet when solving these questions.




Problem #1 [25 pts]

The AVR code below (with some information missing) is designed to initialize and service interrupts from three I/O devices (DevA, DevB, and DevC).




There are 8 external interrupt pins (INT0-INT7) in AVR. Based on the code provided, which three interrupt pins are these I/O devices connected to and what is the immediate value needed in line 1?



Which I/O device’s interrupt is triggered by a low level input?
Assume that pins 7 and 6 on PORTC are connected to an additional output device (DevD). Fill in lines 2-3 such that the corresponding pins are specified as outputs. Do not configure any other pins on the port as outputs.



Suppose DevA requires that no interrupts are detected while it is being serviced. Fill in lines 4-5 with the necessary code to clear any latched interrupts at the end of ISR_DevA.
Consider the RCALL instruction on line 0. Would the program function correctly if this line was replaced with the following underlined text? CALL ISR_DevB Assume that no other lines of code are changed. Provide an explanation to support your answer.



.include “m128def.inc”






.def
mpr = r16






START:
$0000








.org










JMP


INIT






.org
$0006










RJMP
ISR_DevA






.org
$000C
(0)






RCALL
ISR_DevB






RETI










.org
$000E








RJMP
ISR_DevC






INIT:


mpr, 0b00110000


ldi




sts


EICRA, mpr








ldi


mpr, 0b00000100


out


EICRB, mpr
(1)


ldi


mpr,


out


EIMSK,










mpr








ldi


mpr, $00








out


DDRD, mpr








out


DDRE, mpr
(2)
























(3)




sei











MAIN:






















{ …

…do something…

}




ISR_DevA:





(4)

(5)




RETI







ISR_DevB:

{…



RET

}

ISR_DevC:

{…



RETI

}

Problem #2 [25 pts]

Consider the following AVR assembly code that waits for 1 second using the 8-bit Timer/Counter0 with the system clock frequency of 16 MHz operating under Normal mode. Take the time to read through this code and determine how it is designed to function. Write and explain the instructions in lines (1-10) necessary to make this code work properly. More specifically,

Fill in lines (1-2) with the necessary code to enable the interrupt on Timer/Counter0 overflow.
Fill in lines (3-4) with the necessary code to load the value for delay that generates an interrupt after 5 ms.
Fill in lines (5-6) with the necessary code to set the prescalar value and the mode of operation.
Fill in line (7) with the necessary code to set counter.
Fill in lines (8-9) with the necessary code to reload the value for delay.
Fill in line (10) with the necessary code to decrement counter.



.include “m128def.inc”

.def
mpr = r16
.def
counter = r17

$0000


.ORG
Initialize
RJMP
.ORG
$0020
; Timer/Counter0 overflow interrupt vector
JMP
Reload_counter
.ORG
$0046
; End of interrupt vectors



Initialize:





(1)


; Enable interrupt on Timer/Counter0 overflow




(2)


; Enable global interrupt


SEI
(3)








; Load the value for delay






;




(4)








; Set prescalar and mode




(5)






;




(6)








; Set counter




(7)
LOOP:
counter, 0


; Repeat until interrupted


CPI




BRNE
LOOP













Reload_counter:


; Save mpr


PUSH
mpr






(8)


; Load the value for delay






;




(9)




(10) ; Decrement counter








; Restore mpr


POP
mpr




RETI






Problem #3 [25 pts]

Consider the AVR code segment shown below (with some missing information) that configures Timer/Counter0 for Fast PWM operation, and modifies the Fast PWM duty cycle whenever a specific button on Port D is pressed.




Fill in lines (1-2) with the instructions necessary to configure Timer/Counter0 for Fast PWM mode, non-inverting output, and a prescale value of 8.
Based on the prescale value used in part (a), what is the frequency of the PWM signal (fPWM) being generated by Timer/Counter0? Assume the system clock frequency is 16 MHz.



Fill in lines (3-4) to provide the compare value for Timer/Counter0 so that the initial duty cycle is 0%.
What would be the value necessary for the variable step to increase the duty cycle by 10% each time the DUTY_STEP subroutine is executed? Ignore the case when/if the compare value overflows.



.include
"m128def.inc"
.def
mpr = r16
.def
temp = r17
.equ
step =
INIT:













stack pointer is initialized





I/O ports
ldi
mpr, 0b00010000
; set pin 4
(OC0) as output
out
DDRB, mpr




ldi
mpr, 0b00000000
; set pin 0
as input
out
DDRD, mpr
; enable pull-up resistor for pin 0
ldi
mpr, 0b00000001
out
PORTD, mpr







Timer/Counter0
Fast PWM mode, non-inverting, prescale = 8
(1)

(2)




Initial compare value for PWM output



(3)




(4)







MAIN:
PIND, 0


sbis


rcall
DUTY_STEP


rjmp
MAIN


DUTY_STEP:
mpr


push


push
temp


in
mpr, ________
; read the current PWM compare value
ldi
temp, step
; add step value to compare value
add
mpr, temp
out
________, mpr
; write new PWM compare value
pop
temp


pop
mpr
; return
ret


Problem #4 [25 pts]

Write a subroutine initUSART0 to configure the ATmega128 USART0 to operate as a transmitter that will send data every time a USART0 Data Register Empty interrupt occurs. The transmitter operates with the following settings:




8 data bits, 1 stop bits, and odd parity



3,000 Baud rate (note that you will have to choose the closest available rate)



Transmitter enabled



Normal asynchronous mode operation



Interrupt enabled



Assume the system clock is 16 MHz. The skeleton code is shown below:




.include “m128def.inc”

.def mpr = r16

.ORG $0000

RJMP initUSART0



.ORG $0026

RJMP SendData



.ORG $0046

initUSART0:



…Your code goes here. The lines below provide hints as to what you should do…

Configure USART0 (Port E, pin 1) as output


Set baud rate to 3,000


Enable Transmitter and data register empty interrupt


Set asynchronous mode and frame format


Set the global interrupt enable bit


Main:
mpr, X+
; Send first data
ld
out
UDR0, mpr


Loop:
Loop


RJMP


SendData:
mpr, X+
; Send next data
ld
out
UDR0, mpr


reti



More products