Shop OBEX P1 Docs P2 Docs Learn Events
Variable frequency output — Parallax Forums

Variable frequency output

Captain BlammoCaptain Blammo Posts: 3
edited 2005-01-23 19:41 in BASIC Stamp
I'd like to make an LED flash at a rate controlled by a couple of pots (one for frequency, the other for pulse width), but I've got a bit stuck on how to accomplish this with a BS2SX.

I'd just use FREQOUT, but it seems that it generates a sine wave, where I'd really want a square wave. The PWM command doesn't give you neatly divided on/off time, so that's out. If I was going to use a fixed pulse width, I could just use PAUSE and PULSOUT, though this would still limit me to around 1kHz maximum.

So I suppose my question is: How do you generate a square wave with easy-to-specify frequency and pulse width? Is there an easy way that I don't know about?

Thanks for any help!

CB

Comments

  • BeanBean Posts: 8,129
    edited 2005-01-23 14:19
    What frequency range do you want to pulse at ?
    Bean.
  • Captain BlammoCaptain Blammo Posts: 3
    edited 2005-01-23 16:29
    1-1000Hz would be fine, though the higher I can set it the better. The real problem with using PAUSE and PULSOUT is that changing the pulse width will affect the frequency, which I don't want to happen.
  • BeanBean Posts: 8,129
    edited 2005-01-23 19:07
    I don't see how you can do that with just the stamp.

    Take a look at the PWMPAL chip from parallax.

    http://www.parallax.com/detail.asp?product_id=28020

    Or you could do it with the SX chip and SX/B, but you would need the SX-Key programmer.

    Bean.
    ·
  • Jim McCorisonJim McCorison Posts: 359
    edited 2005-01-23 19:41
    CB,

    My brain is a little sleepy from an all-you-can-eat pancake feed this morning, so maybe I'm not catching what you are trying to do. But.. I think the following is what you describe

    freq    var   byte      ' The full wave frequency
    width   var   byte      ' The width of the on pulse
    fudge   var   nib       ' Fudge to account for execution times
    led     PIN  1          ' I/O pin for the LED
    
    do
      led = 1
      pause width
      led = 0
      pause freq - width - fudge
    loop
    
    



    Fudge is used to compensate for the execution overhead in the loop. Depending on how accurate the timing needs to be you'll have to twiddle fudge to make the loop come out right. But once set you shouldn't have to change it unless you move it to another model of stamp.

    Jim

    added: Obviosuly the above is just a control snippet. You've have to work in getting the pot settings and their affect on the timing loop, etc.

    Post Edited (Jim McCorison) : 1/23/2005 7:48:03 PM GMT
Sign In or Register to comment.