Can not count low frequency triangular pulses
The following code will count square wave and triangle wave (more like sawtooth) - in kHz.
The problem is counting low frequency triangle wave - or pulses.
I can count a 5 Hz square wave, but not a 5 Hz triangle wave, or my input for that matter.
FYI, pin 15 flashes an LED to see the interval at which the μC is counting the pulses - so every second my LED flashes, not really necessary, but it is what it is. Same with serial output (i) for anyone curious.
The problem is counting low frequency triangle wave - or pulses.
I can count a 5 Hz square wave, but not a 5 Hz triangle wave, or my input for that matter.
CON _clkmode = xtal1 + pll16x 'System clock → 80 MHz _xinfreq = 5_000_000 pin1 = 16 ms = 1000 VAR long stack[200] long pulse long i OBJ ser : "FullDuplexSerial" PUB TestFrequency Ser.start(31,30,0,115200) waitcnt(clkfreq * 2 + cnt) cognew(Display, @stack[0]) ctra := %01010 << 26 + pin1 'counter A in POSdetect mode frqa := 1 phsa := 0 dira[15]~~ repeat waitcnt(clkfreq/1000 * ms + cnt) pulse := phsa phsa := 0 i := !i ! outa[15] PUB Display Repeat ser.dec(pulse) ser.tx(13) ser.dec(ina[16]) ser.tx(13) ser.dec(i) waitcnt(clkfreq / 15 + cnt) ser.tx(0)Anyone have success counting low frequency triangular pulses? I know I can probably use an OP amp and make a Schmitt Trigger, or a 555, but something tells me I shouldn't have to.
FYI, pin 15 flashes an LED to see the interval at which the μC is counting the pulses - so every second my LED flashes, not really necessary, but it is what it is. Same with serial output (i) for anyone curious.
Comments
I have shown a code and a hardware solution there.
Andy
With your NPN based Schmitt Trigger - what do you connect to FB_pin?
-I'm assuming the input signal goes to the base of the NPN?
I still think something's wrong(that doesn't require extra components) if the code can count kHz and even MHz, but not Hz.
The NPN is a Phototransitor, which was part of the circuit in that thread. You only need 2 resistors for a SchmittTrigger input.
But if you have a spare cog, try this (untested) code:
This simulates the counter pos detector in a cog, but much slower.
The counter samples the input with 80MHz and detects a pos-edge if the previous sample was Low and the current is High. This fast sampling frequency together with the very sharp thershold point of the input pin detects every little noise on the signal while your input signal stays near the threshold voltage.
The code above lowers the sampling fequency to some kHz and therefore the noise between the samples is not detected and not counted as pulses.
Andy
Your code doing the comparison seemed to reduce the offset, however I also noticed increased "false positives".
Right now I've restructured the circuit to give a very "generic" pulse, sinusoidal really, and I've actually seen improved results. I've had to scale down the data - as each pulse likely breaks the threshold multiple times.
Adding a low pass filter reduced the noise and also helped the counts.