Using an interrupt for an input


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425

    Default Using an interrupt for an input

    I'm using DT's interrupt code to blink some LED's at a constant rate and it works awesome. I'd like to use this code to check some hardware and software inputs but I can't get it to work.

    First question - If I'm using DT's interrupt code to check software or hardware inputs, can I use timers zero, one, two and three? (18F4550)

    Second question - The code I'm using on the interrupts is:
    Code:
    '---[INT - interrupt handler]---------------------------------------------------
    Toggleinput1:
         if PORTB.5=0 THEN
            TOGGLE LED_RX
            GOTO RECEIVING_DATA
        ENDIF
    @ INT_RETURN
    Is this the correct way to check an input?

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: Using an interrupt for an input

    Hi Christopher,
    Not sure I understand the first question. DT-Ints doesn't use any of the timers by it self so they are free for you to use which way you see fit.

    The 18F4550 has three external interrupt pins (not counting IOC) and these are PortB.0-PortB.2. Only these three pins can trip an interrupt. So if what you're really trying to do is to trip an interrupt on PortB.5 it will never work. (Actually PortB.5 has the IOC features so it CAN work but it doesn't appear like that's what you're doing).

    Without seeing the rest of your code, with the interrupt declarations ets it looks to me like you're triggering an interrupt on INT0 (PortB.0). When it fires it checks PortB.5 and if it's logoc low you're jumping to RECEIVING_DATA. Using a GOTO to jump out of an interrupt service routine is generally a VERY bad idea because the program execution will then not reach the @ INT_RETURN which is a very bad thing indeed.

    One more thing. IF you're doing this in an effort to receive RS232 data using SERIN or SERIN2 it won't work. I won't elaborate more on that in case that's not what you're doing.

    /Henrik.

  3. #3
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: Using an interrupt for an input

    Hi Henrik,

    I have a mainloop that is executing analog measurements and other "stuff." When the interrupt pin goes low, I want to stop doing whatever my mainloop is checking (non-critical stuff) and goto the receive section for the CAN network. It can be a gosub but I don't know if this would cause the same problem you are describing with the goto. It seems like such a simple concept (if this then that) but I've tried using interrupts before and haven't had any luck. DT's interrupt code made it simple and painless and I was hoping to use that to check hardware or software inputs.

    Putting it more simply, when the interrupt pin goes low, I want the 18F4550 to handle the CAN data and if there's no CAN data then stay in the mainloop.

    I'm not even sure I need an interrupt because the CAN data is so quick that the weak link seems to be the LCD screen. In other words, the LCD screen seems to take a little longer than the CAN data so as soon as one write cycle is finished there's a ton of CAN data to handle so I don't think the 18F4550 is ever waiting for data. But I wanted to try it to see if there's a difference.

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: Using an interrupt for an input

    Hi,
    Interrupts are hardware events, you can't just use any pin you like. You must use a pin which is capable of generating an interrupt. On the 18F4550 these are PortB.0 (INT0), PortB.1 (INT1) and PortB.2 (INT2).

    (PortB.4-7 are also capable of generating interrupts (interrupt on change) but they are trickier to use than the "normal" INT pins).

    In the INTCON2 register you'll find bits which controlls if the interrupt should be triggered off a rising or falling edge, set that up to your needs. You then declare your interrupt as shown in every example of DT-Ints
    Code:
    INTCON2.6 = 0   'Interrupt on falling edge of INT0
    INTCON2.5 = 1   'Interrupt on rising edge of INT1
    
    ' Here two interrupts are declared. One for INT0 (PortB.0) and one for INT1 (PortB.1). 
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler    INT0_INT,  _RcvCAN,   PBP,  yes
            INT_Handler    INT1_INT,  _DoSomething, PBP, yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    
    Main:
      Pause 100
      Toggle LED
    Goto Main
    
    RcvCAN:
    ' Put your code here or GOSUB it but make SURE you have a RETURN at the end of the routine so you come back here, otherwise you'll crash.
    @ INT_RETURN
    
    DoSomethingElse:
    Toggle AnotherLED
    @ INT_RETURN
    Well, something like that.

    /Henrik.

  5. #5
    Join Date
    Dec 2012
    Location
    Tennessee
    Posts
    262


    Did you find this post helpful? Yes | No

    Default Re: Using an interrupt for an input

    Henrik, that looks great. but tell me on first 2 lines

    INTCON2.6 = 0 'Interrupt on falling edge of INT0
    INTCON2.5 = 1 'Interrupt on rising edge of INT1

    why are these different? do you have to alternate?

  6. #6
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Using an interrupt for an input

    Falling edge vs rising edge. Do you want it to activate when you push the button or when you release it?
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  7. #7
    Join Date
    Dec 2012
    Location
    Tennessee
    Posts
    262


    Did you find this post helpful? Yes | No

    Default Re: Using an interrupt for an input

    Dang I think i just made a mistake the other day, i was messing with a 4x4 matrix keypad and i was using the special 922 chip to interface it to the PIC, but i was getting mad because it triggered the INT but it looks like I may have had it set up on the falling edge? when i pressed a keypad button nothing happened, but when I released it , the led to detect a interupt firing flashed but no data read because now no key is pressed, i was going to custom build a pic to interface with that keypad, but now i need to rebuild circuit and see if i can chage the INT edge. feel frusterated. Alot of people have gotten away from using the 922 chips and many manufacturers have stopped making them, but it makes keypads very simple. still learning here and at least seeing the code for leading/falling edge triggered the memory. ive got several keypads and those 922 chips that i would love to use if i can get them to work.
    thanks

Similar Threads

  1. Interrupt on change for IR input - is it a good way to go
    By longpole001 in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 30th July 2012, 10:05
  2. PIC Interrupt and PORTA input
    By Jerade in forum mel PIC BASIC
    Replies: 5
    Last Post: - 29th November 2009, 17:24
  3. Using interrupt as an input
    By kduck63 in forum General
    Replies: 1
    Last Post: - 22nd June 2009, 17:14
  4. Interrupt driven Buffered Serial Input
    By johnmiller in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 13th May 2007, 01:42
  5. realise second RX-interrupt input
    By mischl in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 4th February 2006, 03:42

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