Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Multi-tasking

Status
Not open for further replies.

Broz

New Member
First off I would like to say thanks to the members of this board for helping me so far. I've been programming a 16f690 with my pickit2 successfully. I can make LED's do pretty much anything I want, thanks to the help of this forum.

Now I would like to make the PIC do various jobs depending on an input. I didn't know what to title this thread, so I just titled it multitasking. I don't know all of the language of PIC's yet. I would like the pic to perform a certain task on an LED, say pulsing it, while constantly monitoring an input. When the input pin changes states, say a high to a low, I want the PIC to perform a different task on different LED's for a short time and then go back to performing the original task.

What is this called? Is it called an interrupt? And can someone point me to an example of a program that does this. Thank you.
 
Bear in mind though, there's no need whatsoever to use an interrupt for this simple task - it's trivial without it. I would suggest trying to do it both with, and without, interrupts - see what difference it makes.

It's important to consider what happens during interrupt calls, and it's a great deal more difficult to find bugs if you're using interrupts.
 
The interrupt makes sense and I've done the reading. It looks like it's going to work for me. I'm curious though, Nigel how would I do it without interrupts? Thanks.
 
You can place your key check routine in your main program.

A LED pattern is running continuously. When you press the button it changes to another pattern. For this you don’t need an interrupt. The BUTTON CHECK routine can place in your main program. So no need to write an interrupt routine.

The same thing can do with use of interrupt.

But consider a TIME is displaying on the seven segments. You have to generate accurate time base, you have to show the time, you have to ring the alarm, and you have to check the button press etc…..for this application interrupt is a requirement.
 
Not always. It's damn handy to have interupts available, and good design practice, but 'pseudo-multi- threading' is something I have done and i'm sure we've all done. Micro's run so fast these days....

Sounds like all you need is a subroutine to handle the 'something else' with your main routine turning the LED on...then a delay loop with a button check embedded in it, then turning the LED off again :D

If you are new to programming, regardless of the language you wish to learn, a flow diagram always makes things clearer. Might not be needed for interupts, but program flow....

Blueteeth
 
As suggested, you periodically check for the button press during your program - bear in mind that very early PIC's didn't support interrupts at all, and it was quite possible to build real time clocks with multiplexed displays and buttons to set them.

You need to understand how blindingly fast PIC's actually run!.
 
Here is an example of what you are attempting. One of my Led boards.
**broken link removed**
This board is always keeping track of the current pattern, number of times current pattern repeated, pattern speed, whether or not a pattern should change or advance, brightness level, how long the device has been running since powered-on, when to automatically power off the device to save battery energy, whether or not to auto power off at all. Also keeps track of the 3 types of operator button press be it a single tap, a double tap, or an extended press where the length of time pressed invokes a different function. Thats multitasking and that does not include the actual display of the leds :)

If you do not know interrupts, its good to make the effort to learn it now. But as to your original question, just as Gayan stated, place a CheckInput routine inline with the main code or inline with whatever routine consumes majority of the processing time. I leave interrupts for only very precise time tracking and inputs and highest priority events. I use CheckInput routine when the monitored event is asynchronous, or random, or slow as in manual button presses.

Scrubbed example:

Code:
#DEFINE BUTTON_1 PORTA, 4        

INTERRUPT:
	;REDACTED
	;------CHECK TIMER 1----------------;
	;------AUTO POWER-OFF SELECTION-----;

;*********MAIN**************************

MAIN:	;REDACTED
	BCF	STATUS, C
	RLF	PATTERN_NUM, W
	ADDWF	PCL, F
	NOP
	NOP
	CALL	P1
	GOTO	MAIN_END
	CALL	P3
	GOTO	MAIN_END
	CALL	P4
	GOTO	MAIN_END
	;REDACTED

MAIN_END
	GOTO	MAIN
;**********END 0F MAIN******************

P1:	LOADF	DISPLAY_TIME, .2
	LOADF	TOP_LUX, B'00000001'
	LOADF	MID_LUX, B'00000011'
	LOADF	LOW_LUX, B'00000011'
	CALL	SHOW_LUX
	LOADF	TOP_LUX, B'00000001'
	LOADF	MID_LUX, B'00000101'
	LOADF	LOW_LUX, B'00000101'
	CALL	SHOW_LUX
	RETURN
;---------------------------------------
SHOW_LUX:
	;REDACTED
	CALL	CHECK_BUTTON
	RETURN

;=======================================
;SINGLE BUTTON WITH 4 FUNCTIONS.
;FUNCTION 1: TAP  BUTTON                 FOR CHANGE PATTERN.
;FUNCTION 2: HOLD BUTTON FOR 0.5 SECONDS FOR POWER OFF.
;FUNCTION 3: HOLD BUTTON FOR 2.0 SECONDS FOR CONTINUOUS/CYCLE MODE.
;FUNCTION 4: HOLD BUTTON FOR 2.5 SECONDS FOR BRIGHT/DIM MODE.
CHECK_BUTTON:

CHB_0	BTFSS	BUTTON_1			;ACTIVE HIGH
	RETURN

	;---FUNCTION 1---CHANGING PATTERNS---;
FUNC1	;REDACTED
	GOTO	MAIN
	;-----END FUNCTION 1-----------------;
	;---FUNCTION 2---POWER OFF-----------;
FUNC2	;REDACTED
	SLEEP
	GOTO	MAIN
	;-----END FUNCTION 2-----------------;
 
Last edited:
donniedj said:
Code:
;=======================================
;SINGLE BUTTON WITH 4 FUNCTIONS.
;FUNCTION 1: TAP  BUTTON                 FOR CHANGE PATTERN.
;FUNCTION 2: HOLD BUTTON FOR 0.5 SECONDS FOR POWER OFF.
;FUNCTION 3: HOLD BUTTON FOR 2.0 SECONDS FOR CONTINUOUS/CYCLE MODE.
;FUNCTION 4: HOLD BUTTON FOR 2.5 SECONDS FOR BRIGHT/DIM MODE.

How does that work? If it powers down after you hold the button for ½ a second, how does it ever get to 2 seconds?

Mike.
 
Pommie said:
How does that work? If it powers down after you hold the button for ½ a second, how does it ever get to 2 seconds?

Mike.

Maybe it just looks for a button-down with the next button event being a button-up at around 0.5s, instead of looking for a 0.5s-long button-down.


Torben
 
A device with such characteristic is difficult to power off.

Should it power off if the user hold the key for 1 or 1.5 seconds? What should it do in this case?

Why not adopt the universal accepted method of holding the key to turn a device off?
 
eblc1388 said:
A device with such characteristic is difficult to power off.

Should it power off if the user hold the key for 1 or 1.5 seconds? What should it do in this case?

Why not adopt the universal accepted method of holding the key to turn a device off?

I don't know what the builder intends, but I was thinking that it could have, perhaps, a window when the release would be accepted; say, 0.4s to 1s, or something appropriate to the command mapping.

I'm not commenting on how ideal the idea itself is, just on the fact that I think it could be done. :)


Torben
 
Pommie said:
How does that work? If it powers down after you hold the button for ½ a second, how does it ever get to 2 seconds?
Mike.

Visual Feedback.

https://www.anothercoilgunsite.com/chasers/multi-button.wmv

Turning off has the highest priority, hence the shortest wait time second to a single tap. "Hold for x seconds" means to hold till you see a visual key. 2 flashes of 2 LEDs will power off. 3, 4, and 5 flashes of the 3, 4, or 5 LEDs will do other things.
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top