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.

Masking Upper Nibble

Status
Not open for further replies.

Suraj143

Active Member
Hi I need to mask the upper nibble & must not do any harm to the lower nibble.

But this code never work.

Code:
	movf	Data,W
	andlw	b'11110000'
	iorwf	PORTB,F


But this code works but corrupting lower nibble.

Code:
	movf	Data,W
	andlw	b'11110000'
	movwf	PORTB,F
 
Hi I need to mask the upper nibble & must not do any harm to the lower nibble.

But this code works but corrupting lower nibble.
Code:
	movf	Data,W
	andlw	b'11110000'
	movwf	PORTB,F
That's because you should use AND to clear, or mask, the upper nibble - not the lower nibble. Like this:
Code:
	andlw	b'00001111'
This will zero out the upper nibble and leave the lower nibble exactly the way it was.
 
Last edited:
Do you wish to transfer the upper nibble of Data to the upper nibble of PORTB without disturbing the lower nibble in either?
 
Then first move Data to a temp variable or W and mask the lower nibble. Then mask the upper nibble of PORTB (maybe move it to a temporary variable first). Then OR them together.

Maybe there's a simpler way?

Code:
	movf	Data,W
	andlw	b'00001111'
	????	????
	iorwf	PORTB,F
 
Code:
	movf	Data,W
	andlw	b'11110000'
	movwf	temp1
	movf	PORTB,w
	andlw	b'00001111'
	iorwf	temp1,w
	movwf	PORTB
Hmm... Is that right? Just quickly banged it out. Can't test it quickly.
 
Code:
	movf	Data,W
	andlw	b'11110000'
	movwf	temp1
	movf	PORTB,w
	andlw	b'00001111'
	iorwf	temp1,w
	movwf	PORTB
Hmm... Is that right? Just quickly banged it out. Can't test it quickly.

OK thanks for your help.It will work.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top