View previous topic :: View next topic |
Author |
Message |
Darknight23
Joined: 08 Nov 2005 Posts: 4
|
restart main() in 10 seconds |
Posted: Tue Nov 08, 2005 6:50 pm |
|
|
I'm using a 16F818, and I've read about using Timer1 as a real-time clock, but is there an easier way to do this? If a loop executes for 10 seconds I wanted to restart the main() method. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 08, 2005 6:55 pm |
|
|
What is the reason for doing this ?
1. Is this 10 second period a test for program lock-up ?
In other words, are you really talking about a Watchdog Timer ?
2. Or is it just a normal part of program operation, such that
you want to jump to the start of a loop after 10 seconds ?
3. Do you really want to go to the start of main(), or do you
want to go to the start of a while() loop in your program ? |
|
|
Darknight23
Joined: 08 Nov 2005 Posts: 4
|
|
Posted: Tue Nov 08, 2005 6:59 pm |
|
|
The loop is actually within another function separate from main(). Yes it is apart of the normal operation, therefore a WDT wouldn't be appropriate. Basically restart the PICs operation after a specified time. Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 08, 2005 7:58 pm |
|
|
Can you explain what the basic purpose of your program is ?
The method that you have conceived, may not be the best
possible way to design the program. |
|
|
Darknight23
Joined: 08 Nov 2005 Posts: 4
|
|
Posted: Tue Nov 08, 2005 8:31 pm |
|
|
Sure, I have 3 infrared sensors mounted on the front of a remote control car. The middle IR detects the object around 1 ft and turns on the top/bottom IR. The top and bottom IR are used for close range detection. When top/bottom IR detect the object, the car is supposed to stop. I'm currently working when the middle IR detects an object, the middle IR turns on, and turns on the close range IR. After a specified amount of time, if the top/bottom IR do not detect anything, the car should then restart to its start up state: Middle IR on, Top/bottom IR off, forward motion, with normal speed.
The program is controlled with this in main():
Code: | while(1)
{
reset();
detect_far();
detect_near();
} |
In the detect_near() method, I after a certain time, if the top/bottom IR doesn't detect anything, I want it to go back to the while(1) in the main method. |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Tue Nov 08, 2005 9:14 pm |
|
|
Darknight23 wrote: |
Code: | while(1)
{
reset();
detect_far();
detect_near();
} |
|
It strikes me that this code will never get to detect_far(), becasue it will resetin the first line of the while loop.
As a general comment, resetting the PIC just to restore the initial conditions is quite a non-orthodoxal approach. It may work fine, if you don't want to retain certain values all the time.
Consider the following algorithm (although there are many ways to skin this cat):
Code: |
void main()
{
init_global(); // initialize global variables
init_hardware(); // initialize hardware
while (1)
{
if (detect_far())
{
while(elapsed < 10sec)
{
if (detect_near()
{
stop();
}
}
}
}
}
|
|
|
|
|