Question RE Execution and program flow. Input pulse not detected sometimes

oKors

New Member
I have a very simple circuit containing a PIR and a counter. The PIR passes a high signal to the 08M via Pin1 and then outputs a high through Pin4 which sends this pulse to a totalizer/counter.
My problem is, sometimes even though the PIR sends a brief HI to the chip the chip will not catch the signal, so nothing gets output to the counter. I noticed this more clearly when I ran a Simulation. While the program is running I can watch the code execute from top to bottom, however if I click on Pin1 briefly somewhere in the middle of the code no signal is sent. I believe this is because the program needs to see the HI on Pin1 at the start of the code then it continues down the code following the rest of the commands.

I’m sure this is a big “DUH” to most of you but is there a way to catch these HI inputs more reliably or is this just the nature of PIC’s and program flow?

Code (ignore the LED):
Code:
symbol PIR = pin1
symbol LED = 4

main:
       b0 = PIR
       if b0 = 1 then 
                 high LED
     
       else
                 low LED
       endif
       goto main
 
Last edited:

MartinM57

Moderator
Have a look at Manual 2 for interrupts, specifically the SETINT command - that way, the input pin will be checked between every line of code (and inside PAUSE commands (and TUNE?) as well)

Come back and ask what's not clear after a good read :)
 

hippy

Ex-Staff (retired)
@ oKors : You're right, it does depend on when the PICAXE reads the signal and if it's not present when you come to read it you won't see it.

There are a few tricks to improve response times; SETINT as MartinM57 suggests, plus running with SETFREQ M8, and even rewriting the code in a different way.

Another trick is to run the trigger signal through an RC resistor-capacitor circuit, possibly a diode-capacitor circuit, which will stretch the length of the pulse from the PIR.
 

oKors

New Member
Awesome you guys rule! I will have a look at the SETINT and SETFREQ commands and modify my code to see if that helps.

I'm reluctant to add any delay circuitry to my existing circuit because I expect the PIR to see multiple heat signatures in rapid succession at times. The .5 second HI delay already inherant in the PIR I'm using is already slow enough.

Also @ hippy; how would you suggest rewriting this code? I'm really new at this and the information is overwhelming at times. The code I wrote is just based on simple information I've gathered so far.

Thanks!
 
Last edited:

hippy

Ex-Staff (retired)
If PIR input is normally low and goes high when motion is detected I'd go for ...

Low LED
Do : Loop Until PIR = 1
High LED
etc

That will wait until the signal goes high then continues when it does. You don't need to keep setting the output LED low. This will give the quickest response to PIR going high with only interrupts and SETINT bettering it.
 

alphamike27

New Member
I had a similar problem with picking up a very brief VT output from a Holtek HT12D decoder on a 433MHz rf system.

SETINT solved all of the problems.

But, you need to study the instruction closely because there are some traps...................
 

boriz

Senior Member
“... sometimes even though the PIR sends a brief HI to the chip the chip will not catch the signal ... The .5 second HI delay already inherant in the PIR I'm using is already slow enough.”

If the minimum high time is 500mS, then your first program should be plenty fast enough. Or did I misunderstand you? How are you measuring this ‘brief HI’ from the PIR. A ‘scope? How brief is brief?
 

Dippy

Moderator
I would start off by putting hippy's code (or similar) in a loop.
i.e. force the LED indicator to 'follow' the input from the PIR.
And then, as alluded to by Bowiz, study it on a 'scope if you think some weird triggering is happening.
I would always check that as a matter of good practice - remove 'variables' when doing diags.

We haven't been given a link to the PIR , as usual, so I assume this is an 'average' PIR module which has the usual lazy response times.
I also assume that the output voltage is of a sufficient amplitude? And that it is all wired as per intructions?

Obviously when you get the real code you will have to allow for variable 'on' times.
I'd be tempted to use a button press/release code method.
If PIR=1 then
Do:Loop Until PIR=0
increment counter / flash LED
endif

Some anti-false-trigger pauses may help, but something like that in effect.
Actual timings depend on the PIR module and only you know it's behaviour.
 

boriz

Senior Member
Like Hippy, said. “Obviously ... you will have to allow for variable 'on' times...”

Ultimately the question becomes, how short an ‘on’ time do you want to detect?
 

oKors

New Member
Guys, I'm having a difficult time wrapping my head around this. I read and re-read about the SETINT function, 'attempted' to write it into my existing code. Bottom line is, I'm having no success whatsoever and it's becoming a bit discouraging.

I hate to throw in the towel and ask this but, is there anyway someone could just write a working code for me so that I can copy it to the chip, or run a simulation. I need to 'see' some practical code, something that is working in order to understand it. Once I can see what's going on I can modify and tweak it; its just right now I'm spinning my wheels and probably doing more harm than good for myself.

All I want to do is take a PIR input and output a high with a short delay (0.5 sec to start with). This high output will trigger a self-powered counter.
I've already written a code that does at least that much (in its simplest form), but I need it to watch for the input pin between lines of code as mentioned earlier in this post.

The PIR I'm using is a Panasonic NaPiOn AMN43121, and 08M chip.

Feel free to modify what I already have or just start over if someone has a better way of doing it.

I appreciate any and all help so far
 

geoff07

Senior Member
What you need is something like this:
Code:
symbol intflag = bit0
setint       'make this so that when the PIR signal occurs, an interrupt is triggered
intflag = 0

do
   if intflag = 1 then
      high output_to_counter
      pause 10
      intflag = 0
      low output_to_counter
   endif
loop
end

interrupt:
   if pir = 1 then 
      intflag = 1
   endif
setint  'reset the interrupt as before
return
If you set up the interrupt properly so it triggers on the pir signal, then it will set the flag.
In the main program loop it looks for the flag and if it sees it, it sends the output signal, waits for a period (to give you an output pulse the length you require) and then resets the flag and the output.

I'm afraid you need to add the details for your hardware.
 
Top