sub routine question


Closed Thread
Results 1 to 13 of 13
  1. #1
    Join Date
    Jun 2006
    Posts
    35

    Default sub routine question

    Does a subroutine stop everything like a pause would if it were in the main program? I need ,while running the hpwm code to check continuity on a level sensor every 15 minutes or so then turn an LED on. What would be the best way to do this? Also what would be the best serial LCD that will monitor what's going on in my program and still allow pins for ADC/HPWM/ inputs ETC? 16F877 at 4 Mhz. It would need to be pretty stupid proof as in easy instructions as in pin out's and pre-canned code cause I shouldn't be getting this complicated with this, but that's the way I am! Thanks much, this group is excellent - well worth the cost of PBP!
    Last edited by emmett brown; - 19th July 2006 at 16:06.

  2. #2
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    The subroutine executes just like a regular section of code. Hardware PWM will keep on going as usual (unless your sub changes the HPWM settings). Even the 'pause' would not stop HPWM (It's "hardware" driven - so it works in the background while your PIC is executing code).

    As far as LCD's, on an '877 you should have plenty of pins to play with, so maybe a regular LCD would work (lots cheaper). If you need those pins, many serial LCD's are available (I've never used them so can't recommend a model).
    "Serout" or "Serout2" should work with most.

    Arch
    "Data sheets? I ain't got no data sheets. I don't need no data sheets. I don't have to read any stinking data sheets!"

  3. #3
    Join Date
    Jun 2006
    Posts
    35


    Did you find this post helpful? Yes | No

    Default

    Thanks much. One more query. What would be the best way to have a subroutine execute after say a 15 minute time delay without stopping the rest of the program as in adc samples pause for 1 minute then sample again. (That needs to keep running while the sub waits for 15 minutes). Emmett
    Last edited by emmett brown; - 20th July 2006 at 15:55.

  4. #4
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    I don't know what your project is about, but if "15 min" is not a absolute, and you want to waste a little time on the chip....

    there are approx 900 seconds in 15 minutes.

    Using a long int of 32768 you can break a second down into 1/36 of a second.

    Since the chances are, your main program will be screaming fast, you only need to call a "Sub routine" wherever you want, and think is appropriate.

    Its simple, no big thinking of Code, no interupts...

    double counter

    counter=900
    Main:
    ......
    .....
    ADC in...
    LCD out...
    Rest of program....
    ......
    Goto "Pause and light"
    goto Main

    Pause and Light:
    pause 30 (Pause 1000 is 1 second, and 30 is adjustable to your liking)
    Counter=counter-1;

    If counter==0 then
    Check continuity.
    flash bulb.
    counter=900
    endif

    goto main.
    end

    This is a Psuedo code.

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  5. #5
    Join Date
    Jun 2006
    Posts
    35


    Did you find this post helpful? Yes | No

    Default

    Hmmm I'll try to go this route tonight makes sense but I haven't had PBP very long so I'll have to wrestle with it until it works. So there is no special "counter" that I have to set up to count down as in a var word or something? Ah never mind I need to try it first before I take any more of your time Thanks very much! Emmett
    Last edited by emmett brown; - 20th July 2006 at 18:56.

  6. #6
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    So there is no special "counter" that I have to set up to count down as in a var word or something?
    the "counter" is set at 900, because there are 900 seconds in 15 minutes.

    the pause is the variable that you use to control how long you want to pause every time you go to the subroutine. This Pause I chose arbitrarily...to be 30 milliseconds. thus when you go to the subroutine 900 times (as the counter goes to zero) you are pausing a total of 900 x 30 millseconds = 27000 milliseconds of pause. which is approx. 2.7 min.

    Now, you can adjust the counter or the pause statement to adjust to your 15 minute time. Making a bigger pause (like pause 1000) will cause your chip to "seem" slow, because the chip will be delaying 1 second before going on to your next step in the program. With this Pause 1000,you will have a little over 15 minutes before a light blinks. (900 x 1000)= 900,000 milliseconds which is 900 seconds which is 15 minutes (Plus the time it takes to execute all the rest of the code in your program, which will be very small).

    With a pause of 30 and a counter equal to 30,000, you have approx 900,000 milliseconds also...which is 15 minutes. But your chip will only delay 30 milliseconds before executing its next step, NOT 1000 milliseconds.

    Thus, if you are happy with only a 30 millisecond delay, and it does not affect any critical part of your project, you may just be happy letting your chip waste away time.

    Dwayne

    The program is not made to be "small", "efficient", or "conservative". Its just made to get a job done, and understandable for most to see.
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  7. #7
    Join Date
    Jun 2006
    Posts
    35


    Did you find this post helpful? Yes | No

    Default

    Is there any way I can talk someone into integrating the above that Dwayne contributed into this working code? I've tried several ways and only complicate the errors. Tried the COUNT command but don't know how to make it integrate into it to go to the sub routine. The counter will work perfectly for what I need but I am not experienced enough with PBP to make this addition. Thanks again Emmett
    Last edited by emmett brown; - 22nd July 2006 at 03:22.

  8. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    From what i understand, you want to execute a Subroutine at every 15minutes right?

    The only thing that spring to mind without adding extra hardware is a Timer interrupt.. or measure the mainloop time, execute it untill it reach the 15Minutes, then execute the Subroutine. Once you know the time of the main program, you just calculate the amount of time you'll have to repeat it.

    If you don't know the time... you can even use a extra i/o and measure it with a scope or whatever else.

    Snip
    High ExtraI_O
    'mainRoutine
    Low ExtraI_O

    Assuming you must loop 'x' time

    Code:
    Start:
        for Repeat=0 to x
            ' MainLoop
            Next
    
        'Do your Subroutine here
        GOTO Start
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  9. #9
    Join Date
    Jun 2006
    Posts
    35


    Did you find this post helpful? Yes | No

    Default

    Yes every 15 minutes or so no matter what the time length for the main program. I see what you mean, if the main took longer to execute then the counter wouldn't decrease in the same amount of time. Either way would simply need to be adjusted after running the program an hour or so. I'm still lost as to how or where to plug it into my program though. Yes, It stinks that I am relatively new at this. Thanks again, Emmett

  10. #10
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    well the timer interrupt is still valid.

    Is it going to be a battery powered device or you can access easy to the line AC signal?

    If so you can use a Counter overflow Interrupt. OR, by what i feel of, the Darrel Taylor's Elapsed timer could be another solution.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  11. #11
    Join Date
    Jun 2006
    Posts
    35


    Did you find this post helpful? Yes | No

    Default

    It'll be battery powered

  12. #12
    Join Date
    Jun 2006
    Posts
    35


    Did you find this post helpful? Yes | No

    Default

    I believe I have it tackled Thanks so much for the invaluable input! Emmett

  13. #13
    Join Date
    Jun 2006
    Posts
    35


    Did you find this post helpful? Yes | No

    Default

    Yep, it works ! Thanks much to Dwayne and Mister E without which it wouldn't have happened. The best pic forum on the net! Emmett

Similar Threads

  1. Replies: 4
    Last Post: - 17th December 2009, 00:29
  2. Timer0 Instant Interrupts Question
    By wmarchewka in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 14th December 2009, 05:29
  3. HARDWARE I2C SAMPLE CODE question
    By Michael Wakileh in forum Code Examples
    Replies: 2
    Last Post: - 16th June 2009, 21:07
  4. IOC question
    By Adrian in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 1st April 2008, 19:21
  5. Please answer my first question
    By John_001 in forum Off Topic
    Replies: 1
    Last Post: - 15th September 2006, 06:49

Members who have read this thread : 1

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