Problem With Min

gtslabs

Member
I am having a problem with the MIN function. When I set it to 2 and have a large stepsize on my dec routine below it rolls over to 65xxx.

Is this a bug or am i not using it correctly. This is happening in the sim and on the chip.

The way it fails is set input 2 until it maxes out at 5000 then release and set input 3 and let it run down. It happens only with a large stepsize.

<code><pre><font size=2 face='Courier'>
Symbol Frequency = W0
Symbol Stepsize = W1


Stepsize = 1
Frequency = 2

Main_Freq:
If pin2 = 1 then incfreq
if pin3 = 1 then decfreq
b4 = 0
goto Main_Freq

Incfreq:
gosub counts
pause 250
Frequency = Frequency+Stepsize Max 5000
goto Main_Freq

Decfreq:
gosub counts
pause 250
Frequency = Frequency-Stepsize Min 2
goto Main_Freq



counts:
b4 = b4 + 1
if B4&gt;250 then resize

select case B4
case 1
Stepsize=1
case 11
Stepsize=10
case 20
Stepsize=100
case 30
Stepsize=1000
endselect
goto notresized

Resize:
B4=250
notresized:
return

</font></pre></code>
 

hippy

Technical Support
Staff member
It's not so much a problem with MIN but with 16-bit overflow generally. The PICAXE only deals in positive numbers so 1-2 is -1 ( $FFFF ) but that is 65535 as far as the PICAXE is concerned, so it's greater than 2.

Avoiding overflow and underflow can get a bit tricky, especially when dealing with large numbers to increment or decrement by.
 
Top