I've some question to ask about using the device initialisation from Freescale CodeWarrior 5.
1. I initialise the MCU using device initialisation. (It will generate a file called MCUinit.asm under Generated Code)
2. I'm using a SW2 of DEMO9S08QG8 as KBIP2.
3. When interupt it jump to isrVkeyboard at MCUinit.asm.
4. I dun intend to do the entire routine under isrVkeyboard.
5. I intend to set a flag at isrVkeyboard then RTI. (the flag is declared in main.asm)
As I compile it, it couldn't be recognised at MCUinit.asm (undeclared user defined symbol). May I know how do I share the flag I declared in main.asm in MCUinit.asm. Thanks.
Message Edited by mingyee on 03-01-200605:38 AM
Sorry if the board messed up your formatting. I have not figured out the HTML yet.;constantXDEF flagsw0 equ 0sw1 equ 1sw2 equ 2flag ds 1main;main program goes heremainend jmp main----------------------------------------------------------------------------------;all the I/O inits & interrupt vectors are here & other interrupt routines.XREF flag
isrVkeyboard:
; Write your interrupt code here ...
bclr $02,KBISC ;clr the interrupt flag
bset sw0,flag
rti
Hi Rocco, thanks for you advise. I would liek to ask you further as I've tried as you recommended. I'm able to trigger the irq. However when it's in the IRQ routine, it seems to stuck in an infinite loop. Where it hit RTI it does not return to the main program instead it loop back to the irq routine again. May I know wut's going wrong? Thanks.
<MAIN.asm>
XDEF _Startup, main,FLAG,SW2ON
XREF MCU_init
XREF __SEG_END_SSTACK ; symbol defined by the linker for the end of the stack
include 'derivative.inc'
; variable/data section
MY_ZEROPAGE: section short ; Insert here your data definition
;*******************************************************************************
; constants
;*******************************************************************************
SW1ON equ 0
SW2ON 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 the stack pointer
txs
jsr MCU_init
clr CNT
clr FLAG
cli ; enable interrupts
mainLoop:
lda #$00
feed_watchdog
jsr tsec
detectsw2 brset KBIP2,FLAG,detectsw2a ;flag from irq
mainend jmp main
detectsw2a bset PTBD_PTBD7,PTBD ;on LED
<MCU_init.asm>
XDEF isrVkeyboard
XREF FLAG,SW2ON
isrVkeyboard:
bclr KBISC_KBACK,KBISC
bset SW2ON,FLAG
rti
Hello mingyee,
There seems to be a couple of problems with the code:
Regards,
Mac
Sorry.. I was rather sleepy when i posted tat question. So I screwed up a lil.. the actual code is
XDEF _Startup, main
XDEF FLAG,SW2ON
XREF MCU_init
XREF __SEG_END_SSTACK ; symbol defined by the linker for the end of the stack
include 'derivative.inc'
MY_ZEROPAGE: section short
; constants
SW1ON equ 0
SW2ON 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 the stack pointer
txs
jsr MCU_init
clr CNT
clr FLAG
cli ; enable interrupts
mainLoop:
lda #$00
feed_watchdog
jsr tsec
detectsw2 brset KBIP2,FLAG,detectsw2a ;flag from irq
detectsw2a bset PTBD_PTBD7,PTBD ;on LED
mainend jmp mainLoop
<MCU_init.asm>
XDEF isrVkeyboard
XREF FLAG,SW2ON
isrVkeyboard:
bclr KBISC_KBACK,KBISC
bset $01,FLAG
rti
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