I am having probelms with the question below. I\'ve attached two attempts at sol
ID: 3882761 • Letter: I
Question
I am having probelms with the question below. I've attached two attempts at solving the problem. I think attempt number two is going to be closer to correct. The code is written in assembly .asm. You can see from attempt two that the code i am struggling to write is checking the value of portb is equal to 7 and incrementing a variable named count. In addition, I am slightly confused as to how to return from an interrupt.
Write an interrupt service routine (ISR) that does the following:
• Stores the contents of the W register into a variable named WCopy
• Stores the contents of the STATUS register into a variable named StatCopy
• Checks to see if the value on PORTB is equal to 7, if it is, increment a variable named COUNT
• Restore the value of the STATUS register
• Restore the value of the W register
• Return from interrupt
Attempt1:
MOVWF W_TEMP ;Copy W to TEMP register
SWAPF STATUS,W ;Swap status to be saved into W
;Swaps are used because they do not affect the status bits
MOVWF STATUS_TEMP ;Save status to bank zero STATUS_TEMP register
:
:(ISR) ;Insert user code here
:
SWAPF STATUS_TEMP,W ;Swap STATUS_TEMP register into W
;(sets bank to original state)
MOVWF STATUS ;Move W into STATUS register
SWAPF W_TEMP,F ;Swap W_TEMP
SWAPF W_TEMP,W ;Swap W_TEMP into W
ATTEMPT2:
ORG
WCopy EQU 0x21
StatCopy EQU 0x22
COUNT EQU 0X23
MOVWF WCopy ;Copy W to WCopy register
SWAPF STATUS,W ;Swap status to be saved into W
;Swaps are used because they do not affect the status bits
MOVWF StatCopy ;Save status to bank zero StatCopy register
WRITE CODE HERE ;Check to see if the value on portb is equal to 7
WRITE CODE HERE ;If it is increment a variable named COUNT
;where would by return from interrupt go?
SWAPF StatCopy,W ;Swap StatCopy register into W
;(sets bank to original state)
MOVWF STATUS ;Move W into STATUS register
SWAPF WCopy,F ;Swap WCopy
SWAPF WCopy,W ;Swap WCopy into W
END
Explanation / Answer
Here is your required assembly program,
ORG
WCopy EQU 0x21
StatCopy EQU 0x22
COUNT EQU 0X23
MOVWF WCopy ;Copy W to WCopy register
SWAPF STATUS,W ;Swap status to be saved into W
;Swaps are used because they do not affect the status bits
MOVWF StatCopy ;Save status to bank zero StatCopy register
LDR R7,=PORTB
LDR R0,[R7]
CMP R0,#7 ;Check to see if the value on portb is equal to 7
BEQ LABEL
LABEL INC COUNT ;If it is increment a variable named COUNT
IRT ;return from the interrupt
SWAPF StatCopy,W ;Swap StatCopy register into W
;(sets bank to original state)
MOVWF STATUS ;Move W into STATUS register
SWAPF WCopy,F ;Swap WCopy
SWAPF WCopy,W ;Swap WCopy into W
END
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.