Log in

View Full Version : instant interrupts with comparator



mikendee
- 14th September 2009, 13:41
I have been trying to get instant interrupts working, using the comparator interrupt with a 16f88. In the data sheet it says:
"You may be getting stuck in an infinite loop with the comparator interrupt service routine if you
did not follow the proper sequence to clear the CMIF flag bit. First you must read the CMCON
register, and then you can clear the CMIF flag bit.

I used the following code ( after alot of trial and error) and it seems to work


@__CONFIG _CONFIG1, _INTRC_IO & _LVP_OFF & _BODEN_OFF
DEFINE OSC 8
LED1 VAR PORTB.1
VAR1 VAR BYTE
INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler CMP_INT, _ToggleLED1, PBP, no
endm
INT_CREATE ; Creates the interrupt processor
INT_ENABLE CMP_INT ; enable Comparator interrupts

ENDASM

OSCCON = %01111000
ANSEL = %00000111
CMCON = %00000010
CVRCON = %11001000

TRISA = %00000011
TRISB = 0

PORTA = 0
PORTB = 0
Main:
PAUSE 1
GOTO Main

'---[TMR1 - interrupt handler]--------------------------------------------------
ToggleLED1:
VAR1 = CMCON
PIR2.6 = 0
TOGGLE LED1
@ INT_RETURN


It works if I put no in the ResetFlags column and in the interrupt handler read CMCON, then clear PIR2.6, which is the flag.

I would like to know if this is the correct way to do this or if there is a better way.
Thanks, Mike

Darrel Taylor
- 14th September 2009, 19:51
Sure, that will work.

You could also set the ResetFlag to YES, and just read CMCON in the handler.

Typically, the handler would read CMCON anyway by testing the state of either the C1OUT or C2OUT bits so it can respond to the current state of the comparators.

Right now you're just toggling an LED, so you have to read CMCON separately to end the mismatch condition.

Once you actually do something in the comparator handler, you probably won't need the VAR1 = CMCON statement anymore.
<br>

mikendee
- 15th September 2009, 12:00
Thanks for the reply Darrel and thanks for giving us Instant Interrupt. It lets a hack like me get alot more out of a PIC than just blinking an led.

I am using the comparator to read the output of a hall effect sensor on a flow sensor and it works a treat.

Thanks,
Mike

Darrel Taylor
- 16th September 2009, 05:54
Excellent, another happy customer ...
Don't forget to tip the waitresses.

If you can find one ... have you seen one lately?
My beer's empty :D