RB0 + Internal Pullup + Interrupt


Results 1 to 21 of 21

Threaded View

  1. #7
    Join Date
    May 2008
    Posts
    46


    Did you find this post helpful? Yes | No

    Default

    Here's the code

    Code:
    @     device pic16F628A, hs_osc, wdt_off, pwrt_on, lvp_off, protect_off, bod_on, cpd_off, pwrt_off, mclr_off
    
    DEFINE OSC 20
    DEFINE PULSIN_MAX 3000
    
    CMCON        = 7
    VRCON        = 0
    
    ALL_DIGITAL
    
    ' Alias our pins           
    BEEP           VAR PORTA.0 ' Buzzer
    RFIN           VAR PORTA.1 ' Remote input
    PROXIMITY_DOWN VAR PORTA.2 ' Proximity sensor for DOWN travel
    RELAY1         VAR PORTB.4 ' Relay 1 output
    RELAY2         VAR PORTB.5 ' Relay 2 output
    BUTTONA        VAR PORTB.6 ' Primary panel button input
    BUTTONB        VAR PORTB.7 ' Secondary panel button input
    
    ' Define some aliases for USART
    OERR           VAR RCSTA.1 ' Over-run bit
    CREN           VAR RCSTA.4 ' Continuous receive enable bit
    RCIF           VAR PIR1.5  ' Received character interrupt flag bit
    
    DEFINE HSER_RCSTA 90h      ' Receive register to receiver enabled
    DEFINE HSER_TXSTA 20h      ' Transmit register to transmitter enabled
    DEFINE HSER_BAUD 9600      ' Set baud rate
    
    ' Set the pin directions
    INPUT RFIN
    INPUT BUTTONA
    INPUT BUTTONB
    
    OUTPUT BEEP
    OUTPUT RELAY1
    OUTPUT RELAY2
    OUTPUT PROXIMITY_DOWN
    
    ' Configure the output pins as low
    LOW BEEP
    LOW RELAY1
    LOW RELAY2
    LOW PROXIMITY_DOWN
    
    ' Turn on weak pull-ups
    OPTION_REG.7 = 0
    ' We want to interrupt on the falling edge  
    OPTION_REG.6 = 0
    
    ' Set us up some variables
    PBITS        VAR BYTE[24]  ' The bits we've read
    RAW          VAR WORD      ' The raw pulsin value
    ADDRESS      VAR WORD      ' Storage for the address
    DBYTE        VAR BYTE      ' Storage for the data
    LOOPADDRESS  VAR WORD      ' Second loop storage of first loop addres
    LOOPDBYTE    VAR BYTE      ' Second loop storage of first loop data byte
    X            VAR BYTE      ' Loop/TMP var
    Y            VAR BYTE      ' Loop/TMP var
    STATE        VAR BYTE      ' Current state of the device
    serialbyte   VAR BYTE
    
    ' Default state
    STATE = STATE_STOPPED
    
    ON INTERRUPT GOTO sighup
    INTCON = %11010000
    PIE1   = %00100000
    
    ' Reset all the primary RF variables
    TOP:
        ADDRESS = 0 : RAW = 0 : DBYTE = 0
    
    MAIN:    
        ' Set both buttons for input
        INPUT BUTTONA
        INPUT BUTTONB
        ' Check to see if the individual buttons are toggled
        IF NOT BUTTONA THEN GOSUB subDown
        IF NOT BUTTONB Then GOSUB subUp
        ' Fiddle with the inputs to see if the middle button is pushed
        LOW BUTTONB
        X = BUTTONA ' If it's LOW then there's a good chance the middle button is down
        HIGH BUTTONB
        INPUT BUTTONB
        LOW BUTTONA  
        Y = BUTTONB ' If it's LOW then there's a good chance the middle button is down
        HIGH BUTTONA
        INPUT BUTTONB
        INPUT BUTTONA
        IF NOT Y AND NOT X THEN GOSUB subStop ' Both register as LOW then the middle button is down!
    
        ' Look for one huge low pulse
        PULSIN RFIN, 0, RAW
        ' 2350 is about mean average
        IF RAW < 2340 OR RAW > 2360 THEN MAIN
    
        ' Read 16 address bits and 8 data bits    
        FOR X = 0 TO 23
            PULSIN RFIN, 0, RAW
            ' Check the pulse parameters
            if RAW < 30 OR RAW > 240 THEN MAIN
            PBITS[x] = NCD RAW
        NEXT X
        
        ' Gather the address
        ADDRESS = 65535 ' Maxmimum known ID
        FOR X = 0 to 15
            IF PBITS[x] > 7 THEN ADDRESS.0[X] = 0
        NEXT X
        
        ' Gather the data
        DBYTE = 255 ' Maximum known data value
        Y=0
        FOR X = 16 TO 23
            IF PBITS[X] > 7 THEN DBYTE.0[Y] = 0
            Y = Y + 1
        NEXT X
    
        ' If we've done a loop...
        IF ADDRESS == LOOPADDRESS AND DBYTE == LOOPDBYTE THEN
            SELECT CASE DBYTE
                CASE 3
                    GOSUB subUp
                CASE 12
                    GOSUB subStop
                CASE 192
                    GOSUB subDown
            END SELECT
            LOOPDBYTE = 0
            LOOPADDRESS = 0
        ELSE
            ' Start a loop
            LOOPDBYTE = DBYTE
            LOOPADDRESS = ADDRESS
        ENDIF
    
        GOTO TOP
    END
    
    DISABLE
    
    subUp:
        IF STATE != STATE_GOING_UP THEN
            HIGH BEEP             ' Start making noise
            LOW RELAY1            ' Make sure the down relay is off (Don't know what happens otherwise, don't want to know!)
            LOW PROXIMITY_DOWN    ' Turn off the down proximity sensor
            PAUSE BEEP_WAIT       ' Wait a bit
            LOW BEEP              ' STFU :)
            IF STATE.3 != 1 THEN PAUSE POST_BEEP_WAIT
            HIGH RELAY2           ' Turn the up relay on
            STATE = STATE_GOING_UP
            PAUSE WAIT_WAIT
        ENDIF
        RETURN
        
    subStop:
        IF STATE.3 != 1 THEN
            HIGH BEEP             ' Start making noise
            LOW RELAY1            ' Turn off the down relay
            LOW PROXIMITY_DOWN    ' Turn off the down proximity sensor
            LOW RELAY2            ' Turn off the up relay
            PAUSE BEEP_WAIT       ' Wait a bit
            STATE = STATE_STOPPED
            LOW BEEP              ' STFU :)
            PAUSE WAIT_WAIT
        ENDIF
        RETURN
    
    subDown:
        IF STATE != STATE_GOING_DOWN THEN
            HIGH BEEP             ' Start making noise
            LOW RELAY2            ' Make sure the up relay is off (Don't know what happens otherwise, don't want to know!)
            PAUSE BEEP_WAIT       ' Wait a bit
            LOW BEEP              ' STFU :)
            IF STATE.3 != 1 THEN PAUSE POST_BEEP_WAIT
            HIGH RELAY1           ' Turn the down relay on
            HIGH PROXIMITY_DOWN     ' Turn on the down proximity sensor
            STATE = STATE_GOING_DOWN
            PAUSE WAIT_WAIT
        ENDIF
        RETURN
    
    sighup:
        X = STATE
        IF INTCON.1 == 1 THEN
            GOSUB subStop
            INTCON.1 = 0
        ENDIF
        WHILE RCIF                                 ' While there are bytes to be read
            serialByte = RCREG                    ' Read a byte
            HSEROUT [1, serialByte] ' Tell the sender what byte was received
            IF serialByte = 117 THEN GOSUB subUp
            IF serialByte = 100 THEN GOSUB subDown
        WEND
        IF OERR = 1 THEN
            CREN = 0
            CREN = 1
        ENDIF
        RESUME
        
    ENABLE

    Specifically of interest I guess is this bit - the fiddling with buttons bit
    Code:
        ' Set both buttons for input
        INPUT BUTTONA
        INPUT BUTTONB
        ' Check to see if the individual buttons are toggled
        IF NOT BUTTONA THEN GOSUB subDown
        IF NOT BUTTONB Then GOSUB subUp
        ' Fiddle with the inputs to see if the middle button is pushed
        LOW BUTTONB
        X = BUTTONA ' If it's LOW then there's a good chance the middle button is down
        HIGH BUTTONB
        INPUT BUTTONB
        LOW BUTTONA  
        Y = BUTTONB ' If it's LOW then there's a good chance the middle button is down
        HIGH BUTTONA
        INPUT BUTTONB
        INPUT BUTTONA
        IF NOT Y AND NOT X THEN GOSUB subStop ' Both register as LOW then the middle button is down!

    And here is the basic schematic.




    I'm looking for any solution, hardware or software - I can't change the bit in the unchangeable box, that's the existing pcb that I'm upgrading...
    Last edited by Freman; - 9th August 2009 at 05:59.

Similar Threads

  1. Can't ID interrupt source with this IntHandler??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 3rd June 2009, 02:35
  2. RB0 interrupt problems
    By amindzo in forum General
    Replies: 1
    Last Post: - 26th August 2006, 11:52
  3. NEWBIE: Some basic questions using interrupts
    By JackPollack in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th March 2006, 02:59
  4. Interrupt Problem
    By Kamikaze47 in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 16th November 2005, 20:58
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07

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