Pin OnChange example

Discuss PIC and electronic related things

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
Coccoliso
Posts: 152
Joined: Mon Feb 17, 2014 10:34 am

Pin OnChange example

Post by Coccoliso » Sat Dec 10, 2016 11:30 am

Hi at all,

I'm looking for an example of code to generate an interrupt to the passage from state 1 to 0 state of a pin.
In Firewing an event simplifies everything but with SF compiler and the PIC18?
Maybe there's something in the examples installed but I can not find it.
Perhaps I had already asked but... I can not find the post!
Then I seem to remember that you can only do using port B .. or I am wrong.
HELP!

Jerry Messina
Swordfish Developer
Posts: 1237
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Pin OnChange example

Post by Jerry Messina » Sat Dec 10, 2016 12:38 pm

The 18F Interrupt On Change (IOC) feature varies depending on the device.

For many chips it's only pins on PORTB, and it only detects a change in state, but for some it's available on other ports and you can pick the edge which generates the intr.

There's also the INTx pins, of which there are usually a few (INT0, INT1, INT2), typically on PORTB as well. If you're looking for an edge they're usually easier to setup. They don't interrupt on any change... you pick rising or falling edge.

Any chip with a PPS module normally allows you to reassign the pin.

I'd recommend using an INTx pin if that's a choice. Are you currently using any particular device/pin?

User avatar
Coccoliso
Posts: 152
Joined: Mon Feb 17, 2014 10:34 am

Re: Pin OnChange example

Post by Coccoliso » Sat Dec 10, 2016 1:31 pm

Hi Jerry,

I use a 18F2682 or 18F2685 and for now can use any pin.. I have not yet drawn the circuit, however, I use two port B pin for the CAN.
Free pins are PB0 (INT0) , PB1 (INT1) and PB4,PB5 ( not used for PGM signal)
OK for test only an high to low transition (falling edge).
You have something already written or part of the existing libraries where peek about?

Thanks.

Jerry Messina
Swordfish Developer
Posts: 1237
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Pin OnChange example

Post by Jerry Messina » Sat Dec 10, 2016 2:59 pm

There's probably an existing example somewhere, but maybe something like this will get you started.

Code: Select all

// falling edge INT0 example
//
// INTx Pin Interrupts
// -------------------
// External interrupts on the RB0/INT0, RB1/INT1 and RB2/INT2 pins are edge-triggered. 
// If the corresponding INTEDGx bit in the INTCON2 register is set the interrupt is triggered by a rising edge;
// if the bit is clear the trigger is on the falling edge. 
//
// When a valid edge appears on the RBx/INTx pin, the corresponding flag bit, INTxIF, is set. 
// This interrupt can be disabled by clearing the corresponding enable bit INTxIE. 
// Flag bit INTxIF must be cleared in software in the Interrupt Service Routine before re-enabling the interrupt. 
//
// Interrupt priority for INT1 and INT2 is determined by the value contained in the interrupt priority bits, INT1IP
// (INTCON3<6>) and INT2IP (INTCON3<7>). There is no priority bit associated with INT0. It is always a high
// priority interrupt source.
//

device = 18F2682

// INT0/RB0 definitions
dim INT0IE as INTCON.bits(4),
    INT0IF as INTCON.bits(1),
    INTEDG0 as INTCON2.bits(6)

// INTEDG0 settings
const 
    RISING_EDGE = 1,
    FALLING_EDGE = 0
    
// INT0 is always ipHigh
interrupt isr_high()
    if (INT0IE = 1) and (INT0IF = 1) then
        // got edge interrupt...
        // <user-code goes here>
        
        // clear intr flag
        INT0IF = 0
    endif
end interrupt


main:
    // set pin to input
    input(PORTB.0)

    // set intr edge
    INTEDG0 = FALLING_EDGE

    // init flags and enable
    INT0IF = 0
    INT0IE = 1
    
    enable(isr_high)
    
    while(true)
    end while

User avatar
Coccoliso
Posts: 152
Joined: Mon Feb 17, 2014 10:34 am

Re: Pin OnChange example

Post by Coccoliso » Sat Dec 10, 2016 5:13 pm

Thanks so much Jerry :wink:
Always fast and accurate !

Post Reply