Bob, Read between the lines: >Well, since you're willing to perform btf operations on an I/O port, then you could do this: Can also be read as "This is not generally a great idea but if you are going to try it for he sake of squeezing out some bytes of code here goes......" I generally stay away from bit operations on an I/O port, for several reasons. Better to move the data into a mirror byte, then act on it. Direct reading of ports invites the Read-Write-Modify bug, as well as takes the chance your data will change before you finish working on it. OTOH a well thought out system could probably handle these problems. I also find mirror bytes a good method of being sure a port is set a certain way, rather than guessing that the port is still in the same state I left it last. This all gets into the philosophy of programming and styles and can generate as much heated discussion as gun control, traffic safety or which C compiler is better. -- Lawrence Lile Scott Dattalo Sent by: pic microcontroller discussion list 06/18/2004 12:26 PM Please respond to pic microcontroller discussion list To: PICLIST@MITVMA.MIT.EDU cc: Subject: Re: [PIC]: anding/masking On Fri, 18 Jun 2004, Bob Japundza wrote: > Hi All, > > I was wondering if my code below can be made to be a bit better. It works fine as is, but just seems to be a bit clunky to me. In a nutshell what I'm doing here is checking two buttons on port A, turning off/on the motor and if one button is pressed, that dictates motor direction. I'm wondering if there's a way without using the temporary register "button_status" to check the the two bits I'm looking at. > > movlw b'00001100' > andwf PORTA,W ;mask all but the RA2,3 bits > movwf button_status > movlw b'00001100' > xorwf button_status,w > btfsc STATUS,Z ;are both buttons pressed? > goto stopmotor ;yes, stop the motor > movlw b'00001100' > andwf button_status,w > btfsc STATUS,Z ;are both buttons not pressed? > goto stopmotor ;yes, stop the motor > btfsc PORTA,2 ;check if up button is on > goto motorup > btfsc PORTA,3 ;check if down button is on > goto motordown Well, since you're willing to perform btf operations on an I/O port, then you could do this: btfsc PORTA, RA3 goto RA3_is_high btfsc PORTA, RA2 goto motorup ; RA3:RA2 = 01 goto stopmotor ; RA3:RA2 = 00 RA3_is_high: btfss PORTA, RA2 goto motordown ; RA3:RA2 = 10 ; RA3:RA2 = 11 stopmotor: Scott -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body