Shop OBEX P1 Docs P2 Docs Learn Events
Pulsing two pins to create 40 khz — Parallax Forums

Pulsing two pins to create 40 khz

CenlasoftCenlasoft Posts: 265
edited 2012-03-25 16:50 in Propeller 1
Hello,
I have an h-bridge (ZXMHC3A01T8) that I am using to excite an ultrasonic transducer. I need to turn one prop pin high and the other low to produce a 40 khz pulse. How would I calculate the waitcnt between the pins on/off to get this frequency? I have the hardware figured out. Any help would be great. Thanks, Cenlasoft

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-03-24 19:58
    Let's see ... 40KHz is the cycle time. That means you want the signal to toggle at an 80KHz rate. To get the number of clock cycles to wait, you use CLKFREQ / 80000 since CLKFREQ is the number of clock cycles in one second. You could use a loop like:
    mask := %11 ' for the I/O pins used
    delta := clkfreq / 80000
    t := cnt
    repeat
       waitcnt(t += delta)
       outa ^= mask ' toggle both bits
    
    Even though Spin is relatively slow, the WAITCNTs are precisely timed to the same interval and the toggle operation is at a consistent delay from that timed interval, so the frequency should be quite accurate. The two I/O pins are changed at the same time, within a few nanoseconds because of small propagation delays internal to the Propeller itself.
  • AribaAriba Posts: 2,690
    edited 2012-03-24 23:30
    I don't think Mikes Spin loop works with 80kHz. Either you do it in Assembly or you use a counter in Spin.
    There is a counter mode which outputs NCO on two pins with inverted signals:
    CON
       pin1 = 0    'set your pin numbers here
       pin2 = 1
    
    PUB
      ...   
      ctra := %00101<<26 + pin2<<9 + pin1
      frqa := 1<<31 / (clkfreq / 80_000)
      dira |= |<pin1 + |<pin2
      ...
    
    

    Andy
  • CenlasoftCenlasoft Posts: 265
    edited 2012-03-25 16:50
    Thanks Mike and Ariba for your help. I think the suggestions will help. I'll repost my end results.
    Thanks, Cenlasoft
Sign In or Register to comment.