Why would this simple code hang after 10 cycles:


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2023
    Location
    Cape Town, South Africa
    Posts
    26

    Default Why would this simple code hang after 10 cycles:

    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

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: Why would this simple code hang after 10 cycles:

    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...
    Last edited by HenrikOlsson; - 25th March 2023 at 07:41.

  3. #3
    Join Date
    Mar 2023
    Location
    Cape Town, South Africa
    Posts
    26


    Did you find this post helpful? Yes | No

    Default Re: Why would this simple code hang after 10 cycles:

    Quote Originally Posted by HenrikOlsson View Post
    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)

  4. #4
    Join Date
    Mar 2023
    Location
    Cape Town, South Africa
    Posts
    26


    Did you find this post helpful? Yes | No

    Default Re: Why would this simple code hang after 10 cycles:

    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

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: Why would this simple code hang after 10 cycles:

    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:
    Code:
    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

  6. #6
    Join Date
    Mar 2023
    Location
    Cape Town, South Africa
    Posts
    26


    Did you find this post helpful? Yes | No

    Default Re: Why would this simple code hang after 10 cycles:

    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)

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Why would this simple code hang after 10 cycles:

    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.

  8. #8
    Join Date
    Mar 2023
    Location
    Cape Town, South Africa
    Posts
    26


    Did you find this post helpful? Yes | No

    Default Re: Why would this simple code hang after 10 cycles:

    I am sure the pin is wired correct and I can measure it going from 0 to 4.8V . Let me look some more . . . . . . .

  9. #9
    Join Date
    Mar 2023
    Location
    Cape Town, South Africa
    Posts
    26


    Did you find this post helpful? Yes | No

    Default Re: Why would this simple code hang after 10 cycles:

    ' 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

  10. #10
    Join Date
    Mar 2023
    Location
    Cape Town, South Africa
    Posts
    26


    Did you find this post helpful? Yes | No

    Default Re: Why would this simple code hang after 10 cycles:

    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

Similar Threads

  1. 18LF4680 HSERIN and SEROUT hang my PIC!
    By temp19 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 19th May 2011, 02:02
  2. what's wrong 16F877A simple code?
    By Macgman2000 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 30th October 2009, 01:11
  3. Replies: 1
    Last Post: - 15th December 2008, 05:34
  4. Variant of Serin2/Timeout Hang Problem
    By Jimbo in forum Serial
    Replies: 3
    Last Post: - 2nd October 2007, 19:33
  5. hopefully simple code
    By hoyles in forum General
    Replies: 3
    Last Post: - 8th September 2005, 23:24

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts