PDA

View Full Version : Can't ID interrupt source with this IntHandler??



jellis00
- 31st May 2009, 02:27
I have attached my code below. My application requires sensing an external interrupt from a contact limit switch closure which I am sensing as an On-change interrupt at the RA3 pin of my 16F690. I am also counting pulses from an external interface to a Hall-effect sensor using a TMR0 interrupt when it overflows. I think I have all the register setting correct, but in my attempts during testing to isolate which interrupt occured, I have to If-Then blocks in the code to test the interrupt flags to determine which one occured. I know that neither of these IF-THEN blocks are being triggered in this current code, because of the specific placement of different write statements in the code. The only Write statement that is actually being triggered is the Write 7, low_bat statement right before the first IF-THEN block...none of the others are writing anything which confirms the IF-THEN blocks are not being triggered. Yet when I activate the RA3 switch closure the code is actually triggering the Interrupt Handler.
Can anyone tell me why my code is not triggering the first IF-THEN block to detect the RA3 On-change interrupt?



' This Program uses the PIC16F690 and simulates control of a solenoid operated
' water valve and measures the volume of water flowing through a flow meter.
' This program places the MCU in sleep mode most of the time until it receives
' an interrupt from the activation of the flush handle that closes a
' contact limit switch that grounds an input pin of the microcontroller as an
' interrupt. I/O pin connections to the PIC16F690 MCU are as follows:
'
' RA0...is connected as ICSPDAT when used for ICSP and also to a low battery
' monitor R/C circuit.
' RA1...is connected as ICSPCLK when used for ICSP.
' RA2...is connected to the external Flush contact limit switch as TMR0 input.
' During testing it is connected to a momentary switch on the PICkit2
' board as a clock input for TMR0 counting of pulses as interrupt.
' RA3...During testing it is connected to a momentary switch on the PICkit2
' board as a manually simulated On_interrupt from the FLUSH switch.
' RA4...is connected to pin 6 of the ICSP header for use during programming.
' RC0...is connected as an output to an LED and is on when the valve
' is open.
' RC1...is connected as an output to an LED and is flashed when battery is low
' RC3...is connected as an output to the latching solenoid that opens or
' closes the valve. During program testing with the PICkit2 demo
' board it is connected to an LED to show when the pulse is sent.
' RC4...is connected to pin PDN on the TRM-XXX-LT transceiver.
' RC5...is connected to pin DATA on the TRM-XXX-LT transceiver.
' RC6...is connected to pin RSSI on the TRM-XXX-LT transceiver.
' RB4...is connected via a 10 ohm resistor to provide power to Wireless option.
' RB5...is connected to Hall Effect sensor circuit #2.
' RB6...is connected to Hall Effect sensor circuit #1.
' RB7...is connected to pin R/T SEL on the TRM-XXX-LT transceiver.
' the PICkit2 board momentary switch to simulate Meter pulse inputs.
' -----[ Device Declaration ]----------------------------------------------
'
@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
'@device pic16F690, intrc_osc_noclkout, BOD_OFF, PWRT_OFF, wdt_off,mclr_off,
' protect_off ' For PM assembler only

' -----[ Revision History ]------------------------------------------------
'
'

' -----[ Variables & Aliases Intitialization ]-----------------------------
'
led1 VAR PORTC.0 ' Set RC0 as LED indicator of valve open..water flowing
led2 VAR PORTC.1 ' Set RC1 as LED indicator of low battery
valve VAR PORTC.3 ' Set RC3 as valve solenoid open/close command
low_bat VAR PortA.0 ' Set RA0 as battery low power monitor
meter VAR PORTA.2 ' Set RA2 as input for Hall Sensor meter pulse Interrupt
flush VAR PORTA.3 ' Set RA3 as input for sensing flush switch closure Int
bat_mon VAR Byte ' bat_mon value when Vtrh multiplied by low_bat
dummy VAR Byte ' For read of on-interrupt port & clear mismatch condition


' -----[ Constants Initialization ]----------------------------------------
'
k CON 10 ' Calibration factor for flow meter...# pulses per gal
i VAR Byte ' Index used in Gallon counter loop
Vthr CON 4 ' Low Battery Monitor threshold

' -----[ Initialization ]--------------------------------------------------
'
'EEPROM PRESETS
'DATA @0,0

'OSC DEFINE
'DEFINE OSC 4

' Setup Timer0 as an 8-bit counter with the clock input on RA2.
' 1:1 TMR0 prescaler
' TMR0 counts on high-to-low transitions
OPTION_REG = %0111000 ' PORTA pullups disabled, TMR0 clock source is RA2,
' high-to-low transitions, prescaler to TMR0

'Register Settings
Init:
PORTC = 0 ' LEDs off, PULSOUT RC3 provides a high-going pulse
PORTA = 0
TRISA = %11111111 ' Set all PORTA pins to inputs...RA0, RA2 & RA3 are used
TRISB = %00000000 ' Set all PORTB and PORTC pins to outputs
TRISC = %00000000
PORTC = %00000000 ' Pre-set PORTC pins low, turning LEDs off
TMR0 = 256-K ' Preload TMR0 to overflow after k counts

'A/D & Comparators disabled
ADCON0 = %01110000 ' Set PORTA to digital I/O & FRC (clock derived from a
ADCON1 = %00000000 ' dedicated internal oscillator)
ANSEL=0 ' Set PortA to digital I/O
ANSELH=0 ' Analog module disabled
CM1CON0=0
CM2CON0=0

' Initialization of inputs/outputs
TRISC.3 = 0 ' Set RC3 as output port for valve latching solenoid control
Low Valve ' Initialize RC3 (valve) at Low value
TRISA.2 = 1 ' Set RA2 as input port for clock to TMR0
TRISA.3 = 1 ' Set RA3 as input port for sensing METER switch closure
High meter ' Initialize RA2 (meter) at High value for METER pulse inputs
' RA2 = TMR0 clock input for simulated meter pulse inputs
High flush ' Initialize RA3 (flush) at High value for flush interrupt

'Interrupts Settings
FLUSH_INT_FLAG VAR INTCON.0 ' RA3 (FLUSH) On-change-interrupt flag bit
TMR0_INT_FLAG VAR INTCON.2 ' Timer0 overflow flag bit
INTCON = %10101000 ' Enable global, TMR0 & RABIE (RA3 on-change-INT)
IOCA = %00001000 ' Enable RA3 as on-change-INT

' -----[ Main Code ]-------------------------------------------------------
'Set INT Handler
ON INTERRUPT GOTO Int_handler

'Start...normal code here
MAIN:
PORTC = %00000000 ' Turn all LEDs off
SLEEP 30 ' Put MC in Sleep state at power up for 30 secs
' Microcontroller is in Sleep State waiting for external FLUSH Interrupt
' Valve should be closed at this point and no water flowing

' Put code here to test if flow meter has stopped as an indication that
' no water is flowing...is a double check on valve closure.
GOTO Main ' Loop to Main to wait for next Flush interrupt on RA3 change

DISABLE
Int_handler:
PULSOUT valve,1000 ' Generate required 10 msec pulse to RC3 to open valve
High led1 ' Light indicator that valve is open & water flowing
Low low_bat ' Simulate battery monitor input is low
DEFINE WRITE_INT 1
Write 7, low_bat ' Remove comment for test only
IF FLUSH_INT_FLAG = 1 Then ' Interrupt was from RA3 on change
write 9, flush ' Write RA3 (FLUSH) value during testing

' Put code here to start a timer to run for 50 secs as a fail safe
' to prevent overflow of toilet tank in case of sensor failure.
'Goto Shutvalve

dummy = flush ' Clear mismatch condition
FLUSH_INT_FLAG = 0 ' Clear interrupt flag & enable
'INTCON = %10100000 ' global & TMR0 interrupts..preclude RA3 interrupt
Endif
IF TMR0_INT_FLAG = 1 Then ' Interrupt was a TMR0 overflow..volume reached
PULSOUT valve,2000 ' Generate required 20 msec pulse to RC3..close valve
WRITE 5, TMR0 ' Write TMR0 value..remove comment for test only
bat_mon = low_bat * Vthr
Write 13, bat_mon ' Remove comment for test only
If bat_mon < Vthr Then
'Light the low battery monitor light
HIGH led2
ENDIF
dummy = TMR0 ' Clear mismatch condition
TMR0 = 256-k ' Reload TMR0 to overflow after k counts
TMR0_INT_FLAG = 0 ' Clear overflow flag and enable
'INTCON = %10001000 ' global & RABIE (RA3 on-change-INT)
' ..preclude TMRO overflow interrupt
ENDIF
LOW led1
Low led2
RESUME
ENABLE

' If the user program ends by getting to the last statement of the program
' at an END instruction, the MCU will SLEEP and await a wakeup.
END

Archangel
- 31st May 2009, 03:50
on interrupt goto . . . the devil !
On interrupt is a junk imitation of a real interrupt. It places a flag on the stack when the port change interrupt occurs and continues doing what it was doing . . .until it finishes what it is doing. You have set WDT to off and what is to awaken the PIC from sleep ? I would set WDT to ON and probably set up and use Darrel's instant interrupts. Then you have assembly interrupts at PBP control. On interrupt works like a gosub.

jellis00
- 1st June 2009, 17:46
Thanks, Joe. Am going to study the instant interrupts to see how to use them.
I did finally get the OnInterrupt version working so that the Int_handler would determine which type of interrupt occurred and service them accordingly. Only thing I haven't yet got working is the SLEEP mode, even after turning WDT on as you pointed out. Intention is to put the MCU into SLEEP mode for 45 secs on power up while waiting for the first interrupt from the Flush switch. However, when I do this, the Flush switch interrupt works, but the RA3 OnChange interrupt doesn't work. Can't figure out why. If I use a PAUSE 1 statement instead of SLEEP 45 everything works correctly. Any ideas on how to solve this?
I am posting all of my current code below in case anyone wants to see how the overall interrupt structure does work for use with both types of interrupts.
------------------


*
' -----[ Program Description ]---------------------------------------------*
' This Program is for a PIC16F690 installed in a PICkit2 Starter Kit *
' Purpose: *
' 1) Monitor an input for an external Interrupt from flush *
' 2) On Interrupt, command latching solenoid to open valve *
' 3) Measure flow meter output to determine when 1.6 *
' gallons have passed *
' 4) On 1.6 gallons, command latching solenoid to close *
' 5) Put Microcontroller back in Sleep State to await next *
' Flush Interrupt *
' *
' This Program uses the PIC16F690 and simulates control of a solenoid operated
' water valve and measures the volume of water flowing through a flow meter.
' A normally open, momentary push button switch is installed on the PICkit2
' LPDM board between pin RA2 and ground to simulate the flow meter pulse input.
' This program places the MCU in sleep mode most of the time until it receives
' an interrupt from the activation of the flush handle that closes a
' contact limit switch that grounds an input pin of the microcontroller as an
' interrupt. I/O pin connections to the PIC16F690 MCU are as follows:
'
' RA0...is connected as ICSPDAT when used for ICSP and also to a low battery
' monitor R/C circuit for normal ops.
' RA1...is connected as ICSPCLK when used for ICSP.
' RA2...is connected to the external Flush contact limit switch as TMR0 input.
' During testing it is connected to a momentary switch on the PICkit2
' board as a clock input for TMR0 counting of pulses as interrupt.
' RA3...During testing it is connected to a momentary switch on the PICkit2
' board as a manually simulated On_interrupt from the FLUSH switch.
' RA4...is connected to pin 6 of the ICSP header for use during programming.
' RC0...is connected as an output to an LED and is on when the valve
' is open.
' RC1...is connected as an output to an LED and is flashed when battery is low
' RC3...is connected as an output to the latching solenoid that opens or
' closes the valve. During program testing with the PICkit2 demo
' board it is connected to an LED to show when the pulse is sent.
' RC4...is connected to pin PDN on the TRM-XXX-LT transceiver.
' RC5...is connected to pin DATA on the TRM-XXX-LT transceiver.
' RC6...is connected to pin RSSI on the TRM-XXX-LT transceiver.
' RB4...is connected via a 10 ohm resistor to provide power to Wireless option.
' RB5...is connected to Hall Effect sensor circuit #2.
' RB6...is connected to Hall Effect sensor circuit #1.
' RB7...is connected to pin R/T SEL on the TRM-XXX-LT transceiver.
' the PICkit2 board momentary switch to simulate Meter pulse inputs.

' -----[ Device Declaration ]----------------------------------------------
'
@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF
'@device pic16F690, intrc_osc_noclkout, bod_off, pwrt_off, wdt_off, mclr_off,
' protect_off ' For PM assembler only

' -----[ Revision History ]------------------------------------------------
'

' -----[ Declare Variables & Aliases ]-----------------------------
'
diff VAR Byte ' Difference between bat_mon and Vthr
dummy VAR Byte ' For read of on-interrupt port & clear mismatch condition
flush VAR PORTA.3 ' Set RA3 as input for sensing flush switch closure Int
i VAR Byte ' Index used in Gallon counter loop
led1 VAR PORTC.0 ' Set RC0 as LED indicator of valve open..water flowing
led2 VAR PORTC.1 ' Set RC1 as LED indicator of low battery
bat_mon VAR PortA.0 ' Set RA0 as battery low power monitor
meter VAR PORTA.2 ' Set RA2 as input for Hall Sensor meter pulse Interrupt
valve VAR PORTC.3 ' Set RC3 as valve solenoid open/close command

' -----[ Declare Constants ]----------------------------------------
'
k CON 10 ' Calibration factor for flow meter...# pulses per gal
Vthr CON 3 ' Aasumes Low Battery Monitor threshold = 3 volts

' -----[ Initialization ]--------------------------------------------------

' Setup Timer0 as an 8-bit counter with the clock input on RA2.
' 1:1 TMR0 prescaler
' TMR0 counts on high-to-low transitions
OPTION_REG = %00111000 ' PORTA pullups disabled, TMR0 clock source is RA2,
' high-to-low transitions, prescaler to TMR0
TMR0 = 256 - k ' preload TMR0 to overflow after k counts

'A/D & Comparators disabled
ANSEL=0 ' Set PortA to digital I/O
ANSELH=0 ' Analog module disabled
CM1CON0=0
CM2CON0=0

'Port Settings
Init:
PORTA = 0 ' PortA pins all set to Low
PORTC = 0 ' LEDs off, PULSOUT RC3 provides a high-going pulse
TRISA = %11111111 ' Set all PORTA pins to inputs...RA0, RA2 & RA3 are used
TRISB = %00000000 ' Set all PORTB pins to outputs
TRISC = %11110000 ' Set lower 4 pins of PartB as outputs
PORTC = %00000000 ' Pre-set PORTC pins low, turning LEDs off

' Initialization of inputs/outputs
Valve = 0 ' Initialize RC3 (valve) at Low value
TRISA.2 = 1 ' Set RA2 as input port for clock to TMR0
TRISA.3 = 1 ' Set RA3 as input port for sensing Flush switch closure
meter = 1 ' Initialize RA2 (meter) at High for METER pulse inputs
' RA2 = TMR0 clock input for simulated meter pulse inputs
flush = 1 ' Initialize RA3 (flush) at High value for flush interrupt

'Interrupts Settings
FLUSH_INT_FLAG VAR INTCON.0 ' RA3 (FLUSH) On-change-interrupt flag bit
TMR0_INT_FLAG VAR INTCON.2 ' Timer0 overflow flag bit
INTCON = %10101000 ' Enable global, TMR0 & RABIE (RA3 on-change-INT)
'INTCON = %10100000 ' enable global & Timer0 interrupt only
IOCA = %00001000 ' Enable RA3 as on-change-INT

' -----[ Main Code ]-------------------------------------------------------
'Set INT Handler
ON INTERRUPT GOTO Int_handler

'Start...normal code here
MAIN:
Pause 1 ' Works when SLEEP statement doesn't work!!
'SLEEP 45 ' Put MC in Sleep state at power up for 45 secs..doesn't work!!
Low led2
' Microcontroller is in Sleep State waiting for external FLUSH Interrupt
' Valve should be closed at this point and no water flowing

' Put code here to test if flow meter has stopped as an indication that
' no water is flowing...is a double check on valve closure.
GOTO Main ' Loop to Main to wait for next Flush interrupt on RA3 change

DISABLE
Int_handler:
High bat_mon ' Simulate battery monitor input is Low
DEFINE WRITE_INT 1
'Write 5, bat_mon ' Remove comment for test only
IF FLUSH_INT_FLAG = 1 Then ' Interrupt was from RA3 on change
PULSOUT valve,1000 ' Generate required 10 msec pulse to RC3 to open valve
HIGH led1 ' Light indicator that valve is open & water flowing
'Write 7, flush ' Write RA3 (FLUSH) value during testing
' Put code here to start a timer to run for 50 secs as a fail safe
' to prevent overflow of toilet tank in case of sensor failure.
'Goto Shutvalve
REPEAT
' Wait until the external Flush interrupt is at high level...limits
' interrupt to switch closure only and not for switch open
Until flush = 1
dummy = flush ' Clear mismatch condition
FLUSH_INT_FLAG = 0 ' Clear interrupt flag & enable
'INTCON = %10001000 ' global & RABIE (RA3 on-change-INT)
' ..preclude TMRO overflow interrupt..test only
Endif
IF TMR0_INT_FLAG = 1 Then ' Interrupt was a TMR0 overflow..volume reached
PULSOUT valve,2000 ' Generate required 20 msec pulse to RC3..close valve
'WRITE 11, TMR0 ' Write TMR0 value..remove comment for test only
'Write 13, bat_mon ' Remove comment for test only
'Write 15, Vthr
diff = Vthr - bat_mon
'Write 17, diff
If diff > 2 Then 'Battery is low
'Light the low battery monitor light
HIGH led2
ENDIF
dummy = TMR0 ' Clear mismatch condition
TMR0_INT_FLAG = 0 ' clear overflow flag
TMR0 = 256 - k ' reload TMR0 to overflow after k counts
LOW led1 'Turn off LED indicators
'INTCON = %10100000 ' global & TMR0 interrupts..preclude RA3 interrupt..
' test only
ENDIF
RESUME
ENABLE
' If the user program ends by getting to the last statement of the program
' at an END instruction, the MCU will SLEEP and await a wakeup.
END

Archangel
- 2nd June 2009, 04:06
I have never ever had reason to use sleep directive, I know it is supposed to wake on interrupt. Pause 1 is 1 millisecond not 45 seconds as you comment indicates the sleep directive is asking for, are you sure or your intcon and option_reg settings? They affect interrupts. I am not in the shop right now, and do not have books handy, will look further later.

Archangel
- 2nd June 2009, 17:50
I saw nothing in the PBP Book , check your Data sheet and see what kind of OSC the WDT uses and make sure it is turned on. See sect. 14.6 of the datasheet, it is a pretty good treatise on sleep.

mister_e
- 2nd June 2009, 18:32
Not sure how PBP deal with SLEEP 45, but from memory, the WDT "should" use TMR0 as clock source, and if you don't assign it to, then it may indeed give you some problems... but you're currently using TMR0 for something else, so probably part of the problem.

I would probably opt for a real WDT timeout interrupt with @ SLEEP, or see if there's any other Timer who can wake-up the PIC, leaving TMR0 free to count your pulses.

Check your datasheet to see how each Wake-up source behave.

jellis00
- 3rd June 2009, 00:12
Thanks Mister e! I uses @ SLEEP and @ NOP statements instead of the SLEEP 45 statement and it all works. Didn't need to put it in SLEEP for only 45 seconds since prolonged @ SLEEP mode is still awakened by either of my interrupts implemented in this code.

The contributions on this thread have been outstanding in helping me understand my problems and achieve solution. Thanks again to all of you who posted answers to my questions. I consider this to be the end of this thread.:)

Archangel
- 3rd June 2009, 02:35
Thanks Mister e! I uses @ SLEEP and @ NOP statements instead of the SLEEP 45 statement and it all works.
You know, I was thinking about the @ sign but didn't have time to research it this morning, and didn't have the stones to go out on the limb . . .
Hurray for mister_e !