Thanks Bob! Very helpful. I have worried of the buttons changing state while this was executing. What I'm gathering is that the use of temporary registers or just doing a movlw PORTA and reading from W is important in reading i/o ports. Regards, Bob Bob Ammerman wrote .. > ----- Original Message ----- > From: "Bob Japundza" > To: > Sent: Friday, June 18, 2004 1:01 PM > Subject: [PIC]: anding/masking > > ; -- Here is a version that only fetches PORTA once, ensuring a consistient > view > ; -- of the button state > > movf PORTA,W > movwf button_status ; Bits are in RA2 and RA3 > btfsc button_status,2 ; Is bit two set? > xorlw b'00001000' ; Yes: flip bit 3 > andlw b'00001000' ; Were the two bits the same? > skpnz > goto stopmotor ; Yes: stop it > btfsc button_status,3 ; Was up button on? > goto motorup > goto motordown > > ; -- If you don't mind reading PORTA multiple times then... > > movf PORTA,W > btfsc PORTA,2 > xorlw b'00001000' > andlw b'00001000' > skpnz > goto stopmotor > btfsc PORTA,3 > goto motorup > goto motordown > > ; -- here is a version that only reads port A once and doesn't use any > ; -- temporaries > > movf PORTA,W > andlw b'00001100' ; Are they both zero? > skpnz > goto stopmotor ; Yes > xorlw b'00001100' ; Are they both zero? > skpnz > goto stopmotor > andlw b'00000100' ; Is the up button down? > skpz > goto upmotor > goto downmotor > > Bob Ammerman > RAm Systems > > > > > 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 > > > > Regards, > > Bob > > > > -- > > 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 -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body