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.

Reading 2 bits

Status
Not open for further replies.

be80be

Well-Known Member
I was hoping some one would show me how to read 2 bits like this
Code:
         cblock  0x20
         save
         endc


main:
        movlw  	b'00000001'
	movfw 	save
                                        ; how do you read just the last 2 bits 
                                        ; of save


	end
 
Last edited:
Just and it. (&) like

if(save & 0b00000001) then its a 0
if(save & 0b00000010) = 0 then its a 0
if they both are 1 then its a 11 understand? Ill try to write it in asm

what uC? 18F or 16F or 12F?
 
Last edited:
The technique really depends on what you want to do with the lower two bits of save. Do you want to branch on a condition or use it as a number between 0-3?

Branching:

btfsc save, 0
goto Set
btfsc save, 1
goto Set
goto NeitherSet


Getting value of bits 0&1:

movf save, W
andlw B'00000011'
;Value of lower bits of save now in W.
 
the issue is if the number is 11 then your stuck :

Code:
btfsc save, 0
;Set flag1
btfsc save, 1
;Set flag2


;test both flags if none are set then its   00 
;if flag 1 is set but flag2 isnt it was a   01
;if flag 1 isnt set and flag2 is then it is 10
;if both flags are set then it was          11
 
Last edited:
It really depends on what be80be wants to test for:
My example only tested if either one or the other bit was set hence the statement goto NeitherSet at the end. To test for any value you could just do something like this:

#define TestValue 2
movf save, W
andlw B'00000011'
sublw TestValue
btfsc STATUS, Z
goto Match
 
Last edited:
If it's 01 then it will keep checking if becomes 00 it will it goto. I get it now What i haven't been doing is movf save, w is it better to define TestValue then to set it in cblock or do thay do the same thing I always just used define for like port pins Thank you both this has help a lot. What I'm trying to do is If the last bits become 00 then act on that till thay become 01 say I first put b'00000000' in save my input is high so when it is reads it it will change the last bit to a 1 then if it gos back to 00
 
Iis it better to define TestValue then to set it in cblock or do thay do the same thing I always just used define for like port pins
In my code example you have to use #define because sublw will only accept a literal (constant) and not a variable (GPR). To use a variable you would have to use subwf variable, W instead.
If it's 01 then it will keep checking if becomes 00 it will it goto.
Then the code could be simplified to:

movf save, W
andlw B'00000011'
btfsc STATUS, Z
goto BothBitsAreZero
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top