PDA

View Full Version : better way



trying
- 16th November 2005, 02:26
I guess I need a better way to do this because the way I tried does not work
what I need is after the 1st. event there is a 60 sec. time that if a 2nd and 3rd. event happens . led flash on 2nd. horn blow on 3rd. if 2nd and 3rd. does not happen with in 60 sec. goes back to montioring for 1 st. event.
here is what I tried.
any help fixing this or a better way
thanks

@ DEVICE pic12F629
@ DEVICE pic12F629, INTRC_OSC_NOCLKOUT
@ DEVICE pic12F629, WDT_ON
@ DEVICE pic12F629, MCLR_OFF
@ DEVICE pic12F629, CPD_OFF
@ DEVICE pic12F629, BOD_OFF
@ DEVICE pic12F629, PWRT_ON
@ DEVICE pic12F629, PROTECT_OFF
DEFINE OSC 4
Pause 20000 ' Allow pic to Stabilize
TRISIO = %00001100 'make gpio .2 .3 inputs
OPTION_REG.7 = 0 ' gpio 0 - 2 digital
WPU = 255 ' weak pull ups on all pins
CMCON = 7 ' turn weak pull ups on

SYMBOL HORN = GPIO.0 'FAULT HORN
SYMBOL CT = GPIO.2 'CT PIN 5 (TRIPS LOW)
SYMBOL LED = GPIO.5 'LED PIN 2
HORN =0
LED=0

MONTIOR:

IF CT =0 Then FAULT 'MONTIOR FOR 1ST. FAULT
GoTo MONTIOR

FAULT:
LED=1
Pause 5000 'LED FLASH WITH FAULT
LED=0

A VAR WORD
For A = 0 TO 60000 ' MONTIOR FOR 2ND.& 3RD. FAULTS FOR 1 MIN.
Pause 1

CHECK
IF CT= 0 Then SEC
GoTo CHECK
SEC: 'CT TRIP 2 MORE TIMES AFTER FIRST TRIP
LED = 1 ' IN LESS THAN 1 MIN. HORN SOUNDS FOR 5 SEC.
Pause 5000 'THEN START OVER. LED FLASH WITH FIRST 2 FAULT
LED = 0
'IF IT DOES NOT TRIP 2 IN THE NEXT MIN.
THR: 'THEN BACK TO MONTIOR
IF CT=0 Then ALARM
GoTo THR
Next A
ALARM:
HORN=1
Pause 5000
HORN=0

GoTo MONTIOR

trying
- 17th November 2005, 20:45
sorry did I make someone mad or is this not possably

milestag
- 17th November 2005, 21:18
It's definitely possible, but not quite the way you are trying it.

Take a look at your loops "CHECK" and "THR". After a 1mS pause you go right into the first one and you will wait there FOREVER if CT never happens. Same for the second one.

Your counter "For A =" is NOT incrementing while you are stuck in those nested loops. Remember that those instructions don't multitask. Only one instruction will happen at a time.

You aren't too far off, but things are not in the right sequence. Try doing a flowchart of your code first.

P.S. The PIC does not need 20 seconds to "stabilize"! Heavens to Mergatroid! Only your grandpa's TV ever needed that kind of warm-up. LOL.

milestag
- 18th November 2005, 00:17
I haven't tested it, and it can surely be done more elegantly, but this should be a little closer to what you are attempting....I did not check to see if you are initializing the registers correctly.

SYMBOL HORN = GPIO.0 'FAULT HORN
SYMBOL CT = GPIO.2 'CT PIN 5 (TRIPS LOW)
SYMBOL LED = GPIO.5 'LED PIN 2

A VAR WORD
EVENT VAR BYTE

HORN =0
LED =0
EVENT =0


MONTIOR:
IF CT = 0 Then FAULT
GoTo MONTIOR

FAULT:
LED = 1
Pause 5000
LED = 0
CHECK:
For A = 0 TO 60000
Pause 1

IF CT = 0
GOSUB FAULT_AGAIN
ENDIF

Next A

EVENT = 0
GOTO MONITOR

FAULT_AGAIN:
EVENT = EVENT + 1
IF EVENT = 2 THEN THR

SEC:
LED = 1
Pause 5000
LED = 0
RETURN

THR:
HORN = 1
Pause 5000
HORN = 0

EVENT = 0

GoTo MONTIOR

Charles Linquis
- 18th November 2005, 03:16
Although I haven't tried it, the following code should work.
It uses no pauses whatsoever.


' Set up timer1 for /8 prescaler

T1CON = %00110001

TOP:
IF PIR1.0 = 1 THEN
PIR1.0 = 0
TMR1H = $D
TMR1L = $CF
Hcounter = Hcounter + 1
IF Hcounter.0 = 0
GOSUB DOTHEWORK
ENDIF
ENDIF

If Button1 = 0 THEN SecCounter = 60
IF Button2 = 0 THEN BTN2 = 1
IF Button3 = 0 THEN BTN3 = 1

GOTO TOP


DOTHEWORK: ' Gets here exactly once per second


IF BTN2 = 1 AND SecCounter > 0 THEN
LEDFLAG = 1
ENDIF
IF BTN3 = 1 AND BTN2 = 1 AND SecCounter > 0 THEN
HORN = 1
ENDIF
IF LEDFLAG = 1 THEN
TOGGLE LED
ENDIF
IF SecCounter > 0
SecCounter = SecCounter -1
ENDIF


Return

trying
- 18th November 2005, 04:38
thanks
I will try them and let you know
as you can tell a one of them newbies and not real smart

milestag
- 18th November 2005, 13:39
Well, at least you are attempting a fairly simple program to start. The big mistake I ALWAYS make is that I write 2 pages of code before testing any of it. Or I make too many edits. Then I spend a week debugging. I know I should do one thing at a time and test each new function as it is added, but it's easy to get carried away...

Something to think about...there are still some shortcomings with the way I modified your code: The loop that looks for the second and third events should be fairly close to 60 seconds. But...when either of those events is detected, it will ADD 5 seconds to the overall loop time.

Also, you will miss any events that occur while the LED is on or the Horn is on. But that might be okay for your application. I was mainly trying to stay as close to your code as possible so you can understand the changes.

Charles is using a timer and interrupt flag, which is one of those "more elegant" solutions......IF you understand how it all works....otherwise you just might only make things harder for yourself to debug down the road.

trying
- 18th November 2005, 16:22
thanks
milestag I just had to add 1 then and change spelling on montior for it to compile and it seams to work like planned. I will insert it in to the rest of my code to see how it works.
Charles yours is a little over my head right now BUT you have givien me some thing to work on thanks for that and I may ask for a little help from you on how-to.
update later
thanks again

trying
- 18th November 2005, 19:18
ok I added it to the rest of the code and it works just like I hoped.
the added time in the loops & missed event is not a problem.
oh the 20 sec. warm up was there for some other hardware as well.
And I'm olded enought that the t.v warm up you were talking about
was mine and my wifes 1st. t.v. (black& white) lol. thanks I hope I can come back with more ?'s as I "advance" thanks again