Hello Mingyee,
The primary reason for the continuously repeating interrupt would seem to be that you are not clearing the KBI flag. You need to write a 1 to the KBACK bit. However, even with this corrected, there will still be multiple interrupts due to switch bounce. To prevent this, you need to disable the keyboard interrupt for the necessary de-bounce period, and then re-enable.
There also seemed to be other confusion with identification of flag bits. Also I could not understand what you were trying to achieve with the LED. So I have taken the liberty to modify your code to something that addresses these issues. For the purpose of demonstration, pressing S1 should turn-on the LED, and pressing S2 should turn-off the LED.
XDEF _Startup, main
XDEF FLAG,SW2ON
XREF MCU_init
XREF __SEG_END_SSTACK ; end of stack
include 'derivative.inc'
MY_ZEROPAGE: section short
; Constants
SW1ON equ 0
SW2ON equ 1
LED equ PTBD_PTBD7
KBIP1 equ 1
KBIP2 equ 2
; Start of page zero RAM
CNT: ds 1
FLAG: ds 1
; Code section
MyCode: section
main:
_Startup:
ldhx #__SEG_END_SSTACK ; initialize stack pointer
txs
jsr MCU_init
clr CNT
clr FLAG
cli ; enable interrupts
mainLoop:
feed_watchdog
brclr KBIP1,FLAG,cont_1
bclr KBIP1,FLAG ; Clear flag
bset LED,PTBD ; Turn-on LED
jsr tsec ; Key de-bounce delay
bset KBISC_KBACK,KBISC; Clear KBI flag
bset KBISC_KBIE,KBISC ; Re-enable KBI interrupt
bra mainLoop
cont_1:
brclr KBIP2,FLAG,cont_2
bclr KBIP2,FLAG ; Clear flag
bclr LED,PTBD ; Turn-off LED
jsr tsec ; Key de-bounce delay
bset KBISC_KBACK,KBISC; Clear KBI flag
bset KBISC_KBIE,KBISC ; Re-enable KBI interrupt
bra mainLoop
cont_2:
; Other code within mainLoop could go here.
mainend:
jmp mainLoop
<MCU_init.asm>
XDEF isrVkeyboard
XREF FLAG,SW1ON,SW2ON,KBIP1,KBIP2
isrVkeyboard:
bset KBISC_KBACK,KBISC; Clear KBI flag
bclr KBISC_KBIE,KBISC ; Disable KBI interrupts
brclr SW1ON,PTAD,*+5 ; Skip next if not SW1
bset KBIP1,FLAG ; Set SW1 flag
brclr SW2ON,PTAD,*+5 ; Skip next if not SW2
bset KBIP2,FLAG ; Set SW2 flag
rti
It is assumed that the routine MCU_init correctly initializes the KBI module, and that the routine tsec provides a suitable de-bounce delay (20-100 milliseconds), and is actually located in the main .asm file. Another assumption is that the switches pull the input high when activated.
Regards,
Mac