Shop OBEX P1 Docs P2 Docs Learn Events
How to use the Counter in SPIN to create a carrier? — Parallax Forums

How to use the Counter in SPIN to create a carrier?

william chanwilliam chan Posts: 1,326
edited 2008-12-10 05:31 in Propeller 1
Hi,

Let's say I need to create a carrier for an IR led at 38Khz.
I believe using a loop in SPIN is too slow for 38Khz. ( Assembly would use up another cog )
Can I use a cog's CTRA or CTRB in SPIN to do this?

Any example codes to set up the counters in SPIN or to start or pause the carrier would be most appreciated.

Thanks.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.fd.com.my
www.mercedes.com.my

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-09 02:04
    Download the frequency synthesis object or use the FREQOUT routine in the BS2 compatibility library object, both in the Propeller Object Exchange. Either will do what you want.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-12-09 04:42
    William,

    Once you've been able to get a steady 38KHz output, you can modulate it by alternating the same pin (via OUTA) between 0 and 1. When 0, you will see the 38KHz output; when 1, a straight high. This is because the output from the counter is ORed with OUTA. This also implies that you will want to use current-sinking to drive your IRED: i.e. tie the anode high through a current-limiting resistor, and connect the cathode to the pin. This way, when the output is high, the IRED is off.

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Just a few PropSTICK Kit bare PCBs left!
  • william chanwilliam chan Posts: 1,326
    edited 2008-12-09 06:08
    Mike and Phil,

    You guys are genius!

    Why can't I set

    frqa := 0 and
    outa[noparse][[/noparse]pin] := 0

    to pause (modulate) the 38Khz carrier? Too slow?

    My circuit is already connected to a bipolar NPN transistor which sinks the current from the IRED to ground, so I can't have the
    output pin high when idle.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.fd.com.my
    www.mercedes.com.my

    Post Edited (william chan) : 12/9/2008 6:15:22 AM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-09 06:46
    FRQA set to zero will stop the counter from accumulating more counts, but the output state is still set by the PHSA value. You'll have to zero that as well. You could just leave OUTA[noparse][[/noparse]pin] set to zero since the counter sign bit is or'd with that to produce the output.

    You'd do FRQA~ followed by PHSA~ to stop the 38KHz carrier. You'll have to set them both to the proper values to start the carrier again. This would certainly be fast enough, a fraction of a cycle of the carrier.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-12-09 06:53
    William, outa[noparse][[/noparse]pin] := 0 won't do anyting, since it's ORed with the counter output. Setting frqa[noparse][[/noparse]pin] to zero is not a good idea, since you don't know what state the counter will stop in. A better option would be to set cntra to zero then back to the carrier-generating value to modulate the pin's output. Better still, since you're using an NPN transistor, would be to alternate dira[noparse][[/noparse]pin] between zero and one.

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Just a few PropSTICK Kit bare PCBs left!
  • Ken PetersonKen Peterson Posts: 806
    edited 2008-12-09 12:27
    I was just going to say "try dira[noparse][[/noparse]0]~" to shut it off, but then I noticed Phil covered that one. Only problem with this is that it won't work if you're trying to modulate it with a different cog.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"I have always wished that my computer would be as easy to use as my telephone.· My wish has come true.· I no longer know how to use my telephone."

    - Bjarne Stroustrup
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-12-09 19:14
    Ken Peterson said...
    Only problem with this is that it won't work if you're trying to modulate it with a different cog.
    Good point, Ken, and well-noted! In fact, my other suggestion won't work from another cog, either, since it's a different ctra. The only thing that will work from another cog is to use outa[noparse][[/noparse]pin]~~ in an active-low circuit. So I guess the lesson here is this: drive a modulated LED with a PNP transistor, not an NPN.

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Just a few PropSTICK Kit bare PCBs left!
  • william chanwilliam chan Posts: 1,326
    edited 2008-12-10 03:32
    If we use a PNP transistor, the power for the IRED has to come from a 3.3v source or else it would leak into the Prop pin.
    But using 3.3v to power the IRED would heavily load the 100mA 3.3v regulator.

    If we use a NPN transistor, the power source can be tapped from 3.3v, 5v or 12v supplies. More choices. smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.fd.com.my
    www.mercedes.com.my
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-12-10 03:45
    William,

    You could use a separate 3.3V regulator for the IRED and still drive it with a PNP. Its forward voltage is only about 1.2V, so there's no necessity to sink it from a 5V or greater supply — as there would be for, say, a blue LED with a much higher forward voltage.

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Just a few PropSTICK Kit bare PCBs left!
  • Scott LewisScott Lewis Posts: 18
    edited 2008-12-10 05:31
    to use frqa := notation instead of the object, here is the code you need

    ctra[noparse][[/noparse]30..26] := %00100                    'Configure Counter A to NCO
    ctra[noparse][[/noparse]8..0] := PIN                              'Set Counter A Output to PIN
    frqa := 2_040_109                           '= (38kHz)*(2^32)/(clkfreq)
    
    



    this is assuming clkfreq is 80 MHz
Sign In or Register to comment.