View Full Version : Interruption
ciroman
- 30th December 2009, 03:25
Hello everyone, again!
This time I'm wondering if there is any way to have an event(like a button press) interrupt the code and go to a line label at any time?
The problem with my code is that the controller only checks for the button press after that pause. That pretty much means that you have to hold down the button to get the right timing in order for the moving LEDs to switch direction(which is what my code is about). So is there any way to interrupt the program at ANY time and then point it at a line label to continue there?
this is my code:
x var byte
switch var bit
TRISA = 1
TRISC = 0
switch = 1
mainloop:
x = 1
loop:
PORTC = x
pause 500
if switch = 1 then
if PORTA.3 = 0 then
switch = ~switch
goto loop
endif
if x == 8 then
goto mainloop
else
x = x << 1
goto loop
endif
else
if PORTA.3 = 0 then
switch = ~switch
goto loop
endif
if x == 1 then
x = 8
goto loop
else
x = x >> 1
goto loop
endif
endif
mackrackit
- 30th December 2009, 05:37
Look at interrupt on change normally on PORTB.
ciroman
- 30th December 2009, 11:58
We'll I've got a premade board - The PICKit 2 so the button is on the PORTA.3 pin - just like in the program. Is there no way to do it with that pin?
mackrackit
- 30th December 2009, 13:58
Check the data sheet and see what that pin can do. If it has ADC then with some imagination you could do an ADC interrupt.
But just change things around to use an interrupt pin. That is what they are for.
Acetronics2
- 30th December 2009, 15:03
Hi,
My crystal ball says ...
" Pickit2 with a ready made demo card means a 16F690 " ...
RA.3 shows an Interrupt on change feature ...
soooo ... just use it !!!
Alain
PS: try to give SOME MORE INFOS if you have further questions ...
Art
- 30th December 2009, 22:35
An interrupt is overkill for this, and not the typical way to handle the problem at all.
I'd use a counter to have your button checked every 10ms, and LED routine run every 500ms like this.
and you get to save the interrupt for when it's actually needed.
x var byte
direction var bit
timerbyte var byte
TRISA = 1
TRISC = 0
x = 1
loop:
PORTC = x
pause 10
timerbyte = timerbyte + 1
IF PORTA.3 = 0 then
IF direction = 0 THEN
direction = 1
ELSE
direction = 0
ENDIF ' direction
ENDIF ' button
IF timerbyte = 50 THEN timerbyte = 0 ' 500ms has passed if timerbyte = 0
IF timerbyte = 0 THEN
IF direction = 0 THEN
IF x = 8 THEN x = 1
x = x << 1
ELSE
IF x = 1 THEN x = 8
x = x >> 1
ENDIF ' direction
ENDIF ' timerbyte
goto loop
mackrackit
- 30th December 2009, 22:50
Overkill????
Please explain.
Art
- 30th December 2009, 22:55
Thought I already did.
It's using hardware to do what should and can be done in software.
Like wiring a circuit to turn the pic's power off to turn an LED off.
and developing bad habit at the same time, and not learning anything.
mackrackit
- 30th December 2009, 23:26
Well after you made an edit to your post you have some explanation.
Looping back through the code every so often to check on a button smells like some sort of pasta to me.
Maybe the OP should learn to use the hardware. Then when the code grow to more than a few lines......
Wait a minute. Your joking, right?
Using an interrupt is forming bad habits.
Now I get it.
NOT!
Art
- 31st December 2009, 00:14
When it's not needed, I think it is.
What if the platform didn't support interrupts?
I think using an interrupt when it isn't needed is forming a bad habit.
The loop is happening anyway, and I have one less label in my code.
It's less spaghetti than the original which doesn't run in one single loop.
An interrupt would add more spaghetti in the generated asm to goto 0x04.
This is a simple program, but as it grew, you might need an interrupt for something else.
EDIT; ps I edited to edit the program, but haven't actually run it.
if it has any issue, it still offers a solution.
mackrackit
- 31st December 2009, 00:30
Fair enough.
Good to see all possible solutions and reasons for the solutions.
Bruce
- 31st December 2009, 00:51
I'll give it a shot with a simple ON INTERRUPT version.
x var BYTE
Switch VAR BIT
TRISA = $FF
TRISC = 0
ANSEL = 0 ' A/D disabled (all digital)
ANSELH = 0
IOCA.3 = 1 ' int-on-change enabled for RA3
INTCON = %10001000 ' enable global & int-on-change interrupts
Switch = 1
ON INTERRUPT GOTO MyISR
mainloop:
x = 1
loops:
PORTC = x
pause 500
if switch = 1 then
if x == 8 then
goto mainloop
else
x = x << 1
goto loops
endif
else
if x == 1 then
x = 8
goto loops
else
x = x >> 1
goto loops
endif
endif
GOTO mainloop ' just in case
DISABLE
MyISR:
WHILE PORTA.3=0 ' wait for button release (so we can clear the int flag & be
WEND ' sure it interrupts on a high-to-low transition)
Switch = ~Switch ' toggle Switch
INTCON.0 = 0 ' clear interrupt flag bit
RESUME
END
There must be ten gozillion ways to do something like this, even without an interrupt, but
it's still handy to at least learn about interrupts.
Note that you could also do something similar by just monitoring the interrupt flag-bit with
global interrupts disabled. You just need to get a tad more creative...;o}
Tap & release the button as fast as you can to see how quickly it reacts.
ciroman
- 4th January 2010, 18:35
An interrupt is overkill for this, and not the typical way to handle the problem at all.
I'd use a counter to have your button checked every 10ms, and LED routine run every 500ms like this.
and you get to save the interrupt for when it's actually needed.
x var byte
direction var bit
timerbyte var byte
TRISA = 1
TRISC = 0
x = 1
loop:
PORTC = x
pause 10
timerbyte = timerbyte + 1
IF PORTA.3 = 0 then
IF direction = 0 THEN
direction = 1
ELSE
direction = 0
ENDIF ' direction
ENDIF ' button
IF timerbyte = 50 THEN timerbyte = 0 ' 500ms has passed if timerbyte = 0
IF timerbyte = 0 THEN
IF direction = 0 THEN
IF x = 8 THEN x = 1 <- You forgot that if x = 8 and then x = 1 you must go to loop or else x will never be 1 but MORE than one
x = x << 1
ELSE
IF x = 1 THEN x = 8 <- Same here
x = x >> 1
ENDIF ' direction
ENDIF ' timerbyte
goto loop
I have pointed out some mistakes. Thanks for your code man anyway, cool way to do things, much better than ON INTERRUPT I believe. Don't be too harsh on me guys I've been learning this for less than a week
Bruce
- 4th January 2010, 20:20
I've been learning this for less than a week
I would say you're definitely doing good for someone just getting started.
It's nice here since you find so many various ways of doing something, which really
helps one to think outside-the-box. Embedded programming is just creative problem
solving, and you'll find a lot of different creative approaches around here for
sure.
Which one is best for your application is totally up to you.
ciroman
- 5th January 2010, 06:10
Thanks Bruce, you just pointed out the reason why I love programming and why I love programming a micro controller is because you can see the results in real life, not only digitally.
Thanks, really
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.