Noob Question...
in BASIC Stamp
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
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
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
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.
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)
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?
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?
Ahhh, ok, now it's clear!
Thanks for help!
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...
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.
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
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?
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...
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