Can the position of a bit be defined by a variable, i.e. PortD.Bitnum (where Bitnum is a Byte variable with a value between 0 and 7) ? Thanks for any answer ...
Last edit: Bertrand BAROTH 2019-01-23
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Can I specify the bit of a variable to alter using another variable?
Setting a variable with other variable will expected. Great Cow BASIC support bitwise assignments. As follows:
portc.0 = !porta.1
You can also use a shift function. As in other languages, by using the Shift Function FnLSL. AN example is:
MyVar = FnLSL( 1, BitNum)` is Equivalent to `MyVar = 1<<BitNum`
To set a bit of a port prevent glitches during the output update, use this method.
'add this option for a specific port.
#option volatile portc.0
'then in your code
portc.0 = !porta.1
You can use this method to set a bit of a port. Encapsulate it in the SetWith method, this also eliminates any glitches during the update, use this method.
SetWith(MyPORT, MyPORT OR FnLSL( 1, BitNum))
To clear a bit of a port, use this method.
MyPORT = MyPORT AND NOT FnLSL( 1, BitNum))
To set a bit within an array, use this method.
video_buffer_A1(video_adress) = video_buffer_A1(video_adress) OR FnLSL( 1, BitNum)
See also Set, FnLSL, FnLSR and Rotate
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sorry, I think I didn't explain clearly enough. Instead of using PortD.0 for example, I wanted to use PortD.Bitnum, where bitnum is a variable (with the value 0 in this case). The purpose was to set the pullups 0 to 5 of a port "bitwise", using a loop (because Bits 6 and 7 are used by software RS232), instead of 6 instructions (Finally I must admit that the advantage is not very obvious, I wonder if it would not even use more PRG memory) ...
Last edit: Bertrand BAROTH 2019-01-24
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The bit are numbered 0 to 7..that's values 1,2,4,8,16,32,64,128
that's 1 shifted left 0,1,2,3,4,5,6,7 times.
to find bit 4 of a byte , bit4=byte and (1 << 4)
~~~
for bit=0 to 7
val=byte and (1 << bit)
print val
next
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Can the position of a bit be defined by a variable, i.e. PortD.Bitnum (where Bitnum is a Byte variable with a value between 0 and 7) ? Thanks for any answer ...
Last edit: Bertrand BAROTH 2019-01-23
From the HELP.
Can I specify the bit of a variable to alter using another variable?
Setting a variable with other variable will expected. Great Cow BASIC support bitwise assignments. As follows:
You can also use a shift function. As in other languages, by using the Shift Function FnLSL. AN example is:
To set a bit of a port prevent glitches during the output update, use this method.
You can use this method to set a bit of a port. Encapsulate it in the SetWith method, this also eliminates any glitches during the update, use this method.
To clear a bit of a port, use this method.
To set a bit within an array, use this method.
See also Set, FnLSL, FnLSR and Rotate
Sorry, I think I didn't explain clearly enough. Instead of using PortD.0 for example, I wanted to use PortD.Bitnum, where bitnum is a variable (with the value 0 in this case). The purpose was to set the pullups 0 to 5 of a port "bitwise", using a loop (because Bits 6 and 7 are used by software RS232), instead of 6 instructions (Finally I must admit that the advantage is not very obvious, I wonder if it would not even use more PRG memory) ...
Last edit: Bertrand BAROTH 2019-01-24
Hi Bertrand,
The short answer is NO.
However , as Evan pointed out, there are ways to achieve the same thing.
Set PortB.BitNum ON
Could be written as
SetWith(PortB, PortB OR FnLSL( 1, BitNum))
What is happening is that 1 is being Left shifted BitNum times and then Logically ORed with the current value of PORTB.
for example:
BitNum = 3
Set PortB.BitNum ON
Would set bit 3 or in binary 0b00001000
So, returning to your actual requirement of:
Raises the question, why not just mask the bits directly as in:
PortB = PortB OR 0b00111111
the OR is only there to preserve the current value of Bits 6 and 7, if you were just setting pullups though, they would not need preserving so:
PortB = 0b00111111
should suffice and is far less code than a loop setting bits.
Hope that helps,
Cheers
Chris
Last edit: Chris Roper 2019-01-24
The bit are numbered 0 to 7..that's values 1,2,4,8,16,32,64,128
that's 1 shifted left 0,1,2,3,4,5,6,7 times.
to find bit 4 of a byte , bit4=byte and (1 << 4)
~~~
for bit=0 to 7
val=byte and (1 << bit)
print val
next
Thanks for Your answers ...