RCTIME Object - rosco-pc/propeller-wiki GitHub Wiki
by Beau Schwabe
This object measures the time constant of an RC circuit. You can use RCTIME to read a variable resistance such as a potentiometer (e.g., volume knob or joystick), photosensor (CdS cell), thermistor (temperature), etc.
This object can operate in one of two modes: • Background mode, in which a separate process (cog) asynchronously updates a variable. • Foreground mode, in which a method call performs a "one-shot" time constant measurement.
RCTIME in foreground mode functions much like the Basic Stamp's RCTIME command.
Build your variable resistance into a circuit such as this:
RCTIME charges the capacitor to Vdd (the 200 ohm resistor limits the charging current to protect the Propeller's I/O pin) and then measures the time it takes for the capacitor to discharge through the variable resistance to Vdd/2.
You can also wire up the circuit as follows:
For this second type of circuit, RCTIME first discharges the capacitor and then measures the time for it to charge to Vdd/2.
The first circuit is recommended as it provides slightly higher resolution due to the I/O threshold not being exactly Vdd/2.
The results that RCTIME provides are proportional to the variable resistance R, not a direct measurement of R. The time to discharge (or charge) to Vdd/2 is given by this equation:
t = R C ln 2 where t is the charging time in seconds, R is the resistance in ohms, and C is the capacitance in farads.
The result returned by RCTIME is t x clkfreq / 16 (RCTIME divides by 16 to eliminate the noisy least significant bits).
Assume a resistance that can vary between 1000Ω and 2000Ω and a fixed capacitance of 0.1µF. At the low end (R = 1000Ω), the discharge time is 69.3µs and the result given by RCTIME is 347 (assuming an 80MHz system clock).
At the high end (R = 2000Ω), the discharge time is 139µs and the result given by RCTIME is 693.
Therefore, you would expect readings to vary over the range of the resistance from 347 to 693 (approximately; real numbers will of course differ from the calculated values).
To use RCTIME in background mode, call the **start**
method with the following arguments:
- pin: the number of the Propeller's I/O pin that is attached to the RC circuit
- state: this argument should be 1 for Type I circuits, 0 for Type II.
- variable address: the address of a long variable that is to be updated.
' ... clock definitions go here...
obj RC: "RCTIME"
var long pot_position
pub main
RC.start( 5, 1, @pot_position )
repeat
' here do something with pot_position, knowing that RCTIME is updating it
' automatically in the background with new values
Note that the variable (in this example, **pot_position**
) will only be updated as fast as the circuit discharges (or charges). You can monitor more than one pin, but keep in mind that background mode consumes an additional cog for each pin being monitored.
To use RCTIME in foreground mode, call the **RCTIME**
method to initiate a measurement. Pass the same arguments that you would pass to **start**
:
- pin: the number of the Propeller's I/O pin that is attached to the RC circuit.
- state: this argument should be 1 for Type I circuits, 0 for Type II.
- variable address: the address of a long variable that will contain the result.
' ... clock definitions go here...
obj RC: "RCTIME"
var long pot_position
pub main
repeat
RC.RCTIME( 5, 1, @pot_position ) ' Read pot position
' here do something with pot_position
Note: You cannot mix background and foreground operation in the same program.