Page 1 of 1
Recompose byte with 8 bits
Posted: Wed Apr 03, 2013 1:19 pm
by tello
Hello, I need to rebuild a Byte variable variables with 8 bit, I tried many ways pore not get it to work.
Can someone show me the way and help me?
thanks
Re: Recompose byte with 8 bits
Posted: Wed Apr 03, 2013 4:50 pm
by Benj
Hello,
You can do it like this where the input variables 0-7 contain the value 0 or 1.
Code: Select all
result = (input7 & 0x01)
result = result << 1
result = result | (input6 & 0x01)
result = result << 1
result = result | (input5 & 0x01)
result = result << 1
result = result | (input4 & 0x01)
result = result << 1
result = result | (input3 & 0x01)
result = result << 1
result = result | (input2 & 0x01)
result = result << 1
result = result | (input1 & 0x01)
result = result << 1
result = result | (input0 & 0x01)
Here we are making off the lower bit from the input variables by using the & 0x01.
We then add the bit value to the result variable by using the | OR command.
The left shift << is used to move each bit up to the next position, bit0 becomes bit1, bit1 becomes bit2 etc.
Re: Recompose byte with 8 bits
Posted: Thu Apr 04, 2013 10:19 am
by tello
Thanks Benj.
I replaced my data by variables and the code is this:
Code: Select all
FCV_DAC_SEND_DATA_HI = (FCV_D7_DAC & 0x01)
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI << 1
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI | (FCV_D6_DAC & 0x01)
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI << 1
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI | (FCV_D5_DAC & 0x01)
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI << 1
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI | (FCV_D4_DAC & 0x01)
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI << 1
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI | (FCV_D3_DAC & 0x01)
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI << 1
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI | (FCV_D2_DAC & 0x01)
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI << 1
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI | (FCV_D1_DAC & 0x01)
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI << 1
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI | (FCV_D0_DAC & 0x01)
Is that correct?? because I have to compile error:
Variador_bombas_877_R4.c(2465): error: missing semicolon
Variador_bombas_877_R4.c(2467): error: missing semicolon
Variador_bombas_877_R4.c(2469): error: missing semicolon
Variador_bombas_877_R4.c(2471): error: missing semicolon
Variador_bombas_877_R4.c(2473): error: missing semicolon
Variador_bombas_877_R4.c(2475): error: missing semicolon
Variador_bombas_877_R4.c(2477): error: missing semicolon
Variador_bombas_877_R4.c(2619): error: missing semicolon
Variador_bombas_877_R4.c failure
What is "error: missing semicolon"??
Thank you very much.
Re: Recompose byte with 8 bits
Posted: Thu Apr 04, 2013 11:46 am
by Benj
Hello,
The code I gave you was for a calculation icon.
If your inserting directly into the C code then you need a semicolon after every line.
Code: Select all
FCV_DAC_SEND_DATA_HI = (FCV_D7_DAC & 0x01);
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI << 1;
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI | (FCV_D6_DAC & 0x01);
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI << 1;
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI | (FCV_D5_DAC & 0x01);
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI << 1;
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI | (FCV_D4_DAC & 0x01);
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI << 1;
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI | (FCV_D3_DAC & 0x01);
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI << 1;
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI | (FCV_D2_DAC & 0x01);
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI << 1;
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI | (FCV_D1_DAC & 0x01);
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI << 1;
FCV_DAC_SEND_DATA_HI = FCV_DAC_SEND_DATA_HI | (FCV_D0_DAC & 0x01);
Re: Recompose byte with 8 bits
Posted: Fri Apr 05, 2013 8:11 am
by tello
Hi Benj, thanks for your help.
I put the code in the block calculation:
Variador_bombas_877_R4.c (2463:46): error: failed to generate expression
Variador_bombas_877_R4.c (2463:46): error: invalid operand '|'
And then I tried the code you put in the previous post:
Variador_bombas_877_R4.c (2498:23): warning: expression was optimized out
Variador_bombas_877_R4.c (2504:46): error: failed to generate expression
Variador_bombas_877_R4.c (2504:46): error: invalid operand '|'
Do not understand why is this error.
Re: Recompose byte with 8 bits
Posted: Fri Apr 05, 2013 10:27 am
by Benj
Hello,
Please can you attach your program and I will take a look for you.
Re: Recompose byte with 8 bits
Posted: Fri Apr 05, 2013 11:26 am
by tello
Hello again Benj.
This is the program.
When I simulate the program works fine.
When compiling to pass the microcontroller fails.
Thank you.
Re: Recompose byte with 8 bits
Posted: Fri Apr 05, 2013 5:10 pm
by Benj
Hello,
Right this seems to be down to a BoostC issue where it cannot logically or a byte with a piece of code in the brackets. This seems very odd so I will report it to BoostC and see if it is a bug they are aware of.
This is now compiling correctly.
Re: Recompose byte with 8 bits
Posted: Mon Apr 08, 2013 8:30 am
by tello
Hi Benj, I tried the version you've corrected and works very well.
Thank you very much for your help.
