Is the advantage of hardware interrupt significant in typical applications?


Senior Member
I've been looking for a discussion of why one would choose an X2 chip over a lesser chip simply for the availability of hardware interrupts and I cannot find one. The reason I am curious is that I have a 20X2 running a rotary encoder using hardware interrupts, and it has a lot of leftover pins doing nothing. An 18M2 or possibly a 14M2 would be adequate in terms of pin numbers and the issue is pretty much those hardware interrupts.

Are the hardware interrupts very much more effective than software interrupts?


Senior Member
One less thing to program, I guess, but TTYTT I've never used hardware interrupts.


Senior Member
Hardware interrupts are like corkscrews.

You don't need them very often, but when the need arises nothing else will quite do the job.


Technical Support
Staff member
Hardware interrupts will detect the interrupt condition even if that event is very short lived or occurs while the PICAXE is busy doing something else.

Software interrupts are only acted upon when the PICAXE comes to check the pin states between commands, and in a few other cases within commands.


Well-known member
I'm using an edge-triggered hardware interrupt instead of (or to replace) a flip-flop.

It is important that the input change is stored for my program to detect ASAP.

Saves me putting a whole extra chip in my design.

I also have a fast single-pulse random input to detect.


Senior Member

If you only need to "detect" that one short pulse (or maybe 2 or 3) has occurred previously, then there are several latches within all the M2 PICaxes that can be used. But it can get "messy" if you actually need to generate an interrupt (as opposed to polling a SFR) because it becomes necessary to output the latch signal on one pin and then link it on to another (input) pin to generate the interrupt.

The "S-R Latch" is an obvious contender (supported by PICaxe Basic) but in my current project I'm using the "Timer1 Gate" which contains a flip-flop and can be polled quite independently of the Timer itself. There are also the flip-flops in the "Data Signal Modulator" but I've found their usability rather restricted.

Cheers, Alan.


Senior Member
... but in my current project I'm using the "Timer1 Gate" which contains a flip-flop and can be polled quite independently of the Timer itself.
Cheers, Alan.
Tantalisingly brief description but one that most certainly grabs one's interest; Alan, could you elaborate on this idea and possibly illustrate this with some code example?
Last edited:


Senior Member
The 08M2, 14M2, 20M2, 28X2, & 40X2 all have at least one timer that supports timer "gate".

To use this, the TxCON register must be set for External Osc, the timer enabled. The TxGCON register bits need to be set to enable the gate, set polarity, set mode, and set the gate source. Use Pokesfr to set the bits for these registers.

With things set correctly, you can then read the TMRxL and TMRxH registers to get the "count" value which is the number of pulse "edges" applied to the TxG Pin.

I don't think that the Picaxe firmware allows writing to the INTCON register with pokesfr, but there may be a way to use hintsetup or setintflags in combination with pokesfr of the previoulsly mentioned registers to do some nifty stuff.

How to use timer gate is described in detail in Timer1/3/5 Section of the respective PIC Datasheet.

The only code I have is either in C or another BASIC so it would not be appropriate to post here. However you can "Google" and download "Microchip Compiled Tips 'n Tricks" . This will give some good guidelines and steps needed for using timer x gate for various applications.
Last edited:


Senior Member
Hi Circuit,

The particular feature of interest for this thread is that it's possible to "isolate" the T1Gate hardware from the Timer itself, giving some "free" logic gates/latches, whilst leaving the timer (and the PICaxe operating system) to work completely as normal. You may need to "get your hands dirty" delving into the "base" PIC data sheets (there are several useful timing diagrams for the T1Gate hardware) but the basic data is attached below. In principle it's only necessary to poke and peek one Special Function Register (T1GCON).

Conveniently, the "top" pins of the 08, 14 and 20 M2 chips are functionally identical, so T1G is Leg 3 on all three (or can be swapped to Leg 4 using the APFCON SFRegister). Alternatively, the on-chip comparator(s) (even on M2 devices) can be used as the Gate source, making up to another 6 pins a potential input source. The relevant (T1G) section of Timer 1 is attached below, with the SFR bit functions highlighted. The table describes the functions of the 8 bits which make up the T1GCON register.


The most important feature is that if the Gate Enable bit (.7) is left clear, then the timer is unaffected, but the Gate circuit continues to function and the "status" of the gate hardware can be read by two flags (.2 and .3). Other bits allow either the T1G pin or the comparators to be selected as source (.0 and .1) and the trigger polarity (.6) chosen. Either of the flags may be used to determine when an "event" has occurred. The Single Pulse Mode (.4) method is to set the GO/DONE bit (.3) when setting up the SFR and then poll the same flag to detect when a trigger has occurred. However, an alternative method is to setup the "Toggle" mode (.5) and then continually test for the latch (bit .2) being flipped from its previous state. The latter is potentially faster because there is no need to write to the SFR each time.

My own code is rather more complex (I'm using an on-chip Comparator as source) but the (untested) fundamental elements of the method are:

; Using M2 Gate Control Hardware to detect short pulses
; AllyCat June 2015

symbol T1GCON   = $19 			      ; Timer1 Gate Control Register (SFR=$19 for M2 devices)
symbol APFCON 	= $5D			;  Optional: Alternate Pin Function: T1GSEL = .3 (T1G on Leg 4 instead of Leg 3)

	 pokesfr T1GCON,$18 	 		     ; Trigger by a pulse on T1G pin
		peeksfr T1GCON,b0
		if bit3 = 0 then			; /Done flag
;			do something
			pokesfr T1GCON,$18		; Reset the Go bit
Cheers, Alan.
Last edited:


Senior Member
Alan and Goey,

A complete revelation; most helpful. This is an extremely useful insight into an advanced aspect of PICAXE applications. Alan, your detailed illustration of how to approach this has been very enlightening; I shall be working this one through in detail and exploring further this concept. Thank you for the not inconsiderable time that you spent on your replies. As I said, a complete revelation, and therefore I am left wondering what other such advanced tricks there are in the depths of your treasure chest of microcontroller understanding.


P.S. Goey, after the rather distressing hiatus in your well-being last year that caused you to fear that you would no longer be able to contribute to the forum, may I say how much I (and I would think others on this forum) appreciate that you are able to remain active and productive as one of the most useful gurus on this forum. I always read your posts in detail and never fail to learn.


Senior Member
I tried pwm on b.0 28x2 and interrupt on that pin change to experiment with.It works but didn't go anywhere in reality but interesting.
It's down to code style. Waiting for an event or do other stuff until pin change notification. Like the pc mouse. Pc gets on until mouse event.Not checking mouse all the time. I just got uno timer interrupts sorted with a system I'm using and sending the guy who showed me how, real world demos,not flashing leds more servo update and stepper motor control. The guy suggested I use adc finished interrupt to a project but my code wasn't doing anything else so what was the point. I think pin interrupts could be useful...but I talk bollards.


Senior Member
The main thing that I have set interrupts on is the fast capture unit, it made use of the internal voltage ladder. Response time is around 3us @64Mhz, a fractions of time it take to either trigger a software interrupt of take and ADC reading and compare it to a desired value.
As stated when you need this sort of input and a fast response there isn't anything that quite compares.