PDA

View Full Version : Pulsing two pins to create 40 khz



Cenlasoft
03-25-2012, 02:47 AM
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

Mike Green
03-25-2012, 02: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
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.

Ariba
03-25-2012, 06: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:

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

Cenlasoft
03-25-2012, 11:50 PM
Thanks Mike and Ariba for your help. I think the suggestions will help. I'll repost my end results.
Thanks, Cenlasoft