PDA

View Full Version : Can you give varied levels of priority to blocks of code?



Archangel
- 10th August 2006, 02:08
Hi everyone, I am a real green newbie, first thing I want to do is thank everyone here for thier participation, I have already learned a ton from reading your posts...

What I am trying to do is have a program set up with several blocks of code under several labels ( I have this already and it works) what I need to do is give each labeled block a priority level so they will run ahead of the lower priority if both or several are called by outside input, say a switch or button.

Is this do able with a PIC?

Joe Stokes

Melanie
- 10th August 2006, 07:22
This is usually what interrupts are for... to interrupt what the PIC is doing at any point in time and go service some task of greater priority and then on completion to resume what was previously interrupted.

Archangel
- 11th August 2006, 07:53
Hi Melanie!

Ya I understand about interupts, that is not really my question,
let's suppose we have a program which executes 4 strings to an lcd, each associated to a button or switch, just for clarity I will give them the following labels, FIRE:
CLOSING TIME:
RESTROOMS CLOSED:
LAST CALL:
This might be a sign in a Pub or Bar.

The obvious first priority would be , FIRE, likely followed by Restrooms Closed . . etc . . .

What I need is for FIRE to cancel and interrupt all lower displays, and Restroom Closed to Interrupt Last call without being able to interrupt FIRE and so on. . . Is this something a PIC can do?
Thank You,
Joe Stokes

Archangel
- 11th August 2006, 08:15
HERE IS AN EXMPLE OF MY CODE



'************************************************* ***************
'* Name : Barsign.BAS *
'* Author : [Joe Stokes] *
'* Notice : Copyright (c) 2006 [JOE STOKES] *
'* : All Rights Reserved *
'* Date : 8/5/06 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
CMCON=7 ' DISABLE ANALOG ON 628A.............................................. ..............

INCLUDE "modedefs.bas"


input 0
input 1
input 2
input 3
input 4
input 5
input 6
output 7
Main:

serout 7,N2400,[254,1,"?c0"] 'Sets curser to off . . Anderson Backpack
pause 300
serout 7,N2400,[254,1,"?f?a"] ' special initialization for Anderson Backpack
' sets curser to beginning of row 1
pause 3500 ' give lcd time to initalize after clear
serout 7,N2400,[254,1,"WELCOME TO MY PUB"]
pause 2000 ' WAIT 2 SECONDS BEFORE START
serout 7,N2400,[254,1,"?f?a"] ' RESET LCD
goto start ' FINALLY PROGRAM STARTS



start:
'CHECKS STATUS RESTROOMS AND REPORTS TO PATRONS ON LCD

RESTROOM:
WHILE PortB.4 = 1
serout 7,N2400,[254,1,"?f?a"]
pause 250
SEROUT 7,N2400, [254,1,"RESTROOMS ARE CLOSED"]
PAUSE 3000
serout 7,N2400,[254,1,"?f?a"]
clear
WEND

'CHECKS STATUS OF FIRE ALARM AND REPORTS TO PATRONS ON LCD

FIRE:
WHILE PortB.5 = 1
serout 7,N2400,[254,1,"?f?a"]
pause 250
SEROUT 7,N2400, [254,1,"FIRE, PLEASE EVACUATE"]
pause 3000
serout 7,N2400,[254,1,"?f?a"]
clear
WEND
'CHECKS STATUS OF LAST CALL AND REPORTS TO PATRONS ON LCD
LAST_CALL:
WHILE PortA.1 = 1
serout 7,N2400,[254,1,"?f?a"]
pause 250
SEROUT 7,N2400, [254,1, "LAST CALL FOR ALCOHOL, SOON ONLY COFFEE SERVED!"]
'SEROUT 7,N2400,[254,192,]
pause 3000
serout 7,N2400,[254,1,"?f?a"]
clear
WEND

'CHECKS STATUS OF CLOSING TIME AND REPORTS TO PATRONS ON LCD

CLOSING:
WHILE PortB.0 = 1
serout 7,N2400,[254,1,"?f?a"]
pause 250
serout 7,N2400,[254,1,"CLOSING TIME THANK YOU AND GOODNIGHT!"]
PAUSE 3000
serout 7,N2400,[254,1,"?f?a"]
clear
WEND


GOTO START



END

i am thinking If fire = 1 then start
IF restroom = 1 then start
and then somehow use some and statements to call interrupts
or exclude them . . .

as Daniel Boone once said" I've never been lost, but I've been a might
bewildered . ."

Thanks ,
Joe

SteveB
- 11th August 2006, 14:01
Hi Joe,
Try something like this:


CMCON=7 ' DISABLE ANALOG ON 628A

INCLUDE "modedefs.bas"

input 0
input 1
input 2
input 3
input 4
input 5
input 6
output 7

FIRE_PIN VAR PortB.5
RESTROOM_PIN VAR PortB.4
LAST_CALL_PIN VAR PortA.1
CLOSING_PIN VAR PortB.0


GOTO START
'-----------------------------------------
'---------- Subroutines ------------------
'-----------------------------------------

'---=== Initialize LCD LCD ===---
INIT_LCD:
serout 7,N2400,[254,1,"?c0"] 'Sets curser to off . . Anderson Backpack
pause 300
serout 7,N2400,[254,1,"?f?a"] ' special initialization for Anderson Backpack
' sets curser to beginning of row 1
pause 3500 ' give lcd time to initalize after clear
RETURN

'---=== Reports status of RESTROOM to partons on LCD ===---
RESTROOM:
serout 7,N2400,[254,1,"?f?a"]
pause 250
SEROUT 7,N2400, [254,1,"RESTROOMS ARE CLOSED"]
PAUSE 3000
serout 7,N2400,[254,1,"?f?a"]
RETURN


'---=== Reports status of FIRE to partons on LCD ===---
FIRE:
serout 7,N2400,[254,1,"?f?a"]
pause 250
SEROUT 7,N2400, [254,1,"FIRE, PLEASE EVACUATE"]
pause 3000
serout 7,N2400,[254,1,"?f?a"]
RETURN

'---=== Reports status of LAST_CALL to partons on LCD ===---
LAST_CALL:
serout 7,N2400,[254,1,"?f?a"]
pause 250
SEROUT 7,N2400, [254,1, "LAST CALL FOR ALCOHOL, SOON ONLY COFFEE SERVED!"]
'SEROUT 7,N2400,[254,192,]
pause 3000
serout 7,N2400,[254,1,"?f?a"]
RETURN

'---=== Reports status of CLOSING to partons on LCD ===---
CLOSING:
serout 7,N2400,[254,1,"?f?a"]
pause 250
serout 7,N2400,[254,1,"CLOSING TIME THANK YOU AND GOODNIGHT!"]
PAUSE 3000
serout 7,N2400,[254,1,"?f?a"]
RETURN

'---=== GREETING to partons on LCD ===---
GREETING:
serout 7,N2400,[254,1,"?f?a"]
pause 250
serout 7,N2400,[254,1,"WELCOME TO MY PUB"]
PAUSE 3000
serout 7,N2400,[254,1,"?f?a"]
RETURN

'-----------------------------------------
'---------- Main Program Code ------------
'-----------------------------------------

START:
GOSUB INIT_LCD
GOSUB GREETING

MAIN:
'CHECKS STATUS RESTROOMS AND REPORTS TO PATRONS ON LCD

IF FIRE_PIN = 1 THEN
GOSUB FIRE
ELSE
IF RESTROOM_PIN = 1 THEN
GOSUB RESTROOM
ELSE
IF LAST_CALL_PIN = 1 THEN
GOSUB LAST_CALL
ELsE
IF CLOSING_PIN = 1 THEN
GOSUB CLOSING
ELSE
GOSUB GREETING
ENDIF
ENDIF
ENDIF
ENDIF
PAUSE 1000
GOTO MAIN

END

I did not make any changes to the actual code lines you have, but did rearrange them into subroutines that could be called by nested IF...THEN...ELSE...ENDIF statements. This provides the priority levels you wanted. The first IF...THEN that is true will execute and then jump over the subsequent code in the ELSE portion of the statement. Additionally, I added a 5th option (GREETING) if none of the other conditions are true. You can change around the priority any way you want. Also note that I assigned names to the pins to make them easier to understand later on in the code. I haven't access to my compiler right now, so I haven't "run" my version, it isn't bug tested. Also, I am sure there are more ways to "skin this cat" but this is a start.

EDIT: Got access to my PBP, and it compiled without errors, so you just need to check it out on you equipment.

HTH,
Steve

Archangel
- 11th August 2006, 21:35
HI STEVE B.

Wow that is very close to my first try which I could not get to work I had used button commands and IF THEN statements and included an ENDIF statment within the subroutine, I see you put them all at near the end of program, I am curious about the indenting you used, is it there so you the programmer can keep it straight in your mind or does it affect the program in some way?

Anyway I want to thank you and Melanie for your help, I really appreciate it!
I hope to be helpful to someone else someday!

Best Regards
Joe Stokes

SteveB
- 11th August 2006, 22:43
I am curious about the indenting you used, is it there so you the programmer can keep it straight in your mind or does it affect the program in some way?

Joe,
Indenting is just a way to keep nested levels of code straight. It is purely technique and does not affect the code in any way (with the exception of Labels, which should start in the first column). I've seen a number of different schemes. It is just one aspect in organizing your programs. The key is find something that works for you and sticking to it religiously. Then, when you come back to a piece of code you wrote 6 months ago (or you have a complex, many line program), you can quickly reaquiant yourself with what is happening and find what you need easily.

Here (http://www.picbasic.co.uk/forum/showthread.php?p=3276#post3276) is a very nice post by Melanie on the idea of program structure. If you have nothing to start from, you can't get much better than learning the ropes from someone like Melanie.


I see you put {the IF... statements} all at near the end of program
Again, in this case just another way to orginize things to hopefully make the logic a little clearer.


Wow that is very close to my first try which I could not get to work
Maybe just for fun you could post the code you tried that did not work, and folks here could show you why it didn't. Could be a chance to learn from some very smart, capable folks (from whom I myself am indepted greatly). If so, look here (http://www.picbasic.co.uk/forum/misc.php?do=bbcode) for formating, particularly using
... to format your posts so they are more readable.

Good Luck,
Steve

Archangel
- 12th August 2006, 01:58
I wrote over the file I tried originally, with the while / wend code but I figured out why it didn't work, I had made labels and coded them this way
b1 var byte
b2 var byte
b3 var byte
LABEL!:

button1 ,port ,...
anyway I copied the button command out of the book assigned the variables to them and used IF Var b1= 1 then
serout 7,n2400, [" string"]
endif
LABEL2: more of the same

and what it did was display all the strings in a loop

so I trashed that code and went with my no variable code
LABEL1: while PortA.0 = 1
serout ["mystring"]
wend

LABEL2: WHILE PortA.1 =1
SEROUT ["more strings"]
wend
ETC . . .

and that would display at the push of a button
whereas you put the if statements under the main routine and made the LABEL statements into sub routines being called by the IF's
Like ah said I was Green ! I've only had PBP for about 2 weeks now and never coded anything before, . . . well PC Batch files, but nothing else.
I needed to give my code precedence and you showed me the way!
AND IT WORKS!

Thank You again
Joe Stokes

" I have never been lost, but there were a few times I was a might bewildered" ... Danial Boone