Shop OBEX P1 Docs P2 Docs Learn Events
Creating a Square wav — Parallax Forums

Creating a Square wav

AcadianAcadian Posts: 16
edited 2004-11-29 17:10 in Learn with BlocklyProp
I am kinda stuck on this one. I know it's simple, but I just can't get my head around it.

We have our stamp connected directly to a R2R network with no chip.. We managed to generate a sine wave with this code, but we can't figure out howto get a Square wave or a Saw Tooth wave.

Any ideas would be greatly appreciated.
' {$STAMP BS2}
' {$PBASIC 2.5}
DIRS = %0000111100000000
Degr VAR Word ' Define variables.
Sine VAR Word
'%%%%%%%%%%%%%%%%% Begin  Sine Wave  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FOR Degr = 0 TO 359 ' Use degrees.
Sine = SIN (Degr * 128 / 180) ' Convert to brads, do SIN.
sine = sine + 127
sine = sine / 17
OUTC = sine
DEBUG "Sine: ", BIN sine,CR
PAUSE 15
DEBUG "Angle: ", DEC Degr, TAB, "Sine: ", SDEC Sine, CR ' Display.
NEXT

'%%%%%%%%%%%%%%%% End Sine Wave %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

'%%%%%%%%%%%%%%%% Begin Square Wave %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



·

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-11-29 16:45
    Both should be pretty easy:

    Make_Sqare:
    · OUTC = %0000
    · DO
    ··· PAUSE halfCycle
    ··· OUTC = ~OUTC···
    · LOOP WHILE (MakeWave = 1)
    · RETURN

    Make_Saw:
    · OUTC = %0000
    · DO
    ··· PAUSE delayTime
    ··· OUTC = OUTC + 1
    · LOOP WHILE (MakeWave = 1)
    · RETURN

    The subroutines above have a test input that allows you to jump out of the routine when you're ready to quit.·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • AcadianAcadian Posts: 16
    edited 2004-11-29 16:47
    thanks John I will give it a shot
  • mic911mic911 Posts: 1
    edited 2004-11-29 16:50
    what about those those saw tooths like this: /|/|/| and this |\|\|\ and also triangle waves?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-11-29 17:10
    Yet again, pretty easy:

    Make_Saw2:
    · OUTC = %1111
    · DO
    ··· PAUSE delayTime
    ··· OUTC = OUTC·+ 15 // 16
    · LOOP WHILE (MakeWave = 1)
    · RETURN

    Make_Tri:
    · OUTC = %0000
    · change = 1
    · DO
    ··· PAUSE delayTime
    ··· OUTC = OUTC·+ change // 16
    ··· IF (OUTC = %1111) THEN change = 15··· ' ramp down
    ··· IF (OUTC = %0000) THEN change = 1···· ' ramp up
    · LOOP WHILE (MakeWave = 1)
    · RETURN



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
Sign In or Register to comment.