PDA

View Full Version : Using an interrupt for an input



Christopher4187
- 4th January 2013, 11:58
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:

'---[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?

HenrikOlsson
- 4th January 2013, 12:39
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.

Christopher4187
- 4th January 2013, 13:59
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.

HenrikOlsson
- 4th January 2013, 14:37
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

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.

wdmagic
- 5th January 2013, 00:27
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?

Archangel
- 5th January 2013, 00:50
Falling edge vs rising edge. Do you want it to activate when you push the button or when you release it?

wdmagic
- 5th January 2013, 01:02
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