Shop OBEX P1 Docs P2 Docs Learn Events
sx/b freqout in subroutine — Parallax Forums

sx/b freqout in subroutine

algorhythmalgorhythm Posts: 4
edited 2005-01-02 00:31 in General Discussion
Hello. I'm new to the forum and to sx programming.

I need to make a subroutine that will play notes based on values passed into it. I need the pitch accuracy of FREQOUT but unfortunately it only accepts constants to specify frequency. I want to be able to feed the subroutine a list of frequencies and durations but I get this error:

CONSTANT EXPECTED "__PARAM1"

note:
pitch = __PARAM1
time = __PARAM2
freqout pin, pitch, time
return

Start:
gosub note, 440, 100 ' make a 440Hz 100ms tone
END

What is the best way to solve this problem?

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-28 15:15
    You'll have to write a custom subroutine. I, too, was disappointed that we couldn't craft FREQOUT to accept a variable frequency -- perhaps we can readdress it in version 1.2 which is in development now.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • allanlane5allanlane5 Posts: 3,815
    edited 2004-12-28 15:47
    Brute force solution would be a bunch of 'FREQOUT SoundPin, 440, Time: GOTO BR_Cont' statements, with a BRANCH statement like:
    PlayLoop:

    · MyNote = 440
    · MyTime = 500 ' 1/2 second

    ·' NoteIndex gets: 0,···· 1,·· 2, ...
    ·LOOKUP MyNote, (0, 440,480,3,4,5,6,7,8,9,10), NoteIndex
    ·BRANCH NoteIndex, (Note0, Note1, Note2, Note3) ' ...

    BR_CONT:· ' pseudo-RETURN destination -- 'BRANCH' is 'GOTO' only.
    ··············
    ' Do more stuff...
    · GOTO PlayLoop

    Note0:· '
    · GOTO BR_Cont

    Note1:· ' I don't know the 'size' of FREQOUT assembly -- these may have to go on multiple pages.
    · FREQOUT SoundPin, 440, MyTime
    · GOTO BR_Cont

    Note2:
    · FREQOUT SoundPin, 480, MyTime
    · GOTO BR_Cont
    'etc...

    Post Edited (allanlane5) : 12/28/2004 3:49:27 PM GMT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-28 16:02
    The only problem with that is each instance to FREQOUT is translated to several lines of assembly code, hence the desire for a subroutine. The issue we have with FREQOUT is that it may (in fact, usually) will want a value greater than 255 and SX/B uses byte variables.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • allanlane5allanlane5 Posts: 3,815
    edited 2004-12-28 16:10
    Yes, I implemented the above approach on SX/B, and each FREQOUT took 35 bytes of code. 88 notes would take 3080 bytes just for the freqout commands -- not to mention all those page crossings, yuck.

    Man, only having 'BYTE' values is limiting. I suppose you're working on that though, right? right? Please?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-28 16:55
    No, it is not currently on the list. SX/B is supposed to be a learning tool (and FREE) so we will probably not ever have everything we'd all wish for.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • BeanBean Posts: 8,129
    edited 2004-12-28 18:28
    I think you could make an interrupt routine that would do what your trying. You would have to calculate it out and use "RETURNINT bytevar", and maybe change the OPTION register for different pre-scale rates, but I think it could be done.

    Bean.
  • algorhythmalgorhythm Posts: 4
    edited 2005-01-01 21:30
    Thanks for the replies... I think I'll try to do it by counting clocks in assembly. Happy new year!
  • Jim McCorisonJim McCorison Posts: 359
    edited 2005-01-01 23:39
    Right up front, this is a dangerous idea, codewise, but it might work. nono.gif

    If SX/B has a WRITE statement like BS2, you could identify the location in memory where the constaint for FREQOUT is stored and overwrite it with the desired value. I would be a hassle as there's no syntax to get the address of a variable, like 'C', so if the location moves around with changing code you'd have to adjust the WRITE address for each revision of the code.

    Like I said. This is probably a bad idea. But it might do what you needed as a last resort... or at least a last resort other then assembler.

    Jim
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-01-01 23:43
    SX/B has no WRITE instruction. That works with the BS2 because an external EEPROM is employed, something we'd have to add manually with SX/B

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • Jim McCorisonJim McCorison Posts: 359
    edited 2005-01-02 00:31
    Oh well. I guess I should have RTFM'ed before offering the suggestion.


    Jim
Sign In or Register to comment.