CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PIC24 Bit array equality testing

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
john.ma



Joined: 19 Nov 2012
Posts: 23

View user's profile Send private message

PIC24 Bit array equality testing
PostPosted: Thu Nov 29, 2012 11:47 am     Reply with quote

Hi Guys,

Could someone confirm whether they're also getting the same output through MPSIM when using the below code? Testing for same bits returns false if referenced as array element :/
I tested this with version PCD 4.138

Code:

#include <24fj128GA010.h>

#fuses NODEBUG      // Debug Mode. 1.11   NODEBUG   No Debug mode for ICD
#fuses HS,PR_PLL    // Crystal HS, 4xPLL
#fuses NOIESO       // No 2-speed Start Up
#fuses NOJTAG       // No JTAG
#fuses NOPROTECT    // No Memory Protection
#fuses NOWDT        // No WatchDog Timer

#use delay(clock=32M, crystal=8M)

int outIndex, outDataSize=16, x, duration;
int1 outputData[] = { 1,0,0,1,1,0,1,0,1,0,1,0,1,0,0,0};
int1 temp3= 0, temp2=0, temp;

void main()
{   
   duration = 0;
   outIndex = 0;
   
   temp3 = outputData[outIndex];
   temp2 = outputData[duration];
   
   if(temp2 == outputData[outIndex])
      x = 19; // doesnt work
      
   if(outputData[outIndex] == outputData[duration])
      x = 20;  // doesnt work
      
   if(temp2 == temp3)
      x = 21;  // works
            
   if(temp3 == outputData[duration])
      x = 22;  // doesnt work
   
   if(!(outputData[duration] ^ temp3))
      x = 22;  // works

   // Does not compile. Error 84 Pointers to bits are not permitted
   // Reodering terms resolves compiler error (?!)   
   //if(!(temp3 ^ outputData[duration))
   //   x = 22;
   
   // Doesnt work.
   for( duration=outIndex; temp3 == outputData[duration] ; duration++);
   duration = (duration-outIndex);
   
   // Works. Terms must be in that order otherwise compiler will raise error
   for( duration=outIndex; !(outputData[duration] ^ temp3) ; duration++);
   duration = (duration-outIndex);
}


Thanks Smile
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Nov 30, 2012 4:08 am     Reply with quote

I don't have the PIC24 compiler, but from the release notes for 4.139 I noted this:
Quote:
4.139 Some packed structure problems have been fixed
You might give the new version a try...
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Sun Dec 02, 2012 2:27 am     Reply with quote

This seems to relate to a long standing problem with PCD, and 'types'.

If you code, with 4.137:
Code:

   if(outputData[outIndex]==temp2)
      x = 19;

//Versus

   if(temp2 == outputData[outIndex])
      x = 19; // doesnt work

The former correctly references the bit field, calling a subroutine to solve the value of the bit, while the latter does not.

If then you try:
Code:

   if((int8)temp2==(int8)outputData[outIndex])
      x = 19;

This again forces the subroutine call, and works.
Same applies to the noted error about bitfields....
Code:

   if(!((int8)temp3 ^ (int8)outputData[duration]))
      x = 22;


So:
Code:

   temp3 = outputData[outIndex];
   //Works because though 'temp3' is an int1, in the expression on the right
   //the compiler 'sees' the array as int8, and performs the access.
   if(temp2==outputData[outIndex])
   //Fails because the compiler tries to use 'int1' as the basic type, and this
   //doesn't access the bit array correctly.....
   if(temp2==(int8)outputData[outIndex])
   //works again.....
   //as does:
   if(!(temp3 ^ (int8)outputData[duration]))
      x = 22;


It appears you can make it always work, by preceding every point where you access a bit array, with an int8 cast.

It appears that in getting bit arrays to work, CCS have 'missed' a little bit in the type casting, with the code needed to correctly access the bits, only being called if the expression is using int8 or larger types, while the bit field returned is an int1. Hence if everything in the expression is int1, the access code fails....

The code still does the same, not using the bitfield access, unless you force use of int8, with 4.140.

Best Wishes
john.ma



Joined: 19 Nov 2012
Posts: 23

View user's profile Send private message

PostPosted: Fri Dec 07, 2012 3:59 am     Reply with quote

Ttelmah,

If I could buy you a pint I would. Second time you've helped me :D
Much appreciated.
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Fri Dec 07, 2012 9:37 am     Reply with quote

Glad it worked.

If no-one else has done so, I'll post a bug report to CCS. They might then fix this.

Best Wishes
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Mon Dec 10, 2012 3:21 pm     Reply with quote

FYI, CCS, have said this will be fixed in the next release.
Watch this space. Smile

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group