PDA

View Full Version : Strange behaviour of my PBP code.



Fredrick
- 22nd August 2009, 20:13
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



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

Fredrick
- 22nd August 2009, 23:47
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?

Darrel Taylor
- 23rd August 2009, 00:45
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%.

Fredrick
- 23rd August 2009, 17:15
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.

Fredrick
- 23rd August 2009, 19:40
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

Darrel Taylor
- 23rd August 2009, 20:21
Exactly!

You've got it now. http://www.picbasic.co.uk/forum/images/icons/icon14.gif
<br>

Fredrick
- 24th August 2009, 11:19
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



IF Counter > 254 Then
Counter = 0
Endif

Darrel Taylor
- 24th August 2009, 20:20
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>