Truncated match.
PICList
Thread
'Bit Set/Reset problem.....a puzzle!'
1999\03\02@140317
by
Paul BRITTON
|
Dear allies,
A friend of mine is writing her first PIC program using the 16C64, and she
has asked me this question, because she knows I use PICs, but I can't
think of how to do it!
Her Problem: If you have a value 'n', ranging from 0-7 in W (or a file
reg, whichever makes it work!)
How can you use this to set or clear bit 'n' in another
file reg. (or W)
In other words can you use W or any reg as an indirect bit pointer?
If not (and I think not), what is the best way to implement such a
function?
The best I can think of is something like
.
.
.
MOVLW B'11000001' ; for example obviously these
values will
MOVLW H'02' ; come from somewhere else in the
real prog!
CALL BITSET
IORWF TARGET,1
.
.
.
MOVLW H'06' ;
CALL BITCLR
ANDWF TARGET,1
.
.
.
BITSET ANDLW H'07'
ADDWF PCL,F
RETLW B'00000001'
RETLW B'00000010'
RETLW B'00000100'
RETLW B'00001000'
RETLW B'00010000'
RETLW B'00100000'
RETLW B'01000000'
RETLW B'10000000'
BITCLR ANDLW H'07'
ADDWF PCL,F
RETLW B'11111110'
RETLW B'11111101'
RETLW B'11111011'
RETLW B'11110111'
RETLW B'11101111'
RETLW B'11011111'
RETLW B'10111111'
RETLW B'01111111'
Anyone have any better ideas? Am I missing something in the instruction
set?
I need some help for the sake of my guru-ship!
Regards
Paul
(Preparing to be humble!)
1999\03\02@142828
by
paulb
Paul BRITTON wrote:
> In other words can you use W or any reg as an indirect bit pointer?
Pick another register as pointer, let's call it "ptr".
clrc
movlw 1
btfsc ptr,1
movlw 4
movwf mask
btfsc ptr,0
rlf mask,F
btfsc ptr,2
swapf mask,F ; or W if you need that
Now you've got your mask value; do what you will with it. This code
is isosynchronous, it may be shorter code to rlf mask while decfsz ptr.
Concept not original, copied from Dimitry amongst others.
--
Cheers,
Paul B.
1999\03\02@175342
by
Regulus Berdin
Hi all,
"Paul B. Webster VK2BZC" wrote:
> Pick another register as pointer, let's call it "ptr".
>
> clrc
> movlw 1
> btfsc ptr,1
> movlw 4
> movwf mask
> btfsc ptr,0
> rlf mask,F
> btfsc ptr,2
> swapf mask,F ; or W if you need that
> Concept not original, copied from Dimitry amongst others.
I think the original version (1 cycle less) goes like this:
movlw 1
btfsc ptr,1
movlw 4
movwf mask
btfsc ptr,0
addwf mask,f
btfsc ptr,2
swapf mask,f
The 'rlf' is replaced with an 'addwf' thus 'clrc' is not needed
anymore. I had also posted a 32 bit version last week for the led bar
solution. I copied the technique for a part. The code is also found on
the website below.
regards,
Reggie
--
e-mail: spam_OUTrberdinTakeThisOuT
bigfoot.com
ICQ#: 31651436
URL: http://www.bigfoot.com/~rberdin
1999\03\02@232303
by
Mike Keitz
|
On Tue, 2 Mar 1999 18:57:30 +0000 Paul BRITTON <.....Paul.BRITTONKILLspam
@spam@MMSUK.CO.UK>
writes:
>Dear allies,
>
>A friend of mine is writing her first PIC program using the 16C64, and
>she
>has asked me this question, because she knows I use PICs, but I can't
>think of how to do it!
>
>Her Problem: If you have a value 'n', ranging from 0-7 in W (or a
>file
>reg, whichever makes it work!)
> How can you use this to set or clear bit 'n' in
>another
>file reg. (or W)
The PIC CPU core doesn't have an instruction to do it directly. A common
way is what you've done: translate the bit number to a mask, then use a
bytewise AND or OR to clear or set the masked bit. You only really need
one mask translation table or routine. Use the mask as is (all 0 except
one 1) to set bits with OR, and complememt the mask for the bit-clearing
AND instruction. If you need to optimize for speed, maybe a table of bsf
or bcf instructions would be faster:
setbi
rlf bitno,w ;Multiply bit number by 2
andlw b'00001110' ;Keep only relevant bits
addwf PCL,f ;jump into table below
bsf INDF,0
return
bsf INDF,1
return
[...]
bsf INDF,7
return
Thsi routine needs 19 instructions and always takes 9 cycles (including
the CALL and RETURN instructions). A corresponding routine would be
needed to clear bits. As with any table, ensure that PCLATH is set
properly. If you can store the bit number as bitnumber * 2 that would
avoid needing to multiply it every time to form the table index.
___________________________________________________________________
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/getjuno.html
or call Juno at (800) 654-JUNO [654-5866]
1999\03\03@030401
by
Regulus Berdin
Hi,
Mike Keitz wrote:
> setbi
> rlf bitno,w ;Multiply bit number by 2
> andlw b'00001110' ;Keep only relevant bits
> addwf PCL,f ;jump into table below
> bsf INDF,0
> return
> bsf INDF,1
> return
> [...]
> bsf INDF,7
> return
>
> Thsi routine needs 19 instructions and always takes 9 cycles (including
> the CALL and RETURN instructions). A corresponding routine would be
> needed to clear bits. As with any table, ensure that PCLATH is set
> properly. If you can store the bit number as bitnumber * 2 that would
> avoid needing to multiply it every time to form the table index.
A simple lookup table and masking directly is faster. Only 8 cycles and
lesser code space.
main_routine:
.
.
.
call setbi ;7
iorwf INDF,f ;8 use andwf if clearing
.
.
setbi:
movf bitno,w
addwf PCL,f
retlw B'00000001' ; set to B'11111110' if clearing
retlw B'00000010'
.
.
.
retlw B'10000000'
regards,
Reggie
More... (looser matching)
- Last day of these posts
- In 1999
, 2000 only
- Today
- New search...