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

how to convert string of int1 to one int16?

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



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

how to convert string of int1 to one int16?
PostPosted: Fri Feb 07, 2014 5:31 pm     Reply with quote

Hy, I'm back with another question.
how to transform from int1 to int16 numbers.
int16 C=0;
int8 B=0;
int1 REC[16]=1111000011001100;

B=0; \\used only for start conversion
for (B=0;B<=15;B++){
C=C+REC; \\on this row I receive an error [b]pointers to bits are not permited
C=C<<1;
}


I want to transform that binary numbers in int16 as 61 644.
atoo



Joined: 31 Jan 2014
Posts: 32

View user's profile Send private message Send e-mail

attoo
PostPosted: Fri Feb 07, 2014 7:56 pm     Reply with quote

int1 is a bit
int8 is byte 1


example:
int8 x=8
int1 y;

y = x, can not because int1 has less capacity,

I do not know if it's really what you want to explain better Question Question
should note always has something wrong Exclamation
Charles francês joão pessoa paraiba
newguy



Joined: 24 Jun 2004
Posts: 1909

View user's profile Send private message

PostPosted: Fri Feb 07, 2014 9:48 pm     Reply with quote

Not sure where REC gets set but for instance if you're reading the state of a pin and want to store its state:

Code:

for (B=0; B<=15; B++) {
   if (REC) {
      C = C | 0x0001;
   }
   C = C << 1;
}
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Sat Feb 08, 2014 12:33 am     Reply with quote

I think his question is how to convert the array of 16 bits (REC[16]) to a 16 bit integer with those bit values. Is that correct or am I missing something?

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Sat Feb 08, 2014 1:20 am     Reply with quote

gpsmikey
Yes my answer is how to convert array of 16 bits in 16 integer.

atoo
I know that I can't "add apples and pears" :P.

newguy
this code is generated from optical connection between 2 PIC on high voltage (20KV).
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Sat Feb 08, 2014 1:31 am     Reply with quote

Well 2 ways

As newguy showed:
Code:

for (B=0; B<=15; B++) {
   if (REC[B]) {
      C = C | 0x0001;
   }
   C = C << 1;
}


or by using a struct and union

Regards
Ttelmah



Joined: 11 Mar 2010
Posts: 19544

View user's profile Send private message

PostPosted: Sat Feb 08, 2014 1:41 am     Reply with quote

But of course the key problem was that the declaration of REC wouldn't work....
To initialise a 16 element array you need:
Code:

int1 REC[16]={1,1,1,1,0,0,0,0,1,1,0,0,1,1,0,0};

Why not make it really simple by using a union?.
Code:

union
{
    int1 REC[16];
    int16 C;
} space={1,1,1,1,0,0,0,0,1,1,0,0,1,1,0,0};

    //Then space.rec[0] .. space.rec[15] are the bits, and space.C=61644

Saves a lot of work.....

Best Wishes
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Sat Feb 08, 2014 2:43 am     Reply with quote

alan
now I undestand what write Mr. newguy.

Ttelmah
I think your example is much faster and good for me.
I must undestand completly how is real working.

REC[16] must be variable outside the union.
this variable is changing every time aprox 10-20ms, and oscilator is at 4MHz.

on this code I receive something else.
Code:
union
{
    int1 REC[16];
    int16 C;
} space={1,1,1,1,0,0,0,0,1,1,0,0,1,1,0,0};


result is space.C=0

and for this
...
} space={1,1,1,1,0,0,0,0,1,1,0,0,1,1,0,1};
result is space.C=1

and for this
...
} space={1,1,1,1,0,0,0,0,1,1,0,0,1,1,1,0};
result is space.C=0

best regards.
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Sat Feb 08, 2014 4:50 pm     Reply with quote

Ttelmah
I found where was the problem...
best regards.
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Mon Feb 10, 2014 11:01 am     Reply with quote

Ttelmah wrote:

Why not make it really simple by using a union?.
Code:

union
{
    int1 REC[16];
    int16 C;
} space={1,1,1,1,0,0,0,0,1,1,0,0,1,1,0,0};

    //Then space.rec[0] .. space.rec[15] are the bits, and space.C=61644

Saves a lot of work.....



That's exactly what I was going to suggest.
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Sat Feb 15, 2014 3:04 am     Reply with quote

Dear frends.
I understand (I think) how to use this function UNION, but something escapes me.

Code:
static unsigned int32 IR_SIR;
void IR_PROG()
{
if (IR_A==4){
   if((IR_TIMP_REC[IR_B]<ZERO_MAX)&&(IR_TIMP_REC[IR_B]>ZERO_MIN)){
      IR_SIR.IR_REC[IR_B]=0;
      }
   if((IR_TIMP_REC[IR_B]<UNU_MAX)&&(IR_TIMP_REC[IR_B]>UNU_MIN)){
      IR_SIR.IR_REC[IR_B]=1;
      }
   IR_B++;
   if(IR_B==4){
      union {
//         int32 IR_REC_RESET;
         int1 IR_REC[32];
         }IR_SIR;
      IR_DATA=IR_SIR;
//      IR_DATA=IR_X;
      printf(LCD_SEND_C,"=%lu&",IR_DATA);
      IR_A=0;
      IR_B=0;

      }
   }
}


on this row:
IR_SIR.IR_REC[IR_B]=0;
IR_SIR.IR_REC[IR_B]=1;
my compiler request structure union.
Where is wrong?
Ttelmah



Joined: 11 Mar 2010
Posts: 19544

View user's profile Send private message

PostPosted: Sat Feb 15, 2014 5:00 am     Reply with quote

You are not declaring IR_SIR as a union, but as an int32.....
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Sat Feb 15, 2014 6:04 am     Reply with quote

Ttelmah wrote:
You are not declaring IR_SIR as a union, but as an int32.....


yes Ttelmah I make search on GOOGLE and I found solution:

Code:
union IR_SIR_X{
   int1 IR_REC[32];
   };

union IR_SIR_X IR_SIR;

void IR_PROG()
{
if (IR_A==32){
   if((IR_TIMP_REC[IR_B]<ZERO_MAX)&&(IR_TIMP_REC[IR_B]>ZERO_MIN)){
      IR_SIR.IR_REC[IR_B]=0;
      }
   if((IR_TIMP_REC[IR_B]<UNU_MAX)&&(IR_TIMP_REC[IR_B]>UNU_MIN)){
      IR_SIR.IR_REC[IR_B]=1;
      }
   IR_B++;
   if(IR_B==32){

      IR_DATA=IR_SIR;
//      IR_DATA=IR_X;
      printf(LCD_SEND_C,"=%lu&",IR_DATA);
      IR_A=0;
      IR_B=0;

      }
   }
}


Thank you Ttelmah.
ckielstra



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

View user's profile Send private message

PostPosted: Sat Feb 15, 2014 9:53 am     Reply with quote

You are still using the union in a wrong way and your code is only working because the CCS compiler is not checking the data types when you do:
Code:
IR_DATA=IR_SIR;
Many other compilers would tell you this line is wrong and I'm very surprised that it works.

Correct use of the union instruction requires you to have multiple different data types inside the union. That is what the word union means: 'an act of joining two or more things together' Merriam-Webster dictionary

The union should be like:
Code:
union IR_SIR_X{
   int1 IR_REC[32];
   int32 IR_SIR;
   };
Both variables are now located at the same memory address. Whatever data you now write into IR_REC[x] will also appear in IR_SIR. This means that the line 'IR_DATA=IR_SIR' can be deleted.
Code:
union IR_SIR_X{
   int1 IR_REC[32];
   int32 IR_DATA;       <<-- Added this line
   };

union IR_SIR_X IR_SIR;

void IR_PROG()
{
if (IR_A==32){
   if((IR_TIMP_REC[IR_B]<ZERO_MAX)&&(IR_TIMP_REC[IR_B]>ZERO_MIN)){
      IR_SIR.IR_REC[IR_B]=0;
      }
   if((IR_TIMP_REC[IR_B]<UNU_MAX)&&(IR_TIMP_REC[IR_B]>UNU_MIN)){
      IR_SIR.IR_REC[IR_B]=1;
      }
   IR_B++;
   if(IR_B==32){

//      IR_DATA=IR_SIR;         <--  deleted this line
//      IR_DATA=IR_X;
      printf(LCD_SEND_C,"=%lu&",IR_DATA);
      IR_A=0;
      IR_B=0;

      }
   }
}
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