PDA

View Full Version : Why would this simple code hang after 10 cycles:



Gerald
- 25th March 2023, 07:07
Good Morning

I am having a problem with the process hanging after 10 record cycles. Toggling the CYCLE switch does not get it running again. It hangs in the "not recording" mode every time.

So I whittled the program down to bare bones and played with the 20000 number. Replacing that with 3000, 6000, 10000 or 20000 very repeatedly makes it hang after 10 record cycles. Any ideas? Code follows:

' Name : SHORT CYCLE.pbp
' Compiler : PICBASIC PRO Compiler 3.1.6.2
' Assembler : PM or MPASM
' Target PIC : 12F types
' Hardware : PIC12F683
' Oscillator : internal



#CONFIG
__config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
#ENDCONFIG


CLEAR
DEFINE OSC 4
ANSEL = 0
CMCON0 = 7


RECPB var GPIO.4 ' Assign name "RECPB" to GPIO 4 OUTPUT
CYCLE var GPIO.0 ' Assign name "CYCLE" to GPIO 0 INPUT


j var word ' timing loop count for 3sec record timing
k var word ' timing loop count for 57sec wait timing


Mainloop:


if CYCLE = 0 then goto Mainloop ' do not do any recording
if cycle = 1 then gosub recording ' do the 3/57 record cycle

Recording:
low RECPB ' Low Rec PB to start recording
Pause 500 ' start recording for 3 seconds
high RECPB ' High Rec PB after recording start
for j = 1 to 3000 ' Loop count for rec timing
pause 1
next j
low RECPB ' Low Rec PB to stop recording
Pause 500 ' Stop recording after 3 seconds
high RECPB ' High Rec PB after recording Stop
for k = 1 to 20000 ' **Pause for 57 seconds and repeat cycle.
pause 1 '
next k
return

goto mainloop

End

HenrikOlsson
- 25th March 2023, 07:37
When execution RETURNs from Recording subroutine it will fall into the Recording subroutine by itself which then ends with a RETURN - not good.

Move your Goto Mainloop to after the two IF statements.

EDIT: But I now see you're already got that answered in the other thread...

Gerald
- 25th March 2023, 07:54
When execution RETURNs from Recording subroutine it will fall into the Recording subroutine by itself which then ends with a RETURN - not good.

Move your Goto Mainloop to after the two IF statements.

EDIT: But I now see you're already got that answered in the other thread...

Thanks, you make it clearer than the other reply (the words goto Mainloop appears twice, I was moving the wrong one)

Gerald
- 25th March 2023, 08:01
I tried this, but now I get no result at all:

j var word ' timing loop count for 3sec record timing
k var word ' timing loop count for 57sec wait timing


Mainloop:


if CYCLE = 0 then goto Mainloop ' do not do any recording
if cycle = 1 then gosub recording ' do the 3/57 record cycle


goto mainloop


Recording:
low RECPB ' Low Rec PB to start recording
Pause 500 ' start recording for 3 seconds
high RECPB ' High Rec PB after recording start
for j = 1 to 3000 ' Loop count for rec timing
pause 1
next j
low RECPB ' Low Rec PB to stop recording
Pause 500 ' Stop recording after 3 seconds
high RECPB ' High Rec PB after recording Stop
for k = 1 to 10000 ' **Pause for 57 seconds and repeat cycle.
pause 1 '
next k
return

End

HenrikOlsson
- 25th March 2023, 09:05
Not sure to be honest...
How is the switch wired? Pin to GND with pullup or pin to Vdd with pulldown?

Give this a try:


DEFINE OSC 4

RECPB var GPIO.4 ' Assign name "RECPB" to GPIO 4 OUTPUT
CYCLE var GPIO.0 ' Assign name "CYCLE" to GPIO 0 INPUT

TRISIO.4 = 0 ' GPIO.4 as output
ANSEL = 0
CMCON0 = 7

OPTION_REG.7 = 0 ' Enable individual pullups
WPU.0 = 1 ' Enable weak pullup on GPIO.0

Mainloop:

IF Cycle = 1 THEN ' do the 3/57 record cycle
RECPB = 0 ' Low Rec PB to start recording
Pause 500 ' start recording for 3 seconds
RECPB = 1 ' High Rec PB after recording start
PAUSE 3000
RECPB = 0 ' Low Rec PB to stop recording
Pause 500 ' Stop recording after 3 seconds
RECPB = 1 ' High Rec PB after recording Stop
PAUSE 10000
ENDIF

GOTO Mainloop

End

Gerald
- 25th March 2023, 10:22
Thanks very much Henrik. That works . . . . too good! I can't switch off the cycle, it just keeps running.
The switch pulls pin to ground. (In real life I use this in my car where I have a relay that closes when the engine runs)

HenrikOlsson
- 25th March 2023, 10:46
Are you 100% sure you have the switch on the correct pin? Have you measured the voltage on the pin with the switch in both positions? 0V in one position, 5V (or whatever the supply voltage is) in the other.

Gerald
- 25th March 2023, 11:02
I am sure the pin is wired correct and I can measure it going from 0 to 4.8V . Let me look some more . . . . . . .

Gerald
- 25th March 2023, 11:06
' Name : Henrik.pbp
' Compiler : PICBASIC PRO Compiler 3.1.6.2
' Assembler : PM or MPASM
' Target PIC : 12F types
' Hardware : Non specific
' Oscillator : internal



#CONFIG
__config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
#ENDCONFIG


DEFINE OSC 4


RECPB var GPIO.4 ' Assign name "RECPB" to GPIO 4 OUTPUT
CYCLE var GPIO.0 ' Assign name "CYCLE" to GPIO 0 INPUT


TRISIO.4 = 0 ' GPIO.4 as output
ANSEL = 0
CMCON0 = 7


OPTION_REG.7 = 0 ' Enable individual pullups
WPU.0 = 1 ' Enable weak pullup on GPIO.0


Mainloop:


IF Cycle = 1 THEN ' do the 3/57 record cycle
RECPB = 0 ' Low Rec PB to start recording
Pause 500 ' start recording for 3 seconds
RECPB = 1 ' High Rec PB after recording start
PAUSE 3000
RECPB = 0 ' Low Rec PB to stop recording
Pause 500 ' Stop recording after 3 seconds
RECPB = 1 ' High Rec PB after recording Stop
PAUSE 10000
ENDIF


GOTO Mainloop


End

Gerald
- 25th March 2023, 14:20
Changed the WPU command and in the Mainloop said IF Cycle = 0 instead of 1 (grounding pin7 / GP0) must start the cycle. Ready to go to the next challenge . . . . . .

#CONFIG
__config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
#ENDCONFIG


DEFINE OSC 4


RECPB var GPIO.4 ' Assign name "RECPB" to GPIO 4 OUTPUT
CYCLE var GPIO.0 ' Assign name "CYCLE" to GPIO 0 INPUT


TRISIO.4 = 0 ' GPIO.4 as output
ANSEL = 0
CMCON0 = 7
OPTION_REG.7 = 0 ' Enable individual pullups
WPU=0101 ' Enable weak pullups on GPIO.0 & GPIO.2


Mainloop:


IF Cycle = 0 THEN ' do the 3/57 record cycle
RECPB = 0 ' Low Rec PB to start recording
Pause 500 ' start recording for 3 seconds
RECPB = 1 ' High Rec PB after recording start
PAUSE 3000
RECPB = 0 ' Low Rec PB to stop recording
Pause 500 ' Stop recording after 3 seconds
RECPB = 1 ' High Rec PB after recording Stop
PAUSE 10000
ENDIF


GOTO Mainloop


End