avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Why does this code not work?


From: Ian Caddy
Subject: Re: [avr-gcc-list] Why does this code not work?
Date: Thu, 02 Dec 2004 11:16:07 +0800
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.5) Gecko/20041103 Thunderbird/0.9 Mnenhy/0.6.0.104

Hi,

I would say your problem revolves around the fact that you are using unsigned numbers. In this case, they are not valid below 0 and so it only needs to check if the numbers are the same == 0 to fail the test.

The simple way to solve the issue, would be to either make your numbers in the structure be signed, or change the first test to something like:


  if (Sonar.LeftFront > Sonar.LeftBack )

I hope this helps.

Ian Caddy


wbounce wrote:
avr-gcc (GCC) 3.4.1
With no optimization

typedef struct ranges_t
{
uint16_t Front; uint16_t Back; uint16_t LeftFront; uint16_t LeftBack; uint16_t RightFront; uint16_t RightBack; } RANGES;

RANGES Sonar;
.....
if (Sonar.LeftFront - Sonar.LeftBack > 0 ) {
 if (Sonar.LeftFront - Sonar.LeftBack > 100)
  {
  BearLeft();
  }
 else
  {
  Forward();
  }
}       
else
{
 if (Sonar.LeftBack - Sonar.LeftFront > 100)
  {
  BearRight();
  }
 else
  {
  Forward();
  }
}

In my simulation Sonar.LeftBack = 1010 and Sonar.LeftFront = 900. The
1st if should fail but it does not. When I look at the disassembly it
looks like this
140:                            if (Sonar.LeftFront - Sonar.LeftBack > 0
) +00000D89: 912001DB LDS R18,0x01DB Load direct from data
space
+00000D8B:   913001DC    LDS     R19,0x01DC       Load direct from data
space
+00000D8D:   918001DD    LDS     R24,0x01DD       Load direct from data
space
+00000D8F:   919001DE    LDS     R25,0x01DE       Load direct from data
space
+00000D91:   1728        CP      R18,R24          Compare
+00000D92:   0739        CPC     R19,R25          Compare with carry
+00000D93:   F099        BREQ    PC+0x14          Branch if equal
---- 142: if (Sonar.LeftFront -
Sonar.LeftBack > 100)
+00000D94:   918001DB    LDS     R24,0x01DB       Load direct from data
space
---- No Source +00000D96: 919001DC LDS R25,0x01DC Load direct from data
space
+00000D98:   912001DD    LDS     R18,0x01DD       Load direct from data
space
+00000D9A:   913001DE    LDS     R19,0x01DE       Load direct from data
space
+00000D9C:   1B82        SUB     R24,R18          Subtract without carry
+00000D9D:   0B93        SBC     R25,R19          Subtract with carry
+00000D9E:   3685        CPI     R24,0x65         Compare with immediate
+00000D9F:   0591        CPC     R25,R1           Compare with carry
+00000DA0:   F018        BRCS    PC+0x04          Branch if carry set

It is doing a compare to 0 in the 1st if instead of a subtraction and
then a compare as it does in the 2nd if. What am I doing wrong?



_______________________________________________
avr-gcc-list mailing list
address@hidden
http://www.avr1.org/mailman/listinfo/avr-gcc-list



reply via email to

[Prev in Thread] Current Thread [Next in Thread]