Shop OBEX P1 Docs P2 Docs Learn Events
Code is randomly not working — Parallax Forums

Code is randomly not working

qsuscsqsuscs Posts: 12
edited 2013-03-25 04:38 in Propeller 1
Hello,

this afternoon, I wanted to check which frequencies I can hear, but I wanted to do this by myself. So I wrote a bit of spin which should give me the tones from 10Hz to 20kHz in different intervals.
It works, sometimes, but not every time. There are two problems:
  1. It stops at a random point, there is no sound anymore but the frequencies are printed all at once
  2. The frequencies are off too much
I've put the code here: https://gist.github.com/4504149 Feel free to fork, I haven't specified a license but WTFPL should do (if it is compatibele with the pst-license)

BTW, I use a 5MHz crystal (two legs), but I also have a 4.9152MHz oscillator and some other microcontrollers that I could use as a clock.

qsuscs

EDIT: I also tried different frequencies (i.e. pll1x instead of pll16x), it stopped later, but it stopped nevertheless, and the tone frequencies were still off.

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-01-10 13:28
    It's stopping because of this statement:
    repeat until cnt > counter+clkfreq*2

    That's because cnt doesn't keep going up forever; it overflows from $7FFF_FFFF (biggest positive number) to $8000_0000 (smallest negative number). Do this instead:
    reepat until cnt - counter > clkfreq * 2

    Your frequencies are off (too low) becasue of this statement:
    waitcnt(clkfreq/i*2+cnt)

    It doesn't account for interpreter overhead. Also, you need parens in the denominator. Better would be this:
    waitcnt(k += clkfreq / (i * 2))

    where k is initialized to cnt at the beginning of the loop.

    I'm not certain, but Spin may be too slow to reach 20 kHz this way. But you can use one of the counters to generate your output without having to resort to PASM.

    -Phil
  • qsuscsqsuscs Posts: 12
    edited 2013-01-13 10:22
    It works better, but unfortunately not good enough.
    But the counters sound interesting, how do I use them? AFAIK the Propeller doesn't have interrupts, this is how the counters are used in the world of AVR.

    Just found that: http://www.parallax.com/tabid/780/Default.aspx. I think it’ll be a strange feeling when I do something with AVR again, one core, interrupts, …
  • StefanL38StefanL38 Posts: 2,292
    edited 2013-01-13 10:41
    Hi qsuscs,

    for example the object http://obex.parallax.com/objects/422/ creates frequencies.
    So take a look into the OBEX which frequency-object suits best to your needs

    best regards
    Stefan
  • JonnyMacJonnyMac Posts: 9,108
    edited 2013-01-13 11:12
    You could easily use the a Propeller counter to generate the frequency, in fact, you could have two at once. This is a method I have in my template code for doing that (and yes, you can change frequency on the fly, or stop output by setting the frequency to 0).
    pub set_freq(ctrx, px, fx)
    
    '' Sets ctrx to frequency fx on pin px (NCO/SE mode)
    '' -- fx in hz
    '' -- use fx of 0 to stop counter that is running
    
      if (fx > 0)                             
        fx := ($8000_0000 / (clkfreq / fx)) << 1            ' convert freq for NCO mode    
        case ctrx
          "a", "A":
            ctra := ((%00100) << 26) | px                   ' configure ctra for NCO on pin
            frqa := fx                                      ' set frequency
            dira[px] := 1                                   ' make pin an output
         
          "b", "B":                         
            ctrb := ((%00100) << 26) | px  
            frqb := fx                    
            dira[px] := 1
    
      else
        case ctrx
          "a", "A":
            ctra := 0                                       ' disable counter
            outa[px] := 0                                   ' clear pin/driver 
            dira[px] := 0                                  
         
          "b", "B":                         
            ctrb := 0 
            outa[px] := 0  
            dira[px] := 0
    
  • kuronekokuroneko Posts: 3,623
    edited 2013-01-13 23:28
    JonnyMac wrote: »
      fx := ($8000_0000 / (clkfreq / fx)) << 1            ' convert freq for NCO mode    
    
    You might want to change that to
    fx := ($8000_0000 / [COLOR="#FF0000"]-[/COLOR](clkfreq / fx)) << 1
    
    OK, not critical, you just get inverted phase. Must be the weather ... it's snowing here which happens - if you're lucky - once a year, for a day.
  • prof_brainoprof_braino Posts: 4,313
    edited 2013-01-14 06:34
    Please post you code when you get you hearing test working. Many of us are curious if we are also losing our hearing, or just going senile.

    Also, are you adding very high frequencies? I have always imagined I can "hear" frequencies that are supposed to be near ultra sonic, from working with CRTs, marginal TVs, etc. If convenient, please include a couple brands above what were are supposed to be able to detect. :)
  • qsuscsqsuscs Posts: 12
    edited 2013-01-14 07:46
    Please post you code when you get you hearing test working. Many of us are curious if we are also losing our hearing, or just going senile.

    Also, are you adding very high frequencies? I have always imagined I can "hear" frequencies that are supposed to be near ultra sonic, from working with CRTs, marginal TVs, etc. If convenient, please include a couple brands above what were are supposed to be able to detect. :)
    I will always push the code to GitHub Gist. If you just want to test your ears, there are hundreds of music files everywhere, one minute of google should do. But I thought that it's boring this way.

    I planned to prompt a frequency (PS/2+TV or serial) after all the frequencies were played. There is a sticker on my CRT that says fH: 27-68kHz, I don't think you are able to hear that, but, I don't know, maybe you are, or the CRT emmits other frequencies.
  • qsuscsqsuscs Posts: 12
    edited 2013-03-25 04:38
    After a long time, I finally sat down and read a bit and changed the code. Here you go: https://gist.github.com/qsuscs/4504149
Sign In or Register to comment.