Strange behaviour of my PBP code.


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2008
    Location
    Sweden
    Posts
    187

    Default Strange behaviour of my PBP code.

    I have a strange "bug" in my PBP program that I canīt understand.

    I have taken out the code that contains the bug from my bigger program and run it on two different PICīs on two different devboards and the bug is still there so it must have something with the software to do.

    The code lets you to select number of shooters (1-5), if Shooters = 2 the Status_LED will blink 2 times and then delay for a longer time before the next two blink.

    You change the Shooters value by pressing SW_Forward or SW_Reverse, SW_Launch will start the rest of the program (not included) with the number of shooters stored in the variable Shooters.

    The code works pretty well until yesterday when I tried to press the SW_Forward button for about 10 - 15 times as fast that I could then the program hangs and the Status_LED is constantly on, it looks like the program goes into a never ending loop and I must Reset the PIC to continue. the same thing will happen if I press SW_Reverse as fast I can.

    Well the program works, but I want to know why the program hangs when I press the SW_Forward or SW_Reverse buttons several times fast

    Code:
    <html>
    <head></head>
    <body><!--StartFragment--><pre><code><font color="#008000"><b>Define </b></font>OSC <font color="#000080"><b>8
    </b></font><font color="#008000"><b>Include </b></font><font color="#FF0000">&quot;alldigital.pbp&quot; </font><font color="#000080"><i>;DT&acute;s All Digital
    </i></font><font color="#008000"><b>Define  </b></font>SHOWDIGITAL <font color="#000080"><b>1      
    
    </b></font><font color="#008000"><b>Asm </b></font><font color="#000080"><i>;===========================================================================
           </i></font><font color="#008000">__CONFIG    _CONFIG1L, _PLLDIV_5_1L &amp; _CPUDIV_OSC1_PLL2_1L &amp; _USBDIV_2_1L
           __CONFIG    _CONFIG1H, _FOSC_HS_1H
           __CONFIG    _CONFIG2H, _WDT_OFF_2H &amp; _WDTPS_512_2H
           __CONFIG    _CONFIG3H, _PBADEN_OFF_3H
           __CONFIG    _CONFIG4L, _LVP_OFF_4L &amp; _XINST_OFF_4L &amp; 0DFh
    <b>Endasm</b></font><font color="#000080"><i>;-------------------------------------------------------------------------
    
    </i></font>Shooters  <font color="#008000"><b>Var Byte
    </b></font>i         <font color="#008000"><b>Var Byte
    </b></font>Delayi    <font color="#008000"><b>Var Byte
    
    </b></font>SW_Launch    <font color="#008000"><b>Var </b></font>PORTA.<font color="#000080"><b>0</b><i>;=======================================================
    </i></font>SW_Reverse   <font color="#008000"><b>Var </b></font>PORTA.<font color="#000080"><b>1
    </b></font>SW_Forward   <font color="#008000"><b>Var </b></font>PORTA.<font color="#000080"><b>2
    </b></font>Status_LED   <font color="#008000"><b>Var </b></font>PORTC.<font color="#000080"><b>0</b><i>;-------------------------------------------------------
    
    </i></font>Shooters = <font color="#000080"><b>2 </b><i>;Default value
    
    </i></font>Main:<font color="#000080"><i>;==========================================================================
    </i></font><font color="#008000"><b>Gosub </b></font>Delay2
    
    <font color="#008000"><b>For </b></font>i = <font color="#000080"><b>1 </b></font><font color="#008000"><b>To </b></font>Shooters          
        <font color="#008000"><b>High </b></font>Status_LED
        <font color="#008000"><b>Gosub </b></font>Delay1
        <font color="#008000"><b>Low </b></font>Status_LED
        <font color="#008000"><b>Gosub </b></font>Delay1   
    <font color="#008000"><b>Next </b></font>i
    <font color="#008000"><b>Goto </b></font>Main<font color="#000080"><i>;----------------------------------------------------------------------
    
    </i></font>Delay1:<font color="#000080"><i>;========================================================================
    </i></font><font color="#008000"><b>For </b></font>Delayi = <font color="#000080"><b>0 </b></font><font color="#008000"><b>To </b></font><font color="#000080"><b>100
      </b></font><font color="#008000"><b>Pause </b></font><font color="#000080"><b>2
      </b></font><font color="#008000"><b>Gosub </b></font>Check_Buttons
      <font color="#008000"><b>Next </b></font>Delayi
    <font color="#008000"><b>Return
    
    </b></font>Delay2:
      <font color="#008000"><b>For </b></font>Delayi = <font color="#000080"><b>0 </b></font><font color="#008000"><b>To </b></font><font color="#000080"><b>255
      </b></font><font color="#008000"><b>Pause </b></font><font color="#000080"><b>5
      </b></font><font color="#008000"><b>Gosub </b></font>Check_Buttons
      <font color="#008000"><b>Next </b></font>Delayi 
    <font color="#008000"><b>Return</b></font><font color="#000080"><i>;-------------------------------------------------------------------------
    
    </i></font>Check_Buttons:<font color="#000080"><i>;=================================================================
            </i></font><font color="#008000"><b>If </b></font>SW_Launch = <font color="#000080"><b>0 </b></font><font color="#008000"><b>Then              
                High </b></font>Status_LED             
                <font color="#008000"><b>Pause </b></font><font color="#000080"><b>50  
                </b></font><font color="#008000"><b>While </b></font>SW_Launch = <font color="#000080"><b>0</b></font>: <font color="#008000"><b>Wend
                Pause </b></font><font color="#000080"><b>50
    </b><i>'            goto start_new 
            </i></font><font color="#008000"><b>Endif
    
            If </b></font>SW_Forward = <font color="#000080"><b>0 </b></font><font color="#008000"><b>Then         
                High </b></font>Status_LED
                <font color="#008000"><b>Pause </b></font><font color="#000080"><b>50
                </b></font>Shooters = Shooters + <font color="#000080"><b>1
                </b></font><font color="#008000"><b>If </b></font>Shooters &gt; <font color="#000080"><b>5 </b></font><font color="#008000"><b>Then </b></font>Shooters = <font color="#000080"><b>1 
                </b></font><font color="#008000"><b>While </b></font>SW_Forward = <font color="#000080"><b>0</b></font>:<font color="#008000"><b>Wend
                Pause </b></font><font color="#000080"><b>50
                </b></font><font color="#008000"><b>Low </b></font>Status_LED
                <font color="#008000"><b>Goto </b></font>Main
            <font color="#008000"><b>Endif
         
            If </b></font>SW_Reverse = <font color="#000080"><b>0 </b></font><font color="#008000"><b>Then
                High </b></font>Status_LED
                <font color="#008000"><b>Pause </b></font><font color="#000080"><b>50
                </b></font>Shooters = Shooters - <font color="#000080"><b>1
                </b></font><font color="#008000"><b>If </b></font>Shooters &lt; <font color="#000080"><b>1 </b></font><font color="#008000"><b>Then </b></font>Shooters = <font color="#000080"><b>5
                </b></font><font color="#008000"><b>While </b></font>SW_Reverse = <font color="#000080"><b>0</b></font>:<font color="#008000"><b>Wend
                Pause </b></font><font color="#000080"><b>50
                </b></font><font color="#008000"><b>Low </b></font>Status_LED
                
                <font color="#008000"><b>Goto </b></font>Main
            <font color="#008000"><b>Endif 
    Return</b></font><font color="#000080"><i>;-------------------------------------------------------------------------
    
    
    
    </i></font></code></pre><!--EndFragment--></body>
    </html>

  2. #2
    Join Date
    Jan 2008
    Location
    Sweden
    Posts
    187


    Did you find this post helpful? Yes | No

    Default

    Ok.. now I have tested with another PIC,a PIC16F887 and then it works, the "bug" is gone.
    So why does it hang with a PIC18F2550, PIC184550, and 18F4520?

    Does it have something with the config fuses to do? what have I missed?

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi Fredrick,

    In the Check_Buttons subroutine, there's 2 Goto Main statements.

    You should use RETURN to exit a subroutine.

    Using GOTO will cause the stack to overflow.
    With 18F's a stack overflow can cause a Reset.

    But with 16F's the stack just wraps around and seems to work, although it's probably not working 100%.
    DT

  4. #4
    Join Date
    Jan 2008
    Location
    Sweden
    Posts
    187


    Did you find this post helpful? Yes | No

    Default

    Ohh so simple!

    When I notified the problem fist time I was saying to myself that this time I will try to solve the problem without help from anybody, so I have spent a lot off hours on trying to find the problem, but then I give up and started a thread here again.

    Thank you Darrel.

  5. #5
    Join Date
    Jan 2008
    Location
    Sweden
    Posts
    187


    Did you find this post helpful? Yes | No

    Default

    But itīs OK to use Gotoīs within the subroutine as long as I call a label that is in the subroutine and only leave the subroutine by doing a Return
    Last edited by Fredrick; - 23rd August 2009 at 19:43.

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Exactly!

    You've got it now.
    <br>
    DT

  7. #7
    Join Date
    Jan 2008
    Location
    Sweden
    Posts
    187


    Did you find this post helpful? Yes | No

    Default

    How is it with variables?
    If I overflow a byte sized counter it will turn to 0 after passing 255, but is it OK to let a variable overflow? or should I take care of the overflow like this

    Code:
    IF Counter > 254 Then 
    Counter = 0
    Endif
    Last edited by Fredrick; - 24th August 2009 at 12:15.

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    For BYTE sized variables, when the value goes over 255 the upper bits are lost, which makes it automatically wrap around to 0.
    You don't need to do it manually.

    For words, it wraps when over 65535.
    <br>
    DT

Similar Threads

  1. PBP Extensions, What are they?
    By PJALM in forum PBP Extensions
    Replies: 9
    Last Post: - 28th September 2021, 11:26
  2. PBP code to read R/C signal from receiver
    By malc-c in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 28th October 2009, 21:51
  3. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  4. 4 Chanel Dmx512 ready assembly code to PBP ?
    By syscoder in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 21st March 2007, 23:55
  5. 16f819 - acting strange... only does first line of code
    By 4Lewis in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 10th May 2006, 22:53

Members who have read this thread : 0

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