bug or feature ???

General discussion on mikroBasic for dsPIC30/33 and PIC24.
Post Reply
Author
Message
nervous
Posts: 80
Joined: 13 Feb 2007 08:35
Location: Italy

bug or feature ???

#1 Post by nervous » 29 Sep 2009 16:55

Code: Select all


const half = 512

dim potX        as word
dim potY        as word

dim nul         as integer



 potX = Adc_Read(0)
 potY = Adc_Read(1)

 nul = integer(potX - half)

Well, what aspected IMHO is a signed integer in nul

what I obtain is a saturated word (65535 for -1)

this "feature" obsess me since some month, but I can't reproduce it exactely.

By the way.....

Code: Select all


 nul = integer(potX)  - integer(half)
isn't the solution (same result)

I hope someone now can give to me a work around
Cheers
Nervous

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

#2 Post by anikolic » 01 Oct 2009 14:42

Hi,
For values 0<potX<512 your nul = maxvalue(nul) - half + potX. Because nul is 16-bits wide, then maxvalue(nul) = 2^16=65536.

Nothing unpredictable has happened, it's just a simple integer overflow, which caused that highest (65535) and lowest (0) numbers are actually next door neighbors. So subtracting from 0 leads you into the "saturated" word, as you said.

Best regards,
Aleksandar
Web Department Manager

nervous
Posts: 80
Joined: 13 Feb 2007 08:35
Location: Italy

#3 Post by nervous » 01 Oct 2009 21:01

"aleksandar.nikolic"]Hi,
For valus 0<potX<512 your nul = maxvalue(nul) - half + potX. Because nul is 16-bits wide, then maxvalue(nul) = 2^16=65536.
Rethink about
integer = Signed: −32,768 to +32,767
http://en.wikipedia.org/wiki/Integer_%2 ... science%29
please
Cheers
Nervous

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

#4 Post by anikolic » 02 Oct 2009 15:17

Hi,
This is just a matter of interpretation. If you look at the binary code, you can see it as classical "unsigned", or you can step back and think of it as a conventional representation of negative value. It's just the way we want to look at it. If you used mikroBasic simulator, and added values to WatchList to monitor them, then you should just click on the button that will appear if you click in Value region, and when Edit Value window opens, choose Dec representation, and click on Signed check box, and you will see the result as a signed integer.

Best regards,
Aleksandar
Web Department Manager

nervous
Posts: 80
Joined: 13 Feb 2007 08:35
Location: Italy

#5 Post by nervous » 24 Nov 2009 00:47

Hi Alexandar.

Still I've the problem on a integer between subctraction of 2 words. Still sometime (sic!) I obtain 65535 instead of -1 (what normally happen)

now, I've got also another mathematical problem

Code: Select all


sub function  Ars_Update (dim byref filtredata as Filtred, dim byref angle_m as float) as float

dim y as float

dim bypass0, bypass1, bypass2 as longint


       y  = angle_m - filtredata.Xangle
       S  = float(filtredata.P_00 + filtredata.Rangle)
       
       if S = 0 then
          LATF.7 = 1
       end if
       
       'bypass2 = longint(S*1000000)
       'bypass0 = longint(filtredata.P_00 * 1000000)
       'bypass1 = longint(filtredata.P_10 * 1000000)
       
      ' Divider0 = Divide(filtredata.P_00,S)
       'Divider1 = Divide(filtredata.P_10,S)
       'Divider0 = bypass0/bypass2
       'Divider1 = bypass1/bypass2
       
       
       'Divider1 = float(filtredata.P_10 / S)
        Divider0 = float(filtredata.P_00 / S)
        Divider1 = float(filtredata.P_10 / S)
       
       'Divider0 =  1                'filtredata.P_00 / S
       ' Divider1 =  1                'filtredata.P_10 / S


       ' K0 = filtredata.P_00 / S
       ' K1 = filtredata.P_10 / S
       
       filtredata.XAngle = filtredata.Xangle + (Divider0*y)


       
       filtredata.XBias  = filtredata.XBias  + (Divider1*y)
       
       filtredata.P_00 = filtredata.P_00 - (Divider0 * filtredata.P_00)
       filtredata.P_01 = filtredata.P_00 - (Divider0 * filtredata.P_01)
       filtredata.P_10 = filtredata.P_10 - (Divider1 * filtredata.P_00)
       filtredata.P_11 = filtredata.P_11 - (Divider1 * filtredata.P_01)
       
       result = filtredata.XAngle


end sub


in this division


Code: Select all

 Divider0 = float(filtredata.P_00 / S)

I obtain, basically a NaN.

Now... the first logical approach was.... S=0 than NaN but the other division are done rergulary.

second. The bit LATF.7 (LV24/33 dsPic 33FJ128MC710, 10 MHz crystal x8 PLL) NEVER is set to 1

third, I'm a damned bulldog with the problems. than I've add that :

Code: Select all


sub procedure MathError org $0C

    LATA.1 = 1

    if INTCON1.14 then
    
    
    end if
    
    if INTCON1.14 then

    end if

    if INTCON1.14 then

    end if
    if INTCON1.14 then

    end if
    if INTCON1.14 then

    end if
    if INTCON1.14 then

    end if
    if INTCON1.14 then

    end if
    if INTCON1.14 then

    end if
    if INTCON1.14 then

    end if
    if INTCON1.14 then

    end if
    if INTCON1.14 then

    end if
    if INTCON1.14 then

    end if


end sub



and LATA.1 NEVER go on.....

than my first impressione is

a) that isnn't a division by zero
b) isn't a processor mathematical error (the error trap don't work)
c) than or is a my bloody error (but seems to dont see a pink elephant) or will bhe a compiler error..

Thanks in advance for your kind help
Cheers
Nervous

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

#6 Post by anikolic » 25 Nov 2009 13:01

Hi,
Can you please open the support ticket at http://www.mikroe.com/en/support/ and attach your entire project folder zipped, so I can take a look. This way I cannot be certain what's happening here, because your function uses custom types, and other trap vector related settings cannot be seen from your example code here.

Thank you.
Aleksandar
Web Department Manager

Post Reply

Return to “mikroBasic for dsPIC30/33 and PIC24 General”