+ Reply to Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,816

    Question Can you give varied levels of priority to blocks of code?

    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

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358

    Default

    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.

  3. #3
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,816

    Default Priority to code blocks

    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

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,816

    Default Priority to code blocks

    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

  5. #5
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343

    Default

    Hi Joe,
    Try something like this:
    Code:
    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
    Last edited by SteveB; - 11th August 2006 at 14:41.

  6. #6
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,816

    Smile That Simple?

    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

  7. #7
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343

    Default

    Quote Originally Posted by Joe S.
    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 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 for formating, particularly using [code]...[/code] to format your posts so they are more readable.

    Good Luck,
    Steve

  8. #8
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,816

    Cool Gone

    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

Similar Threads

  1. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  2. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  3. Re-Writing IF-THEN-AND-ENDIF code?
    By jessey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th August 2006, 17:23
  4. Still HSEROUT woes
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 34
    Last Post: - 11th July 2006, 21:13
  5. Timer PIC16F57
    By leonel in forum mel PIC BASIC Pro
    Replies: 25
    Last Post: - 1st July 2005, 08:14

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts