View Full Version : Pulsing two pins to create 40 khz
03-25-2012, 03:47 AM
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
03-25-2012, 03:58 AM
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
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.
03-25-2012, 07:30 AM
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:
pin1 = 0 'set your pin numbers here
pin2 = 1
ctra := %00101<<26 + pin2<<9 + pin1
frqa := 1<<31 / (clkfreq / 80_000)
dira |= |<pin1 + |<pin2
03-26-2012, 12:50 AM
Thanks Mike and Ariba for your help. I think the suggestions will help. I'll repost my end results.