Searching \ for 'Bit setting via a variable' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/index.htm?key=bit+setting+via
Search entire site for: 'Bit setting via a variable'.

Truncated match.
PICList Thread
'Bit setting via a variable'
1998\02\20@015639 by James Tilley

flavicon
face
I'm not sure if I'm missing something completely obvious our what, but
here is my problem:
I am trying to set a specific bit on portb, and I wanted to do this by
using a variable and the bsf function. Problem is, the bsf function
evaluates the variable as the literal file register address. Is there a
way to do this aside from writing to the whole of portb? Like some
compiler directive to evaluate the contents of the file register instead
of its address?

Thanks for any help...

James

1998\02\20@021349 by Andrew Warren

face
flavicon
face
James Tilley <spam_OUTPICLISTTakeThisOuTspamMITVMA.MIT.EDU> wrote:

> I am trying to set a specific bit on portb, and I wanted to do this
> by using a variable and the bsf function. Problem is, the bsf
> function evaluates the variable as the literal file register
> address. Is there a way to do this aside from writing to the whole
> of portb? Like some compiler directive to evaluate the contents of
> the file register instead of its address?

   No; the BSF and BCF instructions take two literal operands.

   -Andy

=== Andrew Warren - .....fastfwdKILLspamspam@spam@ix.netcom.com
=== Fast Forward Engineering - Vista, California
=== http://www.geocities.com/SiliconValley/2499

1998\02\20@023020 by Harold M Hallikainen

picon face
On Thu, 19 Feb 1998 23:08:15 -0800 Andrew Warren <fastfwdspamKILLspamIX.NETCOM.COM>
writes:
>James Tilley <.....PICLISTKILLspamspam.....MITVMA.MIT.EDU> wrote:
>
>> I am trying to set a specific bit on portb, and I wanted to do this
>> by using a variable and the bsf function. Problem is, the bsf
>> function evaluates the variable as the literal file register
>> address. Is there a way to do this aside from writing to the whole
>> of portb? Like some compiler directive to evaluate the contents of
>> the file register instead of its address?
>
>    No; the BSF and BCF instructions take two literal operands.
>


       Although you should be able to point FSR at the port, then do a
BSF or BCF on INDF, right?

Harold

_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

1998\02\20@025315 by Andrew Warren

face
flavicon
face
Harold M Hallikainen <EraseMEPICLISTspam_OUTspamTakeThisOuTMITVMA.MIT.EDU> wrote:

> > the BSF and BCF instructions take two literal operands.
>
> Although you should be able to point FSR at the port, then do a BSF
> or BCF on INDF, right?

   Right, Harold... But as I inmterpreted the question, he was
   asking about using a variable for the SECOND operand (i.e., the
   bit number).

   -Andy

=== Andrew Warren - fastfwdspamspam_OUTix.netcom.com
=== Fast Forward Engineering - Vista, California
=== http://www.geocities.com/SiliconValley/2499

1998\02\20@054438 by George Carastas

picon face
Hello James,

Friday, 20 February 98, you wrote to me:

JT> I am trying to set a specific bit on portb, and I wanted to do
JT> this by
JT> using a variable and the bsf function. Problem is, the bs

       I don't think there's a compiler directive for that,
       you should probably load your value in a file register
       and use the address of that file register to access your
       variable.


Best regards,
George                            @spam@carastasKILLspamspamusa.net

1998\02\20@083220 by paulh

flavicon
face
On Thu, 19 Feb 1998, James Tilley wrote:

> I am trying to set a specific bit on portb, and I wanted to do this by
> using a variable and the bsf function. Problem is, the bsf function
> evaluates the variable as the literal file register address. Is there a
> way to do this aside from writing to the whole of portb?

bcf and bsf do a read/modify/write cycle, so they also write to the whole
of portb.  Only the pins set as outputs by trisb will actually change.

Bitmasks work almost like bit pointers.  The bitmask 00000001b is the "bit
pointer" to bit 0.  00001000b points to bit 3.  Say your "bit pointer" is
in the file register "BitP".  "bsf BitP" and "bcf BitP" can be simulated
with these little functions.

bsfbitp   ;   subroutine to set the bit in portb pointed to by BitP
      movf   BitP,w
      iorwf  portb,f
      return

bcfbitp  ;    subroutine to clear the bit in portb pointed to by BitP
      comf   BitP,w
      andwf  portb,f
      return

Use rotates to increment and decrement these "bit pointers".  If the carry
bit gets set, you've reached the end of the byte.

I'm sure someone on the list has a clever way of converting a number in
the range 0 to 7 into a bitmask with one bit set and vice versa.

--
KILLspampaulhKILLspamspamhamjudo.com  http://www.hamjudo.com
The April 97 WebSight magazine describes me as "(presumably) normal".

1998\02\20@122413 by lilel

flavicon
face

I often write to PORTB indirectly like this:

   BSF   MIRROR, 3
   MOVF   MIRROR, W
  MOVWF  PORTB

This avoids reading PORTB and possible errors associated with that
route.  To use this method you must insure that the variable MIRROR
always reflects the proper states for PORTB and you never write
directly to PORTB without also writing to MIRROR.


{Quote hidden}

Best Regards,

Lawrence Lile

1998\02\20@123722 by Harold Hallikainen

picon face
On Thu, 19 Feb 1998 23:50:37 -0800 Andrew Warren <RemoveMEfastfwdTakeThisOuTspamIX.NETCOM.COM>
writes:
>Harold M Hallikainen <spamBeGonePICLISTspamBeGonespamMITVMA.MIT.EDU> wrote:
>
>> > the BSF and BCF instructions take two literal operands.
>>
>> Although you should be able to point FSR at the port, then do a BSF
>> or BCF on INDF, right?
>
>    Right, Harold... But as I inmterpreted the question, he was
>    asking about using a variable for the SECOND operand (i.e., the
>    bit number).
>


       Oh, I see!  I'd probably do that with a jump table.  I had
something like that where I had to enable demultiplexors based on a
channel number (which I shifted right three places since the demux was 1
in 8 out).  The jump table looked at w, then jumped to the appropriate
"goto enablemux1" or whatever.

Harold

_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

1998\02\20@174253 by Sean Breheny

face picon face
At 09:31 AM 2/20/98 -0400, you wrote:

<snip>

{Quote hidden}

I have never really participated in the discussions about neat programming
algorithims but I have always been fascinated by them. Since  the challenge
has been issued for a "clever" way of creating a bitmask, I decided to give
it a try. The methods given here are probably not the most efficient in
size or speed, but I am looking to improve my skills at programming tight
algorithims so I thought I would throw them out here for critique.


Here is the fastest routene I could come up with to convert a register
INDEX to a bitmask in BitP


This routene ignores the upper five bits of INDEX, and sets the bit in BitP
which corresponds to the value of the lower three bits of INDEX.
It preserves W and needs no other registers besides INDEX and BitP.
It is 13 words long and takes 12 cycles for INDEX[bits 0,1,2] < 4 and 13
cycles otherwise.

       bcf STATUS,C
       clrf BitP,F
       bsf BitP,0
       btfss INDEX,2
       goto NOT4
       clrf BitP,F
       bsf BitP,4
NOT4   btfsc INDEX,1
       rlf BitP,F
       btfsc INDEX,1
       rlf BitP,F
       btfsc INDEX,0
       rlf BitP,F


Here's the smallest routene:

It looks at the whole INDEX register, sets the bit in BitP which
corresponds to the value in INDEX. It trashes W, also needs no additional
registers, and is only 8 words long. It takes 8 cycles for INDEX < 2 and 8
+ 4*(INDEX-1) for INDEX > 1

       bcf STATUS,C
       movlw 1
       movwf BitP
       addwf INDEX,F
ALOOP  rlf BitP,F
       decfsz INDEX,F
       goto ALOOP
       rrf BitP,F


DISCLAIMER: Anyone can use these routenes for anything, however, I have not
tested them.

Sean

+--------------------------------+
| Sean Breheny                   |
| Amateur Radio Callsign: KA3YXM |
| Electrical Engineering Student |
+--------------------------------+
Fight injustice, please look at
http://homepages.enterprise.net/toolan/joanandrews/

Personal page: http://www.people.cornell.edu/pages/shb7
RemoveMEshb7spamTakeThisOuTcornell.edu
Phone(USA): (607) 253-0315

1998\02\20@201711 by Andrew Warren

face
flavicon
face
Paul Haas <paulhEraseMEspam.....hamjudo.com> wrote:

> I'm sure someone on the list has a clever way of converting a number
> in the range 0 to 7 into a bitmask with one bit set and vice versa.

Paul:

Here's the most obscure way I could come up with to convert from a
number in the range [0-7] to a bitmask.

9 words of program space and between 3 and 10 cycles to execute.

Note that this routine is subject to the usual "direct modification
of the Program Counter" limitations (e.g., on a 16C5x, the whole
thing must be in the first half of a page; and on the other parts,
PCLATH must be set apropriately before the "ADDWF" is executed).

   ADDWF   PCL             ;CALCULATE 2^W, WHERE W IS IN THE
   ADDLW   0               ;RANGE [0-7].
   ADDLW   -1              ;
   ADDLW   -3              ;
   ADDLW   -7              ;
   ADDLW   -15             ;
   ADDLW   -31             ;
   ADDLW   -63             ;
   ADDLW   121             ;

-Andy

=== Andrew Warren - EraseMEfastfwdspamix.netcom.com
=== Fast Forward Engineering - Vista, California
=== http://www.geocities.com/SiliconValley/2499

1998\02\20@202302 by Mike Keitz

picon face
On Fri, 20 Feb 1998 15:46:34 -0500 Sean Breheny <RemoveMEshb7EraseMEspamEraseMECORNELL.EDU>
writes:
[...]
>        bcf STATUS,C
>        movlw 1
>        movwf BitP
>        addwf INDEX,F
>ALOOP  rlf BitP,F
>        decfsz INDEX,F
>        goto ALOOP
>        rrf BitP,F

This is a good start, but can be simplified quite a bit:

       setc
       clrf    BitP
       incf    INDEX,f
ALOOP   rlf     BitP,f
       decfsz  INDEX,f
       goto    ALOOP

Here we start with BitP=0 and C=1, and rotate left INDEX+1 times.  The
worst-case execution time (when INDEX=7) is about 34 cycles.  This
routine doesn't affect W, but it does affect the C and Z flags.  INDEX
must be in the range of 0 to 7.

There is a trick method that works for 4 bits.  It replaces a 2-bit
number in a variable with a 4-bit mask:

       incf    BitP,w  ;W = 0001 0010 0011 0100
       btfsc   BitP,1  ;If 0 or 1, result is almost correct
now.
       iorwf   BitP,f  ;BitP = [0000] [0001] 0011 0111
       incf    BitP,f  ;BitP = 0001 0010 0100 1000

It could be the core of an 8-bit routine thus:
; Convert 3-bit number (0-7) in INDEX to a 8-bit mask (00000001 ...
; 10000000) in BitP.
       movfw   INDEX
       andlw   b'00000011'     ;Start with half of the mask.
       movwf   BitP
       incf    BitP,w          ;The 4-bit converter
       btfsc   BitP,1
       iorwf   BitP,f
       incf    BitP,f
       btfsc   INDEX,2         ;Is it high 4 bits?
       swapf   BitP,f

This is more code space than the repeated shifting methods, but fast
execution of only 9 cycles.  Also, the high 5 bits of INDEX are ignored.
The routine doesn't offer any trememdous advantage over using a table
other than not having to pay attention to the placement of the routine,
set up PCLATH, etc.  I think someone did present a better 8-bit routine
the last time this topic came up.


_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

More... (looser matching)
- Last day of these posts
- In 1998 , 1999 only
- Today
- New search...