Shop OBEX P1 Docs P2 Docs Learn Events
Can not count low frequency triangular pulses — Parallax Forums

Can not count low frequency triangular pulses

pthartmannpthartmann Posts: 27
edited 2011-05-04 07:05 in Propeller 1
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.
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

  • AribaAriba Posts: 2,690
    edited 2011-05-02 20:32
    Look at this thread: http://forums.parallax.com/showthread.php?130559-Problem-counting-pulses&highlight=count+pulses
    I have shown a code and a hardware solution there.

    Andy
  • pthartmannpthartmann Posts: 27
    edited 2011-05-03 09:17
    Thanks Andy. I had actually read through that thread several times before posting.

    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.
  • AribaAriba Posts: 2,690
    edited 2011-05-03 10:49
    pthartmann wrote: »
    Thanks Ariba. I had actually read through that thread several times before posting.

    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:
    CON
       
      _clkmode = xtal1 + pll16x                   'System clock &#8594; 80 MHz
      _xinfreq = 5_000_000
    
     pin1 = 16
     ms = 1000
      
    VAR
    
      long stack[200]
      long pulse, pcount
      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    
        cognew(PulsCount, @stack[180])
    
      frqa := 1
      phsa := 0 
      dira[15]~~    
          
     repeat
         waitcnt(clkfreq/1000 * ms + cnt)
         pulse := pcount
         pcount := 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)
    
    PUB PulsCount | t1,t2
     t1 := ina[pin1]
     repeat
       t2 := t1
       t1 := ina[pin1]
       if (t1^t2) & t1  'detect pos edge
         pcount++
    

    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.
    attachment.php?attachmentid=80838&d=1304445153
    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
    313 x 130 - 2K
  • pthartmannpthartmann Posts: 27
    edited 2011-05-04 07:05
    Thank you 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.
Sign In or Register to comment.