Shop OBEX P1 Docs P2 Docs Learn Events
Help with Interrupt please — Parallax Forums

Help with Interrupt please

basicstampedebasicstampede Posts: 214
edited 2008-09-06 14:43 in General Discussion
I am learning about Interrupt on SX/B.· So I want a simple program.
I have two LEDs.· The main program will blink one LED on & off.
When SX senses a keypress (on a RB pin), then I want the ISR to run (toggle a different LED).

I wrote the below code, but I must be missing some code (e.g. setting up RB port for interrupt, for setting high to low transition) etc.

Can someone please modify this program?· The purpose section has more details of what I want.

I also attach the file.·

' =========================================================================
'
'·· File...... TEMPLATE.SXB
'·· Purpose... to learn about external interrupt
'·· Author....·
'·· E-mail....·
'·· Started...· 5 Sept. 2008
'·· Updated...
'
' =========================================================================

'
' Program Description
'
' there are two LEDs, a green LED on RB.5 and a red LED on RB.6
' and there is 1 input switch connected to RB.6
' the main program will blink green LED 1 sec. on and 1 sec. off continuously
' when user presses the input switch (falling edge), go into ISR
' in ISR, toggle the red LED and exit ISR

'
' Device Settings
'
DEVICE········· SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ··········· 4_000_000

'
' IO Pins
'
IntSource RB.4· INPUT
LED_Main RB.5 OUTPUT
LED_ISR RB.6 OUTPUT
'
' Constants
'

'
' Variables
'

'
· INTERRUPT
'
ISR_Start:
· ' ISR code here
··· TOGGLE LED_ISR
ISR_Exit:
· RETURNINT ' {cycles}································

' =========================================================================
· PROGRAM Start
' =========================================================================
Pgm_ID:
·
'
' Subroutines / Functions Declaration
'

'
' Program Code
'
Start:
· ' initialization code here
Main:
· ' main code here
· HIGH LED_Main
· PAUSE 1000
· LOW LED_Main
· PAUSE 1000
·
· GOTO Main

'
' Subroutines / Functions Listing
'

'
' User Data
'

Comments

  • basicstampedebasicstampede Posts: 214
    edited 2008-09-05 17:18
    Sorry for typo. The input switch is on RB.4 (code is correct).
  • JonnyMacJonnyMac Posts: 9,215
    edited 2008-09-05 18:15
    Have a look at the program I've attached; it will get you there. It's not as easy as you want it to be, but it's not that bad if you'll just take a few minutes to study it and learn from what I've done (which I, of course, learned from others). In the program the foreground blinking is not affected by the background blinking when activated.

    Notes:
    A. Don't post your full listings; attach them (posting just takes a lot of space and hoses up the formatting)
    B. If you're going to use an interrupt, there's no getting around the fact that time-based functions like PAUSE will get screwed up -- don't use them (recode them)
    C. When possible, set your interrupt rate to a convenient value; if you have to divide down from that, no problem (it's easy)
    D. Get used to enabling pull-ups on unused pins; this will help keep current consumption down

    PS: I've adjusted the order of times in my programming template to allow for slightly-longer ISR code before pushing the jump table passed the half-page boundary

    [noparse][[/noparse]Edit] Since the next logical question is how to do a background blink that is asymmetrical (on and off times are different) I've attached a version that does that, too.

    Post Edited (JonnyMac) : 9/5/2008 6:57:57 PM GMT
  • basicstampedebasicstampede Posts: 214
    edited 2008-09-05 19:35
    JonnyMac, thanks.

    Which statement in the program sets up the IntSource pin (RB.4) as an interrupt pin? (looking for high to low transition)
    I don't know how to set up RB.4 as an interrupt pin.
  • basicstampedebasicstampede Posts: 214
    edited 2008-09-05 19:50
    OK. I think I now undersstand your code. In your code, the ISR is invoked 1000 times per second. Then inside the ISR, it checks to see if the IntSource pin was pressed ornot.

    Instead of doing it that way, how do you structure it so that you ONLY enter the ISR when IntSource pin is pressed (on high to low transition)?

    That way, if the user never presses the IntSource switch, then ISR is never invoked. But as soon as the switch is pressed, it goes to ISR, toggles the appropriate LED, then leaves the ISR.

    Thanks in advance.
  • JonnyMacJonnyMac Posts: 9,215
    edited 2008-09-05 20:13
    Believe it or not, this stuf _is_in the help file. You really should crack it open and spend some time studying it. No, I'm not saying it's perfect, but I'm suggesting that you may not get as many answers here as you will form the docs....

    See attached. I know you have PDB so I've configured the RB.4 input as active-low. Pressing the button will toggle the LED. Remember, though, the SX is fast (even at 4 MHz) and contact bounce could cause multiple toggles.
  • basicstampedebasicstampede Posts: 214
    edited 2008-09-06 03:21
    JonnyMac, in your code

    ISR_Start:
    · ledBtn = %0000_0000
    · WKPND_B = ledBtn
    · IF ledBtn.4 = IsOn THEN

    I think the statement

    WKPND_B = ledBtn should be reversed to ledBtn = WKPND_B

    Otherwise, how can ledBtn.4 ever equal IsOn (1) since it was initialized to 0 at beginning of ISR, then written onto WKPND_B?

    Thanks.
  • JonnyMacJonnyMac Posts: 9,215
    edited 2008-09-06 04:59
    Okay, I'll start asking the questions and you starting answering them. Deal?.... tongue.gif

    In fact, while either syntax works, you're wrong in your assumption. Some registers, like WKPND_B, actually exchange values on what looks like an assignment. In this case what was in WKPND_B ends up in ledBtn and vice-versa.

    Now, let me ask two serious questions:
    1. Did you actually run my code -- unmolested -- to see that it does in fact work?
    2. If you answered 'No' to number 1, why should I or anyone else every spend our own time again to help you?

    I know you're loathe to crack open the SX documentation but if you ever actually do, section 7.2 will explain the exchange of values. I tend to favor syntax that more closely models SX Assembly, hence I use:

    WKPND_B = ledBits
    


    ... where in assembly (or as compiled by SX/B) it would be:

    MODE  #$09
    MOV   !RB, ledBtn 
    MOV   ledBits, W
    


    The documentation will tell after the MOV !RB, someVar instruction that W contains the contents of the WKPND_B register. SX/B takes care of moving W to the variable that was used.
  • basicstampedebasicstampede Posts: 214
    edited 2008-09-06 12:03
    JonnyMac,

    Thanks for all your help. Yes, I did run your code, and it runs perfectly. Thank you for helping me undsrstand more about RB INT.

    I was merely asking because I did not know about the exchange of values. But now I know better.

    I would love to crack open the SX docs. But when I click on Help, there are only 3 sections called Welcome, Reference, and Example Projects on my on-line Help. And I searched all these 3 sections, but there is no such thing as section 7.2.
    Where is that found please?

    Actually, I read "Practical SX/B" from cover to cover a couple of weeks ago in order to learn SX/B (although I confess that I am still trying to digest some of the materials in Chapter 14 and 15.)

    Thank you for your assistance.
  • JonnyMacJonnyMac Posts: 9,215
    edited 2008-09-06 14:34
    These are the SX docs I keep referring to:
    -- www.parallax.com/Portals/0/Downloads/docs/prod/datast/SX20AC-SX28AC-Data-v1.6.pdf

    If you're serious about using the SX beyond the capabilities of the BASIC Stamp you're going to have to dig into this documentation.
  • basicstampedebasicstampede Posts: 214
    edited 2008-09-06 14:43
    Thanks JonnyMac, I will study it. Yes, I want to be good in SX.
Sign In or Register to comment.