How do I detect a polarity change on an input?


Closed Thread
Results 1 to 22 of 22

Hybrid View

  1. #1
    jessey's Avatar
    jessey Guest

    Default How do I detect a polarity change on an input?

    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

  2. #2
    Join Date
    Sep 2004
    Location
    Mentor, Ohio
    Posts
    352


    Did you find this post helpful? Yes | No

    Smile

    Hi Jessey,

    Probably what you want to do is set up flags for each input and make them a bit in the PICs EEPROM. (By storing them in the PIC's EEPROM, you can restart your program pretty much where your left off.) For example, let's call your first switch Input0. We'll call the flag for this switch In0. The same setup would be used for the other switches. You can also set a flag for "new start" and "restart" and have a routine check for the status of this bit before continuing on with the program.

    When your system is first setup, In0 will be set to 0. When the switch condition = 0 and the flag = 0 nothing would be done as it was already setup as normal. Now your loop that checks the inputs would "scan" all the switches. When there is a difference between Input(x) and Flag(x) your IF..Then sequence would select the appropriate subroutine to go to. If the current loop value (either 1 or 0) is the same as the flag, then nothing really needs to be done. If there is a change the program would then handle it, change the status flag and resume the normal program.

    I do most of my programs like this and for me it works quite well.

    HTH,

    BobK

    I know there must be several ways to handle this type of programming but I like to keep my programs real easy to follow. Maybe someone else will jump in here during the day an enlighten you AND ME! When I get off of work this evening I see if I can send you a sample of code that I used.
    Last edited by BobK; - 13th January 2008 at 15:56. Reason: After thoughts!

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    This doesn't use EEPROM, but I think it does the original request.

    Code:
    ' start of program...
    
    LastRequest_1  VAR BYTE : LastRequest_1 = 255
    LastRequest_2  VAR BYTE : LastRequest_2 = 255
    LastRequest_3  VAR BYTE : LastRequest_3 = 255
    
    mainloop:
    
    'If request 1 changes states then poll the 2 If-Then's below
    if A_Request_For_1 <> LastRequest_1 then  
       LastRequest_1 = A_Request_For_1 
       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 <> LastRequest_2 then  
       LastRequest_2 = A_Request_For_2 
       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 <> LastRequest_3 then  
       LastRequest_3 = A_Request_For_3 
       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
    DT

  4. #4
    Join Date
    Sep 2004
    Location
    Mentor, Ohio
    Posts
    352


    Did you find this post helpful? Yes | No

    Smile

    Hi Jessey,
    Here are the code snippets I created to handle the inputs to my project. I used a 4051 with 8 inputs feeding into a window comparator. The two outputs of the window comparator gave me one signal for loop open condition and another for loop shorted condition. PortC pins 5,6, and 7 were the bcd out to select one of 8 inputs to be tested by the micro. First is tested for an open loop, then it tested for a shorted loop. If either condition was already handled then a bit flag is set when the condition is first handled and the bit flag gets reset when the reset of the condition is handled. I did this code 3 years ago and was my first big project so the coding is rather crude and I'm sure it can be done some other way but it did the job and has been working great in the field since so I guess that's all that matters.

    loop0op:
    portC.5 = 0: portC.6 = 0: portC.7 = 0 'set portC to look at 1st loop
    pause delay 'take a short break
    If portA.0 = 0 Then G0on 'if loop is open turn led on
    If portA.0 = 1 then G0off 'if loop is normal turn led off
    goto loop0sh 'go test for shorts

    loop0sh:
    If portA.1 = 0 then R0on 'if loop is shorted turn led on
    If portA.1 = 1 then R0off 'if loop is normal turn led off
    Goto loop1op 'goto next loop


    G0on:
    If Bit0 = 1 then loop0sh 'if already open goto next test
    Portb.0 = 1 'turn led on
    Bit0 = 1 'set flag that loop is open
    Goto loop0sh 'go test for shorts

    G0off:
    If Bit0 = 0 then loop0sh 'If loop is normal goto short test
    Portb.0 = 0 'turn led off
    Bit0 = 0 'reset flag
    Goto loop1op 'goto next test

    R0on:
    If Bit8 =1 then loop1op 'if already tripped goto next loop
    PortD.0 = 1 'turn led on
    B2 = 120 'set apt #
    gosub Sendata 'send apt # to main display board
    Pause 10
    Bit8 = 1 'set flag that we're tripped
    Goto loop1op 'goto next test

    R0off:
    PortD.0 = 0 'turn led off
    pause 10
    if bit8 = 0 then loop1op 'if flag already cleared then next loop
    B2 = 280 'restore code for 1st zone
    gosub Sendata 'send restore report to main board
    Bit8 = 0 'clear flag so we're back to normal
    Goto loop1op 'goto next test


    Hope this helps you!

    BobK
    Last edited by BobK; - 14th January 2008 at 01:51.

  5. #5
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default Looking good

    Thanks BobK and Darrel,

    I tried Darrels code snip and it works great to lite Led's and extinguish them when I phone my LabXT board and enter the correct DTMF's from my cell phone, now all I have to do is connect the wires from my X10 transmitter when I get off work tonight. Thanks a lot Darrel, I really appericate your input, could you please explain the logic as to how the <> works and why you set each byte variable to 255? I'd sure be interested to be able to understand how and why that works.

    Also thanks for your code snip BobK, I'll go over it in detail when I get home tonight. That's great guys....

    Thanks Again
    jessey

  6. #6


    Did you find this post helpful? Yes | No

    Default

    The <> just means the two values are not equal.

Similar Threads

  1. Sony LanC Program
    By l_gaminde in forum Code Examples
    Replies: 2
    Last Post: - 25th September 2009, 18:51
  2. Replies: 1
    Last Post: - 7th November 2007, 20:36
  3. Timing input pulses and re-outputting them
    By jamie_s in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 28th February 2007, 01:50
  4. Using LEDs as light sensors
    By skimask in forum Code Examples
    Replies: 3
    Last Post: - 30th December 2006, 22:19
  5. detect oscillating input
    By fester addams in forum General
    Replies: 1
    Last Post: - 22nd November 2004, 18:08

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts