PDA

View Full Version : sub routine question



emmett brown
- 19th July 2006, 15:49
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!

Archilochus
- 20th July 2006, 01:41
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

emmett brown
- 20th July 2006, 15:53
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

Dwayne
- 20th July 2006, 18:17
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

emmett brown
- 20th July 2006, 18:41
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

Dwayne
- 20th July 2006, 19:18
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.

emmett brown
- 21st July 2006, 18:39
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

mister_e
- 21st July 2006, 19:02
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



Start:
for Repeat=0 to x
' MainLoop
Next

'Do your Subroutine here
GOTO Start

emmett brown
- 21st July 2006, 19:32
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

mister_e
- 21st July 2006, 20:38
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.

emmett brown
- 21st July 2006, 23:09
It'll be battery powered

emmett brown
- 22nd July 2006, 03:24
I believe I have it tackled Thanks so much for the invaluable input! Emmett

emmett brown
- 31st July 2006, 17:55
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