I am converting a program from a basic stamp and have run into a set of command
I can not find a solution for.
I read from the 1 wire bus 2 8 bit values. I then need to chop up the bits and
convert to a value.
Example
PBASIC ' What is the cross for these
tempIn VAR Word
TSign VAR Bit
Sign VAR tempIn.Bit11
TLo VAR tempIn.LowByte
THi VAR tempIn.HighByte
LS Byte I need to chop off the first 4 bits. I then need to chop up this nibble
by 1,2,3 bits
I then need to chop off the last 5 bits of the MS Byte and then put the left
over bits MS + LS and convert to a decimal value.
I can not find any examples or fucntions workign with bits.
Any help qould be great
Thanks
bit operations
-
YahooArchive
- Posts: 1462
- Joined: Fri Oct 19, 2012 5:11 am
Re: bit operations
I believe you will need to mask and shift, e.g., to isolate the 3rd bit from the
right:
(value AND &H4) >> 2
You can work with multuple contiguous bits, just mask off the ones you don't
need to the left, and shift off those on the right.
There is no need for conversion to decimal, value is value, number base is an
output formatting issue (decimal is the default.)
-MM
right:
(value AND &H4) >> 2
You can work with multuple contiguous bits, just mask off the ones you don't
need to the left, and shift off those on the right.
There is no need for conversion to decimal, value is value, number base is an
output formatting issue (decimal is the default.)
-MM
-
YahooArchive
- Posts: 1462
- Joined: Fri Oct 19, 2012 5:11 am
Re: bit operations
> > tempIn VAR Word
> > TSign VAR Bit
> > Sign VAR tempIn.Bit11
> > TLo VAR tempIn.LowByte
> > THi VAR tempIn.HighByte
You could do it with defines
read--
#define Sign tempIn & &H800 ' did you really mean to overlap highbyte
#define TLo tempIn & &HFF
#define THi tempIn & &HFF00
write--
#define setSign(x) tempIn=tempIn | (x << 11)
#define setTLo(x) tempIn=tempIn | (x) ' you may need to mask the x
' like (x & &HFF)
#define setTHi(x) tempIn=tempIn | (x << 8)
> > TSign VAR Bit
> > Sign VAR tempIn.Bit11
> > TLo VAR tempIn.LowByte
> > THi VAR tempIn.HighByte
You could do it with defines
read--
#define Sign tempIn & &H800 ' did you really mean to overlap highbyte
#define TLo tempIn & &HFF
#define THi tempIn & &HFF00
write--
#define setSign(x) tempIn=tempIn | (x << 11)
#define setTLo(x) tempIn=tempIn | (x) ' you may need to mask the x
' like (x & &HFF)
#define setTHi(x) tempIn=tempIn | (x << 8)
-
YahooArchive
- Posts: 1462
- Joined: Fri Oct 19, 2012 5:11 am
Re: bit operations
Sorry
I am new to the Armweb and may not understand the define but i get errors trying
your example.
However here is what I have done, please advise any improvement
I am reading a DS1822 1 wire device
I get 2 Byts from the bus representing LSB & MSB.
the LSB reps (bits 0-3 are Decimal, bits 4-7 are First part of value)Bit 0 = 2-4
Bit 1 = 2-3
Bit 2 = 2-2
Bit 3 = 2-1
Bit 4 = 2^0
Bit 5 = 2^1
Bit 6 = 2^2
Bit 7 = 2^3
the MSB reps (Bits 0-2 are part of value, bit 3-7 are 0 for Pos values and bit
3-7 are 1 for neg values)
Bit 0 = 2^4
Bit 1 = 2^5
Bit 2 = 2^6
Bit 3~7 = Sign
SO here is what I have doen in code
RawTemp = (MSB << 8) + LSB ' This adds both values to create a WORD
ActTemp = RawTemp >> 4 ' This creates the actual temp
However how if this is a neg number then my routine fails.
How do I make sure all bits 11..31 are 0?
How do I check and see if Bit 15 is a 1?
I am new to the Armweb and may not understand the define but i get errors trying
your example.
However here is what I have done, please advise any improvement
I am reading a DS1822 1 wire device
I get 2 Byts from the bus representing LSB & MSB.
the LSB reps (bits 0-3 are Decimal, bits 4-7 are First part of value)Bit 0 = 2-4
Bit 1 = 2-3
Bit 2 = 2-2
Bit 3 = 2-1
Bit 4 = 2^0
Bit 5 = 2^1
Bit 6 = 2^2
Bit 7 = 2^3
the MSB reps (Bits 0-2 are part of value, bit 3-7 are 0 for Pos values and bit
3-7 are 1 for neg values)
Bit 0 = 2^4
Bit 1 = 2^5
Bit 2 = 2^6
Bit 3~7 = Sign
SO here is what I have doen in code
RawTemp = (MSB << 8) + LSB ' This adds both values to create a WORD
ActTemp = RawTemp >> 4 ' This creates the actual temp
However how if this is a neg number then my routine fails.
How do I make sure all bits 11..31 are 0?
How do I check and see if Bit 15 is a 1?
-
YahooArchive
- Posts: 1462
- Joined: Fri Oct 19, 2012 5:11 am
Re: bit operations
> How do I make sure all bits 11..31 are 0?
if x and &HFFFFF800 then print "not all bits 31-11 are 0"
> How do I check and see if Bit 15 is a 1?
if x and &H8000 then print "bit 15 is a 1"
This is not specific to the ARMweb, but would be common in BASIC or C though the
syntax varies.
PS in my example the & should have been AND
sorry I'm switching between C BASIC and Tcl and can't always keep track
if x and &HFFFFF800 then print "not all bits 31-11 are 0"
> How do I check and see if Bit 15 is a 1?
if x and &H8000 then print "bit 15 is a 1"
This is not specific to the ARMweb, but would be common in BASIC or C though the
syntax varies.
PS in my example the & should have been AND
sorry I'm switching between C BASIC and Tcl and can't always keep track
-
YahooArchive
- Posts: 1462
- Joined: Fri Oct 19, 2012 5:11 am
Re: bit operations
Thanks
I can use these examples, but I am still having a problem clearing the 11..31
bits. I other basic lanquages I can just grab the end bits and truncate the
others.
Howver if I shift up and then back it fills all with 1's and.
Most of the time 16..31 will be 0, I would only need to truncate the 11..15 and
I know I could AND but then I have to assume all bits would be 1.
I can use these examples, but I am still having a problem clearing the 11..31
bits. I other basic lanquages I can just grab the end bits and truncate the
others.
Howver if I shift up and then back it fills all with 1's and.
Most of the time 16..31 will be 0, I would only need to truncate the 11..15 and
I know I could AND but then I have to assume all bits would be 1.
-
YahooArchive
- Posts: 1462
- Joined: Fri Oct 19, 2012 5:11 am
Re: bit operations
> I can use these examples, but I am still having a problem clearing the 11..31
bits. I other basic lanquages I can just grab the end bits and truncate the
others.
>
> However if I shift up and then back it fills all with 1's and.
AND in most BASICs is by default a bitwise operator. In this form it can be
used to mask bits (this is how VB does it). It can also be a logical operator,
but does that when the expression performs a logical operation
For instance
x = y and &H100 ' masks for bit 8
x = y>1 and z ' here its a logical operation
Shifts just do that, shift bits, and it does it across the whole 32 bit field.
bits. I other basic lanquages I can just grab the end bits and truncate the
others.
>
> However if I shift up and then back it fills all with 1's and.
AND in most BASICs is by default a bitwise operator. In this form it can be
used to mask bits (this is how VB does it). It can also be a logical operator,
but does that when the expression performs a logical operation
For instance
x = y and &H100 ' masks for bit 8
x = y>1 and z ' here its a logical operation
Shifts just do that, shift bits, and it does it across the whole 32 bit field.
-
YahooArchive
- Posts: 1462
- Joined: Fri Oct 19, 2012 5:11 am
Re: bit operations
> >
> > However if I shift up and then back it fills all with 1's and.
>
>
> Shifts just do that, shift bits, and it does it across the whole 32 bit field.
>
What exactly do you mean by this?
There are normally two types of shifts - logical and arithmetic. The difference
is that arithmetic preserves the sign bit.
In other languages typically you would use a division operator if you wanted an
arithmetic shift and the shift operator if you wanted a logical shift.
Does BASIC allow for both types of shift?
Regards,
Chris Burrows
CFB Software
http://www.astrobe.com
> > However if I shift up and then back it fills all with 1's and.
>
>
> Shifts just do that, shift bits, and it does it across the whole 32 bit field.
>
What exactly do you mean by this?
There are normally two types of shifts - logical and arithmetic. The difference
is that arithmetic preserves the sign bit.
In other languages typically you would use a division operator if you wanted an
arithmetic shift and the shift operator if you wanted a logical shift.
Does BASIC allow for both types of shift?
Regards,
Chris Burrows
CFB Software
http://www.astrobe.com