View previous topic :: View next topic |
Author |
Message |
ssaakmnt
Joined: 03 Dec 2011 Posts: 27
|
pic hanging |
Posted: Sat Mar 30, 2013 5:46 am |
|
|
I wrote this simple control routine and it was working fine most of the time,
some times it get stuck and do not execute at all Code: | #include <16F84.h>
#use delay(clock=4000000)
#fuses xt
#define floor1 pin_a0
#define floor2 pin_a1
#define floor3 pin_a2
#define sensor1 pin_a3
#define sensor2 pin_b0
#define sensor3 pin_b1
#define call1 pin_b2
#define call2 pin_b3
#define call3 pin_b4
#define motor_up pin_b5
#define motor_down pin_b6
void main() {
while (true)
{
if (input(floor1)) // always floor 1
{while (1){
output_high(motor_down);
if (input(sensor1))
break;}}
output_low(motor_down);
// the call1
if (input(call1))
{while (1){
output_high(motor_down);
if (input(sensor1))
break;}}
output_low(motor_down);
if (input(floor3)) // always floor 3
{while (1){
output_high(motor_up);
if (input(sensor3))
break;}}
output_low(motor_up);
// the call3
if (input(call3))
{while (1){
output_high(motor_up);
if (input(sensor3))
break;}}
output_low(motor_up);
///////////////////////////////
// from floor 1 to 2
if (input(floor2))
{if (input(sensor1))
while (1){
output_high(motor_up);
if (input(sensor2))
break;}}
output_low(motor_up);
// from floor 3 to 2
if (input(floor2))
{if (input(sensor3))
while (1){
output_high(motor_down);
if (input(sensor2))
break;}}
output_low(motor_down);
// the call2
if (input(call2))
{if (input(sensor1))
while (1){
output_high(motor_up);
if (input(sensor2))
break;}}
output_low(motor_up);
// the call3
if (input(call2))
{if (input(sensor3))
while (1){
output_high(motor_down);
if (input(sensor2))
break;}}
output_low(motor_down);
}
}
| it works only when disconnect the power source and reconnected again so what I can do about hardware or software to fix this. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Mar 30, 2013 6:56 am |
|
|
The PIC hardware is so simple that it is quiet difficult to make it 'hang'. The most likely problems when the processor hangs:
1) There is an interrupt not being cleared and the processors stays in the interrupt loop.
2) You programmed an endless loop.
3) Power supply problems.
4) The processor goes into Low Voltage Programming mode when pin B3(PIC16) or B5(PIC18) goes high.
Option 4 is not implemented in your processor, and option 1 is not in your program. That leaves 2 & 3, of which number 2 is the most likely.
I didn't look into your code as it is difficult to follow. Please use an extra level of indentation for every '{}' block. That also means you can never put two '}' characters on the same line.
A second remark on your program is that your elevator system is a classical example for code to be implemented with a State Machine. Using that design pattern your code will become easier to understand, and from easier understanding automatically a better program will result with fewer bugs.
Please search the internet for a detailed explanation on what State Machines are and how to implement them. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sat Mar 30, 2013 7:14 am |
|
|
Could be noise from your motors causing code to go off somewhere not intended.
I also don't care to analyse your code.
You could include a WDT, but that doesn't answer the question of what's happening.
I'd be adding some diagnostics, allows the PIC to tell me what it thinks it's doing.
That way you can find out if the failure is truely random, or always from the same or a similar point.
Mike |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Sat Mar 30, 2013 7:26 am |
|
|
To follow up on Mike's 'noise' issue, replace the motors with LEDs.
Quick, simple visual indicators that will NOT generate 'noise' that could crash your program.
Another source of 'noise' could be your 'floor switches'. All mechanical switches need some form of 'debounce', either hardware (R-C networks) or software (timers).
You could also reduce your code to say 2 floors, get it working, then add floors, testing each addition of code.
hth
jay |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sat Mar 30, 2013 8:05 am |
|
|
Failure of a sensor to "sense" would do it.
I would NEVER have so many while(1) statements w/o some form of escape other than the sensor.
You might consider a timer based emergency exit from each of those while(1) CONSTRUCTS using say a timer overflow?
A var set to a unique value could be used to tell which loop is failing to exit when the timer saves the day.... |
|
|
ssaakmnt
Joined: 03 Dec 2011 Posts: 27
|
|
Posted: Sat Mar 30, 2013 12:36 pm |
|
|
Thank you guys for your responses. I will consider all information. I suspect the problem from sensor because it always stop there and it sometimes escapes sensor to stop on the next one. So its probably from sensor endless loop i miss to note. |
|
|
|