Help needed with Counters
DRMorrison
Posts: 81
Hello, Hope someone can help.
I've created a small SPIN program to test a signal coming from my car. The alarm system has a LED that flashes once each second. The signal is pos 12V with a 20ms dip to zeroV.
I'm trying to use the Neg Edge detector of the counter to count dips in the signal that happen 4 or more times in a 2 second period. This lets me know that the alarm has been activated.
Otherwise, the signal drops to zero once each second, and the counter should only count a maximum of 2 dips in a 2 second period.
The code listed does not work as expected. The counter should not count but more than two neg edges in a 2 second period. But, the count variable increases by one no matter how
few neg edges are put on pin 0.
I'm new to using counters, so I suspect that I'm missing something. If you can see it, let me know. And thanks in advance.
(This is run on a demo board.)
I've created a small SPIN program to test a signal coming from my car. The alarm system has a LED that flashes once each second. The signal is pos 12V with a 20ms dip to zeroV.
I'm trying to use the Neg Edge detector of the counter to count dips in the signal that happen 4 or more times in a 2 second period. This lets me know that the alarm has been activated.
Otherwise, the signal drops to zero once each second, and the counter should only count a maximum of 2 dips in a 2 second period.
The code listed does not work as expected. The counter should not count but more than two neg edges in a 2 second period. But, the count variable increases by one no matter how
few neg edges are put on pin 0.
I'm new to using counters, so I suspect that I'm missing something. If you can see it, let me know. And thanks in advance.
(This is run on a demo board.)
CON {{Test LED alarm signal for over-count during ON state of alarm: If the uProcesor counts incorrectly the number of LED pulses during the alarm ON state, count variable will increase and cause LED 23 on the demo board to flash once for each error. }} _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 LEDpin = 0 var long count PUB Main dira[LEDpin]~ 'input dira[16..23]~~ 'output LEDs ctra := constant(0110 << 26 | LEDpin) '0100 = NEG Edge detector. LED of alarm is active low when flashing. frqa := 1 count := 0 Repeat repeat 1 'flash LED 16 once/loop to indicate running app. !outa[16] waitcnt(clkfreq/50 + cnt) !outa[16] phsa := 0 'clear counter waitcnt(clkfreq*2 + cnt) 'wait 2sec if phsa => 4 'check for over-count count += 1 'add to flasher count if over-count repeat count !outa[23] 'flash LED 23 count times waitcnt(clkfreq/20 + cnt) !outa[23] waitcnt(clkfreq/30 + cnt)
Comments
You set phsa to 0, then you have an IF statement testing to see if it's => 4. What are you trying to do there?
The other thing is that you don't clear count, i.e. once you had an alarm going on count will stay e.g. 1 (which in turn will falsh you LED). That's based on the code you posted.
I'm trying to count the number of low pulses on the alarm LED flasher of my auto. The signal is goes low for 20ms once per second, but then goes low twice per second when an alarm
is detected. By counting the low pulses for two seconds, I thought I could detect when the alarm went active: phsa => 4
PM
kuroneko
For some reason some of the code was not pasted when I copied it over from the Parallax Tool, such as ctra := constant(01110 << 26 | LEDpin)
should have read (110 << 26 | LEDpin)
I'm currently testing this on the bench, and each time I send a neg pulse the count variable gets incremented, causing the second LED to flash.
The first LED flashes once for each trip through the loop, jut to indicate that it is running. The second LED should only flash if the count var increases,
and it should only increase if and when phsa sees more that 4 negative edges on the input, pin 0 in a 2 second period.
The count var increases if phsa indicates more than 3 low edges have been detected on pin 0 during the 2 second period. If so, the second LED flashes
more than once to indicate such.
Maybe this is clearer: I want to count the number of negative pulses on the alarm LED signal during a 2 second period. If it goes to 4 or above, an alarm
is active.
I plan on setting this up in the auto over night to see if the timing is correct; a sort of trial run.
Hope this is clear, Thank you, Daniel
Do you have a way of tracing the number of edges detected during the 2 second interval?
As for your binary numbers, they still miss the leading % character. Copy/paste works for me from the PropTool. I never lost anything so far. Can you confirm that they are in the PropTool?
I think kuroneko has some very good points.
Also, when you do this:
!outa[16]
waitcnt(clkfreq/50 + cnt)
!outa[16]
I think you're only giving yourself 1/50 of a second to see the LED, true? Can you even see it blink in that short amount of time?
It's okay, 'cause the second LED will blink more than once only if phsa counts 4 or more multiple times; at least that is how it is suppose to happen.
If phsa counts 4 or more on several occasions, the LED will blink that many times, telling me that 4 or more were detected during the test period.
The fault is that the counter is suppose to count the edges in phsa, and only advance the count var if phsa goes to 4 or more during the 2 second period.
It increases even if there is only one neg pulse.
ElectricAye
I can see the flash; those LEDs are bright.
This is suppose to only count during the 2 second pause, and then report if phsa goes to 4 or more, increasing count by one, and then causing the second LED to flash
count times. This tells me later that the count was indeed 4 or more. This is to be set up in the car to monitor the signal overnight. I'm testing this on the bench, and it's
behaving badly. I've never used these counters before, so I suspect that this is my problem.
Thank you, Daniel
I did however figure out why this was not working. phsa was counting more than 4 counts on various button pushes due to button bounce. Duh!
This should now work once connected to the auto's alarm signal wire--no bounce.
Thanks for everyones help, Daniel