I've been playing around with some very basic assembly code on the PIC16F84A.
I don't think my diagram here is important, as this is mostly about the code.
I've got a pull up resistor to 5v on each switch. When the switch is pressed it takes the input pin to 0.
Here's a piece of two variations of code that do the same thing:
METHOD A:
SW0TEST
BTFSC PORTA,0 ;test bit 0 in file PORTA, skip if clear
BTFSS PORTA,0 ;test bit 0 in file PORTA, skip if set (go to SW1 test)
GOTO SW0ACT ;IF SW0 IS PUSHED, DO THE ACTION FOR IT
SW1TEST
BTFSC PORTA,1 ;test bit 1 in file PORTA, skip if clear
GOTO BEGIN ;IF SWITCH 0 IS NOT PUSHED, GO BACK TO START
GOTO SW1ACT ;IF SWITCH 1 IS PUSHED, GO TO SWITCH 1 ACTION
--------------------------------------------------------
METHOD B:
BEGIN
COND0
MOVLW B'00000011' ;put that in W register
SUBWF PORTA ;SUBTRACT ABOVE FROM PORTA BITS
BTFSC STATUS,ZEROBIT ;DOES PORTA = 00000011?
GOTO BEGIN
SW0TEST
MOVLW B'00000010' ;put into W reg
SUBWF PORTA ;SUBTRACT ABOVE FROM PORTA BITS
BTFSC STATUS,ZEROBIT ;DOES PORTA = 00000010
GOTO SW0ACT ;GOT TO SW0 ACTION IF SW0 IS PRESSED
SW1TEST
MOVLW B'00000001' ;PUT THIS IN THE W REGISTER
SUBWF PORTA ;SUBTRACT ABOVE FROM PORTA BITS
BTFSS STATUS,ZEROBIT ;DOES PORTA = 00000001?
GOTO BEGIN ;IF ABOVE IS FALSE, GO BACK TO START
GOTO SW1ACT ;GO TO SW1 ACTION
-------------------------------------------
As I said, both pieces of code accomplish the same thing.
I just need one thing cleared up: If I only need to check one input at a time, should I just use method A (seems to be MUCH more simple)? Is method B more for testing multiple inputs at the same time? If I'm testing 5 different ports, the number of conditions that I'd have to write would grow quite large given all the combinations. Is there a better way, or is it just a matter of plugging away at getting all the combos in the code?
Thanks for the help!
EDIT:
SW0 = switch 0 on porta bit 0 pin
SW1 = switch 1 on porta bit 1 pin
In method B, "COND0" just makes the code keep looping back to the beginning until a button is pressed.
I don't think my diagram here is important, as this is mostly about the code.
I've got a pull up resistor to 5v on each switch. When the switch is pressed it takes the input pin to 0.
Here's a piece of two variations of code that do the same thing:
METHOD A:
SW0TEST
BTFSC PORTA,0 ;test bit 0 in file PORTA, skip if clear
BTFSS PORTA,0 ;test bit 0 in file PORTA, skip if set (go to SW1 test)
GOTO SW0ACT ;IF SW0 IS PUSHED, DO THE ACTION FOR IT
SW1TEST
BTFSC PORTA,1 ;test bit 1 in file PORTA, skip if clear
GOTO BEGIN ;IF SWITCH 0 IS NOT PUSHED, GO BACK TO START
GOTO SW1ACT ;IF SWITCH 1 IS PUSHED, GO TO SWITCH 1 ACTION
--------------------------------------------------------
METHOD B:
BEGIN
COND0
MOVLW B'00000011' ;put that in W register
SUBWF PORTA ;SUBTRACT ABOVE FROM PORTA BITS
BTFSC STATUS,ZEROBIT ;DOES PORTA = 00000011?
GOTO BEGIN
SW0TEST
MOVLW B'00000010' ;put into W reg
SUBWF PORTA ;SUBTRACT ABOVE FROM PORTA BITS
BTFSC STATUS,ZEROBIT ;DOES PORTA = 00000010
GOTO SW0ACT ;GOT TO SW0 ACTION IF SW0 IS PRESSED
SW1TEST
MOVLW B'00000001' ;PUT THIS IN THE W REGISTER
SUBWF PORTA ;SUBTRACT ABOVE FROM PORTA BITS
BTFSS STATUS,ZEROBIT ;DOES PORTA = 00000001?
GOTO BEGIN ;IF ABOVE IS FALSE, GO BACK TO START
GOTO SW1ACT ;GO TO SW1 ACTION
-------------------------------------------
As I said, both pieces of code accomplish the same thing.
I just need one thing cleared up: If I only need to check one input at a time, should I just use method A (seems to be MUCH more simple)? Is method B more for testing multiple inputs at the same time? If I'm testing 5 different ports, the number of conditions that I'd have to write would grow quite large given all the combinations. Is there a better way, or is it just a matter of plugging away at getting all the combos in the code?
Thanks for the help!
EDIT:
SW0 = switch 0 on porta bit 0 pin
SW1 = switch 1 on porta bit 1 pin
In method B, "COND0" just makes the code keep looping back to the beginning until a button is pressed.