[PASM] Trying to use POSEDGE counter - turn on a LED if freq threshold exceeded
I'm trying to measure a variable frequency 50% duty cycle square wave on pin P0 (physical pin 1 on the P8x32A-D40 package).
I'm feeding a square wave into P0, and I put an LED on P1. There are no other connections.
If the detected frequency (sampled over 1 second intervals, resetting PHSA each time) exceeds 100 Hz then I want to turn the LED on.
But it's as if the counter isn't running or something, I can't figure out why this isn't working. The signal into the pin is definitely toggling at something around 1500 Hz (it varies with ambient light levels).
The muxnc never turns the led on, even though the frequency is much higher than 100 Hz. If sample is 1500 then the cmp 1500, 100 WC should clear the C flag and the LED should always be on, right?
CON _clkmode = xtal1 | pll16x _xinfreq = 5_000_000 PUB Main coginit(0, @PASM, 0) DAT org 0 PASM or dira, LED 'output mov sample, #0 'clear the sample cache 'start the counter mov CTRA, CTRA_MODE mov FRQA, #1 'each detected rising edge adds this number to PHSA mov time, CNT add time, ONE_SECOND :sample_period_start mov PHSA, #0 'reset the count 'examine the previous cached sample cmp sample, INTENSITY_THRESHOLD WC muxnc outa, LED 'if (sample >= INTENSITY_THRESHOLD), turn on the LED waitcnt time, ONE_SECOND 'when we get here, exactly 1 second has passed, so cache the sample mov sample, PHSA jmp #:sample_period_start ONE_SECOND long 80_000_000 'PLL div/1, POSEDGE detect single ended, pin P0 input CTRA_MODE long %0_01010_111_00000000_000000_000_000001 INTENSITY_THRESHOLD long 100 'Hz SAMPLE_PIN long |< 0 LED long |< 1 time res 1 sample res 1 fit
I'm obviously being a bonehead, can anyone find the problem?