Shop OBEX P1 Docs P2 Docs Learn Events
How to generate a square wave — Parallax Forums

How to generate a square wave

inakiinaki Posts: 262
edited 2005-11-27 00:23 in BASIC Stamp
I need to generate·a square wave with 3·cycles of 854 us each. Then pause for a second and repeat.
Each cycle has payload of 50%, so each part of·a cycle takes 427 us.

I am using a BS2 to generate the train of 3 cycles on PIN 7 with the code shown bellow so I divide the time by 2 us.
The resulting signal has nothing to do which what I intended. Obviously the BS takes too much time to process the code and the wave is distorted.

Is there any better way to achieve this goal? Is it possible to achieve this in a BS at all ?

T CON 213 'About 427/2

Main:
· LOW 7
· PULSOUT 7,T
· HIGH 7
· PULSOUT 7,T
· LOW 7
· PULSOUT 7,T
··HIGH 7
· PULSOUT 7,T
· LOW 7
· PULSOUT 7,T
· HIGH 7
· PULSOUT 7,T

· PAUSE 1000
· GOTO Main
· END


·

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-27 00:23
    The problem with using PULSOUT on the same pin that you're driving is that PULSOUT inverts the pin state before starting the timing.· This, coupled with the instruction load time (a big source of delay) creates more pulses than you're looking for.· Have a look at the attached 'scope images and you'll see what your original program is doing versus a corrected -- albeit similarly brute force -- version that uses PULSOUT on a spare pin to do the cycle timing.· In the end this may not be the best use of a BASIC Stamp.· The SX would give you far finer control.

    Pulses········· PIN···· 7
    Spare·········· PIN···· 15

    HalfCyc········ CON···· 28

    Main:
    · HIGH·Pulses
    · PULSOUT Spare, HalfCyc
    · LOW Pulses
    · PULSOUT Spare, HalfCyc
    · HIGH Pulses
    · PULSOUT Spare, HalfCyc
    · LOW Pulses
    · PULSOUT Spare, HalfCyc
    · HIGH Pulses
    · PULSOUT Spare, HalfCyc
    · LOW Pulses
    · PULSOUT Spare, HalfCyc
    · PAUSE 1000
    · GOTO Main

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    952 x 767 - 171K
    952 x 767 - 166K
Sign In or Register to comment.