PDA

View Full Version : Spin help



Cenlasoft
04-02-2012, 03:27 AM
Hello,
I am trying to send a squarewave on two pins that are 180 degrees apart. I got that done I think. I am using an h-bridge to excite an ultrasonic transducer. from 5 V I will come out with a 10 vpp. It works. My spin code has something wrong. On the scope I see a squarewave (40KHZ). How do I time it to get .5 ms of the 40khz pulse then turnoff the h-bridge and listen for an echo.
Thanks, Cenlasoft


{{
Template.spin
Curtis Desselles
}}
Con

_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000

Pin1 = 0
Pin2 = 1



Var


Obj
Pub Main
repeat
TX_Out 'TX a pulse x 5
dira[Pin1..Pin2]~~ 'Make pins output
outa[Pin1..Pin2] := %00 'Turn off h-bridge

Pub TX_Out | i
repeat i from 0 to 4
ctra := %00101 <<26 + Pin2<<9 +Pin1
frqa := 1<<31 / (clkfreq / 80_000)
dira |= |<Pin1 + |<Pin2
Return

Mike Green
04-02-2012, 03:41 AM
1) Note that in Main, the DIRA bits for pins 1 and 2 are already set to output mode. You don't have to do it again.

2) If you still want pins 1 and 2 to be outputs with the h-bridge off, you could zero CTRA and do: OUTA[Pin1..Pin2]~ or OUTA[Pin1..Pin2] := $00

3) To get a particular pulse length, just wait that time, then set CTRA and the output pins to zero. Use: WAITCNT(CLKFREQ/2000 + CNT)

4) Remember that other statements will take a few microseconds. That may affect the 0.5ms pulse length. You may have to shorten the delay to compensate.

The code brackets use [] not <>

Ariba
04-02-2012, 05:09 AM
The simplest way is to set the pins high in outa to stop the 40kHz output. This works because outa and the counter output are ORed together:

_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000

Pin1 = 0
Pin2 = 1

Pub Main
ctra := %00101 <<26 + Pin2<<9 +Pin1
frqa := 1<<31 / (clkfreq / 80_000)
dira[Pin1..Pin2] := %11

repeat
outa[Pin1..Pin2] := %00 'on
waitcnt(clkfreq/2000 + cnt) 'wait 0.5ms
outa[Pin1..Pin2] := %11 'off
' wait for response here
waitcnt(clkfreq/2 + cnt) 'pause 0.5s (optional)


Andy

Mark_T
04-02-2012, 01:57 PM
The simplest way is to set the pins high in outa to stop the 40kHz output. This works because outa and the counter output are ORed together:


Andy
Depending on how the pins control the H-bridge setting them both high may short it out. We know from the original code that 00 turns it off.

Cenlasoft
04-03-2012, 01:14 AM
Thanks everyone. Mark_T got it right. Setting both pins high on the h-bridge burned it. No problem though, I have plenty of them. I learned something. Thanks Ariba, good code to use. I'll play with the new setup and post the results. Thanks Mike for the suggestions, they helped.
Cenlasoft.

Ariba
04-03-2012, 01:42 AM
You can also switch on and off with dira[Pin0..Pin1] an use two pulldown resistors for the off state.
dira[Pin0..Pin1] := %11 'ON
dira[Pin0..Pin1] := %00 'OFF

Andy

Mickster
04-03-2012, 08:58 PM
Hello, Curtis. I sent you a PM.

Regards,

Mickster