Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

can i use variable to assign bit number?

Status
Not open for further replies.

skyrock

New Member
Hi everyone,

Once i've tried to use a variable to change for bit number but it wouldn't work.. didn't know whether this can be done. can anyone point out if this is workable? (it is not a full code but just to get the picture across)

Code:
pic=16f628

cblock
var1
endc

movlw   .1
movwf   var1

bsf        STATUS, var1

It actually worked if I define "var1 equ 1", but it wouldnt work with the code above?

Regards..
 
Hi,
Code:
movlw   .1
movwf   var1
This is to put number 1 into the memory location of var1. Another word, store number 1 into var1.
While what you need is the "var1 equ 1". As the assembler replace 1 whenever it sees 'var1'.
You can do it this way too:
Code:
cblock .1
var1
var2
var3
endc
This is also the same as
Code:
var1 equ .1
var2 equ .2
var3 equ .3

Get me?
 
You can't do it that way. You have to use the OR function to do that.

Code:
        movlw   b'00000010'
        iorwf   Variable,F
The above will set bit 1 of Variable.

Mike.
 
i see.. the reason I wanted to use variable for bit number assignment is maybe I can do a loop to assign bit for several ports.. like this:

Code:
loop
       incf      var1
       bsf       PORTB, var1
       goto     loop
 
Code:
loop
       incf      var1
       bsf       PORTB, var1  [COLOR=Blue];var1 here is the memory location assigned to it, but not the value inside it. So you'll get the same result no matter what is in var1[/COLOR]
       goto     loop
 
I mean, if i tried to make a sub to set a bit to a port.. which the sub can be repeatedly used, then using "bsf PORTB, var1" would save me doing:

Code:
setRB0
      bsf    PORTB, 0

setRB1
      bsf    PORTB, 1
.
.
.

you get what i mean?
 
hehehe... understood now.. seems like i'm out of my mind to actually hoped it would work..

thanks..
 
skyrock said:
I mean, if i tried to make a sub to set a bit to a port.. which the sub can be repeatedly used, then using "bsf PORTB, var1" would save me doing:

You can write a subroutine to do it. It's a little long winded,
Code:
SetBit	movfw	Address		;get address
	movwf	FSR		;put it in indirect register
	bcf	STATUS,IRP	;clear the 9th bit
	movlw	1		;initialise mask
	movwf	Mask
	movfw	BitNum		;get the bit count in W
GetMask	btfsc	STATUS,Z	;if zero we have the mask
	goto	GotMask		
	bcf	STATUS,C	;shift in a zero
	rlf	Mask,F		;move mask left
	addlw	0xff		;add -1 to W
	goto	GetMask		;go around again
GotMask	iorwf	INDF,F		;Set the bit
	return

You can then do something like,
Code:
	movlw	0x23
	movwf	Address
	movlw	0x03
	movwf	BitNum
	call	SetBit		;set bit 3 of location 0x23
	incf	BitNum,F
	call	SetBit		;set bit 4 of 0x23

Mike.
 
Great example Mike (Pommie).

You can also use a somewhat general purpose index2bitmask subroutine.

Code:
;--< macros >------------------------------------------------------
 
setbit  macro   file,ndxvar
        movf    ndxvar,W        ; W = index 0..7                  |B0
        call    ndx2mask        ; convert to bitmask              |B0
        iorwf   file,F          ; set bit                         |B0
        endm
clrbit  macro   file,ndxvar
        movf    ndxvar,W        ; W = index 0..7                  |B0
        call    ndx2mask        ;                                 |B0
        xorlw   0xFF            ; invert mask                     |B0
        andwf   file,F          ; clr bit                         |B0
        endm                    ;
tglbit  macro   file,ndxvar
        movf    ndxvar,W        ; W = index 0..7                  |B0
        call    ndx2mask        ; convert to bitmask              |B0
        xorwf   file,F          ; toggle bit                      |B0
        endm
 
;--< example usage >-----------------------------------------------
 
        setbit  STATUS,Var1     ; Var1 contains 0..7              |B0
        clrbit  STATUS,Var1     ; Var1 contains 0..7              |B0
        tglbit  STATUS,Var1     ; Var1 contains 0..7              |B0
Plenty of index2bitmask examples on PICLIST. Some use simple table lookup method or simple loop method. You could also use boundary tolerant or non boundary tolerant inline tables.

Code:
;-------
;
;       index-to-bitmask    b0 = 0    b0 = 1    b1 = 0    b1 = 1
;  0 00000000 -> 00000001  00000001            00000001
;  1 00000001 -> 00000010            00000010  00000010
;  2 00000010 -> 00000100  00000100                      00000100
;  3 00000011 -> 00001000            00001000            00001000
;
;  4 00000100 -> 00010000
;  5 00000101 -> 00100000
;  6 00000110 -> 01000000
;  7 00000111 -> 10000000
;
;  11 words, 14 cycles (sub), 2 vars
;  10 words, 10 cycles (in-line), 2 vars
;
ndx2mask
        movwf   index           ; save index (0..7)               |B0
        movlw   b'00000100'     ; ndx 2                           |B0
        btfss   index,1         ; bit 1 set? no, skip, else       |B0
        movlw   b'00000001'     ; ndx 0                           |B0
        movwf   mask            ;                                 |B0
        btfsc   index,0         ; bit 0 set? no, skip, else       |B0
        addwf   mask,F          ; ndx 3 or ndx 1                  |B0
        btfsc   index,2         ; bit 2 set? no, skip, else       |B0
        swapf   mask,F          ;                                 |B0
        movf    mask,W          ;                                 |B0
        return                  ;                                 |B0
Here's a simple loop variation. Enter with the index value 0..7 in W and exit with W containing the bitmask.

Code:
ndx2mask
        xorlw   0xFF            ; 9 words, 1 var, 12..47 cycles   |B0
        clrf    mask            ;                                 |B0
        setc                    ;                                 |B0
blp6    rlf     mask,F          ;                                 |B0
        addlw   1               ;                                 |B0
        bnc     blp6            ;                                 |B0
        movf    mask,W          ;                                 |B0
        return                  ;                                 |B0
 
Last edited:
Hi,
This should be working too :)
Code:
	movlw	.1
	movwf	bitnum		;set bit 1
	call	Rotate
	movwf	PORTB
	;
	;

Rotate
	movlw	b'00000001'
	movwf	outpattern	;pre-assign bit 0 as high
	movf	bitnum,	w
Rotate_1
	btfsc	STATUS,	Z
	goto	GetPattern
	rlf	outpattern,	f
	addlw	0xff
	goto	Rotate_1

GetPattern
	movf	outpattern,	w
	return
*EDIT: Code edited.
 
Last edited:
wow.. these are great examples of what i had intended to do.. and alot better than what I had done. Nice..

thanks for the examples.
 
bananasiong said:
Hi,
This should be working too :)
Code:
    movlw    .1
    movwf    bitnum        ;set bit 1
    call    Rotate
    movwf    PORTB
    ;
    ;

Rotate
    movlw    b'00000001'
    movwf    outpattern    ;pre-assign bit 0 as high
    movf    bitnum,    w
Rotate_1
    btfsc    STATUS,    Z
    goto    GetPattern
    rlf    outpattern,    f
    addlw    0xff
    goto    Rotate_1

GetPattern
    movf    outpattern,    w
    return
*EDIT: Code edited.
That's not a good routine... You're not accounting for the state of the Carry bit on entry and after the addlw 0xFF instruction...
 
Oh ya, the carry bit should be cleared before rotating the register :p.
How about, after the addlw 0xff instruction?
 
bananasiong said:
Oh ya, the carry bit should be cleared before rotating the register :p.
How about, after the addlw 0xff instruction?
Study and simulate Pommie's example which is very similar to yours and you'll be all set Sir.

Regards, Mike
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top