Help with Interrupt please
basicstampede
Posts: 214
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
'
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
'
SXB
4K
Comments
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
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.
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.
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.
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.
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:
... where in assembly (or as compiled by SX/B) it would be:
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.
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.
-- 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.