16F872 and interrupt handling


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    44

    Question 16F872 and interrupt handling

    Hi there,
    i wrote my first program and i donīt understand, how to define the interrupts.
    I īve set RB7 and RB6 as inputs and theyīre normally high "1".
    If one of these inputs change the state, then i need to go to the interrupt routine. Can somebody help me how to define interrupts in PicBasic and which options needed for my example ?

    Thanks a lot
    Robson


    ADCON1 = %111
    DEFINE OSC 4

    TRISA = %00110000
    TRISB = %11000000 ' RB7 & RB6 as inputs
    TRISC = %00000000

    ENABLE
    ON INTERRUPT GOTO myint
    INTCON = ??? ' Donīt know what comes here
    OPTION_REG = ' ???
    main:
    ...
    ...
    GOTO main

    DISABLE
    myint:
    LED = 1
    RESUME
    ENABLE

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


    Did you find this post helpful? Yes | No

    Default

    Hi Robson,

    This might fill in a few of the blanks...
    Code:
    ADCON1 = %111
    DEFINE OSC 4
    
    LED1     VAR PORTB.0
    LED2     VAR PORTB.1
    RBCpins  VAR BYTE
    LastPins VAR BYTE
    
    TRISA = %00110000
    TRISB = %11000000 ' RB7 & RB6 as inputs
    TRISC = %00000000
    
    
    OPTION_REG.7 = 0       ; If you want Internal PORTB Pull-ups
    PAUSE 10               ; Let weak pull-ups stabilize
    LastPins = PORTB       ; save current pin state, 
                           ; and end any mismatch conditions
    
    INTCON.0 = 0           ; Clear RB Port change Int Flag
    INTCON.3 = 1           ; Enable RB Port Change Interrupts
                       
    ON INTERRUPT GOTO myint
    
    main:
    '...
    '...
    GOTO main
    
    DISABLE
    myint:
        RBCpins = PORTB                  ; read PORTB and end the mismatch
        INTCON.0 = 0                     ;  reset RB Port change INT flag
        if RBCpins.7 != LastPins.7 then  ; Pin 7 changed
            LastPins.7 = RBCpins.7
            Toggle LED1
        endif
        
        if RBCpins.6 != LastPins.6 then  ; Pin 6 changed
            LastPins.6 = RBCpins.6
            Toggle LED2
        endif
    RESUME
    ENABLE
    DT

  3. #3
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    Thanks Daylor.
    I`ll try to insert your program into my.

  4. #4
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    Hello Darrel,

    now the interrupt works fine, but after interrupt the pic hangs. I need to reset it. Can you tell me where is the failure ?

    code:
    ADCON1 = %111

    define OSC 4

    TRISA = %00111000 ' RA0-2 OUT, RA3-5 INPUT
    TRISB = %11000000 ' OUTPUT
    TRISC = %00000000 ' OUTPUT

    col1 var PORTA.0
    col2 var PORTA.1
    col3 var PORTA.2

    row1 var PORTA.3
    row2 var PORTA.4
    row3 var PORTA.5

    id1 var PORTB.7 ' Input1 Rotary
    id2 var PORTB.6 ' Input2 Rotary

    click var PORTB.5
    tp var PORTB.4
    tm var PORTB.3

    vm var PORTC.0
    vp var PORTC.1
    unt var PORTC.2

    srcl var PORTC.3
    srcr var PORTC.4

    dp4 var PORTC.5
    dp3 var PORTC.6
    dp2 var PORTC.7

    dpo1 var PORTB.2 ' Simulation OUT 1
    dpo2 var PORTB.1 ' Simulation OUT 2
    summer var PORTB.0 ' Beep active

    lp var word
    drehpos var byte
    savepos var byte
    cnt var byte
    ums var bit
    delay var word
    startclk var word

    RBCPins var Byte
    LastPins var Byte

    Lastpins = PORTB

    INTCON.0 = 0
    INTCON.3 = 1

    on interrupt goto myint


    main:
    ...
    GOTO main

    switchd:
    dpo2 = 0
    for lp = 0 to 20
    pause 1 ' Time for Simulation 20ms
    next lp
    dpo1 = 0
    for lp = 0 to 40
    pause 1 ' 40ms Pause
    next lp
    dpo2 = 1 : dpo1 = 1
    return

    switchh:
    dpo1 = 0
    for lp = 0 to 20
    pause 1 ' Time for Simulation 20ms
    next lp
    dpo2 = 0
    for lp = 0 to 40
    pause 1 ' 40ms Pause
    next lp
    dpo1 = 1 : dpo2 = 1
    return



    DISABLE
    myint:
    RBCpins = PORTB
    INTCON.0 = 0
    if RBCpins.7 <> Lastpins.7 then
    Lastpins.7 = rbcpins.7
    gosub switchh
    endif

    if RBCpins.6 <> Lastpins.6 then
    lastpins.6 = rbcpins.6
    gosub switchd
    resume
    endif
    enable
    Last edited by Robson; - 27th June 2007 at 11:16.

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


    Did you find this post helpful? Yes | No

    Default

    Somehow, the RESUME got moved inside the IF statement.
    The only way it will resume is if PORTB.6 changes.

    Otherwise it falls off into never never land.
    <br>
    DT

  6. #6
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    Ok Darrel iīve found the error.
    I changed the program like this and itīs working perfect.

    DISABLE
    myint:
    RBCpins = PORTB
    INTCON.0 = 0

    if rbcpins.7 <> Lastpins.7 then
    dpo1 = 0
    for lp = 0 to 20
    pause 1
    next lp
    dpo2 = 0
    for lp = 0 to 40
    pause 1
    next lp
    dpo1 = 1
    dpo2 = dpo1
    endif

    if rbcpins.6 <> lastpins.6 then
    dpo2 = 0
    for lp = 0 to 20
    pause 1
    next lp
    dpo1 = 0
    for lp = 0 to 40
    pause 1
    next lp
    dpo2 = 1
    dpo1 = dpo2
    endif
    RESUME
    ENABLE

    Maybe the error comes from the subroutine. I include the subroutine directly and it works fine.

    Thanks a lot Darrel

    PS: How you insert a code in a window like you posted it before ?

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


    Did you find this post helpful? Yes | No

    Default

    Using [code][/code] tags puts it in a window.

    At the bottom of the page, there's a box that says "Posting Rules". Inside that there's a link to vB code. Check it out. You can do all kinds of formatting.

    hth,
    DT

  8. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Darrel and ON INTERRUPT in the same thread ???
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

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


    Did you find this post helpful? Yes | No

    Default

    Well, I figure you can't appreciate "Instant Interrupts" until you've had to deal with ON INTERRUPT a few times.

    Got to get them started.
    <br>
    DT

  10. #10
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    I wish Melabs would be willing to replace ON INTERRUPT with DT-INT... man you have to talk to them

    Another compiler use something around that... you know which one
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  11. #11
    Join Date
    Jul 2007
    Posts
    74


    Did you find this post helpful? Yes | No

    Default

    is it possible to use a momentary switch (operating on 5v) as a digital input? so I can use interrupts.

Similar Threads

  1. @Darrel Taylor Interrupt ;-)
    By Robson in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 31st August 2007, 00:59

Members who have read this thread : 1

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