Shop OBEX P1 Docs P2 Docs Learn Events
Noob Question... — Parallax Forums

Noob Question...

Hi there,

so, I'm new to BS, and I have a little question for you All.

I'm try to run a very simply routine as below:


do
pulsout 14, 6000
pause 20
pulsout 15, 200
pause 20
loop


I need a frequency of 50hz with around 50% duty on PIN 14 and a frequency of 350hz around 50% duty on PIN 15....in the same moment.

So, is very simply routine...but I have a problem... the instructions are followed line by line from the BS2...and the result is that the pulse on the pins is the same, for example on PIN 14 I have a pulse of 50hz with duty of 30% and on PIN 15 I have a pulse of 49hz but with a duty of 80%.

So, I think the problem is because the BS2 flow the routine line by line...but this is not right for me.

How I can do for have the right result?

Thanks for your help, and sorry for the very noob/stupid question.

Regards
Fabry

Comments

  • Fabry,

    You're right, each instruction is executed in sequence. But you can do something else that works up to about 500Hz. See the sample code below, which gives 350Hz and 50Hz on my BS2.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    x VAR Word
    
    Hz50 PIN 14
    Hz350 PIN 15
    
    
    OUTPUT Hz50    ' make outputs
    OUTPUT Hz350
    
    DO
    
      TOGGLE Hz350      ' toggle 350Hz pin
    
      x = x + 1 // 7    ' count 350Hz, zero every 7th time
    
      TOGGLE Hz50-x     ' toggle 50Hz pin(s)
    
      x = x             ' waste time
    
    LOOP
    
    
    ' note pins 8-13 will also toggle at 50Hz if set to output
    
  • The BS2 can only do one thing at a time, either generate a stream of 50 Hz pulses or a stream of 350 Hz pulses. While it is making the 50 Hz pulse on pin 14 NOTHING will be happening on pin 15. The result of your program is that you will get a single long pulse on pin 14, then a single shorter pulse on pin 15, repeating at about a 50 Hz rate. In order to have two, independent streams of pulses at different frequencies you will need another processor.
    This is where a Propeller will do the job for you. You can have one cog produce the 50 Hz stream and have another cog produce a 350 Hz stream without any conflicts.
  • Actually, the prop can do this without *any* extra cog. Just use the counter logic.
    CON
       _clkmode = xtal1 + pll16x    'Standard clock mode 
       _xinfreq = 5_000_000         '* crystal frequency = 80 MHz
    
    OBJ
      Freq : "Synth"
               
      
    PUB main
        Freq.Synth("A",5, 50)          'start up a sythesizer in this cog
                                       ' FrequencySynth v1.1    
                                       'page 4 of signals in OBEX
        Freq.Synth("B", 6, 350)        'the other on pin 6
    
        repeat                 'forever
          waitcnt(clkfreq+cnt)
    
    
    
  • Hi All, sorry for the late reply, but I had some problem with my internet.

    First of all, Thanks a lot for all your answers!

    So, I will try with the code of Sapphire, but I look the propeller, and i think the Propeller Mini is ok for me.
    Just...I need to connect a EEPROM externally?
  • Prop mini has a built-in EEPROM. You *do* need a prop-plug to program it.
  • Prop mini has a built-in EEPROM. You *do* need a prop-plug to program it.

    Hi Tom,

    so, about the eeprom, I don't remember where, but I've read if I do't use a external EEPROM, once I shut down power the Prop lose the program...
    I'm worng about this?
  • PublisonPublison Posts: 12,366
    edited 2016-02-11 15:47
    With the Propeller Mini, you can use the Propeller Tool to program the EEPROM, (which is built one board), so when the Mini is powered up, it will run the program you saved.
  • Publison wrote: »
    With the Propeller Mini, you can use the Propeller Tool to program the EEPROM, (which is built one board), so when the Mini is powered up, it will run the program you saved.

    Ahhh, ok, now it's clear!

    Thanks for help!

  • Actually, the prop can do this without *any* extra cog. Just use the counter logic.
    CON
       _clkmode = xtal1 + pll16x    'Standard clock mode 
       _xinfreq = 5_000_000         '* crystal frequency = 80 MHz
    
    OBJ
      Freq : "Synth"
               
      
    PUB main
        Freq.Synth("A",5, 50)          'start up a sythesizer in this cog
                                       ' FrequencySynth v1.1    
                                       'page 4 of signals in OBEX
        Freq.Synth("B", 6, 350)        'the other on pin 6
    
        repeat                 'forever
          waitcnt(clkfreq+cnt)
    
    
    

    Hi Tom,

    I try to add other insctruction on your program, like this: Freq.Synth("C", 7, 350).
    But the program doesn't work.
    Can You explame me why?

    Other problem that I've encountered is:

    I try to have 3 frequencies when I push 3 different button:

    push first button = first freq
    push second button = second freq
    push third button = third freq

    How can I do this?

    Sorry but I've read the tutorial on Propeller SW, but I can't understand. So, the propeller is too difficult then PBasic...

  • Fabry:
    Each cog has access to two "synthesizers" (counter logic). They are referred to as "A" and "B". So to answer your first question,

    Freq.Synth("C", 7, 350) doesn't work because there is no "C" synth. In a perfect world, it wouldn't even compile.

    If you want just a single frequency at a time, you could do something like:
      if (ina[first button] == pressed)
         Freq.Synth("A", 5, firstFreq)
      else if (ina[second button] == pressed)
         Freq.Synth("A", 5, secondFreq)
      else if (ina[third button] == pressed
         Freq.Synth("A", 5, thirdFreq)
    

    If you want two frequencies at a time, you could use the "B" synth in the above fragment to suit yourself.

    If you want more than two frequencies at once, you will have to start an additional cog. Have a look at cogstart, etc in the manual.
  • Mike GreenMike Green Posts: 23,101
    edited 2016-03-12 17:14
    Each cog has two counters. Each counter can generate a frequency on an I/O pin. The program above produces two different frequencies on two different I/O pins at the same time using one cog. If you want to generate more frequencies at the same time, you need to use more than one cog. That doesn't sound like what you want. It sounds like you want to produce one frequency at a time depending on the button pushed.

    Your main loop might look like:
    PUB main | buttons, previous
    previous := %000
    repeat   ' do forever
      buttons := ina[1..3]   ' read the buttons
      if previous == %000 and buttons == %100  ' if button 1 pushed
        Freq.Synth("A",5,100)   ' start 100Hz on I/O pin 5
      if previous == %000 and buttons == %010   ' if button 2 pushed
        Freq.Synth("A",5,150)   ' start 150Hz on I/O pin 5
      if previous == %000 and buttons == %001  ' if button 3 pushed
        Freq.Synth("A",5,200)   ' start 200Hz on I/O pin 5
      previous := buttons   ' save button state
      waitcnt(clkfreq/10+cnt)   ' wait 100ms
    
  • Oh, thanks for quick reply!

    So, I've make a mash-up of both the code in this thread:
    CON
    
        _xinfreq = 5_000_000                     ' 5 MHz external crystal        
        _clkmode = xtal1 + pll16x 
    
    OBJ
      Freq : "Synth"
               
      
    PUB main   
    
    repeat                                       ' do forever
    
       Freq.Synth("B",1, 25)
        
                            
       dira[10..12] := 0                    ' read the buttons
      if ina[10]                                 ' if button 1 pushed
        Freq.Synth("A",6,50)              ' start 20Hz on I/O pin 6
      if ina[11]                                 ' if button 2 pushed
        Freq.Synth("A",6,150)            ' start 150Hz on I/O pin 6
      if ina[12]                                 ' if button 3 pushed
        Freq.Synth("A",6,300)            ' start 300Hz on I/O pin 6
    


    But I have a problem...the pin 10 doesn't work. Other pins, 11 and 12, work perfectly, but 10...NO. I dont understand why.
    Any suggestion?
  • What happens if you use some other pin instead of 10? There's no particular reason why 11 and 12 work, but not 10. Maybe there's something wrong with your wiring or maybe you damaged pin 10 earlier. The other code was there for a reason. Without the delays (WAITCNT) contact bounce is an issue. Without the other stuff (buttons and previous) Freq.Synth keeps getting called until the button gets released. The Freq. routines will still work, but there will be unnecessary noise and transients in your signal. Why keep the Freq.Synth("B",1,25) call?
  • So, is the same with other pin.
    Sorry, but I've not understand the functions of (WAITCNT), (button and previous).
    Right, the routines work well (for my use) without the functions mentioned.

    So, I need to have a signal present in the same time that I power the propeller, and I need to have the possibility to choose a freq. with the buttons on other pin.
    I use a rotary switch instead 3 buttons, like to AUTO, LOW, MED, HIGH functions.
    So, I've tried to put a routine that stops all the functions except the Freq.Synth("B",1,25), but I'm not so clever with propeller...it's too hard then PBasic... :(:(:(
  • Fabry,

    Most likely your P10 button circuit is faulty.

    WAITCNT is similar to the PAUSE command.
    The Propeller has a Count register called CNT that counts up on each clock cycle or "tick".

    Wire each Rotary switch position to an I/O pin just like it was a pushbutton and connect power to the common pin.
    Just remember to put a long delay in your program so you see the final switch position and not on of the changes.

    If you can use PBASIC then you can use Spin.
    It's a very easy to use language and the Propeller Education Kit text will teach it to you step by step.
    https://www.parallax.com/downloads/propeller-education-kit-labs-fundamentals-text
    https://www.parallax.com/downloads/propeller-education-kit-labs-fundamentals-example-code




Sign In or Register to comment.