Move a bit from a byte to another byte?
Archiver
Posts: 46,084
Hi!
I'm new here! And to basic-stamps also.
I'm building an board-computer for my car, based on a BS2p-40.
The BS is interfaced with the data logging stream of my aftermarket
fuel/ignition ECU (MBE 970).
I recive 2 bytes for each parameter (engine rpm, boost pressure, air temp,
water temp, injection time, ignition advance etc).
For example:
Byte 1, LSB: 00111111
Byte 2, MSB: 01000000
I want to "move" bit 6 from byte 2 to bit 7 in byte 1.
So the result will be:
Byte 1, LSB: 10111111
Byte 2, MSB: 00000000
Any ideas on how to do this in a fast simple way?
The bytes from the ECU is saved in arrays, and I can't get
the "variable_name.BIT6(x)" to work.. =(
Thanks.
/Johan
I'm new here! And to basic-stamps also.
I'm building an board-computer for my car, based on a BS2p-40.
The BS is interfaced with the data logging stream of my aftermarket
fuel/ignition ECU (MBE 970).
I recive 2 bytes for each parameter (engine rpm, boost pressure, air temp,
water temp, injection time, ignition advance etc).
For example:
Byte 1, LSB: 00111111
Byte 2, MSB: 01000000
I want to "move" bit 6 from byte 2 to bit 7 in byte 1.
So the result will be:
Byte 1, LSB: 10111111
Byte 2, MSB: 00000000
Any ideas on how to do this in a fast simple way?
The bytes from the ECU is saved in arrays, and I can't get
the "variable_name.BIT6(x)" to work.. =(
Thanks.
/Johan
Comments
writes:
> Byte 1, LSB: 00111111
> Byte 2, MSB: 01000000
>
>
> I want to "move" bit 6 from byte 2 to bit 7 in byte 1.
>
> So the result will be:
>
> Byte 1, LSB: 10111111
> Byte 2, MSB: 00000000
Does bit 6 of the MSB change, or is it constant? Assuming bit6 changes...
My syntax may be incorrect, but the basic idea is.....
1) Get the value of bit6 from Byte 2 by using an AND function and put this
into a temporary variable
2) Shift left the temporary variable, this will move the value from bit6 into
bit7
3) Perform an OR function with the temporary byte and Byte 1
bit7 of Byte 1 should now contain the value of bit6 of Byte 2
4) When done, if you need to ensure all bits of Byte 2 are cleared (as in
your example) perform an AND fucntion with Byte 2
......Byte 2 = Byte 2 AND %00000000
Temp Byte = Byte 2 AND %01000000
Shift left Temp Byte
Byte 1 = Byte 1 OR Temp Byte
(I am almost sure my syntax for "Shift Left is incorrect and I don't have my
Stamp manual here with me)
ken
[noparse][[/noparse]Non-text portions of this message have been removed]
Welcome to Stamping. How about:
b(x) = b(y) & $40 << 1 | ( b(x) & $7F )
Where b(x) represent byte 1 and b(y) represent byte 2 in your
example. Might want to dry-run it on your Debug screen to make sure
it works as desired.
Regards,
Steve
Another way to do this is to define your array in a different way.
Currently, you might have the following syntax,
parameters var byte(12) ' array for 12 bytes, 6 parameters
and your program fills the array something like this:
for i=0 to 11
serin ......,[noparse][[/noparse]parameters(i)]
next
Instead, define the array _implicitly_ as follows:
enginerpm var word
erpm0 var enginerpm.byte0
boostpressure var word
airtemp var word
watertemp var word
injectiontime var word
ignitionadvance var word
Be sure these parameters are defined in the order you will get them
from the ECU. Now you can fill the _implicit_ array:
for i=0 to 11
serin ......,[noparse][[/noparse]erpm0(i)]
next
The Stamp happily fills the bytes in order in memory. You don't have
to define an explicit array.
Then, to move one bit, all you have to do is the straightforward,
enginrpm.bit15=boostpressure.bit7
which you can't do if you define the array explicitly. I generally
prefer to use implicit arrays, because they are so much more flexible
in the Stampese way of doing things.
(BTW, the above assumes the words come out of the ECU with least
significant byte first, which is the way the Stamp stores data in
RAM--you will have to make an adjustment if the ECU sends out most
significant byte first)
-- regards,
Tracy Allen
electronically monitored ecosystems
mailto:tracy@e...
http://www.emesystems.com
>Hi!
>
>I'm new here! And to basic-stamps also.
>
>I'm building an board-computer for my car, based on a BS2p-40.
>
>The BS is interfaced with the data logging stream of my aftermarket
>fuel/ignition ECU (MBE 970).
>
>I recive 2 bytes for each parameter (engine rpm, boost pressure, air temp,
>water temp, injection time, ignition advance etc).
>
>For example:
>
>Byte 1, LSB: 00111111
>Byte 2, MSB: 01000000
>
>
>I want to "move" bit 6 from byte 2 to bit 7 in byte 1.
>
>So the result will be:
>
>Byte 1, LSB: 10111111
>Byte 2, MSB: 00000000
>
>
>Any ideas on how to do this in a fast simple way?
>
>The bytes from the ECU is saved in arrays, and I can't get
>the "variable_name.BIT6(x)" to work.. =(
>
>
>Thanks.
>
>
>/Johan