Shop OBEX P1 Docs P2 Docs Learn Events
Spin help — Parallax Forums

Spin help

CenlasoftCenlasoft Posts: 265
edited 2012-04-03 12:58 in Propeller 1
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  

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-01 19:41
    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 <>
  • AribaAriba Posts: 2,690
    edited 2012-04-01 21:09
    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_TMark_T Posts: 1,981
    edited 2012-04-02 05:57
    Ariba wrote: »
    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.
  • CenlasoftCenlasoft Posts: 265
    edited 2012-04-02 17:14
    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.
  • AribaAriba Posts: 2,690
    edited 2012-04-02 17:42
    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
  • MicksterMickster Posts: 2,721
    edited 2012-04-03 12:58
    Hello, Curtis. I sent you a PM.

    Regards,

    Mickster
Sign In or Register to comment.