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

Very simple for loop problem

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



Joined: 01 Jun 2012
Posts: 2

View user's profile Send private message

Very simple for loop problem
PostPosted: Fri Jun 01, 2012 1:18 am     Reply with quote

Hi,

I have a code that stores bits of a byte in a bit array. I will not post all the code, I think it is not necessary. But instead I am going to post working and not working versions.

WORKING *******************************************
TX_BUFFER[ 0] = BIT_TEST(TX_BYTES[0],0);
TX_BUFFER[ 1] = BIT_TEST(TX_BYTES[0],1);
TX_BUFFER[ 2] = BIT_TEST(TX_BYTES[0],2);
TX_BUFFER[ 3] = BIT_TEST(TX_BYTES[0],3);
TX_BUFFER[ 4] = BIT_TEST(TX_BYTES[0],4);
TX_BUFFER[ 5] = BIT_TEST(TX_BYTES[0],5);
TX_BUFFER[ 6] = BIT_TEST(TX_BYTES[0],6);
TX_BUFFER[ 7] = BIT_TEST(TX_BYTES[0],7);

NOT WORKING ****************************************
FOR ( CNT = 0; CNT<=7; CNT++ )
TX_BUFFER[CNT] = BIT_TEST(TX_BYTES[0],CNT);

Does any one understand why? My version is 4.068.

Regards,

dc
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Fri Jun 01, 2012 1:49 am     Reply with quote

How is TX_BUFFER declared?.
Realistically, 4.068, is 'borderline' for working as a compiler.

However, 'what fails'?. I just put a simple program around this, and tested in 4.070, the oldest V4 compiler I have kept, and both work fine.
Code:

void main(void) {
   int8 tx_buffer[8];
   int8 tx_bytes[1];
   int8 cnt;

   tx_bytes[0]=0xAA;
   TX_BUFFER[ 0] = BIT_TEST(TX_BYTES[0],0);
   TX_BUFFER[ 1] = BIT_TEST(TX_BYTES[0],1);
   TX_BUFFER[ 2] = BIT_TEST(TX_BYTES[0],2);
   TX_BUFFER[ 3] = BIT_TEST(TX_BYTES[0],3);
   TX_BUFFER[ 4] = BIT_TEST(TX_BYTES[0],4);
   TX_BUFFER[ 5] = BIT_TEST(TX_BYTES[0],5);
   TX_BUFFER[ 6] = BIT_TEST(TX_BYTES[0],6);
   TX_BUFFER[ 7] = BIT_TEST(TX_BYTES[0],7);
   delay_us(1);  //Stop here and look at tx_buffer 0,1,0,1 etc...

   tx_bytes[0]=0x55;
   FOR ( cnt= 0; cnt<=7; cnt++ )
      TX_BUFFER[cnt] = BIT_TEST(TX_BYTES[0],cnt);     
   delay_us(1); //Then look here, and it is 1,0,1,0 etc..

   do {
   } while(TRUE);
}

So, how is your version 'not working'. One thing the loop version will take a lot longer. The individual bit tests, directly feeding to fixed addresses in the RAM, code as 4 instructions each. Using the counter, the array has to be accessed, and the bits have to be located by rotation, taking a minimum of 14 instructions, rising to nearly 50 instructions for the highest bits. Average about 34 instructions. Over 8* slower. If you are using this code for some form of serial output, the timing might cause a problem.

Best Wishes
dc



Joined: 01 Jun 2012
Posts: 2

View user's profile Send private message

PostPosted: Fri Jun 01, 2012 2:13 am     Reply with quote

Hi,

TX_BUFFER is declared as bit array.

*********************
INT1 TX_BUFFER[96];
*********************

But I did not get reason for declaring 8 bit array for a bit. Actually this way will take more time in actual time critic part, since I must get a bit from a byte in order to send bits.

instead of using this

output_bit(TX_PIN,TX_BUFFER[x]);

you will have to use this right?

output_bit(TX_PIN,bit_test(TX_BUFFER[x],0));



If you try it like I did is your code works?

Regards,

dc
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Fri Jun 01, 2012 3:05 am     Reply with quote

Read the manual. Bit arrays are not legal.
That it is working at all, is 'luck'.

Best Wishes
Geps



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

PostPosted: Fri Jun 01, 2012 3:16 am     Reply with quote

Ttelmah,

You can now use bit arrays but as the OP is finding operations with them are very buggy.

I've written functions that use bit arrays, released the code, and two revisions (of my code - not the compiler) when I've added functionality to separate parts of the firmware the functions no longer work. Conversion over to int8 has solved the problem or even in some case just casting to int8.

I just refuse to use int1 for the time being - all new code is int8 upwards.
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Fri Jun 01, 2012 3:54 am     Reply with quote

Not with 4.068.

I agree that with the latest compilers these are nominally available, but the poster has 4.068. Bit arrays at that time are specifically listed as 'not supported', and basically did not work at all.

Best Wishes
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Jun 01, 2012 12:23 pm     Reply with quote

the part that makes no sense to me is:
Code:

 TX_BUFFER[ 0] = BIT_TEST(TX_BYTES[0],0);


since i have trouble understanding the usefulness of it, assuming that
TX_buffer might be used for BYTE transmission.
cuz if so, all you will ever transmit is ASCII 0 or 1.

if the result is meant for terminal display
mapping the bit test result to Bit_test() +48;

gives a much more friendly result, IMHO
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Fri Jun 01, 2012 2:46 pm     Reply with quote

I also have to point out, that if you are using bits in a byte, the entire transfer could be down by just transferring the byte contents of TX_BYTES[0], to the byte holding the bits of TX_BUFFER. Single byte move.....

Best Wishes
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Jun 12, 2012 1:54 pm     Reply with quote

revisiting illegal bit arrays.

using 4.132

I just converted a sloppy old int1 virtual array technique to:

Code:

int1  hits[128];


and yee compiler is quite happy and accurate with subsequent direct access
as in
Code:

if (hits[7]) {hits[7]=0;}
else if (0==hits[i]) {hits[i]=1;}


I had believed this was supposed to be illegal but at least for simple int1 arrays of modest dimension I can attest that it definitely works now.

The .LST file agrees.
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Wed Jun 13, 2012 1:14 am     Reply with quote

Yes, I think it started working in the low 4.100's. There are still a few problems if you put them into structures, but they are now 99% working.
But as I said, not in 4.068....

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