More PBASIC to Spin questions
Genetix
Posts: 1,754
WAM 3.0: Chapter 6, Activity #4 (p. 189) - Dial Display
LOOKDOWN time, <= [40, 150, 275, 400, 550, 800], index
Apparently PBASIC allows comparison operations in LOOKDOWN while Spin doesn't so how would I do this?
Also, I would like to imitate FREQOUT (Chapter 8) using 2 frequencies.
I am thinking of having Counter A and B use the same APIN at the same time but the Propeller manual doesn't mention what will happen.
LOOKDOWN time, <= [40, 150, 275, 400, 550, 800], index
Apparently PBASIC allows comparison operations in LOOKDOWN while Spin doesn't so how would I do this?
Also, I would like to imitate FREQOUT (Chapter 8) using 2 frequencies.
I am thinking of having Counter A and B use the same APIN at the same time but the Propeller manual doesn't mention what will happen.
Comments
Here are three different ways to work around this. I have a hard time believing there isn't a better way.
None of the methods are as simple as the PBASIC technique.
Maybe someone will have a better solution.
This is the output from the above code.
I'm not sure if time is over 800 if the index is supposed to be zero or seven. I assumed seven. The code would need to changed if the index were supposed be zero when time is over 800.
As with most computer languages, there is often more than one way to accomplish a task in Spin.
There's some good information about counters in the PEK (Propeller Education Kit) available through the "Help" menu of the Propeller Tool. I don't know the answer to your counter question myself.
Freqout on the Stamp is constructed using duty mode. The sine values for the two frequencies at each point in time are computed and summed, and that sets the instantaneous duty cycle. On the Prop, for anything but very low frequencies you would have to use PASM to do the sines and transfer them to a counter operating in duty mode. I don't think simple overlapping of two NCO mode ctra and ctrb would give you satisfactory results. The overlap is wired-or. On the other hand, if you direct the NCO mode output to two different pins, resistors can be used to analog sum NCO outputs.
LOOKUP is the same, but it would start at the opposite end of the list and work in the opposite direction.
So I suppose in SPIN, you would have an array of values, and just keep walking the array until you find the right fit. This would usually be paired with another address to jump to or another value to plug in somewhere.
Tracy Allen provided the SPIN code above, but this is also very easy to do in PASM. As I mentioned before, if you can get to the generic concept, you can easily manage to get the same results in any language.
Here's my version using a pointer. It's just a small variation on the "Dat" method.
Does Lookdown return an index starting at one or zero? I've been assuming it starts the index at one.
I forgot the Stamp outputs a PWM signal that gets filtered into a sine wave when you use FREQOUT.
I came into 3 more issues.
The DCD command - Returns the value of the highest set bit (Chapter 8 - Activity #4-5, used to determine the octave change)
Given the way the Propeller counters work I would either need the add the PEK routine for determined the FRQx constant or have a large table with all the values.
I don't see a PST (Parallax Serial Terminal) equivalent for DEBUG DEC#.
I am not sure about this one, also from Ch. 8, #4-5.
The Notes are stored in DATA statements and letters (in quotes and separated by commas).
The note is read into a variable and then printed (DEBUG variable_name).
I use an updated version of FullDuplexSerial that has a method called rjdec (right-justified decimal). I do a lot of formatted display so this is useful. Note, too, that you can select the leading pad character.
And look what I found in my own template!
I trust you're enjoying the Propeller and will even more as you get comfortable with it.
Once you're very comfortable with Spin, you'll be able to translate projects for a variety of devices.
Geeze, I feel stupid. I just looked at the DCD command in the BASIC Stamp manual and realized how simple it is. It takes in a Nibble (0-15) and outputs 2^N. That is same as Shifting 1 by N places. 1 line of code (assuming the number is in the valid range).
Result = DCD input_value ---> Result := 1 << input_value
Funny considering shifts have always been baffling to me.
Tip 1: Shifting left is a faster way to multiply by a power of 2 (2, 4, 8, 16....)
Tip 2: Shifting right is a faster way to divide by a power of 2
Tip 3: You can use shifting to divide negative numbers in Spin using the ~> operator ( instead of >> )
There are assembly versions of these operators << is shl, >> is shr, ~> is sar.
The Propeller also does rotating which can be very powerful.