Hardware interrupt 20X2 - negative edge

inglewoodpete

Senior Member
I have spent about 9 hours trying to get a 20X2 (Firmware C.1/PE Version 5.3.1) to interrupt on alternate rising/falling edges of a square wave.

The input signal has a frequency of about 500Hz, so the is plenty of time for the 20x2 to react to both the rising and falling edges. I am using a 60MHz/150MS/s oscilloscope to observe the input and output waveforms.

I have managed to get the PICAXE to trigger reliably on rising edges.
Code:
Symbol Version = 4 '0.4 27-Sep-2010   68 bytes testing negative triggering
'
#Picaxe 20X2
#No_Table         'Also prevents EEPROM Data from Downloading
'
'Connections for 20x2
Symbol oSynch     = B.7      'Leg 11 B.7 
Symbol iInfra     = pinB.1   'Leg 17 B.1/hInt2 
Symbol oLED1      = B.0      'Leg 18 B.0
Symbol oLED0      = A.0      'Leg 19 A.0/Serial Out
'
Init:    SetFreq m64
         Input B.1            'hInt2
         hIntSetup %01000100  ' spare|cond2|cond1|spare||spare|hInt2|hInt1|spare (20x2: hInt2 = B.1)
         '           Condition  Input Mask
         SetIntFlags %00000100, %00000100      'set hardware int 2 (B.1) to interrupt
         '            ||||||||_ bit0: hInt0Flag INT0 (X2 series chips only      28x2 40x2)
         '            |||||||__ bit1: hInt1Flag INT1 (X2 series chips only 20x2 28x2 40x2)
         '            ||||||___ bit2: hInt2Flag INT2 (X2 series chips only 20x2 28x2 40x2)
         '            |||||____ bit3: hIntFlag  INT0, 1 or 2 (X2 series chips only)
         '            ||||_____ bit4: CompFlag  Comparator flag (X2 series chips only)
         '            |||______ bit5: hSerFlag  Background receive has occurred
         '            ||_______ bit6: hi2cFlag  hi2c write has occurred (slave mode)
         '            |________ bit7: TOFlag    Timer overflow flag
         '
         Pause 4000
         Low oLED0
         Low oLED1
         Do                                   'Do something in the foreground
            For w10 = 1 To 200
               w11 = w10 + 3
            Next w10
         Loop
'
' ***** Interrupt Routine **********************************
'
Interrupt:If iInfra = 0 Then
            'iInfra is currently on (low). Configure for trigger on next rising edge.
            PulsOut oSynch, 10
            Low  oLED0
            High oLED1
            hInt2Flag = 0
            hIntSetup %01000100               'Next interrupt will be when hInt2/B.1 goes high
            SetIntFlags %00000100, %00000100  'Set hardware int hInt2/B.1 to interrupt
         Else   'Infra = 1 (normal)
            'Infra is currently off (high). Configure for trigger on falling edge.
            PulsOut oSynch, 80
            High oLED0
            Low  oLED1
            hInt2Flag = 0
            hIntSetup %00000100               'Next interrupt will be when hInt2/B.1 goes low
            SetIntFlags %00000000, %00000100  'Set hardware int hInt2/B.1 to interrupt
         EndIf
         Return
Is there further documentation on hardare interrupts, other than Manual 2, that I have overlooked?

Yes, I have read the latest command manual (Version 7.0). I have to add that I think it is inadequate in the area of interrupts. There are no examples in the manual - only examples of individual commands. There appear to be a lot of duplication of the bits in the hIntSetup and SetIntFlags commands: are they all required as per my code, or are they interacting? Am I understanding the descriptions correctly?

Finally, I find the statement in the SetIntFlags command description distracting: "Please also see the detailed usage notes under the ‘setint’ command, which also apply to the ‘setintflags’ command." What parts, specifically? Also, the statement "A polled interrupt is a quicker way of reacting to a particular event. It is the only type of interrupt available in the PICAXE system.", under SetIntFlags description. Why do we have hardware interrupts then?

Peter
 

hippy

Technical Support
Staff member
The issue is with the SETINTFLAGS %00000000, %00000100 - This interrupts when the HINT2FLAG drops low. The rest seems correct ...

First you set HINTSETUP to flag the input going high, which sets HINT2FLAG, needs to cause an interrupt.

Second you then set HINTSETUP to flag the input going low, which also sets HINT2FLG, needs to cause an interrupt.

IN both cases you need to interrupt on HINT2FLAG going high.

Code:
#Picaxe 20X2
#No_data
#No_Table

SetFreq M64

b0 = %01000100
Gosub EnableInterrupts

Do : Loop

Interrupt:
  Toggle B.7
  b0 = b0   ^ %01000000

EnableInterrupts:
  hInt2Flag = 0
  HIntSetup   b0
  SetIntFlags %00000100, %00000100
  Return
There are no instantaneous hardware interrupts on the PICAXE which immediately interrupt what it may be doing, but there are deferred hardware interrupts, which is what HINTSETUP with SETINTFLAGS gives you.
 
Last edited:

inglewoodpete

Senior Member
The issue is with the SETINTFLAGS %00000000, %00000100 - This interrupts when the HINT2FLAG drops low. The rest seems correct ...

First you set HINTSETUP to flag the input going high, which sets HINT2FLAG, needs to cause an interrupt.

Second you then set HINTSETUP to flag the input going low, which also sets HINT2FLG, needs to cause an interrupt.

IN both cases you need to interrupt on HINT2FLAG going high.

(code omitted)

There are no instantaneous hardware interrupts on the PICAXE which immediately interrupt what it may be doing, but there are deferred hardware interrupts, which is what HINTSETUP with SETINTFLAGS gives you.
Fantastic! Your code works (of course it would!). It's after midnight here, so I'll digest what is happening and where I was going wrong after I've had some sleep.

Many thanks, once again, hippy
 

inglewoodpete

Senior Member
Suggested change to the command description

I was aware of polled nature of PICAXE's interrupts and the benefits of the hInt being able to 'catch' narrow pulses that could occur during the execution of a single compiled command. These narrow pulses could easily be missed with the earlier form of polled interrupts.

In the light of hippy's comments, I've had a re-read of the hIntSetup and SetIntFlags command descriptions. Now that I know the relationship and sequence between the (event)-->hIntSetup-->(flags)-->SetIntFlags-->(gosub interrupt), I have a better understanding of configuring hardware interrupts. However, I still find the command descriptions far from definitive. I'm sure others will fall into the same traps I did.

I suggest, at the very least, the following text be added to the command descriptions for hIntSetup and/or SetIntFlags:

"The hardware interrupt is a two stage process where the edge detection, configured via the hIntSetup command, sets the appropriate bits in the 'flags' byte. The SetIntFlags command configures how the polling detects the a changed flag state and results in the Interrupt routine being executed when required."

I also suggest a code snippet be added to the command description/s to show what is required to set and reset hardware interrupts.
 

geoff07

Senior Member
Having wrestled with this myself today I second inglewoodpete's remarks concerning documentation. I feel it could all be said with fewer words and much more clarity including some examples. The hardware interrupt flag seems exactly the way to go for any project that needs interrupts and is using a chip with the capability. It only looks complicated because it is presented that way.

Specifically, the fact that the individual flag needs to be cleared to reset the interrupt was not obvious to me. I recall that interrupts were automatically cleared when the interrupt routine was called. Not so with these, but only obvious when you think about after it went wrong.
 

billacj

Member
Now that my edge triggered interrupts are working I would like to add my support for a clearer explanation of the 20X2 interrupts and echo the thoughts from the previous posts on this thread. These interrupts are powerful, they work but it was a long time getting there. Thanks to Hippy for the final details.

I find the explanations for Program SLOTs to be another subject needing clearer explanation and working detail. Program SLOTs combined with interrupts look like a very powerful combination.

Thanks for your consideration, Billacj picaxe 08m, picaxe 18m, picaxe20X2
Projects completed: PWM light and motor controls, Solar Controller, Multi-motor controller, Robotics . Looking forward to display panel development.
 
Top