{{ ──────────────────────────────────────────────────────────────────────────────────────── File: RC Time.spin Version: 0.70 Date: 2010.01.20 Author: Andy Lindsay Copyright (c) 2009 Parallax Incorporated See end of file for terms of use. Resource Documents and Code Examples Resource Documents and Code Examples "PE Kit Tools - Measure Resistance and Capacitance v0.92.zip" contains: - "PE Kit Tools - Measure Resistance and Capacitance.pdf" documentation of all test code examples in RC Time.zip. Includes when and why to use certain features. - "Documentation.MeasureTime.Spin" has information about RC decay measurements and examples of using this object's methods in application code. - The following example programs: • Test Simple RCTIME.spin • Test Repeated RCTIME.spin • Test RCTIME Timeout.spin • Test RCTIME Timeout.ASM.spin • Test Parallel RCTIME.spin • RC Resistance Meter.spin • 200 kHz Sampling Rate.spin - Other objects that the example programs depend on to function properly - Additional objects with schematics and more documenation. Updates: • v0.65 - First release • v0.70 - Rename Rc method to Time To-Do List: • Clean up and optimize the ASM code (currently first working prototype) ──────────────────────────────────────────────────────────────────────────────────────── }} CON SEQUENTIAL = 0 ' Mode constants PARALLEL = 1 REPEATED = 2 CHARGE_TIME = 2 TIME_OUT = 3 SAMPLE_INTERVAL = 5 CONFIG_MODE = 6 VAR long cog, tI, TC, mode, params[6], stack[40], repetitions PUB Start(pin, state, chargeTimeTicks, timeOutTicks, sampleTicks, resultAddr) : repsAddr {{Start process that repeatedly takes RC measurements at a sampling rate. - pin..............the pin to take RC measurements on - state............starting state of the measurement. 1 -> decay, 0 -> growth - chargeTimeTicks..Clock ticks for charging (or discharging) the circuit. Minimum 2775 clock ticks. - timeOutTicks.....Clock ticks elapsed before the measurement times out. - sampleTicks......Number of ticks in the sample interval. Must be at least chargeTimeTicks + timeOutTicks + 5000 - resultAddr.......Address of the variable that stores the latest RC measurement result - repsAddr.........Returns the address of the variable that stores the current measurement number, or 0 if the cog failed to launch. }} stop ' Stop the cog if it's already running mode:=REPEATED ' Set mode to 2 ChargeTime(chargeTimeTicks) TimeOut(timeOutTicks) SamplingInterval(sampleTicks) cog := cognew(MeasureLoop(pin, state, resultAddr), @stack) + 1 ' Launch the cog if cog ' Return 0 or address of repsAddr := @repetitions ' repetitions variable PUB Stop '' Stops RC decay measurements - frees a cog if cog ' If cog is running cogstop(cog~ - 1) ' Stop the cog and clear cog mode := 0 PUB SetMode(value) ''Configure mode mode := value '' value = #SEQUENTIAL or #PARALLEL PUB ChargeTime(ticks) ''Configure charge time params[CHARGE_TIME] := ticks #> 2775 '' ticks - Charge time in clock ticks '' must be at least 2775 ticks PUB TimeOut(ticks) ''Configure timeout params[TIME_OUT] := ticks '' ticks - Timeout in clock ticks ''NOTE: This timeout value is not precise. For a precise timeout, use MeasereTime.ASM '' instead. PUB Time(pin, state, resultAddr) : done ''RC Time Measurement {{Measures growth/decay time. - pin..............the pin to take RC measurements on - state............starting state of the measurement. 1 -> decay, 0 -> growth - resultAddr.......Address of the variable that stores the latest RC measurement result - done.............sequential mode - same as measurement parallel mode - 0 if failed to launch a cog - nonzero if success NOTES: 1) The charge time defaults to 0.1 ms, and the timeout to 10 ms. You can change these values by calling the ChargeTime and TimeOut methods. 2) Mode defaults to SEQUENTIAL, so this method waits for the measurement to finish. If you instead want to take parallel measurements or have the parent object's code move on and check for the result later, use the Mode method and set to PARALLEL. Example - time.Mode(time#PARALLEL). }} if params[CHARGE_TIME]==0 ' Check charge time ChargeTime(clkfreq/10_000) ' if zero, then set to default if params[TIME_OUT] == 0 ' Check timeout TimeOut(clkfreq/100) ' if zero, then set to default case mode ' check mode SEQUENTIAL: ' If sequential measure(pin, state, resultAddr) ' call measure done := long[resultAddr] ' return done = measurement PARALLEL: ' if parallel, return done <> 0 if cog done := (cog := cognew(measure(pin, state, resultAddr), @stack) + 1) PRI measure(pin, state, resultAddr) | t, temp ' RC Decay Measurement ' For insights into how this code works, see the Counter Modules and Circuit ' Applications Lab in Propeller Education Kit Labs: Fundamentals book. case state ' Set to measure decay or rise 1: ctra[30..26] := %01000 0: ctra[30..26] := %01100 ctra[5..0] := pin ' Configure counter module frqa := 1 tI := cnt ' Mark current time tC := tI + params[CHARGE_TIME] ' Calculate charge time outa[pin] := state ' Configure I/O pin dira[pin]~~ waitcnt(tC) ' Wait for charge phsa~ ' Clear phase accumulator dira[pin]:=0 ' Set I/O pin to input ' Wait for either timeout or change in state repeat until cnt-tC => params[TIME_OUT] OR ina[pin] <> state ' Fix interpreted code overhead in result and store at variable address long[resultAddr]:=(phsa-588)#>0 if mode == PARALLEL ' If Parallel mode cogstop(cogid) ' Cog self-stops PRI MeasureLoop(pin, state, resultAddr) | t, dt ' This method gets called if by the start method for repeated measurements at a given ' sampling rate. t:= cnt ' Mark current time repeat measure(pin, state, resultAddr) ' Measure RC decay repetitions++ waitcnt(t+=params[SAMPLE_INTERVAL]) PRI SamplingInterval(value) ''Set sampling interval '' ticks - Sample interval in clock ticks params[SAMPLE_INTERVAL]:= value #> (params[CHARGE_TIME] + params[TIME_OUT] + 25_000) '' Should be at least '' charge time + timeout + 25000 DAT {{ ┌──────────────────────────────────────────────────────────────────────────────────────┐ │ TERMS OF USE: MIT License │ ├──────────────────────────────────────────────────────────────────────────────────────┤ │Permission is hereby granted, free of charge, to any person obtaining a copy of this │ │software and associated documentation files (the "Software"), to deal in the Software │ │without restriction, including without limitation the rights to use, copy, modify, │ │merge, publish, distribute, sublicense, and/or sell copies of the Software, and to │ │permit persons to whom the Software is furnished to do so, subject to the following │ │conditions: │ │ │ │The above copyright notice and this permission notice shall be included in all copies │ │or substantial portions of the Software. │ │ │ │THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, │ │INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A │ │PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT │ │HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION │ │OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ └──────────────────────────────────────────────────────────────────────────────────────┘ }}