PDA

View Full Version : Continuous Interrupts when using DT_INTS and an INCLUDE file



jellis00
- 14th January 2010, 22:47
Am using DT_INTS with 18F4550 to detect an alarm interrupt on INT2 from DS1330 real-time-clock. So that I can reuse a tested routine for operating the clock and a SRF02 peripheral via I2C bus, I adapted it into an INCLUDE file as a callable routine from the Main program. The INCLUDE processes the clock/SRF02 operations and provides results via global variables to the Main program. The program compiles and assembles OK without any errors in either the Main or INCLUDE file. However, when the program runs, I get continous interrupts that continuously trigger the Alarm handler in the INCLUDE file. I had seen this problem in previous versions and solved it (without understanding why) by placing a PAUSE 100 at beginning of the Main program. This previous fix didn't work in this case.
I am posting the applicable code from my Main program below. Can anyone tell me why it would create continuous INT2 interrupts??


''----------------[ Define Include Files & special DEFINES ]
INCLUDE "DS1337_SRF02.inc" ' Include use of DS1337_SRF02 Alarm handler
INCLUDE "Modedefs.Bas"
INCLUDE "DT_INTS-18.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ' Include if using PBP high priority interrupts
INCLUDE "ALLDIGITAL.pbp" DEFINE OSC 16

DEFINE I2C_SLOW 1 ' Set i2c to the standard speed
DEFINE I2C_HOLD 1 ' Enable recieving i2c device to pause communication

'--------------[ Declare Variables, Aliases & Constants ]
' Global variables & constants used in both Main program and Include files
'rng0 VAR w0.Byte0 ' LSB of range measurement when right justified
'rng1 VAR w0.byte1 ' MSB of range measurement when right justified
'SCL VAR PORTB.1 ' I2C clock pin
'SDA VAR PORTB.0 ' I2C data pin
'w0 var word ' W0 is the word value to store the range data
'rtcdevice CON $D0 ' Device address for DS1337 Real-time-clock (RTC)
'srfdevice CON $E0 ' Device address for SRF02 ultrasonic range finder
'Alarm1 VAR PORTB.2 ' Alarm1 input from DS1337 INTA (pin-3)
'Alarm2 VAR PORTB.3 ' Alarm2 input from DS1337 INTB (pin-7)
'RTC_INT_FLG VAR INTCON3.1 'Alias for RB2 INTA interrupt flag from RTC
' Clock Variables
'sec VAR BYTE ' seconds
'MINs VAR BYTE ' minutes
'hr VAR BYTE ' hours
'day VAR BYTE ' day
'date VAR BYTE ' date
'mon VAR BYTE ' month
'yr VAR BYTE ' year
' ALARM1 VARIABLES
'A1sec VAR BYTE ' seconds
'A1MINs VAR BYTE ' minutes
'A1hr VAR BYTE ' hours
'A1day VAR BYTE ' day
' ALARM2 VARIABLES
'A2MINs VAR BYTE ' minutes
'A2hr VAR BYTE ' hours
'A2day VAR BYTE ' day

GoSub SetTimeAndDate ' Setup current time & alarm settings

'----------------------[ SETUP FOR INTERRUPTS ]
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
;INT_Handler USB_Handler
INT_Handler INT2_INT, _AlarmHdlr, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
@ INT_ENABLE INT2_INT ; enable external (INT) interrupts

' Per DT, DT_INTS already takes care of setting INTCON and RCON registers
' but this doesn't work unles INTCON2 set per below:
'INTCON.7 = 1 ' Set Global Interrupt Enable bit
INTCON2 = %00000000 ' Set INT2 for falling edge (Bit4-low)
' on RTC's interrupt.
'INTCON3 = %10010000 ' Set INT2 high priority (Bit7-high), enable INT2
' (Bit4-high)

' Initialize Hardware
'Set registers
'OSCCON.7 = 0 ' Clear IDLEN bit to allow SLEEP mode
' According to data sheet, must clear CONFIG2H.0 (WDTEN bit)
' in order to use WDTCON. Not clear from Data sheet how to reach
' WDTEN bit with with config fuses??
'WDTCON.0 = 1 ' Set SWDTEN bit to turn on WDT
TRISA = 0 ' PORTA all outputs for LCD use
TRISB =%00001100 ' RB2 & RB3 set as RTC Alarm1 & Alarm2 inputs
TRISC = 0
TRISD = 0
TRISE = 0

'--------------------[ Begin Main Program Loop ]
Main:
PAUSE 100 ' ?? For some reason this 100 millisec delay has been
'required in previous versions
' to keep RTC interrupts from happening every 11 secs ??
Low PORTC.0 ' Turn off Test LED while in Main loop

GoTo Main ' Endless Loop waiting for interrupt
End ' Safety measure to insure program stops if reaches here

'-------------------[ Begin Interrupt Handler }-----------------------
AlarmHdlr:
Goto Alarm ' Call Alarm handler subroutine in INCLUDE file
@ INT_RETURN

Bruce
- 14th January 2010, 23:03
Can anyone tell me why it would create continuous INT2 interrupts?
My crystal ball's in for repairs, so you'll need to post all your code....;o}



AlarmHdlr:
Goto Alarm ' Call Alarm handler subroutine in INCLUDE file
@ INT_RETURN
With your interrupt handler jumping to AlarmHdlr, what happens in your Alarm routine
to send it back to the proper place to return from the interrupt, to land on your
@ INT_RETURN?

@ INT_RETURN clears the interrupt flag bit (& handles GIE), which keeps it from
returning to your int handler.

Your GOTO Alarm is probably leaving the interrupt flag bits set, and it simply returns assuming
the interrupt is still pending.

jellis00
- 15th January 2010, 01:22
you'll need to post all your code....;o}

With your interrupt handler jumping to AlarmHdlr, what happens in your Alarm routine
to send it back to the proper place to return from the interrupt, to land on your
@ INT_RETURN?

@ INT_RETURN clears the interrupt flag bit (& handles GIE), which keeps it from
returning to your int handler.

Your GOTO Alarm is probably leaving the interrupt flag bits set, and it simply returns assuming
the interrupt is still pending.

Notice that I am using two different INCLUDE files: one to setup the DS1337 and the other as an Alarm handler of an INT2 interrupt. I am posting below the code for the INCLUDE file that is involved with the servicing of the INT2 interrupt. You can see that I have a RETURN statement at the end of the routine that is called to service the interrupte, so I presumed the @INT_RETURN that is the next statement after the RETURN would take care of flags. Do I need to separately clear the GIE flag at the end of the called routine before the RETURN to keep the continuous interrupts from happening?


'--------------[ Declare Variables, Aliases & Constants ]----------------
' Global variables, constants & aliases used in both Main program and
' INCLUDE files that are declared in Main are commented out here.
'Alarm1 VAR PORTB.2 ' Alarm1 input from DS1337 INTA (pin-3)
'Alarm2 VAR PORTB.3 ' Alarm2 input from DS1337 INTB (pin-7)
IN2_ext_pwr VAR PORTC.6 ' Alias to control power to Ultrasonic sensor
rng0 VAR w0.Byte0 ' LSB of range measurement when right justified
rng1 VAR w0.byte1 ' MSB of range measurement when right justified
IN2_SCL VAR PORTB.1 ' I2C clock pin
IN2_SDA VAR PORTB.0 ' I2C data pin
w0 var word ' W0 is the word value to store the range data
'rtcdevice CON $D0 ' Device address for DS1337 Real-time-clock (RTC)
srfdevice CON $E0 ' Device address for SRF02 ultrasonic range finder
'Other variables in DS1337 setup below that DON'T have INC_ prefix
' Local variables & constants only used in this Include file
' Other variables in DS1337 setup below that have INC_ prefix
' Initialize Hardware
' ===================
'Set registers
'OSCCON.7 = 0 ' Clear IDLEN bit to allow SLEEP mode
TRISA = 0 ' PORTA reserved as outputs for LCD use
TRISB =%00001100 ' RB2 & RB3 set as RTC Alarm1 & Alarm2 inputs
TRISC = 0 ' RC0 used for test LED

GOTO Alarm ' Jump over Subroutines, so they don't try to execute

'-------------------[ Begin Alarm Interrupt Handler ]--------------------
Alarm:
High PORTC.0 ' Turn on Test LED to indicate Alarm ISR entered
HIGH IN2_ext_pwr ' Turn on power to the SRF02 during interrupt service
' in order to use the I2C bus without distortion.
PAUSE 3000 ' Delay to let I2C bus stabilize after SRF02 turn on
'DEFINE WRITE_USED 1 'Required if only writing WORD variables in v2.6
' Process the RTC alarm
' Read the current time from the DS1337
I2CREAD IN2_SDA,IN2_SCL,$D0,SecReg,[sec,MINs,hr,day,date,mon,yr]
PAUSE 10
'Write time of latest alarm to EEPROM
WRITE 21,hr
PAUSE 20
WRITE 22,MINs
PAUSE 20
WRITE 23,sec
Pause 20
' Make SRF02 Range Measurement
I2CWRITE IN2_SDA,IN2_SCL,$E0,0,[80]' Request start of ranging in inches
pause 100 ' Wait for ranging to finish
I2CREAD IN2_SDA,IN2_SCL,$E0,2,[rng1,rng0] ' Get ranging results
pause 10
WRITE 253,rng1 ' Write range to EEPROM
PAUSE 20
Write 254,rng0
PAUSE 20
' Read the ALARM settings from the DS1337
'I2CREAD IN2_SDA, IN2_SCL, $D0, IN2_Alm1sec,[A1sec,A1MINs,A1hr,A1day]
'Pause 10
'I2CRead IN2_SDA, IN2_SCL, $D0, IN2_Alm2min,[A2MINs,A2hr,A2day]
'PAUSE 10
' Clear the Alarm flags to be ready for next day's interrupt
' Clear STATUS register
I2CWrite IN2_SDA,IN2_SCL,$D0,IN2_StatusReg,[$00]
Pause 1000
WRITE 255, MINs
PAUSE 20
LOW IN2_ext_pwr ' Turn off power to the SRF02 at end of interrupt
' This statement will distort the I2C bus and ext_pwr
' must be HIGH before any attempts are made to use the
' I2C bus.
PAUSE 1000 ' Delay to permit I2C bus to stabilize after SRF02 off
RETURN
' Resume Main Program
'----------------{ End of Alarm Interrupt Handler }------------------

HenrikOlsson
- 15th January 2010, 06:15
You can see that I have a RETURN statement at the end of the routine that is called to service the interrupte, so I presumed the @INT_RETURN that is the next statement after the RETURN would take care of flags.
It would if you had called the routine with a GOSUB instead of with a GOTO as you currently are doing:

'-------------------[ Begin Interrupt Handler }-----------------------
AlarmHdlr:
Goto Alarm ' Call Alarm handler subroutine in INCLUDE file
@ INT_RETURN

jellis00
- 15th January 2010, 06:47
That mistake makes me feel really stupid. Thanks for catching it, Henrick.

However, I made both calls to the INCLUDE files GOSUB and made sure there was a RETURN at the end, and I still am seeing an interrupt from the INT2 every 3-4 seconds.
From previous discussions with Darrel Taylor he said users should not mess with the INTCON register because the DT_INTS takes care of it. But as was pointed out it appears that the continuous interrupts are occurring because evidently the interrupts are enabled during the subroutine being executed in the INCLUDE file.
Am attaching my most recent code in hopes someone can advise me what is causing the continuous interrupts from INT2. Will also attach applicable INCLUDE file in another following post.


'----------------[ Define Include Files & special DEFINES ]--------------
INCLUDE "DS1337_Setup.inc" ' Include subroutine that setsup DS1337
INCLUDE "DS1337_SRF02.inc" ' Include subroutine as ISR for Alarm handler
Include "Modedefs.Bas"
INCLUDE "DT_INTS-18.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ' Include if using PBP high priority interrupts
INCLUDE "ALLDIGITAL.pbp" ' Sets all registers for digital ops.
DEFINE OSC 16
DEFINE I2C_SLOW 1 ' Set i2c to the standard speed
DEFINE I2C_HOLD 1 ' Enable recieving i2c device to pause communication

'--------------[ Declare Variables, Aliases & Constants ]----------------
' Variables, Constants & Aliases for Pins
' Global variables, constants & aliases used in both Main program and
' INCLUDE files that are declared in INCLUDE files are commented out here
'Alarm1 VAR PORTB.2 ' Alarm1 input from DS1337 INTA (pin-3)
'Alarm2 VAR PORTB.3 ' Alarm2 input from DS1337 INTB (pin-7)
'ext_pwr VAR PORTC.6 ' Alias to control power to Ultrasonic sensor
'rng0 VAR w0.Byte0 ' LSB of range measurement when right justified
'rng1 VAR w0.byte1 ' MSB of range measurement when right justified
SCL VAR PORTB.1 ' I2C clock pin
SDA VAR PORTB.0 ' I2C data pin
'w0 var word ' W0 is the word value to store the range data
rtcdevice CON $D0 ' Device address for DS1337 Real-time-clock (RTC)
'srfdevice CON $E0 ' Device address for SRF02 ultrasonic range finder

' Alias of Clock register addresses
ContReg CON $0E ' CONTROL register address
StatusReg CON $0F ' STATUS register address
RTC_INT_FLG VAR INTCON3.1 'Alias for RB2 INTA interrupt flag from RTC

' RTC Address definitions
' Time/Date
'SecReg CON $00 ' seconds address (00 - 59)
' MSB of SecReg must be set to a 0 to enable RTC
'MinReg CON $01 ' minutes address (00 - 59)
'HrReg CON $02 ' hours address (01 - 12) or (00 - 23)
'DayReg CON $03 ' day address (1 - 7)
'DateReg CON $04 ' date address (01 - 28/29, 30, 31)
'MonReg CON $05 ' month address (01 - 12)
'YearReg CON $06 ' year address (00 - 99)
' Alarm 1 Address definitions
'Alm1sec CON $07 ' Alarm 1 seconds address (00 - 59)
'Alm1min CON $08 ' Alarm 1 minutes address (00 - 59)
'Alm1hr CON $09 ' Alarm 1 hours address (01 - 12) or (00 - 23)
'Alm1Day CON $0A ' Alarm 1 day address (1 - 7)
' Alarm 2 Address definitions
'Alm2min CON $0B ' Alarm 2 minutes address (00 - 59)
'Alm2hr CON $0C ' Alarm 2 hours address (01 - 12) or (00 - 23)
'Alm2Day CON $0D ' Alarm 2 day address (1 - 7)
' Clock Variables
'sec VAR BYTE ' seconds
'MINs VAR BYTE ' minutes
'hr VAR BYTE ' hours
'day VAR BYTE ' day
'date VAR BYTE ' date
'mon VAR BYTE ' month
'yr VAR BYTE ' year
' ALARM1 VARIABLES
'A1sec VAR BYTE ' seconds
'A1MINs VAR BYTE ' minutes
'A1hr VAR BYTE ' hours
'A1day VAR BYTE ' day
' ALARM2 VARIABLES
'A2MINs VAR BYTE ' minutes
'A2hr VAR BYTE ' hours
'A2day VAR BYTE ' day

GOSUB SetTimeAndDate ' Set current time & alarm settings by
' calling routine in INCLUDE file

'----------------------[ SETUP FOR INTERRUPTS ]-------------------------
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
;INT_Handler USB_Handler
INT_Handler INT2_INT, _AlarmHdlr, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

@ INT_ENABLE INT2_INT ; enable external (INT) interrupts
' Per DT, DT_INTS already takes care of setting INTCON and RCON registers
' but this doesn't work unles INTCON2 set per below:
'INTCON.7 = 1 ' Set Global Interrupt Enable bit
INTCON2 = %00000000 ' Set INT2 for falling edge (Bit4-low)
' on RTC's interrupt.
'INTCON3 = %10010000 ' Set INT2 high priority (Bit7-high), enable INT2
' (Bit4-high)

' Initialize Hardware
' ===================
'Set registers
'OSCCON.7 = 0 ' Clear IDLEN bit to allow SLEEP mode
TRISA = 0 ' PORTA all outputs for LCD use
TRISB =%00001100 ' RB2 & RB3 set as RTC Alarm1 & Alarm2 inputs
TRISC = 0
TRISD = 0
TRISE = 0

'--------------------[ Begin Main Program Loop ]------------------------
Main:
PAUSE 500 ' ?? For some reason this 100 millisec delay is required
' to keep RTC interrupts from happening every 3-4 secs ??
Low PORTC.0 ' Turn off Test LED while in Main loop

' Place code here to put MCU and Ultrasonic Ranger in SLEEP mode.
' while waiting for clock interrupt to wakeup from SLEEP mode.

GoTo Main ' Endless Loop waiting for interrupt
End ' Safety measure to insure program stops if reaches here

'-------------------[ Begin Interrupt Handlers }-----------------------
AlarmHdlr:
GoSUB Alarm ' Call Alarm handler subroutine in INCLUDE file
@ INT_RETURN

END

jellis00
- 15th January 2010, 06:49
Here is code for the related INCLUDE file that services the INT2 interrupt:


'----------------[ Define special DEFINES ]--------------
' These I2C settings must be defined in the Main Program
'DEFINE I2C_SLOW 1 ' Set i2c to the standard speed
'DEFINE I2C_HOLD 1 ' Enable recieving i2c device to pause communication

'--------------[ Declare Variables, Aliases & Constants ]----------------
' Global variables, constants & aliases used in both Main program and
' INCLUDE files that are declared in Main files are commented out here
'Alarm1 VAR PORTB.2 ' Alarm1 input from DS1337 INTA (pin-3)
'Alarm2 VAR PORTB.3 ' Alarm2 input from DS1337 INTB (pin-7)
IN2_ext_pwr VAR PORTC.6 ' Alias to control power to Ultrasonic sensor
rng0 VAR w0.Byte0 ' LSB of range measurement when right justified
rng1 VAR w0.byte1 ' MSB of range measurement when right justified
IN2_SCL VAR PORTB.1 ' I2C clock pin
IN2_SDA VAR PORTB.0 ' I2C data pin
w0 var word ' W0 is the word value to store the range data
'rtcdevice CON $D0 ' Device address for DS1337 Real-time-clock (RTC)
srfdevice CON $E0 ' Device address for SRF02 ultrasonic range finder
'Other variables in DS1337 setup below that DON'T have IN2_ prefix
' Local variables & constants only used in this Include file
' Other variables in DS1337 setup below that have IN2_ prefix

'---------------[ Initialize this INCLUDE module ]-----------------------
'---------------SETUP FOR USING DS1337 Real Time Clock-------------------
' Setup Hardware for uart...already done by DS1337_Setup
'DEFINE HSER_BAUD 115200
'DEFINE HSER_RCSTA 90h
'DEFINE HSER_TXSTA 24h
'DEFINE HSER_CLROERR 1

' Alias of Clock register addresses
IN2_ContReg CON $0E ' CONTROL register address
IN2_StatusReg CON $0F ' STATUS register address
'RTC_INT_FLG VAR INTCON3.1 'Alias for RB2 INTA interrupt flag from RTC

' Initialize Hardware
' ===================
'Set registers
'OSCCON.7 = 0 ' Clear IDLEN bit to allow SLEEP mode
TRISA = 0 ' PORTA reserved as outputs for LCD use
TRISB =%00001100 ' RB2 & RB3 set as RTC Alarm1 & Alarm2 inputs
TRISC = 0 ' RC0 used for test LED

GOTO Alarm ' Jump over Subroutines, so they don't try to execute

'-------------------[ Begin Alarm Interrupt Handler ]--------------------
Alarm:
High PORTC.0 ' Turn on Test LED to indicate Alarm interrupt entered
HIGH IN2_ext_pwr ' Turn on power to the SRF02 during interrupt service
' in order to use the I2C bus without distortion.
PAUSE 3000 ' Delay to let I2C bus stabilize after SRF02 turn on
'DEFINE WRITE_USED 1 'Required if only writing WORD variables in v2.6
' Process the RTC alarm
' Read the current time from the DS1337
I2CREAD IN2_SDA,IN2_SCL,$D0,SecReg,[sec,MINs,hr,day,date,mon,yr]
PAUSE 10
'Write time of latest alarm to EEPROM
WRITE 21,hr
PAUSE 20
WRITE 22,MINs
PAUSE 20
WRITE 23,sec
Pause 20
' Make SRF02 Range Measurement
I2CWRITE IN2_SDA,IN2_SCL,$E0,0,[80]' Request start of ranging in inches
pause 100 ' Wait for ranging to finish
I2CREAD IN2_SDA,IN2_SCL,$E0,2,[rng1,rng0] ' Get ranging results
pause 10
WRITE 253,rng1 ' Write range to EEPROM
PAUSE 20
Write 254,rng0
PAUSE 20
' Read the ALARM settings from the DS1337
I2CREAD IN2_SDA, IN2_SCL, $D0, Alm1sec,[A1sec,A1MINs,A1hr,A1day]
Pause 10
I2CRead IN2_SDA, IN2_SCL, $D0, Alm2min,[A2MINs,A2hr,A2day]
PAUSE 10
' Clear the Alarm flags to be ready for next day's interrupt
' Clear STATUS register
I2CWrite IN2_SDA,IN2_SCL,$D0,IN2_StatusReg,[$00]
Pause 1000
WRITE 255, MINs
PAUSE 20
LOW IN2_ext_pwr ' Turn off power to the SRF02 at end of interrupt
' This statement will distort the I2C bus and ext_pwr
' must be HIGH before any attempts are made to use the
' I2C bus.
PAUSE 1000 ' Delay to permit I2C bus to stabilize after SRF02 off
RETURN
' Resume Main Program
'------------------{ End of Alarm Interrupt Handler }--------------------

HenrikOlsson
- 15th January 2010, 08:23
Hi,
Perhaps not related to your problem but in the Alarm subroutine you have this:

GOTO Alarm ' Jump over Subroutines, so they don't try to execute

'-------------------[ Begin Alarm Interrupt Handler ]--------------------
Alarm:

Which seems to not jump OVER the Alarm subroutine but rather TO it.

Try setting only the related bits in INTCON2 and do it before you enable the interrupts ie:

INTCON2.4 = 0 'INT2 interrupts on falling edge
@ INT_ENABLE INT2_INT ; enable external (INT) interrupts
It is correct that you don't need to mess with the BITS concerning the interrupt flag, interrupt enable flag and interrupt priority bit but you still need to set it up to respond the way you want.

The reason it does not initially work if you don't write to INTCON2 is because if you look at table 5.2 in the datasheet you see that bit4 defaults to 1 at power on which means it interrupts on the falling edge as default and you'll need to change that.

Another thing is that because you write all zeros to INTCON2 you are enabling the pullups on PortB (which are disabled by default) and as it happens INT2 is on PORTB.2. So if you by any chance have an external pull down resistor to hold the line low you are effectively creating a voltage divider and the voltage on the pin might actually be anywhere between Vdd and Vss - a little noice there and it might trip the interrupts.

Well, that's all just speculations but IMO worth looking into.

/Henrik.

Bruce
- 15th January 2010, 15:45
Try this as a quick test.

In your Alarm routine replace High PORTC.0 with PORTC.0 = INTCON3.1.

In your Main routine replace LOW PORTC.0 with PORTC.0 = INTCON3.1.

Your LED will show the state of the INT2 interrupt flag bit. What does
the LED do?

jellis00
- 15th January 2010, 21:42
Hi,
Perhaps not related to your problem but in the Alarm subroutine you have this:

GOTO Alarm ' Jump over Subroutines, so they don't try to execute

'-------------------[ Begin Alarm Interrupt Handler ]--------------------
Alarm:

Which seems to not jump OVER the Alarm subroutine but rather TO it.


Henrik was on the right track. In mean time I received a separate notice from Darrel Taylor that cleared it all up. For the benefit of all of you who tried to help me and any future visitors to this thread, let me explain.
I had failed to create a JumpLocation label after the include file's subroutine and a GOTO JumpLocation statement ahead of the subroutines. Therefore, as Darrel explains it:
" DS1337_Setup.inc is being executed but it's being exeucted at the wrong time...On power up. The processor is resetting every 4.5 seconds, because that's how long the DS1337 routine takes with all the PAUSES in it.
At the end, it returns, but it was never called by a gosub and just fell-into the code from the start. So that RETURN resets the processor on a Stack Underflow, and hence repeated interrupts.
All include files MUST jump over their own code."

Hope this may help someone in the future avoid my learning curve on creating/using INCLUDE files.