X2 Hardware Interrupts(INT0,INT1,INT2)

jim1248

Member
This shows how to use INT1 and INT2 on a Picaxe20X2.It has be simulated on 28X2 and 40X2 parts and seemed to work fine.Watch out for the pullup command as it will cause problems with the simulator

Code:
; *******************************
; ***** Sample Header File  *****
; *******************************
;    Filename: 	Interrupt.bas		
;    Date: 		28/01/10	
;    File Version:v1.0 	
;    Written by: 	Jimmy Allen	
;    Function:	Demonstrate how to use INT0,INT1 on PicaxeX2 parts	
;    Target PICAXE:PicaxeX2 parts
'    Compiler: 	Program Editor 5.2.9	
; ******************************* 
note:{
#rem
	This code has been tested on a Picaxe20X2,Simulated on a Picaxe20X2 Picaxe28X2 and Picaxe40X2 and all seem to function properly
	Line 26-28(pullup) needs to be commented out during simulation as it causes the Program Editor to stop responding.
	w0 will overflow from 0xff to 0 or 0 to 0xff
	there is no code to remove bouncing of switch contacts so w0 many inc or dec more than 1 for each button press
#endrem
}
init:{
	#picaxe20x2				
	#terminal 9600
	#no_data
	#no_table
	let w0 = 1000
	'pullup %00011000 				'Picaxe20X2   	(refer to note)
	'pullup on 					'Picaxe28/40X2	(refer to note)
	'pullup %00000110 			'Picaxe28/40X2-3V	(refer to note)
	hintsetup %0000110 			'set INT1 and INT2 to trigger Interrupt at falling edge
		;Bit 7 - reserved
		;Bit 6 - Interrupt 2 Trigger (1 = rising edge, 0 = falling edge)
		;Bit 5 - Interrupt 1 Trigger (1 = rising edge, 0 = falling edge)
		;Bit 4 - Interrupt 0 Trigger (1 = rising edge, 0 = falling edge)
		;Bit 3 - reserved
		;Bit 2 - Interrupt 2 Enable
		;Bit 1 - Interrupt 1 Enable
		;Bit 0 - Interrupt 0 Enable (not 20X2)
	setintflags %00001000,%00001000  	'enable interrupt flags
		;flag0 hint0flag X2 parts - interrupt on INT0 hintsetup
		;flag1 hint1flag X2 parts - interrupt on INT1 hintsetup
		;flag2 hint2flag X2 parts - interrupt on INT2 hintsetup
		;flag3 hintflag X2 parts - interrupt on any pin 0,1,2 hintsetup
		;flag4 compflag X2 parts - comparator flag compsetup
		;flag5 hserflag hserial background receive has occurred hsersetup
		;flag6 hi2cflag hi2c write has occurred (slave mode) hi2csetuphint interrupts      '
		;flag7 toflag timer overflow flag settimer
	setint %00000000,%00111110,C
}
main:{
	sleep 0					'goto low power mode disabling all hardware except
	goto main 					'start infinite loop   				 
	end
}
Interrupt:{
  	let b0 = pinsC
  	hintflag = 0 				'rest hintflags to 0
  	If hint1flag = 1 Then 			'check to see if INT1 has bee triggered
    		inc w0				'increment w0 by 1
    		hint1flag = 0			'reset hint1flag to 0
  	End If
  	
  	If hint2flag = 1 Then	'check to see if INT2 has bee triggered
    		dec w0		'decrement w0 by 1
    		hint2flag = 0	'reset hint2flag to 0
  	End If
  	sertxd (#w0,13,10)			'send value of w0 to serial terminal at N9600
  	SetIntFlags %00001000, %00001000	'renable hint interrupts
  	Return	
}
 
Last edited:

lbenson

Senior Member
Thanks for this very well-commented example. Your formating will be preserved and code put in a code box (as you seem to have intended), if you edit the post and put a "/" in the closing square-bracketed terminator, like so: [/code].
 

Linzmeister

New Member
Thanks for a clear example.

Other examples I have read use only 2 of the 3 keywords:
hintsetup
setintflags
setint

but you have fleshed out the thought that has been skulking in the back of my mind. I plan to use a 28X2 or 40X2 because it has 3 external interrupts. I want to use:

Code:
hintsetup %01110111
setintflags %00011000,%00011000             'interrupt an any external pin or serial receive
setint NOT %00000000,%00000111, [port yet to be dertermined]
to detect if any 1 of the three pins goes high - not if all three go high at the same time - which given the proposed hardware is a wind direction sensor seems impossible.

Under SETINT command the manuals states:
Restrictions.

Due to internal port configuration on some of the chips there is a limitation on which pins can be used:
14M only inputs 0,1,2 may be used
20M only inputs 1-5 may be used
20X2 only portC may be used, and only C.1 to C.5 on portC may be used
40X2 when using portA, only A.0 to A.3 may be used
but the 20X2 only has 2 useable interrupts and the pinout for the 40X2 shows

B2, B1, B0 as being the hInt0~2 pins... so what gives?? Are the hInt0~2 pins output indicators of interrupts received on other pins or are they the only pins that can be used to trigger an external interrupt??... apart from comparator and serial based interrupts...

Any clarifications greatly appreciated.

Regards,
Linz.
 
Top