Hello,

I'm looking to write a bit of code that will only execute when there's a change of state, either from high to low or vise versa of some of the inputs on my 16f877 and I just can't seem to wrap my head around on how to accomplish that.

What I'm doing is:
I'm using a LAB-XT Telephony Experimenter's Board to generate the high and low signals that will be sent to various inputs of my 16f877.

I've taken apart an X10 remote control and have soldered multiple wires to the circuit board and will use transistors to simulate button presses via the 16f877 to turn On/Off various transceivers and receivers modules located through-out my house. When the 877 is first turned on then it looks to see what the polarity of the voltages were before the power was turned off (saved in the eeprom) and jumps to a subroutine to restore the last known state then the program jumps to the mainloop to poll the inputs for any polarity changes that will be generated by the Telephony Experimenter's Board.


This is the code that I want to be able to execute in the mainloop but the way it is, it'll just keep GOSUBing and will be repeatedly turning either On or Off the X10 modules depending on the polarity. Anyone have any suggestions? Any help will be greatly appreciated.
Code:
' start of program...
mainloop:

'If request 1 changes states then poll the 2 If-Then's below
 IF A_Request_For_1 = To_Turn_On THEN GOSUB Turn_On_Relay_1
 IF A_Request_For_1 = To_Turn_Off THEN GOSUB Turn_Off_Relay_1
'ENDIF

'If request 2 changes states then poll the 2 If-Then's below
 IF A_Request_For_2 = To_Turn_On THEN GOSUB Turn_On_Relay_2
 IF A_Request_For_2 = To_Turn_Off THEN GOSUB Turn_Off_Relay_2
'ENDIF

'If request 3 changes states then poll the 2 If-Then's below
 IF A_Request_For_3 = To_Turn_On THEN GOSUB Turn_On_Relay_3
 IF A_Request_For_3 = To_Turn_Off THEN GOSUB Turn_Off_Relay_3
'ENDIF

' ect, ect.........

GOTO mainloop
Thanks
jessey