Searching \ for '[PIC]: Multiple Interrupts?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/microchip/ints.htm?key=interrupt
Search entire site for: 'Multiple Interrupts?'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Multiple Interrupts?'
2003\06\21@023917 by ?q?Debbie=20Hynes?=

flavicon
face
Hi PICers,
I'm planning to use a PIC to control a camera. It's in a waterproof case which
I don't want to open for power on/off so I'm gonna use a reed switch, closed
momentarily by a magnet, to set the thing in HUNT mode. It should be in SLEEP
before it's activated. The reed/magnet should toggle the system between HUNT &
STANDBY modes.

When in HUNT mode, battery life is a problem so I want it to be in SLEEP mode
while it's waiting for a bandit. (The bandit has 4 legs + fur & the camera is
out in a forest. Very cold, wet so the camera + pic etc is sealed.)

These requirements seem to indicate 2 interrupts - which the programme must
discriminate between.
How does the code fragment below look? Any pitfalls traps or snares? Be good to
know before I start writing the rest of the code. :|

Just kicking round code ideas right now.
Thankz 4 thoughts - Debbie :)

        org       0x00
start    goto      main

        org       0x04
intrpt                            ;Start interrupt routine
        movwf     w_
        movf      STATUS,w
        movwf     STATUS_
        movf      PCLATH,w       ;Save PCLATH
        movwf     PCLATH_
        movf      FSR,w
        movwf     FSR_
        movf      flags,w
        movwf     flags_

alarm    clrf      TMR0

        btfss     INTCON,INTF       ;Is it RB0?
         goto     another           ;No - it's RB7 so do another thing
        call      do_one_thing      ;Yes - it's RB0 so so one thing
        goto      another+1
another  call      do_another_thing  ;
        nop
int_ret  movf      flags_,w
        movwf     flags
        movlw     b'00011000'    ;Reset the INTCON bits BUT ...
        movwf     INTCON         ;.. let the PIC set GIE upon retfie
        movf      FSR_,w
        movwf     FSR
        movf      PCLATH_
        movwf     PCLATH
        movf      STATUS_,w      ;Return from interrupt
        movwf     STATUS
        swapf     w_             ;Flip & load the w register
        swapf     w_,w           ;without affecting the STATUS reg.
        retfie

;************************************************************
;SUBROUTINES ...
;************************************************************

do_one_thing
       nop
       retlw  0x00

do_another_thing
       nop
       retlw 0x01

;**********************************************************
;Main Programme
;**********************************************************
main
ports   movlw   b'00000000'    ;
       movwf   PORTB          ;Make PORTB LCD all LOW, avoid turn-on glitch??
       BANK_1
       movlw   b'10000101'
       movwf   TRISB          ;TRISB=0x0C5
       BANK_0
       movlw   b'00011111'
       movwf   PORTA
       BANK_1
       movlw   b'000000000'   ;Make PORTA all o/p's 4 now
       movwf   TRISA
       BANK_0
       clrf    FSR

setint  nop                     ;Get ready to enable the interrupts
       BANK_1
       movlw  0x0D
       movwf  OPTION_REG
       BANK_0
       movlw  b'10011000'      ;Enable the interrupts - RB0int + PORTBint
       movwf  INTCON
       clrf    TMR0            ;Disable WDT generated resets.
       nop
shutdown
        sleep
        nop
        goto   shutdown        ;loop or goto sleep?
        end

http://mobile.yahoo.com.au - Yahoo! Mobile
- Check & compose your email via SMS on your Telstra or Vodafone mobile.

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2003\06\21@040738 by Bob Axtell

face picon face
Looks pretty good, Debbie but a couple of suggestions:

To reduce the code size during an interrupt, I always confine my code to
BANK0 and PAGE0 (first 2K or program memory) if possible. So the first
thing I would do after saving STATUS is to clrf STATUS, which forces it to
that condition. So right after saving PCLATH, you'll need to clrf PCLATH if
any computed jumps are used.

In addition, unless variables w_, fsr_, pclath_ are global (all banks)
variables, you will be in trouble, right? I make all variables except w_ in
bank0, so I don't waste precious global regs.

Computed jumps work great in ints, because they can be used to set
priorities instantly. For example, if you make a jump table from intcon
(its designed for it) you can set a priority. Here's a sample from some
active code:

        movf   intcon,W
       andlw   7                :
        addwf   PCL,f
        goto    int_exit          ;000 , can't happen
        goto    PBChanged   ;001
        goto    PB0stuff         ;010
        goto    PBChanged   ;011
        goto    Timer0stuff    ;100
        goto    PBChanged   ;101
        goto    Timer0stuff    ;110
        goto   PBchanged     ;111

Note that when multiple ints occur, I make PBchange higher priority than
Timer0stuff, and PB0stuff lower yet. This jump table is located in the
first 256 bytes of page0, so that  I only fiddle with PCLATH  when I enter
the INT routine.

Finally, reserve a set of tmporary registers to be used JUST FOR INTS. In
this way, you won't accidentally change a register inside an INT that
you're using in main code. In the same vein, if your interrupt code doesn't
USE FSR, don't save or recover it, same with PCLATH.

--Bob Axtell

At 04:38 PM 6/21/2003 +1000, you wrote:
{Quote hidden}

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2003\06\21@151647 by Olin Lathrop

face picon face
> (The bandit has 4 legs + fur & the
> camera is out in a forest. Very cold, wet so the camera + pic etc is
> sealed.)

That sounds cool.  I've been meaning to set up a camera with an IR tripwire
in my woods, but unfortunately its one of many wishlist projects.

>          org       0x00

I strongly advise against absolute mode.

{Quote hidden}

Note that this means STATUS, PCLATH, FSR, and FLAGS must either be in the
precious global memory (70h - 7F on many 16 family PICs) or have a byte
reserved for each in each bank.  It would be better to CLRF STATUS right
after the MOVF STATUS,W to guarantee the bank.  You're going to have to set
it sooner or later anyway.

> alarm    clrf      TMR0

Bank setting?

>          btfss     INTCON,INTF       ;Is it RB0?
>           goto     another           ;No - it's RB7 so do another thing

You saved PCLATH but didn't set it to a known value.  This GOTO will go to
one of four locations depending on the page setting at the time of the
interrupt.

Frankly, I'm getting tired of people reinventing interrupt service routines,
or at least showing us the reinventions.  They are a solved problem, with
many examples out there.  One of these is my own QQQ_INTR.ASPIC module at
http://www.embedinc.com/pic.  It's an ISR template that deals with all the
issues I noted above.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

More... (looser matching)
- Last day of these posts
- In 2003 , 2004 only
- Today
- New search...