Convert a BS2 RCTIME constant to Spin
Genetix
Posts: 1,754
I have been converting the BS2 code from What's a Microcontroller into Spin but I am stuck on Chapter 5, Activity #4 (Controlling a Servo with a Potentiometer).
I have Spin code equivalent to RCTIME but the values are different because the Propeller is faster and the voltage levels are different.
The BS2 uses 2 us units but the Propeller can go down to 12.5 ns (80 MHz). ---> 160X greater
The BS2 uses 5 Volts for Vdd while the Propeller uses 3.3 Volts.
The BS2 voltage threshold is about 1.3 Volts and I don't know what it is for the Propeller.
The activity uses a 0.1 uF capacitor and a 50K Potentiometer.
I found a formula for the time: t = ln (Vdd/Vpin) x RC
For the BS2 the nominal time is 1347 us or an RCTIME value of 674. The maximum time (using a 10% tolerance for R and 20% for C) is 1630 us or 815 for RCTIME.
Another issue is that the BS2 uses 16-Bit math while the Propeller uses 32-Bit math. Spin does not have the */ operator.
Is there an easy way to convert an RCTIME constant into a Spin equivalent.
I have Spin code equivalent to RCTIME but the values are different because the Propeller is faster and the voltage levels are different.
The BS2 uses 2 us units but the Propeller can go down to 12.5 ns (80 MHz). ---> 160X greater
The BS2 uses 5 Volts for Vdd while the Propeller uses 3.3 Volts.
The BS2 voltage threshold is about 1.3 Volts and I don't know what it is for the Propeller.
The activity uses a 0.1 uF capacitor and a 50K Potentiometer.
I found a formula for the time: t = ln (Vdd/Vpin) x RC
For the BS2 the nominal time is 1347 us or an RCTIME value of 674. The maximum time (using a 10% tolerance for R and 20% for C) is 1630 us or 815 for RCTIME.
Another issue is that the BS2 uses 16-Bit math while the Propeller uses 32-Bit math. Spin does not have the */ operator.
Is there an easy way to convert an RCTIME constant into a Spin equivalent.
Comments
Here's a method that I use in a product. This takes advantage of the timer's ability to measure while in a particular state. Note that if you experiment with this, you will see variations due to the variations in RC components. This returns the discharge time in system ticks (you can convert to microseconds by dividing by clkfreq/1000000). To your question, the threshold is 1.65v.
Also, how do I create constants in the CON section of a program that use clkfreq. I can only use clkfreq in a method.
NWCCTV, I was doing this for myself and I would be very surprised is no one else has done this before. The only object I am using is PST but maybe as Jon suggested I should have methods for the BS2 commands instead of just code within the program. You wouldn't believe how many lines of Spin code it sometimes takes to do the same thing as 1 PBASIC line.
I gave you working code, now crack open the manual and figure it out. You'll be better for it, honest.
What I can tell you is that it is meausring the discharge of the cap so you need to connect the cap and pot in parallel; one side goes to the desired IO pin, the other to ground.
You cannot use clkfreq in a constant declaration as it is a run-time variable. Have a look at my template code (attached). It shows how to create compile-time constants. I don't remember which forum member came up with this, but I snapped it up as soon as I saw it. The beauty is you can change the crystal and PLL settings and CLK_FREQ, MS_001, and US_001 take care of themselves.
I want to learn how to do things for myself and not rely on someone else to do it for me.
Someone has to make it and the Propeller documentation is greatly lacking.
Jon,
Some of the lines of code were confusing especially that repeat while line.
I've never seen that before and it took me a while to figure out that it's only executed when the condition is true or in this case equals 1.
I still don't understand shifts well but I understand what the CTRA line does. BTW, I have also seen people use add instead of OR for APIN.
Also, if it were me I would use Pin instead of p to make the code clearer.
I noticed that you changed the pin before zeroing PHSA while Andy did the opposite in the PE Kit and made a 624 tick adjustment (80 MHz) later.
The RC time constant is dependent on Voltage, Resistance, and Capacitance. These can either be a discharge or a charge curve.
Essentially you want to time the point in time that your have 50% of 3.3volts as your transition from High to Low on an i/o pin.
Timing is just more precise on the Propeller for a variety of reasons.
The BS2 might offer only 2ms precision, but the Propeller's WaitCnt makes much smaller units available. You can use all the precision of the Propeller or limit it if you don't need to get into nano-second measurments. Micro-seconds or milli-seconds may be all you desire.
So learn the additional utility of the Propeller for timing over the BS2.
In other words,
Instead of cloning the BS2 in the Propeller, a comparison and contrast approach will be more informative and reinforce your knowledge of fundamental electronics. Dig for the real meaning and principles behind the code; not mere translation.
I understand the Propeller is more capable and versatile than the BS2. The Cog counters can count down to a clock cycle and there is a Positive and Negative transition detection mode. Once the counter is configured it runs by itself freeing up the Cog to work on something else.
The BS2 may be slow and it can only do 1 thing at a time but it has some powerful commands as I am learning as I am trying to do the same thing using Spin. I now need to learn how to use PASM with Spin especially when timing is critical.
My main point is that the RC timing is just a fundamental exponential curve that is calculated as the decay or charge of volts over time.
These days we prefer to use resistors and capacitors for circuits while ignoring inductors for the most part. Inductors have been more costly to miniaturize. It becomes handy when we can measure changes in a variable resistance provided by a potentiometer or sensor of some sort. But inductors can work with resistors to create a useful timing scheme as well.
http://en.wikipedia.org/wiki/RC_circuit
The measurement of time in the micro-controller is a tool that is versatile and used in many, many ways. RCTIME is just one.
So, RCTIME is essentially just a time measurement within the microcontroller of one i/o pin transistion. While the circuit could be an R-C decay, R-C charger, or other inputs that have nothing to do with an R-C circuit. In other words, it is just a Timer routine.