![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
nailuy
Joined: 21 Sep 2010 Posts: 159
|
how to convert string of int1 to one int16? |
Posted: Fri Feb 07, 2014 5:31 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
atoo
Joined: 31 Jan 2014 Posts: 32
|
attoo |
Posted: Fri Feb 07, 2014 7:56 pm |
|
|
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
should note always has something wrong
Charles francês joão pessoa paraiba |
|
![](templates/subSilver/images/spacer.gif) |
newguy
Joined: 24 Jun 2004 Posts: 1903
|
|
Posted: Fri Feb 07, 2014 9:48 pm |
|
|
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;
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Sat Feb 08, 2014 12:33 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
nailuy
Joined: 21 Sep 2010 Posts: 159
|
|
Posted: Sat Feb 08, 2014 1:20 am |
|
|
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). |
|
![](templates/subSilver/images/spacer.gif) |
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Sat Feb 08, 2014 1:31 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19383
|
|
Posted: Sat Feb 08, 2014 1:41 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
nailuy
Joined: 21 Sep 2010 Posts: 159
|
|
Posted: Sat Feb 08, 2014 2:43 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
nailuy
Joined: 21 Sep 2010 Posts: 159
|
|
Posted: Sat Feb 08, 2014 4:50 pm |
|
|
Ttelmah
I found where was the problem...
best regards. |
|
![](templates/subSilver/images/spacer.gif) |
bkamen
Joined: 07 Jan 2004 Posts: 1611 Location: Central Illinois, USA
|
|
Posted: Mon Feb 10, 2014 11:01 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
nailuy
Joined: 21 Sep 2010 Posts: 159
|
|
Posted: Sat Feb 15, 2014 3:04 am |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19383
|
|
Posted: Sat Feb 15, 2014 5:00 am |
|
|
You are not declaring IR_SIR as a union, but as an int32..... |
|
![](templates/subSilver/images/spacer.gif) |
nailuy
Joined: 21 Sep 2010 Posts: 159
|
|
Posted: Sat Feb 15, 2014 6:04 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Feb 15, 2014 9:53 am |
|
|
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: 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;
}
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|