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.

AND Contents of a Register

Status
Not open for further replies.

burg

New Member
Hi,

I'm trying to "AND" the contents of a register with values from 0 - 255. I'm not sure how to approach this. Here is what I have so far:

Code:
                     movlw	b'11100001'		
			andlw		PORTB	
			movlw		PORTB

I don't know what the contents of the top 3 bits are on PORTB, and I don't want to change them. I don't think my code will work, however, cause PORTB is not a literal. I could move the value of PORTB into W, but how do I do that? Does "movf PORTB" work without chopping the MSB off?

Thanks!
 
Sure you can MOVF PORTB without losing anything.
I'm not really sure what you are trying to do but you can

movf PORTB,W ;move port b to W register.
andwf PORTB,F ;AND the contents of the W register with PORTB.
Is this what you are trying to do ?
 
you want to AND from 0 to 255, but you don't want the top 3 bits to be affected? Then you can only AND from 0 to 31 because 2^5 - 1 = 31.
you have to move PORTB to w
movf PORTB, w ;move the content of PORTB to w
andlw b'11100000' ;AND b'11100000' with w
movwf Store ;move the result to Store

but why you want to do this?
 
AND ing PORT input through W register

Hola Burg,

Look what I do in a 16F877 for reading the upper nibble of PORTB (b7:b4)


MOVLW B'11110000' ;load mask for lower nibble in W
ANDWF PORTB,W ;read upper nibble - bit clear means key pressed - reading saved in W
MOVWF JOKER ;copy reading to aux file


Instead of my mask you put your values from 0 to 255.

Compare with the syntax of your code and revert if needed.
 
Last edited:
Brevor said:
movf PORTB,W ;move port b to W register.
andwf PORTB,F ;AND the contents of the W register with PORTB.
Is this what you are trying to do ?
This is doing nothing isn't it? The same thing in the PORTB will be stored back to PORTB.
 
This is doing nothing isn't it? The same thing in the PORTB will be stored back to PORTB.

I was just showing some examples of data moves between port B and W.
They were not meant to be used together.
 
Sorry about the confusion. What I have is 7 LED's connected to PortB on a 16F88. 5 LED's act as a Tachometer, and the other 2 act as warning lights. I want 5 LED's to change according to RPM, yet I dont want the other 2 LED's to be changed due to the Tach.

I was just reading the datasheet and was confused, cause it mentions only number between 0 - 127 can be moved. But anyways, would this be fine:

movf PORTB,W
andwf b'11100001'
movwf REGISTER

As suggested by banaasiong. Would it work for my situation? Thanks!
 
burg said:
Sorry about the confusion. What I have is 7 LED's connected to PortB on a 16F88. 5 LED's act as a Tachometer, and the other 2 act as warning lights. I want 5 LED's to change according to RPM, yet I dont want the other 2 LED's to be changed due to the Tach.

I was just reading the datasheet and was confused, cause it mentions only number between 0 - 127 can be moved. But anyways, would this be fine:

movf PORTB,W
andwf b'11100001'
movwf REGISTER

As suggested by banaasiong. Would it work for my situation? Thanks!
?? Did I? I have suggested this:
Code:
movf   PORTB, w
andlw  b'11100001'   AND Literal with w, store into w
movwf REGISTER
If you use andwf b'11100001', not b'11100001' ANDed with w, but the content of address 0x00E1 is ANDed with w.
 
It sounds like you want to preserve the upper 3 bits of PORTB then overwrite the lower 5 bits with LED data representing RPM, yes, no? If so, I would assume you have some variable with data in the 5 lower bits that represents the new LED RPM pattern and I would code it like this;

Code:
;
        movf    PORTB,W         ; get current PORTB
        andlw   b'11100000'     ; preserve upper 3 bits (in W)
        iorwf   RPMdata,W       ; add lower 5 bits from RPMdata (in W)
        movwf   PORTB           ; update PORTB with new RPM LED pattern
;
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top