Positron Compiler Documentation

RCin

Source: Positron8 Compiler User Manual, PDF page 183

+5 Volts

+5 Volts

C

220Ω

To

I/O Pin

R

C

220Ω

To

I/O Pin

R

Syntax

Variable = RCin Pin, State

Overview

Count time while pin remains in state, usually used to measure the charge/ discharge time of resistor/capacitor (RC) circuit.

Parameters

Pin is a Port.Pin constant that specifies the I/O pin to use. This pin will be placed into input mode and left in that state when the instruction finishes. State is a variable or constant (1 or 0) that will end the Rcin period. Text, High or Low may also be used instead of 1 or 0. Variable is a variable in which the time measurement will be stored.

Example

Device = 18F25K40
                                  ' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600
                                  ' Set the Baud rate for HRsoutLn
Dim wMyResult as Word
                                  ' Create a Word variable to hold the result.
High PORTB.0
                                  ' Discharge the cap
DelayMs 1
                                  ' Wait for 1 ms.
wMyResult = RCin PORTB.0, High  ' Measure RC charge time.
HRsoutLn Dec wMyResult, " "
                                  ' Display the value on a serial terminal.

Notes

The resolution of RCin is dependent upon the oscillator frequency. If a 4MHz oscillator is used, the time in state is returned in 10us increments. If a 20MHz oscillator is used, the time in state will have a 2us resolution. Declaring an Xtal value has no effect on RCin. The resolution always changes with the actual oscillator speed. If the pin never changes state 0 is returned.

When RCin executes, it starts a counter. The counter stops as soon as the specified pin is no longer in State (0 or 1). If pin is not in State when the instruction executes, RCin will return 1 in Variable, since the instruction requires one timing cycle to discover this fact. If pin remains in State longer than 65535 timing cycles RCin returns 0.

Figure A Figure B

The diagrams above show two suitable RC circuits for use with RCin. The circuit in figure B is preferred, because the PICmicro’s logic threshold is approximately 1.5 volts. This means that the voltage seen by the pin will start at 5V then fall to 1.5V (a span of 3.5V) before RCin stops. With the circuit in figure A, the voltage will start at 0V and rise to 1.5V (spanning only 1.5V) before RCin stops. For the same combination of R and C, the circuit shown in figure A will produce a higher result, and therefore more resolution than figure B.

Before RCin executes, the capacitor must be put into the state specified in the RCin command. For example, with figure B, the capacitor must be discharged until both plates (sides of the capacitor) are at 5V. It may seem strange that discharging the capacitor makes the input high, but you must remember that a capacitor is charged when there is a voltage difference between its plates. When both sides are at +5 Volts, the capacitor is considered discharged. Below is a typical sequence of instructions for the circuit in figure A.

Device = 18F25K40
                                  ' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600
                                  ' Set the Baud rate for HRsoutLn
Dim wMyResult as Word
                                  ' Create a Word variable to hold the result
High PORTB.0
                                  ' Discharge the cap
DelayMs 1
                                  ' Wait for 1 ms
wMyResult = RCin PORTB.0, High  ' Measure RC charge time.
HRsoutLn Dec wMyResult, "  "
                                  ' Display the value on a serial terminal

Using RCin is very straightforward, except for one detail: For a given R and C, what value will RCin return? It’s actually rather easy to calculate, based on a value called the RC time constant, or tau (τ) for short. Tau represents the time required for a given RC combination to charge or discharge by 63 percent of the total change in voltage that they will undergo. More importantly, the value τ is used in the generalized RC timing calculation. Tau’s formula is just R multiplied by C: -

τ = R x C

The general RC timing formula uses τ to tell us the time required for an RC circuit to change from one voltage to another: -

time = -τ * ( ln (Vfinal / Vinitial ) )

In this formula ln is the natural logarithm. Assume we’re interested in a 10kΩ resistor and 0.1µF cap. Calculate τ: -

τ = (10 x 103) x (0.1 x 10-6) = 1 x 10-3

The RC time constant is 1 x 10-3 or 1 millisecond. Now calculate the time required for this RC circuit to go from 5V to 1.5V (as in figure B):

Time = -1 x 10-3* ( ln(5.0v / 1.5v) ) = 1.204 x 10-3

Using a 20MHz crystal, the unit of time is 2µs, that time (1.204 x 10-3) works out to 602 units. With a 10kΩ resistor and 0.1µF capacitor, RCin would return a value of approximately 600. Since Vinitial and Vfinal don't change, we can use a simplified rule of thumb to estimate RCin results for circuits similar to figure A: -

RCin units = 600 x R (in kΩ) x C (in µF)

Another useful rule of thumb can help calculate how long to charge/discharge the capacitor before RCin. In the example shown, that’s the purpose of the High and DelayMs commands. A given RC charges or discharges 98 percent of the way in 4 time constants (4 x R x C). In both circuits, the charge/discharge current passes through a 220Ω series resistor and the capacitor. So if the capacitor were 0.1µF, the minimum charge/discharge time should be: -

Charge time = 4 x 220 x (0.1 x 10-6) = 88 x 10-6

So it takes only 88µs for the cap to charge/discharge, which means that the 1ms charge/discharge time of the example is more than adequate.

You may be wondering why the 220Ω resistor is necessary at all. Consider what would happen if resistor R in figure A were a pot, and was adjusted to 0Ω. When the I/O pin went high to discharge the cap, it would see a short direct to ground. The 220Ω series resistor would limit the short circuit current to 5V/220Ω = 23mA and protect the PICmicro™ from any possible damage.

See also : ADin, Counter, Pot, PulseIn.